Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/oih-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async function oihGen() {
outputDir: options.output,
snapshot: options.snapshot,
rateLimit: options.rateLimit,
authFormat: options.authFormat,
syncParamFormat: options.syncParamFormat,
paginationConfig: {
pageTokenOption: {
Expand Down
7 changes: 5 additions & 2 deletions lib/do-generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ const generate = require("./generate");
* @param {string} paginationConfig.pageTokenOption.fieldName
* @param {number} rateLimit
* @param {string} syncParamFormat
* @param {object} authFormat
* @param {string} authFormat.prefix
* @returns {Promise<*>}
*/
module.exports = async function doGenerate({ swaggerUrl, outputDir, connectorName, snapshot, paginationConfig, rateLimit, syncParamFormat }) {
module.exports = async function doGenerate({ swaggerUrl, outputDir, connectorName, snapshot, paginationConfig, rateLimit, syncParamFormat, authFormat, }) {
const downloadedSpecFile = path.join(outputDir, "openapi-original.json");
const validatedSpecFile = path.join(outputDir, "openapi-validated.json");
const generatePath = path.join(outputDir, connectorName);
Expand Down Expand Up @@ -49,7 +51,8 @@ module.exports = async function doGenerate({ swaggerUrl, outputDir, connectorNam
snapshot: snapshot || "",
paginationConfig,
rateLimit: rateLimit || -1,
syncParamFormat: syncParamFormat
syncParamFormat: syncParamFormat,
authFormat: authFormat || {},
});

console.log("\x1b[32m", "Successfully generated. Connector has been saved in output directory:", generatePath);
Expand Down
3 changes: 2 additions & 1 deletion lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = async function generate({
suffixServiceNameToTitle = false,
rateLimit = -1,
syncParamFormat,
authFormat = {},
}) {
let api = await SwaggerParser.parse(inputFile);
if (!api.components) {
Expand All @@ -53,7 +54,7 @@ module.exports = async function generate({
fse.removeSync(libDir);
}

let componentJson = await getComponentJson(apiTitle, api, swaggerUrl, rateLimit, syncParamFormat, this);
let componentJson = await getComponentJson(apiTitle, api, swaggerUrl, rateLimit, syncParamFormat, authFormat, this);
let existingNames = {};

// generate actions
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, syncParamFormat) {
async function getComponentJson(apiTitle, api, swaggerUrl, rateLimit, syncParamFormat, authFormat) {
const textDescription = toText(api.info.description);

const componentJson = Object.assign(JSON.parse(templates.componentTemplate), {
Expand All @@ -90,6 +90,7 @@ async function getComponentJson(apiTitle, api, swaggerUrl, rateLimit, syncParamF
url: swaggerUrl,
rateLimit: parseInt(rateLimit),
syncParamFormat: syncParamFormat,
authFormat: authFormat,
});

await addCredentials(componentJson, api);
Expand Down
1 change: 1 addition & 0 deletions templates/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"docsUrl": "",
"url": "",
"rateLimit": -1,
"authFormat": {},
"envVars": {},
"credentials": {},
"triggers": {},
Expand Down
6 changes: 5 additions & 1 deletion templates/lib/actions/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

const spec = require("../spec.json");
const { mapFieldNames, getMetadata, mapFormDataBody, putAdditionalParamsInBody, executeCall } = require("../utils/helpers");
const { mapFieldNames, getMetadata, mapFormDataBody, putAdditionalParamsInBody, executeCall, formatApiKey } = require("../utils/helpers");
const componentJson = require("../../component.json");

async function processAction(msg, cfg, snapshot, incomingMessageHeaders, tokenData) {
Expand All @@ -35,6 +35,10 @@ async function processAction(msg, cfg, snapshot, incomingMessageHeaders, tokenDa
logger.debug("Incoming message headers: %j", incomingMessageHeaders);
logger.debug("Incoming token data: %j", tokenData);

if (cfg?.key && componentJson?.authFormat) {
cfg.key = formatApiKey(cfg.key, componentJson.authFormat);
}

const actionFunction = tokenData["function"];
logger.info("Starting to execute action '%s'", actionFunction);

Expand Down
7 changes: 6 additions & 1 deletion templates/lib/triggers/trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const {
getMetadata,
getElementDataFromResponse,
executeCall,
getInitialSnapshotValue
getInitialSnapshotValue,
formatApiKey,
} = require("../utils/helpers");
const { createPaginator } = require("../utils/paginator");
const componentJson = require("../../component.json");
Expand All @@ -43,6 +44,10 @@ async function processTrigger(msg, cfg, snapshot, incomingMessageHeaders, tokenD
logger.debug("Incoming message headers: %j", incomingMessageHeaders);
logger.debug("Incoming token data: %j", tokenData);

if (cfg?.key && componentJson?.authFormat) {
cfg.key = formatApiKey(cfg.key, componentJson.authFormat);
}

const triggerFunction = tokenData["function"];
logger.info("Starting to execute trigger \"%s\"", triggerFunction);

Expand Down
15 changes: 14 additions & 1 deletion templates/lib/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,18 @@ async function getResponseData(response) {
}
}

function formatApiKey(key, format) {
let formattedKey = key;

if (format.prefix) {
if (!formattedKey?.startsWith(format.prefix)) {
formattedKey = `${format.prefix}${formattedKey}`;
}
}

return formattedKey;
}

module.exports = {
compareDate,
mapFieldNames,
Expand All @@ -317,5 +329,6 @@ module.exports = {
executeCall,
getInitialSnapshotValue,
getInputMetadataSchema,
putAdditionalParamsInBody
putAdditionalParamsInBody,
formatApiKey
};
13 changes: 13 additions & 0 deletions templates/lib/utils/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {
isMicrosoftJsonDate,
getInitialSnapshotValue,
compareDate,
formatApiKey
} = require("./helpers");
const dayjs = require('dayjs');

Expand Down Expand Up @@ -56,4 +57,16 @@ describe("Helpers", () => {
});

});

describe("formatApiKey", () => {
it("should prefix a key with a given prefix if it is missing", () => {
const tokenKey = formatApiKey('abcd123', { prefix: 'Token ' });
expect(tokenKey).toEqual('Token abcd123');
});

it("should not prefix a key if it's already correctly formatted", () => {
const BearerKey = formatApiKey('Bearer abcd123', { prefix: 'Bearer ' });
expect(BearerKey).toEqual('Bearer abcd123');
});
});
});