UNPKG

nope-js-node

Version:

NoPE Runtime for Nodejs. For Browser-Support please use nope-browser

97 lines (96 loc) 3.48 kB
"use strict"; /** * @author Martin Karkowski * @email m.karkowski@zema.de */ Object.defineProperty(exports, "__esModule", { value: true }); exports.parseFunctionToJsonSchema = void 0; const jsonSchemaMethods_1 = require("./jsonSchemaMethods"); const objectMethods_1 = require("./objectMethods"); /** * Helper, to parse a {@link INopeDescriptor} to a * @param schema * @param toJSONSchema * @param workWithRefs * @param definitions * @returns */ function parseFunctionToJsonSchema(schema, toJSONSchema = true, workWithRefs = true, definitions = {}, prePathInput = "input", prePathOutput = "output", splitChar = objectMethods_1.SPLITCHAR) { if (schema.type === "function") { const inputSchema = { type: "object", properties: {}, required: [], definitions: {}, }; /** * A Helper Function, to parse the Parameter * @param schemaToStore * @param name * @param optional * @param schema * @param preString */ function parseParameter(schemaToStore, name, optional, schema, preString, isInput) { if (toJSONSchema && !(0, jsonSchemaMethods_1.isJsonSchema)(schema)) { throw Error("Schema contains functions as paramter"); } if (isInput) order.push(name); if (!optional) { schemaToStore.required.push(name); } if (workWithRefs) { const ref = preString ? preString + splitChar + name : name; // store the id. ids.push(ref); // We only want to store the Reference. schemaToStore.properties[name] = { $ref: ref, }; // Now store the element as Reference schema["$id"] = ref; definitions[ref] = schema; } else { schemaToStore.properties[name] = schema; } } const order = []; const ids = []; for (const input of schema.inputs || []) { parseParameter(inputSchema, input.name, input.optional, input.schema, prePathInput, true); } // Now lets store the definitions[prePathInput] = inputSchema; if (Array.isArray(schema.outputs)) { const outputSchema = { type: "object", properties: {}, required: [], definitions: {}, }; for (const output of schema.outputs) { parseParameter(outputSchema, output.name, output.optional, output.schema, prePathOutput, false); } definitions[prePathInput] = outputSchema; } else if (schema.outputs) { if (toJSONSchema && !(0, jsonSchemaMethods_1.isJsonSchema)(schema.outputs)) { throw Error("Output contains a Function => it can not be parsed"); } definitions[prePathOutput] = schema.outputs; } return { definitions, order, ids, inputId: prePathInput, outputId: prePathOutput, }; } else { throw Error("Expecting a function"); } } exports.parseFunctionToJsonSchema = parseFunctionToJsonSchema;