SDK object
The sdk
object is the one which provides you the functionality of our App Integration SDK. There are 4 available functionalities that you can access with the sdk
object. This is also the first argument that is passed into the perform function.
sdk.console
The console function provides a simple debugging console that is similar to the Node.js console module. To write a log you can do the following:
sdk.console.log('This is my first log');
Read more about logging here.
sdk.request
The request function provides you the functionality of doing Http requests. The request function has the following signature:
request(requestOptions)
Arguments | Description | VALUE TYPE |
---|---|---|
requestOptions | The options to pass into the request function | RequestOptions (See below) |
RequestOptions
The options that you can send into the request function
PROPERTIES | DESCRIPTION | VALUE TYPE | DEFAULT VALUE |
---|---|---|---|
url * | The URL for the http request | string | |
method | The HTTP Request method | string | GET |
body | The body to ve passed along with your request | string | object |
allowGetBody | An option to pass body into GET request | boolean | false |
headers | The HTTP headers | object | |
redirect | How to handle a redirect response. follow: Automatically follow redirects. Unless otherwise stated the redirect mode is set to follow. error: Abort with an error if a redirect occurs. manual: Caller intends to process the response in another context. | ||
follow | maximum redirect count. 0 to not follow redirect | number | 20 |
compress | support gzip/deflate content encoding. false to disable | boolean | true |
agent | The Http agent to use to do the request. | string | function |
timeout | The time limit on which the request should respond | number | |
size | the maximum response body size in bytes. 0 to disable | number | 0 |
form | The object to pass formdata into. If passed, it converts the header to have content-type 'application/x-www-form-urlencoded' |
object | |
skipThrowForStatus | Return request status instead of throwing error on request directly | boolean | false |
let {clientId, questionId, answer} = bundle.inputData;
return sdk.request({
method: "POST",
url: `YOUR_URL_HERE`,
body: {
clientId,
questionId,
answer,
},
}).then((res) => {
if (res.status === 201) {
return res.data;
}
throw new Error("Something went wrong");
});
sdk.store
This object provides you the access to an asynchronous key-value store that is provided by our SDK. With this object, you can have access to the get
and set
methods.
set
With the set method, you can add key value pairs into our key-value store.
const res = await sdk.store.set("key", "value");
Arguments
ARGUMENTS | DESCRIPTION | VALUE TYPE |
---|---|---|
key | An unique identifier for some item of data to be stored | string |
value | The actual value to be stored into the store. | string |
Return
It returns you the value that you just stored.
Keep in mind that if you try to set the value of the key that already exists, it replaces the old value with the new one.
The key and value both are of type string. So it is recommended that you don't store huge data sets by converting them into strings
const key = bundle.inputData.key;
const value = bundle.inputData.sessionId;
const res = await sdk.store.set(key, value);
sdk.console.log(`${res} has been stored with key: ${key}`);
The above code stores the data we get from bundle.inputData
and logs them in the console.
sdk.require
The require function provides user with features provided by the Node.js require method. It basically can be used to import modules, JSON, and local files. Modules can be imported from node_modules.
const moduleName = sdk.require("MODULE_NAME");
Arguments
ARGUMENTS | DESCRIPTION | VALUE TYPE |
---|---|---|
MODULE_NAME | The name of the module that you want to import into your method | string |
Example
function removeDuplicates(val) {
if (!val) return val;
if (val.length < 2) return val;
const _ = sdk.require('lodash');
return _.uniq(val);
}
If you want to import modules that are not a part of your app then make sure that they are added in the package.json
file of your app so that they will be part of your node_modules
.