ThingWorx Flow > ThingWorx Flow Connectors > ThingWorx Flow Connector SDK
  
ThingWorx Flow Connector SDK
ThingWorx Flow Connector SDK such as ptc-flow-sdk needs to be included in every connector module. Running the init command generates a package.json file that already includes this module.
ThingWorx Flow server uses the flow-sdk to load the connector and access the Connections, Actions, Triggers, and Lookups contained in it.
ThingWorx Flow Connector SDK also provides the following APIs for connectors usage:
Logging
All logging inside the Connector must be done using the logging API provided by the SDK. This helps to debug the issues effectively. To access the logging API, add the following line to your node js code at the top of your source file:
const logger = require('ptc-flow-sdk').getLogger('module-name')
Replace the module name above with the name of the Action, Trigger, or Lookup, etc.
The logger has 4 methods debug, error, warn and info. Use the appropriate method as per the reason.
The log statements can log strings and JSON objects.
CustomFilters
CustomFilters is a helper API for filtering arrays of JSONs, strings, or numbers. CustomFilters can be used for filtering inputs provided to actions or triggers. Using customFilters is a two step process. The first step is to create a customFilters property in the input schema as shown below. This is done to allow a user to pick the operator and the operands in the GUI.
"input": {
"type": "object",
"title": "Select Trigger",
"properties": {
"customFilters": {
"type": "array",
"title": "Custom Filters",
"items": {
"type": "object",
"title": "filter",
"properties": {
"input": {
"type": "string",
"title": "Input",
"minLength": 1
},
"operator": {
"type": "string",
"title": "Condition",
"enum": [
"Equals",
"Equals(Number)",
"GreaterThan",
"LessThan",
"Contains",
"DoesNotContains",
"matches",
"isNull",
"isEmpty",
"isNumber",
"isObject",
"isArray",
"isBoolean",
"isDate",
"isUndefined"
]
},
"expected": {
"type": "string",
"title": "Expected",
"minLength": 1
}
}
}
}
}
The second step is to use the filter method of the CustomFilters API. The snippet for using CustomFilters is as follows:
//add the first line of the code to the top of the node js script
const customFilters = require('ptc-flow-sdk').CustomFilters;
function execute (input, options, output) {
run(input, options, function (err, data) {
if (err || !data) {
return output(err || 'empty')
}
customFilters.filter(input.customFilters, data, output
})
}
In the above snippet, the run method executes some code, possibly an http call that returns an array of data. The input.customFilters are the filtering criteria previously selected by you while designing the workflow. outputis the callback function to be called with the results of the filtering.
helper
The helper object has a number of utility JavaScript method.
toStr(obj)—Converts any object to a string
toISO(str)—Converts a string or a milliseconds representation of a string to an ISO format string.
toNumber(obj)—Attempts to convert an string to a number.
clone()—Performs a shallow copy of an object
deepClone()—Performs a deep copy of an object
mix(a,b)—Combines objects a and b to return a new object with properties from both a and b