@prismatic-io/spectral
Version:
Utility library for building Prismatic connectors and code-native integrations
97 lines (96 loc) • 3.58 kB
JavaScript
;
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 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 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",
};
const getInputValueType = (input) => {
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;
};