UNPKG

@vara/custom-logic-sdk

Version:

Server Side JavaScript SDK for Custom Business Logic

82 lines (80 loc) 2.67 kB
/** * Created by marlonjoseph on 2/2/17. */ const _ = require('lodash'); const rest = require('request-promise'); // The client /** @function TxClient * @param {string} baseTxUrl - The base url of the ETX API instance for eg. https://api.us1.engaugetx.com/v1 * @param {string} baseTxInternalUrl - The internal base url of the ETX API instance for eg. http://api.app-123456.internal/v1 * @param {string} appId - The Application ID of the Application. * @param {string} clientKey - The clientKey of the Application. * @param {string} masterKey - The masterKey of the Application. */ const clientFactory = function TxClient(baseTxUrl, baseTxInternalUrl, appId, clientKey, masterKey) { const client = { context: { settings: { baseTxUrl: baseTxUrl, baseTxInternalUrl: baseTxInternalUrl, appId: appId, clientKey: clientKey, masterKey: masterKey, headers: { 'App-Id': appId, }, }, }, }; const jobsEndpoint = `${baseTxInternalUrl}/jobs`; client.jobs = { /** @function scheduleFunction - Schedule a Standalone Function * * @param fnName - The name of the Standalone Function to be schedule * @param options - Options to control the date, recurrence and method. * @param options.recurrence - The recurrence as a human readable string. * @param options.date - The schedule date as a date string or human readable string. * @param options.method - The method to use when executing the Standalone Functions. Defaults to 'POST'. * @param cb - The callback function which will be called with err, result params. */ scheduleFunction: function scheduleFunction(fnName, options = {}, cb) { const body = { params: { url: `${baseTxInternalUrl}/run/${fnName}?appId=${appId}&clientKey=${clientKey}`, payload: options.payload, }, type: 'http', }; if (options.recurrence) { body.recurrence = options.recurrence; } if (options.date) { body.date = options.date; } if (options.method) { body.params.method = options.method; } rest({ uri: jobsEndpoint, headers: _.merge(client.context.settings.headers, { 'Client-Key': client.context.settings.masterKey, }), method: 'POST', body: body, json: true, }) .then((res) => { if (_.isFunction(cb)) { cb(undefined, res.result); } }, (err) => { if (_.isFunction(cb)) { cb(err); } }); }, }; return client; }; module.exports = clientFactory;