@mintlify/common 1.0.331 → 1.0.332

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
+ import { camelToSentenceCase } from '../camelToSentenceCase.js';
1
2
  import { parseAsyncApiString } from './parseAsyncApiString.js';
2
3
  export const getAsyncApiChannelMetadata = (asyncApiMetaField, asyncApiFiles) => {
3
- var _a, _b;
4
4
  const potentiallyParsedAsyncApiString = parseAsyncApiString(asyncApiMetaField);
5
5
  if (potentiallyParsedAsyncApiString == undefined) {
6
6
  return undefined;
@@ -9,7 +9,7 @@ export const getAsyncApiChannelMetadata = (asyncApiMetaField, asyncApiFiles) =>
9
9
  let asyncApiFile = undefined;
10
10
  for (const file of asyncApiFiles) {
11
11
  const asyncApiSpec = file.spec;
12
- const hasAsyncApiChannel = asyncApiSpec.allChannels().has(channelId);
12
+ const hasAsyncApiChannel = channelId && asyncApiSpec.allChannels().has(channelId);
13
13
  const filenameMatches = !filename || filename === file.filename || filename === file.originalFileLocation;
14
14
  if (hasAsyncApiChannel && filenameMatches) {
15
15
  asyncApiFile = file;
@@ -18,9 +18,11 @@ export const getAsyncApiChannelMetadata = (asyncApiMetaField, asyncApiFiles) =>
18
18
  if (asyncApiFile == null) {
19
19
  return undefined;
20
20
  }
21
- const channelInterface = asyncApiFile.spec.channels().get(channelId);
22
- const channelTitle = (_a = channelInterface.title()) !== null && _a !== void 0 ? _a : '';
23
- const channelDescription = (_b = channelInterface.description()) !== null && _b !== void 0 ? _b : '';
21
+ const channelInterface = channelId
22
+ ? asyncApiFile.spec.channels().get(channelId)
23
+ : undefined;
24
+ const channelTitle = (channelInterface === null || channelInterface === void 0 ? void 0 : channelInterface.title()) || camelToSentenceCase(channelId);
25
+ const channelDescription = (channelInterface === null || channelInterface === void 0 ? void 0 : channelInterface.description()) || (channelInterface === null || channelInterface === void 0 ? void 0 : channelInterface.summary()) || '';
24
26
  return {
25
27
  channelId,
26
28
  filename: asyncApiFile.filename,
@@ -0,0 +1,5 @@
1
+ export * from './getAsyncApiDocumentFromUrl.js';
2
+ export * from './validateAsyncApi.js';
3
+ export * from './parseAsyncApiString.js';
4
+ export * from './parser/getChannelData.js';
5
+ export * from './prepAsyncApiFrontmatter.js';
@@ -0,0 +1,5 @@
1
+ export * from './getAsyncApiDocumentFromUrl.js';
2
+ export * from './validateAsyncApi.js';
3
+ export * from './parseAsyncApiString.js';
4
+ export * from './parser/getChannelData.js';
5
+ export * from './prepAsyncApiFrontmatter.js';
@@ -0,0 +1,2 @@
1
+ import { SchemaProperty, AnyRecord } from '../../types/asyncapi.js';
2
+ export declare const extractSchemaProperties: (schema: AnyRecord | undefined) => SchemaProperty[];
@@ -0,0 +1,67 @@
1
+ export const extractSchemaProperties = (schema) => {
2
+ if (!schema) {
3
+ return [];
4
+ }
5
+ const properties = [];
6
+ const requiredFields = new Set(schema.required || []);
7
+ for (const [key, value] of Object.entries(schema)) {
8
+ const name = key;
9
+ if (name === 'required' || name.includes('x-parser')) {
10
+ continue;
11
+ }
12
+ if (name === 'oneOf' || name === 'anyOf' || name === 'allOf') {
13
+ const descriptions = {
14
+ oneOf: 'Must be one of these types',
15
+ anyOf: 'Can be any of these types',
16
+ allOf: 'Must contain all of these types',
17
+ };
18
+ const prop = {
19
+ name,
20
+ type: name,
21
+ description: descriptions[name],
22
+ properties: value
23
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
+ .map((item) => {
25
+ const schemaWithRequired = Object.assign(Object.assign({}, (item.properties || item)), { required: item.required });
26
+ const extracted = extractSchemaProperties(schemaWithRequired);
27
+ return extracted;
28
+ })
29
+ .flat(),
30
+ };
31
+ properties.push(prop);
32
+ continue;
33
+ }
34
+ if (value === null || value === undefined) {
35
+ continue;
36
+ }
37
+ if (typeof value === 'object' && !Array.isArray(value)) {
38
+ const keys = Object.keys(value);
39
+ if ('type' in value || keys.some((key) => /^x-.*type$/.test(key))) {
40
+ const typeKey = 'type' in value ? 'type' : keys.find((key) => /^x-.*type$/.test(key));
41
+ const prop = {
42
+ name: name,
43
+ type: value[typeKey],
44
+ description: value.description || value['const'],
45
+ deprecated: value.deprecated,
46
+ required: requiredFields.has(name),
47
+ };
48
+ if (value.properties) {
49
+ prop.properties = extractSchemaProperties(value.properties);
50
+ }
51
+ properties.push(prop);
52
+ }
53
+ else {
54
+ properties.push(...extractSchemaProperties(value));
55
+ }
56
+ }
57
+ else {
58
+ properties.push({
59
+ name,
60
+ type: Array.isArray(value) ? 'array' : typeof value,
61
+ description: typeof value === 'string' ? value : undefined,
62
+ required: requiredFields.has(name),
63
+ });
64
+ }
65
+ }
66
+ return properties;
67
+ };
@@ -0,0 +1,2 @@
1
+ import { AsyncApiBinding, BindingInterface } from '../../types/asyncapi.js';
2
+ export declare const getBindingsData: (bindings: BindingInterface[]) => AsyncApiBinding[];
@@ -0,0 +1,12 @@
1
+ import { extractSchemaProperties } from './extractSchemaProperties.js';
2
+ export const getBindingsData = (bindings) => {
3
+ return bindings.map((binding) => {
4
+ const properties = extractSchemaProperties(binding.value());
5
+ return {
6
+ protocol: binding.protocol(),
7
+ version: binding.version(),
8
+ value: binding.value(),
9
+ schemaProperties: properties,
10
+ };
11
+ });
12
+ };
@@ -0,0 +1,7 @@
1
+ import { AsyncAPIDocumentInterface, ChannelData } from '../../types/asyncapi.js';
2
+ type GetChannelDataParams = {
3
+ document: AsyncAPIDocumentInterface;
4
+ channelId: string | undefined;
5
+ };
6
+ export declare const getChannelData: ({ document, channelId, }: GetChannelDataParams) => ChannelData | undefined;
7
+ export {};
@@ -0,0 +1,57 @@
1
+ import { camelToSentenceCase } from '../../camelToSentenceCase.js';
2
+ import { getBindingsData } from './getBindingsData.js';
3
+ import { getExtensionsData } from './getExtensionsData.js';
4
+ import { getOperationsData } from './getOperationsData.js';
5
+ import { getParametersData } from './getParametersData.js';
6
+ import { getSecuritySchemesData } from './getSecuritySchemesData.js';
7
+ import { getServersData } from './getServersData.js';
8
+ export const getChannelData = ({ document, channelId, }) => {
9
+ if (!channelId)
10
+ return undefined;
11
+ const channel = document.channels().get(channelId);
12
+ if (!channel)
13
+ return undefined;
14
+ const title = channel.title() || camelToSentenceCase(channelId);
15
+ const description = channel.description() || channel.summary() || '';
16
+ const servers = getServersData(channel.servers().all());
17
+ const address = channel.address();
18
+ const parameters = getParametersData(channel);
19
+ const bindings = getBindingsData(channel.bindings().all());
20
+ const extensions = getExtensionsData(channel.extensions().all());
21
+ const { operations, sendOperations, receiveOperations, sendMessages, receiveMessages } = getOperationsAndMessagesData(channel);
22
+ const securitySchemes = getSecuritySchemesData(document.securitySchemes().all());
23
+ return {
24
+ id: channelId,
25
+ title,
26
+ description,
27
+ servers,
28
+ address,
29
+ parameters,
30
+ bindings,
31
+ operations,
32
+ sendOperations,
33
+ receiveOperations,
34
+ sendMessages,
35
+ receiveMessages,
36
+ extensions,
37
+ securitySchemes,
38
+ };
39
+ };
40
+ const getOperationsAndMessagesData = (channel) => {
41
+ const operations = channel.operations().all();
42
+ const extensions = getExtensionsData(channel.extensions().all());
43
+ const allOperations = getOperationsData(operations, extensions);
44
+ // the schema defines "send" and "receive" as whether the _websocket_ sends or receives messages
45
+ // 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
46
+ const sendOperations = allOperations.filter(({ type }) => type === 'receive');
47
+ const receiveOperations = allOperations.filter(({ type }) => type === 'send');
48
+ const sendMessages = sendOperations.flatMap(({ messages }) => messages);
49
+ const receiveMessages = receiveOperations.flatMap(({ messages }) => messages);
50
+ return {
51
+ operations: allOperations,
52
+ sendOperations,
53
+ receiveOperations,
54
+ sendMessages,
55
+ receiveMessages,
56
+ };
57
+ };
@@ -0,0 +1,6 @@
1
+ import { MessageInterface, AnyRecord, AsyncApiExtension } from '../../types/asyncapi.js';
2
+ export declare const getExamplesData: ({ message, payloadSchema, extensions, }: {
3
+ message: MessageInterface;
4
+ payloadSchema: AnyRecord;
5
+ extensions: AsyncApiExtension[];
6
+ }) => string;
@@ -0,0 +1,68 @@
1
+ import { generateExampleFromSchema } from '@mintlify/validation';
2
+ export const getExamplesData = ({ message, payloadSchema, extensions, }) => {
3
+ var _a, _b;
4
+ const id = message.id().toLowerCase();
5
+ const examples = message.examples();
6
+ const hasInlineExamples = examples.length > 0;
7
+ if (hasInlineExamples) {
8
+ const example = (_b = (_a = examples[0]) === null || _a === void 0 ? void 0 : _a['_json']) === null || _b === void 0 ? void 0 : _b['payload'];
9
+ return JSON.stringify(example, null, 2);
10
+ }
11
+ const extensionExamples = createMessageExamplesMap(extensions);
12
+ const hasExtensionExamples = extensionExamples.has(id);
13
+ if (hasExtensionExamples) {
14
+ return JSON.stringify(extensionExamples.get(id), null, 2);
15
+ }
16
+ const generatedExample = generateMessageExample(payloadSchema);
17
+ return JSON.stringify(generatedExample, null, 2);
18
+ };
19
+ const generateMessageExample = (payloadSchema) => {
20
+ const schema = {
21
+ type: 'object',
22
+ properties: payloadSchema.reduce((acc, schema) => {
23
+ if (schema.properties) {
24
+ return Object.assign(Object.assign({}, acc), schema.properties.reduce((props, prop) => {
25
+ if (prop.properties) {
26
+ return Object.assign(Object.assign({}, props), { [prop.name]: [
27
+ {
28
+ type: 'object',
29
+ properties: prop.properties.reduce((nestedProps, nestedProp) => (Object.assign(Object.assign({}, nestedProps), { [nestedProp.name]: [
30
+ {
31
+ type: nestedProp.type,
32
+ required: nestedProp.required,
33
+ description: nestedProp.description,
34
+ },
35
+ ] })), {}),
36
+ },
37
+ ] });
38
+ }
39
+ return Object.assign(Object.assign({}, props), { [prop.name]: [
40
+ {
41
+ type: prop.type,
42
+ required: prop.required,
43
+ description: prop.description,
44
+ },
45
+ ] });
46
+ }, {}));
47
+ }
48
+ return acc;
49
+ }, {}),
50
+ };
51
+ const example = generateExampleFromSchema(schema, 0);
52
+ return example;
53
+ };
54
+ const createMessageExamplesMap = (extensions) => {
55
+ const examplesMap = new Map();
56
+ const examplesExt = extensions.find((ext) => ext.id.match(/^x-.*examples$/));
57
+ if (!examplesExt)
58
+ return examplesMap;
59
+ examplesExt.value.forEach((item) => {
60
+ item.messages.forEach((message) => {
61
+ const id = message.messageId.toLowerCase();
62
+ examplesMap.set(id, {
63
+ value: message.value,
64
+ });
65
+ });
66
+ });
67
+ return examplesMap;
68
+ };
@@ -0,0 +1,2 @@
1
+ import { AsyncApiExtension, ExtensionInterface } from '../../types/asyncapi.js';
2
+ export declare const getExtensionsData: (extensions: ExtensionInterface[]) => AsyncApiExtension[];
@@ -0,0 +1,8 @@
1
+ export const getExtensionsData = (extensions) => {
2
+ return extensions.map((extension) => {
3
+ return {
4
+ id: extension.id(),
5
+ value: extension.value(),
6
+ };
7
+ });
8
+ };
@@ -0,0 +1,8 @@
1
+ import { AnyRecord, AsyncApiMessage, AsyncApiExtension, MessageInterface } from '../../types/asyncapi.js';
2
+ export declare const getMessagesData: (messages: MessageInterface[], extensions: AsyncApiExtension[]) => AsyncApiMessage[];
3
+ export declare const getMessagePayloadSchema: (title: string, description: string | undefined, payload: AnyRecord) => {
4
+ name: string;
5
+ description: string | undefined;
6
+ type: string | undefined;
7
+ properties: import("../../types/asyncapi.js").SchemaProperty[] | undefined;
8
+ }[];
@@ -0,0 +1,77 @@
1
+ import { camelToSentenceCase } from '../../camelToSentenceCase.js';
2
+ import { extractSchemaProperties } from './extractSchemaProperties.js';
3
+ import { getBindingsData } from './getBindingsData.js';
4
+ import { getExamplesData } from './getExamplesData.js';
5
+ import { getExtensionsData } from './getExtensionsData.js';
6
+ export const getMessagesData = (messages, extensions) => {
7
+ return messages.map((message) => {
8
+ var _a, _b, _c, _d, _e;
9
+ const id = message.id();
10
+ const title = (_b = (_a = message.title()) !== null && _a !== void 0 ? _a : message.name()) !== null && _b !== void 0 ? _b : message.id();
11
+ const niceTitle = title === id ? camelToSentenceCase(id) : title;
12
+ const description = (_c = message.summary()) !== null && _c !== void 0 ? _c : message.description();
13
+ const payload = (_d = message.payload()) === null || _d === void 0 ? void 0 : _d['_json'];
14
+ const headers = (_e = message.headers()) === null || _e === void 0 ? void 0 : _e['_json'];
15
+ const payloadSchema = getMessagePayloadSchema(title, description, payload);
16
+ const example = getExamplesData({
17
+ message,
18
+ payloadSchema,
19
+ extensions,
20
+ });
21
+ return {
22
+ id,
23
+ contentType: message.contentType(),
24
+ payload: payloadSchema,
25
+ headers: getMessageHeaders(headers),
26
+ jsonPayloadSchema: payload,
27
+ jsonHeadersSchema: headers,
28
+ title: niceTitle,
29
+ description,
30
+ example,
31
+ bindings: getBindingsData(message.bindings().all()),
32
+ extensions: getExtensionsData(message.extensions().all()),
33
+ };
34
+ });
35
+ };
36
+ export const getMessagePayloadSchema = (title, description, payload) => {
37
+ var _a, _b;
38
+ const schemaProperties = payload.properties || ((_a = payload['schema']) === null || _a === void 0 ? void 0 : _a.properties) || payload['schema'] || payload;
39
+ const schemaRequired = payload.required || ((_b = payload['schema']) === null || _b === void 0 ? void 0 : _b.required);
40
+ const requiredAndProperties = Object.assign(Object.assign({}, schemaProperties), { required: schemaRequired });
41
+ const properties = requiredAndProperties
42
+ ? extractSchemaProperties(requiredAndProperties)
43
+ : undefined;
44
+ const schema = [
45
+ {
46
+ name: title,
47
+ description,
48
+ type: payload.type || properties ? 'object' : undefined,
49
+ properties,
50
+ },
51
+ ];
52
+ return schema;
53
+ };
54
+ const getMessageHeaders = (headers) => {
55
+ if (!headers)
56
+ return [];
57
+ const schemaProperties = headers.properties;
58
+ if (!schemaProperties)
59
+ return [];
60
+ const schemaRequired = headers.required;
61
+ const requiredAndProperties = Object.assign(Object.assign({}, schemaProperties), { required: schemaRequired });
62
+ const properties = requiredAndProperties
63
+ ? extractSchemaProperties(requiredAndProperties)
64
+ : undefined;
65
+ const schema = [
66
+ {
67
+ name: 'headers',
68
+ description: undefined,
69
+ type: 'object',
70
+ properties,
71
+ },
72
+ ];
73
+ if (properties && properties.length > 0) {
74
+ return schema;
75
+ }
76
+ return [];
77
+ };
@@ -0,0 +1,2 @@
1
+ import { OperationInterface, AsyncApiExtension, AsyncApiOperation } from '../../types/asyncapi.js';
2
+ export declare const getOperationsData: (operations: OperationInterface[], extensions: AsyncApiExtension[]) => AsyncApiOperation[];
@@ -0,0 +1,24 @@
1
+ import { camelToSentenceCase } from '../../camelToSentenceCase.js';
2
+ import { getBindingsData } from './getBindingsData.js';
3
+ import { getMessagesData } from './getMessagesData.js';
4
+ export const getOperationsData = (operations, extensions) => {
5
+ return operations.map((operation) => {
6
+ var _a, _b, _c, _d;
7
+ const id = (_a = operation.operationId()) !== null && _a !== void 0 ? _a : operation.id();
8
+ const title = (_c = (_b = operation['_json']) === null || _b === void 0 ? void 0 : _b.title) !== null && _c !== void 0 ? _c : id;
9
+ const type = operation.action();
10
+ const niceTitle = id && title === id ? camelToSentenceCase(id) : title;
11
+ const description = (_d = operation.summary()) !== null && _d !== void 0 ? _d : operation.description();
12
+ const messages = getMessagesData(operation.messages().all(), extensions);
13
+ const bindings = getBindingsData(operation.bindings().all());
14
+ return {
15
+ id,
16
+ title: niceTitle,
17
+ description,
18
+ type,
19
+ messages,
20
+ bindings,
21
+ extensions,
22
+ };
23
+ });
24
+ };
@@ -0,0 +1,2 @@
1
+ import { AsyncAPIChannel, AsyncApiParameter } from '../../types/asyncapi.js';
2
+ export declare const getParametersData: (channel: AsyncAPIChannel) => AsyncApiParameter[];
@@ -0,0 +1,20 @@
1
+ export const getParametersData = (channel) => {
2
+ const parameters = channel.parameters().all();
3
+ return parameters.map((parameter) => {
4
+ var _a, _b, _c;
5
+ const id = parameter.id();
6
+ const deprecated = (_a = parameter.schema()) === null || _a === void 0 ? void 0 : _a.deprecated();
7
+ const jsonSchema = (_b = parameter.schema()) === null || _b === void 0 ? void 0 : _b['_json'];
8
+ const type = jsonSchema['type'] || undefined;
9
+ const propertiesCount = ((_c = jsonSchema['properties']) === null || _c === void 0 ? void 0 : _c.length) || undefined;
10
+ return {
11
+ id,
12
+ jsonSchema,
13
+ description: parameter.description(),
14
+ type,
15
+ propertiesCount,
16
+ required: true,
17
+ deprecated,
18
+ };
19
+ });
20
+ };
@@ -0,0 +1,2 @@
1
+ import { AsyncApiSecurityScheme, SecuritySchemeInterface } from '../../types/asyncapi.js';
2
+ export declare const getSecuritySchemesData: (securitySchemes: SecuritySchemeInterface[]) => AsyncApiSecurityScheme[];
@@ -0,0 +1,16 @@
1
+ import { getExtensionsData } from './getExtensionsData.js';
2
+ export const getSecuritySchemesData = (securitySchemes) => {
3
+ return securitySchemes.map((securityScheme) => {
4
+ return {
5
+ id: securityScheme.id(),
6
+ name: securityScheme.name() || securityScheme.id(),
7
+ type: securityScheme.type(),
8
+ description: securityScheme.description(),
9
+ in: securityScheme.in(),
10
+ scheme: securityScheme.scheme(),
11
+ bearerFormat: securityScheme.bearerFormat(),
12
+ openIdConnectUrl: securityScheme.openIdConnectUrl(),
13
+ extensions: getExtensionsData(securityScheme.extensions().all()),
14
+ };
15
+ });
16
+ };
@@ -0,0 +1,2 @@
1
+ import { Server, ServerInterface } from '../../types/asyncapi.js';
2
+ export declare const getServersData: (servers: ServerInterface[]) => Server[];
@@ -0,0 +1,22 @@
1
+ import { getBindingsData } from './getBindingsData.js';
2
+ export const getServersData = (servers) => {
3
+ return servers.map((server) => {
4
+ const variables = server.variables().all();
5
+ const variablesData = variables.map((variable) => {
6
+ return {
7
+ id: variable.id(),
8
+ description: variable.description(),
9
+ defaultValue: variable.defaultValue(),
10
+ allowedValues: variable.allowedValues(),
11
+ examples: variable.examples(),
12
+ };
13
+ });
14
+ return {
15
+ id: server.id(),
16
+ protocol: server.protocol(),
17
+ host: server.host(),
18
+ bindings: getBindingsData(server.bindings().all()),
19
+ variables: variablesData,
20
+ };
21
+ });
22
+ };
package/dist/index.d.ts CHANGED
@@ -15,5 +15,4 @@ export * from './divisions/index.js';
15
15
  export * from './title.js';
16
16
  export * from './schema/common.js';
17
17
  export * from './camelToSentenceCase.js';
18
- export * from './asyncapi/getAsyncApiDocumentFromUrl.js';
19
- export * from './asyncapi/validateAsyncApi.js';
18
+ export * from './asyncapi/index.js';
package/dist/index.js CHANGED
@@ -15,5 +15,4 @@ export * from './divisions/index.js';
15
15
  export * from './title.js';
16
16
  export * from './schema/common.js';
17
17
  export * from './camelToSentenceCase.js';
18
- export * from './asyncapi/getAsyncApiDocumentFromUrl.js';
19
- export * from './asyncapi/validateAsyncApi.js';
18
+ export * from './asyncapi/index.js';