Accessing passed data
Script functions can access data with the input, libraries, and ctx arguments:
async yourFunction(input, libraries, ctx) {
//your function body
}
| Argument | Contains |
|---|---|
input | Request data |
libraries | Platform libraries |
ctx | Session context |
Accessing argument data
libraries argument
The libraries argument contains the platform libraries. For example, to access the IafItemSvc API, you can destructure in the following manner:
async searchAssets(input, libraries, ctx) {
//destructuring
const { IafItemSvc } = libraries.PlatformApi
//API call
const items = await IafItemSvc.getNamedUserItems(
{ query: { _userType: 'assets' }
});
}
ctx argument
Use the ctx argument to extract contextual data such as namespaces, an auth token, and any other relevant context; you can also pass context to your API calls:
async searchAssets(input, libraries, ctx) {
const { IafItemSvc } = libraries.PlatformApi //access APIs
const { IafScriptEngine } = libraries //access script engine directly from libraries
const items = await IafItemSvc.getNamedUserItems(
{ query: { _userType: 'assets' },
ctx //passing context
});
//extracting namespace data
const projectNamespaces = ctx._namespaces;
}
input argument
With the input argument, scripts can access request data, as well as passed data, such as path variables and query parameters.
Accessing passed data
You can use this input object to access the following:
Accessing request and request body
You can access the following data that is contained in the input object structure using dot notation, for example input.script or input.params.HTTP_METHOD:
{
script: {},
params: {
nsFilter: " "
HTTP_METHOD: " "
}
}
| Property | Type | Description |
|---|---|---|
| script | Object | The _userType of the script file and the name of the script that executes |
| params.nsFilter | String | Namespace that the script is executing in |
| params.HTTP_METHOD | String | HTTP method used to call the endpoint |
Scripts can also access a request body from the input.params key. You can access the request body value with the dot notation input.params.your-req-body-key:
// example:
// POST /path
// request body
// {"query": {"properties.dtCategory.val": "Curtain Walls"}}
async searchAssets(input, libraries, ctx) {
let reqQueryValue = input.params.query;
}
Note: The body is available in the script for all POST requests.
Accessing path variables
You can access the path variable as $my-path-variable in the script, or from the input object with the dot notation input.my-path-variable:
// example: GET /assets/:assetid
async searchAssets(input, libraries, ctx) {
console.log("assetid path param:", input.assetid);
console.log("assetid path param:", $assetid);
}
Accessing path query parameters
You can access a query parameter as $my-query-param in the script, or with the dot notation input.params.my-query-param:
// example: GET /path?nsFilter=testNamespace&queryParam=paramValue
async searchAssets(input, libraries, ctx) {
console.log("query param:", input.params.queryParam)
console.log("query param:", $queryParam)
}
Note: All endpoints must be called with an
"nsfilter"query parameter that is set to the project's namespace.