@unito/integration-debugger
Version:
The Unito Integration Debugger
100 lines (99 loc) • 3.88 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.encodeObjectToQueryString = void 0;
const integration_api_1 = require("@unito/integration-api");
const UNSUPPORTED_FIELD_TYPES = Array(integration_api_1.FieldValueTypes.OBJECT, integration_api_1.FieldValueTypes.BLOB);
const encodeObjectToQueryString = (obj) => {
const queryString = Object.keys(obj)
.map(key => {
const value = obj[key];
if (Array.isArray(value)) {
return `${key}=${value.map(encodeURIComponent).join(',')}`;
}
return `${key}=${encodeURIComponent(value)}`;
})
.join('&');
return queryString;
};
exports.encodeObjectToQueryString = encodeObjectToQueryString;
/**
* Check: Request schema of an item.
*
* @example
* The following itemSummary represents an item fetchable with dynamic parameters:
* {
* "data": [
* {
* "path": "/my/super/path/custom",
* "requestSchema": {
* "parameters": [{
* "name": "firstParam",
* "type": "string",
* "nullable": true
* }]
* }]
* }
*
* The path with dynamic parameters wont be automatically infer by the crawler. It needs to be generated.
* A new step will be added to the crawler with the generated path.
*
*/
const check = {
label: 'Request schema',
prepareOnPreparedSteps: false,
validateOnError: false,
activatedByDefault: false,
prepare: async (stepResult, crawlerDriver) => {
const preparedSteps = [];
for (const step of stepResult.discoveredSteps) {
if (!step.requestSchema) {
continue;
}
const generatedValues = await crawlerDriver.generator.generate(step.requestSchema?.parameters);
const processedRequestParams = {};
// For reference fields, the generated value will be an ItemSummary and we need to extract the path from it.
for (const parameter of step.requestSchema?.parameters ?? []) {
const generatedValue = generatedValues[parameter.name];
if (generatedValue === undefined ||
generatedValue === null ||
UNSUPPORTED_FIELD_TYPES.includes(parameter.type)) {
continue;
}
let value;
if (parameter.type === integration_api_1.FieldValueTypes.REFERENCE) {
if (Array.isArray(generatedValue)) {
value = generatedValue.map(itemSummary => itemSummary.path);
}
else {
value = generatedValue.path;
}
}
else {
value = generatedValue;
}
processedRequestParams[parameter.name] = value;
}
// FieldSchema does not accept value of type Object or Reference for now, the generated object will always be a Record<string, string | boolean | number | unknown[]>
const queryParams = (0, exports.encodeObjectToQueryString)(processedRequestParams);
let dynamicPath = step.path;
if (queryParams) {
dynamicPath = [step.path, queryParams].join(step.path.includes('?') ? '&' : '?');
}
preparedSteps.push({
path: dynamicPath,
requestSchema: undefined,
schemaPath: step.schemaPath,
parentOperation: step.parentOperation,
parentPath: step.parentPath,
operation: step.operation,
headersIn: step.headersIn,
warnings: [],
errors: [],
});
}
return preparedSteps;
},
// Nothing to validate, the crawler will do it for us.
validate: undefined,
};
exports.default = check;
;