UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

58 lines (57 loc) 2.63 kB
import { camelToSentenceCase } from '../../camelToSentenceCase.js'; import { getBindingsData } from './getBindingsData.js'; import { getExtensionsData } from './getExtensionsData.js'; import { getOperationsData } from './getOperationsData.js'; import { getParametersData } from './getParametersData.js'; import { getSecuritySchemesData } from './getSecuritySchemesData.js'; import { getServersData } from './getServersData.js'; export const getChannelData = ({ document, channelId, }) => { if (!channelId) return undefined; const channel = document.channels().get(channelId); if (!channel) return undefined; const title = channel.title() || camelToSentenceCase(channelId); const description = channel.description() || channel.summary() || ''; const servers = getServersData(channel.servers().all()); const address = channel.address(); const parameters = getParametersData(channel); const bindings = getBindingsData(channel.bindings().all()); const extensions = getExtensionsData(channel.extensions().all()); const { operations, sendOperations, receiveOperations, sendMessages, receiveMessages } = getOperationsAndMessagesData(channel); const securitySchemes = getSecuritySchemesData(document.securitySchemes().all()); return { id: channelId, title, description, servers, address, parameters, bindings, operations, sendOperations, receiveOperations, sendMessages, receiveMessages, extensions, securitySchemes, }; }; const getOperationsAndMessagesData = (channel) => { const operations = channel.operations().all(); const extensions = getExtensionsData(channel.extensions().all()); const allOperations = getOperationsData(operations, extensions); // the schema defines "send" and "receive" as whether the _websocket_ sends or receives messages // we need to flip them from the perspective of the user, so that "send" messages are ones the user sends and the websocket receives, and "receive" is the user receiving messages the websocket sends const sendOperations = allOperations.filter(({ type }) => type === 'receive'); const receiveOperations = allOperations.filter(({ type }) => type === 'send'); const sendMessages = sendOperations.flatMap(({ messages }) => messages); const receiveMessages = receiveOperations.flatMap(({ messages }) => messages); return { operations: allOperations, sendOperations, receiveOperations, sendMessages, receiveMessages, }; };