diff --git a/bin/oih-gen.js b/bin/oih-gen.js index 22a6924..7837e9b 100755 --- a/bin/oih-gen.js +++ b/bin/oih-gen.js @@ -63,6 +63,7 @@ async function oihGen() { outputDir: options.output, snapshot: options.snapshot, rateLimit: options.rateLimit, + syncParamFormat: options.syncParamFormat, paginationConfig: { pageTokenOption: { fieldName: options.paginationTokenFieldName, diff --git a/lib/do-generate.js b/lib/do-generate.js index b39cb04..6c7c97c 100644 --- a/lib/do-generate.js +++ b/lib/do-generate.js @@ -17,9 +17,10 @@ const generate = require("./generate"); * @param {object} paginationConfig.pageTokenOption * @param {string} paginationConfig.pageTokenOption.fieldName * @param {number} rateLimit + * @param {string} syncParamFormat * @returns {Promise<*>} */ -module.exports = async function doGenerate({ swaggerUrl, outputDir, connectorName, snapshot, paginationConfig, rateLimit }) { +module.exports = async function doGenerate({ swaggerUrl, outputDir, connectorName, snapshot, paginationConfig, rateLimit, syncParamFormat }) { const downloadedSpecFile = path.join(outputDir, "openapi-original.json"); const validatedSpecFile = path.join(outputDir, "openapi-validated.json"); const generatePath = path.join(outputDir, connectorName); @@ -47,7 +48,8 @@ module.exports = async function doGenerate({ swaggerUrl, outputDir, connectorNam swaggerUrl: swaggerUrl, snapshot: snapshot || "", paginationConfig, - rateLimit: rateLimit || -1 + rateLimit: rateLimit || -1, + syncParamFormat: syncParamFormat }); console.log("\x1b[32m", "Successfully generated. Connector has been saved in output directory:", generatePath); diff --git a/lib/generate.js b/lib/generate.js index 63e371b..52d2e76 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -33,6 +33,7 @@ module.exports = async function generate({ paginationConfig, suffixServiceNameToTitle = false, rateLimit = -1, + syncParamFormat, }) { let api = await SwaggerParser.parse(inputFile); if (!api.components) { @@ -52,7 +53,7 @@ module.exports = async function generate({ fse.removeSync(libDir); } - let componentJson = await getComponentJson(apiTitle, api, swaggerUrl, rateLimit, this); + let componentJson = await getComponentJson(apiTitle, api, swaggerUrl, rateLimit, syncParamFormat, this); let existingNames = {}; // generate actions diff --git a/lib/utils/functions.js b/lib/utils/functions.js index 2769097..82dcda1 100644 --- a/lib/utils/functions.js +++ b/lib/utils/functions.js @@ -80,7 +80,7 @@ async function output(filename, text, data, outputDir) { //console.log('Writing file %s', filename); return await fse.outputFile(path.join(outputDir, filename), text); } -async function getComponentJson(apiTitle, api, swaggerUrl, rateLimit) { +async function getComponentJson(apiTitle, api, swaggerUrl, rateLimit, syncParamFormat) { const textDescription = toText(api.info.description); const componentJson = Object.assign(JSON.parse(templates.componentTemplate), { @@ -89,6 +89,7 @@ async function getComponentJson(apiTitle, api, swaggerUrl, rateLimit) { docsUrl: (api.externalDocs && api.externalDocs.url) || "", url: swaggerUrl, rateLimit: parseInt(rateLimit), + syncParamFormat: syncParamFormat, }); await addCredentials(componentJson, api); diff --git a/templates/lib/triggers/trigger.js b/templates/lib/triggers/trigger.js index 1196476..36fc66b 100644 --- a/templates/lib/triggers/trigger.js +++ b/templates/lib/triggers/trigger.js @@ -21,6 +21,7 @@ const { } = require("../utils/helpers"); const { createPaginator } = require("../utils/paginator"); const componentJson = require("../../component.json"); +const dayjs = require("dayjs"); async function processTrigger(msg, cfg, snapshot, incomingMessageHeaders, tokenData) { let logger = this.logger; @@ -28,7 +29,7 @@ async function processTrigger(msg, cfg, snapshot, incomingMessageHeaders, tokenD if (cfg && cfg.nodeSettings && cfg.nodeSettings.continueOnError) continueOnError = true; try { - const { snapshotKey, arraySplittingKey, syncParam, skipSnapshot, logLevel } = cfg.nodeSettings; + const { snapshotKey, arraySplittingKey, syncParam, skipSnapshot, logLevel, syncParamFormat } = cfg.nodeSettings; if (["fatal", "error", "warn", "info", "debug", "trace"].includes(logLevel)) { logger = this.logger.child({}); @@ -96,15 +97,21 @@ async function processTrigger(msg, cfg, snapshot, incomingMessageHeaders, tokenD } if (syncParam && snapshot.lastUpdated) { + let formattedTimestamp = snapshot.lastUpdated; + if (syncParamFormat) { + formattedTimestamp = dayjs(snapshot.lastUpdated).utc().format(syncParamFormat); + } + logger.info("SyncParam: %s", formattedTimestamp); if (syncParam === "$FILTER") { if (!snapshotKey) { throw new Error("snapshotKey params should be specified!"); } - parameters[syncParam] = `${snapshotKey} gt datetime'${snapshot.lastUpdated}'`; + parameters[syncParam] = `${snapshotKey} gt datetime'${formattedTimestamp}'`; } else { - parameters[syncParam] = snapshot.lastUpdated; + parameters[syncParam] = formattedTimestamp; } } + logger.debug("Parameters were populated from configuration: %j", parameters); $SECURITIES;