Skip to main content
Version: v5.2

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.

ParameterRequiredTypeDescription
urlYesStringPass the HTTP URL.
optionsYesObjectPass a JSON object with your options.
Returns

String - The complete URL with your query parameters appended to the URL string

Examples
// 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.

ParameterRequiredTypeDescription
urlYesStringPass the HTTP URL.
criteriaYesobjectPass a JSON object with your query parameters.
Returns

String - The complete URL with your query parameters appended to the URL string

Examples
// 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.

ParameterRequiredTypeDescription
criteriaYesNamedUserItemCriteriaPass a NamedUserItemCriteria object.
itemClassYesStringPass 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
Returns

NamedUserItemCriteria - The NamedUserItemCriteria object with an added item class property and value.

Examples
let criteria = {"_name":"example_file"};

IafHelper.addItemClassToCriteria(criteria, IafItemSvc.NamedFileItemClass);

addQueryParams

Adds query parameters to a URL string when you pass a JSON object.

ParameterRequiredTypeDescription
urlYesStringPass the HTTP URL.
queryParamsYesundefined
queryParams-nullYesObjectPass a JSON object with your query parameters.
Returns

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.

ParameterRequiredTypeDescription
arrayYesArrayPass an array for your function to iterate.
callbackYesfunctionPass your JavaScript callback function. The function must contain a loop.
Returns

Promise<void> -

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.

ParameterRequiredTypeDescription
urlYesStringPass the URL you want to poll.
ctxNoCtxContext storage that contains data, such as authorization token requirements, namespaces, or session storage.
optionsNoPollOptionsPoll headers and timing overrides.
Returns

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.

Examples
// 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.

ParameterRequiredTypeDescription
fnYesanyPass a JavaScript function that makes an API call.
optsYesObjectPass an object with your page options, such as "project", "sort", and "offset". You must add the "getAllItems" property and set its value to true.
Returns

Array - Returns an array of your results

Examples
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.

ParameterRequiredTypeDescription
reqYesObjectPass the request object.
Returns

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.

Returns

boolean -

isNode

Checks if a HTTP request comes from Node.js.

ParameterRequiredTypeDescription
reqYesObjectPass the HTTP request object.
Returns

boolean - Returns true if the request is from Node.js.

isValidParams

Checks if the properties in the objects you pass have valid values.

ParameterRequiredTypeDescription
paramsYesArray<Object>Pass an array of objects that contain properties you want to validate.
Returns

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.

Examples
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.

ParameterRequiredTypeDescription
strYesStringPass the string you want to validate.
Returns

boolean - Returns true if the string is a valid URL

resultExists

Check whether result exists for Page results

ParameterRequiredTypeDescription
resYesPagePage of results
Returns

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.

ParameterRequiredTypeDescription
ctxYesctxContext, such as namespaces and authentication token information.
Returns

any -

stringifyJS

Stringifies the JavaScript you pass.

ParameterRequiredTypeDescription
aYesundefined
Returns

string - Returns your JavaScript as a string

tipVersion

Finds the tip version of a named user item.

ParameterRequiredTypeDescription
itemYesNamedUserItemPass the NamedUserItem object.
Returns

NamedUserItemVersion - Returns the tip version of the named user item.

toArrayWithKey

Convert's a JSON object to an array of separate key-value pairs.

ParameterRequiredTypeDescription
objYesObjectPass the JSON object.
Returns

Array<Object> - An array of objects that contain the separate key-value pairs

Examples
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.

ParameterRequiredTypeDescription
objYesStringPass your object as a string.
Returns

Object - A JSON object.

whatIsIt

Returns the data type of the parameter you pass.

ParameterRequiredTypeDescription
objectYesobjectPass an object that contains the data you want to check.
Returns

string - Returns the data type