JetClient / jet-client-support

JetClient is an advanced REST API Client plugin for JetBrains IDEs
https://plugins.jetbrains.com/plugin/21173-jetclient
111 stars 0 forks source link

Support finding requests/folders by Name, Description, or Endpoint. #26

Closed Unnoen closed 6 months ago

Unnoen commented 1 year ago

Is your feature request related to a problem?

Currently, as far as I understand, the jc.findFolder() and jc.findRequest() functions can only search by ID, even though the ID of these objects are not exposed anywhere and must be found by checking the JetClient state JSON.

Describe the solution you'd like

Ideally, new methods would be created.

The ability to use {{variables}} in the strings without needing to call jc.variables etc. would be a very nice feature. Perhaps passing {{variables}} could be a boolean value? e.g., jc.findFolderByName(folderName: string, parseVariables: boolean) Used like: jc.findFolderByName("{{integrationFolder}}", true)

Finding folders:

Finding requests:

Additional context

No response

AntonShuvaev commented 1 year ago

I've chosen to use IDs instead of names as they uniquely identify a request or folder, ensuring that renaming won't break the scripts. You don't need to copy ID from JSON state. You can drag and drop a request or folder into the script or variables editor to obtain its ID. The inlay hint displays the corresponding name of a folder or request within the scripts. However, I understand that this might not be convenient during code reviews, where only the ID is visible.

I'm leaning towards the solution where users will be able to define their own functions. Currently this can be done by defining a global function global.findRequestByName in the pre-request script of the root folder like:

global.findRequestByName = (name) => {
    const rootFolder = jc.findFolder("47473add")
    return findRequestByNameInFolder(name, rootFolder)
}

function findRequestByNameInFolder(name, folder) {
    return folder.requests.find(r => r.name === name) || folder.folders.reduce((acc, f) => {
        return acc || findRequestByNameInFolder(name, f)
    }, null)
}

However, this will only work in request and folder scripts, and won't work in test suites, because parent folder scripts are not executed for test suites.

But I plan to implement a dynamic variables feature, where besides the predefined functions users will be able to define their own functions. This will allow to define findRequestByName function and use it in any script.

AntonShuvaev commented 6 months ago

Starting from version 2024.2.0, the methods jc.runRequest, jc.sendRequest, jc.runFolder, jc.runTestSuite now accept a request or folder path relative to the project (the root collection) instead of an ID