UNPKG

@n8n/n8n-nodes-langchain

Version:

![Banner image](https://user-images.githubusercontent.com/10284570/173569848-c624317f-42b1-45a6-ab09-f0ea3c247648.png)

248 lines 7.54 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var utils_exports = {}; __export(utils_exports, { convertJsonSchemaToResourceMapperFields: () => convertJsonSchemaToResourceMapperFields, jsonSchemaTypeToDefaultValue: () => jsonSchemaTypeToDefaultValue, jsonSchemaTypeToFieldType: () => jsonSchemaTypeToFieldType }); module.exports = __toCommonJS(utils_exports); function pickFirstSchema(schema) { if (typeof schema === "object" && (schema?.anyOf || schema?.oneOf)) { if (Array.isArray(schema.anyOf) && schema.anyOf[0] !== void 0) { return schema.anyOf[0]; } if (Array.isArray(schema.oneOf) && schema.oneOf[0] !== void 0) { return schema.oneOf[0]; } } return schema; } function mergeTwoSchemas(a, b) { if (a === void 0) { return b; } if (b === void 0) { return a; } a = pickFirstSchema(a); b = pickFirstSchema(b); if (a === false || b === false) { return false; } if (a === true || b === true) { return true; } if (a.type === "object" && b.type === "object") { const properties = { ...a.properties ?? {}, ...b.properties ?? {} }; const required = [...a.required ?? [], ...b.required ?? []]; const additionalProperties = mergeTwoSchemas(a.additionalProperties, b.additionalProperties); return { ...a, ...b, properties, required, additionalProperties }; } if (a.type === "array" && b.type === "array") { if (Array.isArray(a.items) && Array.isArray(b.items)) { return a.items.length > b.items.length ? a : b; } if (Array.isArray(a.items) || Array.isArray(b.items)) { return Array.isArray(a.items) ? a : b; } const items = mergeTwoSchemas(a.items, b.items); return { ...a, ...b, items }; } return void 0; } function mergeAllOfSchemas(schemas) { if (schemas.length === 0) { return void 0; } if (schemas.length === 1) { return schemas[0]; } return schemas.reduce( (acc, schema) => mergeTwoSchemas(acc, schema), void 0 ); } function jsonSchemaTypeToDefaultValue(schema) { if (schema === false) { return null; } if (schema === true) { return "any"; } if (schema.allOf) { const mergedSchema = mergeAllOfSchemas(schema.allOf); if (mergedSchema !== void 0) { return jsonSchemaTypeToDefaultValue(mergedSchema); } } if (schema.anyOf) { const anyOfSchemas = schema.anyOf; for (const anyOfSchema of anyOfSchemas) { const defaultValue = jsonSchemaTypeToDefaultValue(anyOfSchema); if (defaultValue !== null) { return defaultValue; } } } if (schema.oneOf) { const oneOfSchemas = schema.oneOf; for (const oneOfSchema of oneOfSchemas) { const defaultValue = jsonSchemaTypeToDefaultValue(oneOfSchema); if (defaultValue !== null) { return defaultValue; } } } if (schema.enum && Array.isArray(schema.enum)) { return schema.enum[0]; } if (Array.isArray(schema.type)) { const types = schema.type; for (const type of types) { const defaultValue = jsonSchemaTypeToDefaultValue({ type }); if (defaultValue !== null) { return defaultValue; } } } if (schema.type === "number" || schema.type === "integer") { if (schema.minimum !== void 0) { return schema.minimum; } if (schema.maximum !== void 0) { return schema.maximum; } return 0; } if (schema.type === "boolean") { return false; } if (schema.type === "string") { if (schema.format === "date-time") { return "2025-01-01T00:00:00Z"; } if (schema.format === "uri" || schema.format === "url") { return "https://example.com"; } if (schema.format === "date") { return "2025-01-01"; } if (schema.format === "time") { return "00:00:00"; } return "string"; } if (schema.type === "array") { if (!schema.items) { return []; } if (Array.isArray(schema.items)) { return schema.items.map((item) => jsonSchemaTypeToDefaultValue(item)); } return [jsonSchemaTypeToDefaultValue(schema.items)]; } if (schema.type === "object") { const properties = schema.properties ?? {}; const exampleObject = {}; for (const [key, propertySchema] of Object.entries(properties)) { const propertyValue = jsonSchemaTypeToDefaultValue(propertySchema); if (propertyValue !== null) { exampleObject[key] = propertyValue; } } if (schema.additionalProperties) { const additionalProperties = jsonSchemaTypeToDefaultValue(schema.additionalProperties); if (additionalProperties !== null) { exampleObject["<additionalProperty>"] = additionalProperties; } } return exampleObject; } return null; } function jsonSchemaTypeToFieldType(schema) { if (schema.type === "string" && schema.format === "date-time") { return "dateTime"; } if (schema.type === "number" || schema.type === "integer") { return "number"; } if (schema.type === "boolean" || schema.type === "array" || schema.type === "object") { return schema.type; } return "string"; } function convertJsonSchemaToResourceMapperFields(schema) { const fields = []; if (schema.type !== "object" || !schema.properties) { return fields; } const required = Array.isArray(schema.required) ? schema.required : []; for (const [key, propertySchema] of Object.entries(schema.properties)) { if (propertySchema === false) { continue; } if (propertySchema === true) { fields.push({ id: key, displayName: key, defaultMatch: false, required: required.includes(key), display: true, type: "string" // use string as a "catch all" for any values }); continue; } const schemaType = jsonSchemaTypeToFieldType(propertySchema); let defaultValue; if (schemaType === "object" || schemaType === "array") { const result = jsonSchemaTypeToDefaultValue(propertySchema); if (result !== null) { defaultValue = JSON.stringify(result, null, 2); } } const field = { id: key, displayName: propertySchema.title ?? key, defaultMatch: false, required: required.includes(key), display: true, type: schemaType, defaultValue }; if (propertySchema.enum && Array.isArray(propertySchema.enum)) { field.type = "options"; field.options = propertySchema.enum.map((value) => ({ name: value, value })); } fields.push(field); } return fields; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { convertJsonSchemaToResourceMapperFields, jsonSchemaTypeToDefaultValue, jsonSchemaTypeToFieldType }); //# sourceMappingURL=utils.js.map