UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

88 lines (87 loc) 3.77 kB
import { camelToSentenceCase } from '../../camelToSentenceCase.js'; import { extractSchemaProperties } from './extractSchemaProperties.js'; import { getBindingsData } from './getBindingsData.js'; import { getExamplesData } from './getExamplesData.js'; import { getExtensionsData } from './getExtensionsData.js'; export const getMessagesData = (messages, extensions) => { return messages.map((message) => { var _a, _b, _c, _d, _e; const id = message.id(); const title = (_b = (_a = message.title()) !== null && _a !== void 0 ? _a : message.name()) !== null && _b !== void 0 ? _b : message.id(); const niceTitle = title === id ? camelToSentenceCase(id) : title; const description = (_c = message.summary()) !== null && _c !== void 0 ? _c : message.description(); const payload = (_d = message.payload()) === null || _d === void 0 ? void 0 : _d['_json']; const headers = (_e = message.headers()) === null || _e === void 0 ? void 0 : _e['_json']; const payloadSchema = getMessagePayloadSchema(title, description, payload); const example = getExamplesData({ message, jsonSchema: payload, payloadSchema, extensions }); return { id, contentType: message.contentType(), payload: payloadSchema, headers: getMessageHeaders(headers), jsonPayloadSchema: payload, jsonHeadersSchema: headers, title: niceTitle, description, example, bindings: getBindingsData(message.bindings().all()), extensions: getExtensionsData(message.extensions().all()), }; }); }; const expandObjectSchema = (title, description, schema, requiredProperties) => { const schemaWithRequiredAndProperties = Object.assign(Object.assign({}, schema), { required: requiredProperties }); const properties = extractSchemaProperties(schemaWithRequiredAndProperties); const expandedSchema = [ { name: title, description, type: 'object', properties, }, ]; return expandedSchema; }; export const getMessagePayloadSchema = (title, description, payload) => { var _a, _b; const objectSchema = payload.properties || ((_a = payload['schema']) === null || _a === void 0 ? void 0 : _a.properties); const unknownTypeSchema = payload['schema'] || payload; const schemaRequired = payload.required || ((_b = payload['schema']) === null || _b === void 0 ? void 0 : _b.required); if (objectSchema) { return expandObjectSchema(title, description, objectSchema, schemaRequired); } else if (unknownTypeSchema) { if (unknownTypeSchema.type === 'object') { return expandObjectSchema(title, description, unknownTypeSchema, schemaRequired); } else { return [ Object.assign(Object.assign({}, unknownTypeSchema), { name: title, description, type: unknownTypeSchema.type || undefined }), ]; } } }; const getMessageHeaders = (headers) => { if (!headers) return []; const schemaProperties = headers.properties; if (!schemaProperties) return []; const schemaRequired = headers.required; const requiredAndProperties = Object.assign(Object.assign({}, schemaProperties), { required: schemaRequired }); const properties = requiredAndProperties ? extractSchemaProperties(requiredAndProperties) : undefined; const schema = [ { name: 'headers', description: undefined, type: 'object', properties, }, ]; if (properties && properties.length > 0) { return schema; } return []; };