UNPKG

@n8n/n8n-nodes-langchain

Version:

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

218 lines 7.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.jsonSchemaTypeToDefaultValue = jsonSchemaTypeToDefaultValue; exports.jsonSchemaTypeToFieldType = jsonSchemaTypeToFieldType; exports.convertJsonSchemaToResourceMapperFields = convertJsonSchemaToResourceMapperFields; function pickFirstSchema(schema) { if (typeof schema === 'object' && (schema?.anyOf || schema?.oneOf)) { if (Array.isArray(schema.anyOf) && schema.anyOf[0] !== undefined) { return schema.anyOf[0]; } if (Array.isArray(schema.oneOf) && schema.oneOf[0] !== undefined) { return schema.oneOf[0]; } } return schema; } function mergeTwoSchemas(a, b) { if (a === undefined) { return b; } if (b === undefined) { 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 undefined; } function mergeAllOfSchemas(schemas) { if (schemas.length === 0) { return undefined; } if (schemas.length === 1) { return schemas[0]; } return schemas.reduce((acc, schema) => mergeTwoSchemas(acc, schema), undefined); } function jsonSchemaTypeToDefaultValue(schema) { if (schema === false) { return null; } if (schema === true) { return 'any'; } if (schema.allOf) { const mergedSchema = mergeAllOfSchemas(schema.allOf); if (mergedSchema !== undefined) { 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 !== undefined) { return schema.minimum; } if (schema.maximum !== undefined) { 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', }); 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; } //# sourceMappingURL=utils.js.map