IafHelper
Use the IafHelper API to use helper methods that check, add, convert, ignore, and get data.
addControlOptions
Adds output control options, such as projection, sorting, and pagination to a URL query string.
| Parameter | Required | Type | Description |
|---|---|---|---|
| url | Yes | String | Pass the HTTP URL. |
| options | Yes | Object | Pass a JSON object with your options. |
String - The complete URL with your query parameters appended to the URL string
// When properties in the options contain string only but not JSON object, Output will be http://example.com/?_pageSize=25&_offset=0
let url = "http://example.com";
let options = {"_pageSize":"25", "_offset": "0"}
let res = IafHelper.addControlOptions(url, options);
// When properties in the options contain JSON object, Output will be http://example.com/?project={"_userType":1,"_description":1}&sort={"_id":1}&page={"_pageSize":100,"_offset":0}
let url = "http://example.com";
let options = {"project": {"_userType": 1, "_description": 1}, "sort": {"_id": 1}, "page": {"_pageSize": 100, "_offset": 0}};
let res = IafHelper.addControlOptions(url, options);
addCriteria
Adds query parameters to a URL string when you pass a JSON object.
| Parameter | Required | Type | Description |
|---|---|---|---|
| url | Yes | String | Pass the HTTP URL. |
| criteria | Yes | object | Pass a JSON object with your query parameters. |
String - The complete URL with your query parameters appended to the URL string
// When criteria contains string, Output will be http://example.com/?_name=project_name&_userType=project_workspace
let url = "http://example.com";
let criteria = {"_name":"project_name", "_userType": "project_workspace"}
let res = IafHelper.addCriteria(url, criteria);
// When criteria contains array, add them as multi value params. The output will be http://example.com/?_id=ae1f7499-1674-432c-a1e8-04fb399dd8f6&_id=16abf498-1f52-4050-a330-e0d720ca4b51 for below
let url = "http://example.com";
let criteria = {"_id": ["ae1f7499-1674-432c-a1e8-04fb399dd8f6", "16abf498-1f52-4050-a330-e0d720ca4b51"]};
let res = IafHelper.addCriteria(url, criteria);
// When criteria contains JSON object, Output will be http://example.com/?query={"$and":[{"_userType":"file_container","_name":"root_container"}]}
let url = "http://example.com";
let criteria = {"query": {"$and" : [{"_userType": "file_container", "_name": "root_container"}]}};
let res = IafHelper.addCriteria(url, criteria);
addItemClassToCriteria
Adds an item class to a NamedUserItemCriteria object you pass.
| Parameter | Required | Type | Description |
|---|---|---|---|
| criteria | Yes | NamedUserItemCriteria | Pass a NamedUserItemCriteria object. |
| itemClass | Yes | String | Pass one of the following item classes for your named user item: NamedCompositeItem, NamedUserCollection, NamedFileCollection, NamedTelemetryCollection, UserConfig, NamedUserItem, NamedFileItem, NamedTelemetryItem, Script. For constant values, pass the following construction: IafItemSvc.<YOUR_ITEM_CLASS>Class |
NamedUserItemCriteria - The NamedUserItemCriteria object with an added item class property and value.
let criteria = {"_name":"example_file"};
IafHelper.addItemClassToCriteria(criteria, IafItemSvc.NamedFileItemClass);
addQueryParams
Adds query parameters to a URL string when you pass a JSON object.
| Parameter | Required | Type | Description |
|---|---|---|---|
| url | Yes | String | Pass the HTTP URL. |
| queryParams | Yes | undefined | |
| queryParams-null | Yes | Object | Pass a JSON object with your query parameters. |
String - The complete URL with your query parameters appended to the URL string
asyncForEach
Asynchronously runs a function you pass on each item of your array.
| Parameter | Required | Type | Description |
|---|---|---|---|
| array | Yes | Array | Pass an array for your function to iterate. |
| callback | Yes | function | Pass your JavaScript callback function. The function must contain a loop. |
doPoll
Poll a URL until it returns a COMPLETED or FAILED status. By default polls every 5 seconds up to 1080 attempts (90 minutes) before timing out.
| Parameter | Required | Type | Description |
|---|---|---|---|
| url | Yes | String | Pass the URL you want to poll. |
| ctx | No | Ctx | Context storage that contains data, such as authorization token requirements, namespaces, or session storage. |
| options | No | PollOptions | Poll headers and timing overrides. |
Promise<(String | Object)> - Resolves once the polled resource reaches a COMPLETED status, with the response body parsed as JSON (or the string if the response was a 204 No Content). Rejects with an Error if the resource reaches a FAILED status, or with a timeout message string once attempts are exhausted.
// Poll a long-running job every 2 seconds, up to 30 attempts (1 minute)
try {
const result = await IafHelper.doPoll(jobStatusUrl, ctx, { pollMilliSecs: 2000, maxPoll: 30 });
console.log('Job finished:', result);
} catch (e) {
console.log('Job failed or timed out:', e);
}
fetchAllPages
Gets the results in all the pages for a function you pass.
| Parameter | Required | Type | Description |
|---|---|---|---|
| fn | Yes | any | Pass a JavaScript function that makes an API call. |
| opts | Yes | Object | Pass an object with your page options, such as "project", "sort", and "offset". You must add the "getAllItems" property and set its value to true. |
Array - Returns an array of your results
let res = await IafHelper.fetchAllPages(async (opts)=>await IafPassSvc.getUsersInGroup(userGroup._id, ctx, opts), options);
isBrowser
Checks if a HTTP request is from a browser.
| Parameter | Required | Type | Description |
|---|---|---|---|
| req | Yes | Object | Pass the request object. |
boolean - Returns true if the request is from a browser.
isBrowserEnv
Returns true if the run time environment is a browser and returns false if the run time environment is node.js.
boolean -
isNode
Checks if a HTTP request comes from Node.js.
| Parameter | Required | Type | Description |
|---|---|---|---|
| req | Yes | Object | Pass the HTTP request object. |
boolean - Returns true if the request is from Node.js.
isValidParams
Checks if the properties in the objects you pass have valid values.
| Parameter | Required | Type | Description |
|---|---|---|---|
| params | Yes | Array<Object> | Pass an array of objects that contain properties you want to validate. |
boolean - Returns true if all values are valid. Returns false if there is an invalid value and logs a warning in the console that identifies the property that contains the invalid value.
let params = [{obj: null, name: 'userConfigId'}] // returns false and logs the following warning in the console: 'Please provide userConfigId'
IafHelper.isValidParams(params);
let params = [{obj: {key: value}, name: 'userConfigId'}] // returns true
IafHelper.isValidParams(params);
isValidUrl
Checks if a string is a valid URL.
| Parameter | Required | Type | Description |
|---|---|---|---|
| str | Yes | String | Pass the string you want to validate. |
boolean - Returns true if the string is a valid URL
resultExists
Check whether result exists for Page results
| Parameter | Required | Type | Description |
|---|---|---|---|
| res | Yes | Page | Page of results |
boolean - Returns true if the list has items, otherwise false
skipSessionStorage
Ignores the name space for your request by setting the ctx storage's "nsfilter" property to true.
| Parameter | Required | Type | Description |
|---|---|---|---|
| ctx | Yes | ctx | Context, such as namespaces and authentication token information. |
any -
stringifyJS
Stringifies the JavaScript you pass.
| Parameter | Required | Type | Description |
|---|---|---|---|
| a | Yes | undefined |
string - Returns your JavaScript as a string
tipVersion
Finds the tip version of a named user item.
| Parameter | Required | Type | Description |
|---|---|---|---|
| item | Yes | NamedUserItem | Pass the NamedUserItem object. |
NamedUserItemVersion - Returns the tip version of the named user item.
toArrayWithKey
Convert's a JSON object to an array of separate key-value pairs.
| Parameter | Required | Type | Description |
|---|---|---|---|
| obj | Yes | Object | Pass the JSON object. |
Array<Object> - An array of objects that contain the separate key-value pairs
let user = { user: 'barney', age: 36, active: true };
IafHelper.toArrayWithKey(userObj);
// Returns the following array: [{"user": "barney"},{"age": 36},{"active": true}]
toJson
Safely parses an object and converts it to JSON.
| Parameter | Required | Type | Description |
|---|---|---|---|
| obj | Yes | String | Pass your object as a string. |
Object - A JSON object.
whatIsIt
Returns the data type of the parameter you pass.
| Parameter | Required | Type | Description |
|---|---|---|---|
| object | Yes | object | Pass an object that contains the data you want to check. |
string - Returns the data type