UNPKG

@prismatic-io/spectral

Version:

Utility library for building Prismatic connectors and code-native integrations

172 lines (171 loc) 6.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.INPUT_TYPE_MAP = exports.getInputs = void 0; const escapeSpecialCharacters_1 = require("../utils/escapeSpecialCharacters"); const docBlock_1 = require("./docBlock"); const getInputs = ({ inputs, docBlock = docBlock_1.DOC_BLOCK_DEFAULT }) => { return inputs.reduce((acc, input) => { if ((typeof input.shown === "boolean" && input.shown === false) || input.type === "dynamicObjectSelection" || input.type === "dynamicFieldSelection") { return acc; } return [ ...acc, { key: input.key, label: input.label, inputType: input.type, valueType: getInputValueType(input), required: input.required && (input.default === undefined || input.default === null || input.default === ""), collection: input.collection, onPremControlled: input.onPremiseControlled || input.onPremControlled, docBlock: docBlock(input), default: getDefaultValue(input.default, Boolean(input.collection)), }, ]; }, []); }; exports.getInputs = getInputs; exports.INPUT_TYPE_MAP = { string: "string", data: "string", text: "string", password: "string", boolean: "boolean", code: "string", conditional: { module: "@prismatic-io/spectral/dist/util", type: "ConditionalExpression", }, connection: { module: "@prismatic-io/spectral/dist/types", type: "Connection", }, objectSelection: { module: "@prismatic-io/spectral/dist/types", type: "ObjectSelection", }, objectFieldMap: { module: "@prismatic-io/spectral/dist/types", type: "ObjectFieldMap", }, jsonForm: { module: "@prismatic-io/spectral/dist/types", type: "JSONForm", }, dynamicObjectSelection: "string", dynamicFieldSelection: "string", date: "string", timestamp: "string", flow: "string", template: "string", structuredObject: { module: "@prismatic-io/spectral/dist/types", type: "StructuredObject", }, dynamicObject: { module: "@prismatic-io/spectral/dist/types", type: "DynamicObject", }, }; const getInputValueType = (input) => { var _a, _b; if (input.type === "structuredObject") { return wrapCollection(structuredObjectTypeString((_a = input.inputs) !== null && _a !== void 0 ? _a : []), input.collection); } if (input.type === "dynamicObject") { return dynamicObjectTypeString((_b = input.inputs) !== null && _b !== void 0 ? _b : []); } const inputType = exports.INPUT_TYPE_MAP[input.type]; const valueType = input.model ? input.model .map((choice) => { return `\`${choice.value.replaceAll("\r", "\\r").replaceAll("\n", "\\n")}\``; }) .join(" | ") : inputType ? typeof inputType === "string" ? inputType : Object.assign(Object.assign({}, inputType), { import: inputType.type }) : "never"; if (input.collection === "keyvaluelist") { return typeof valueType === "string" ? `Record<string, ${valueType}> | Array<{key: string, value: ${valueType}}>` : Object.assign(Object.assign({}, valueType), { type: `Record<string, ${valueType.type}> | Array<{key: string, value: ${valueType.type}}>` }); } if (input.collection === "valuelist") { return typeof valueType === "string" ? `Array<${valueType}>` : Object.assign(Object.assign({}, valueType), { type: `Array<${valueType.type}>` }); } return valueType; }; const getDefaultValue = (value, isCollection) => { if (value === undefined || value === "") { return isCollection ? [] : value; } const stringValue = typeof value === "string" ? value : JSON.stringify(value); return (0, escapeSpecialCharacters_1.escapeSpecialCharacters)(stringValue); }; const isValidIdentifier = (key) => /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key); const wrapCollection = (valueType, collection) => { if (collection === "valuelist") { return `Array<${valueType}>`; } if (collection === "keyvaluelist") { return `Record<string, ${valueType}> | Array<{key: string, value: ${valueType}}>`; } return valueType; }; const getLeafBaseType = (child) => { var _a; if ((_a = child.model) === null || _a === void 0 ? void 0 : _a.length) { return child.model .map(({ value }) => `\`${value.replaceAll("\r", "\\r").replaceAll("\n", "\\n")}\``) .join(" | "); } const mapped = exports.INPUT_TYPE_MAP[child.type]; if (!mapped) { return "unknown"; } if (typeof mapped === "string") { return mapped; } return `import("${mapped.module}").${mapped.type}`; }; const getLeafTypeString = (child) => { var _a; if (child.type === "structuredObject") { return wrapCollection(structuredObjectTypeString((_a = child.inputs) !== null && _a !== void 0 ? _a : []), child.collection); } return wrapCollection(getLeafBaseType(child), child.collection); }; const SPECTRAL_TYPES_MODULE = "@prismatic-io/spectral/dist/types"; const structuredObjectTypeString = (inputs) => { if (!inputs.length) { return `import("${SPECTRAL_TYPES_MODULE}").StructuredObject`; } const fields = inputs .map((child) => { const key = isValidIdentifier(child.key) ? child.key : JSON.stringify(child.key); return `${key}: ${getLeafTypeString(child)}`; }) .join("; "); return `{ ${fields} }`; }; const dynamicObjectTypeString = (configurations) => { if (!configurations.length) { return `import("${SPECTRAL_TYPES_MODULE}").DynamicObject`; } return configurations .map((config) => { var _a; const valuesType = ((_a = config.inputs) === null || _a === void 0 ? void 0 : _a.length) ? structuredObjectTypeString(config.inputs) : `import("${SPECTRAL_TYPES_MODULE}").StructuredObject`; return `{ configuration: ${JSON.stringify(config.key)}; values: ${valuesType} }`; }) .join(" | "); };