UNPKG

vitepress-openapi

Version:

Generate VitePress API Documentation from OpenAPI Specification.

51 lines (45 loc) 1.79 kB
import type { OAExampleObject } from '../../types' import type { OAProperty } from '../parser/getSchemaUi' import { isFile, isFormUrlEncoded, isMultipartFormData, isXml } from '../utils/contentTypeUtils' import { getSchemaUiJson } from './getSchemaUiJson' import { getSchemaUiXml } from './getSchemaUiXml' interface ContentTypeSchemaExample { key: string summary: string valueGenerator: (uiProperties: OAProperty[] | OAProperty, useExample: boolean) => any lang?: string hideOnSchemaTabs?: boolean } function getContentTypeSchemaExample(contentType: string): ContentTypeSchemaExample { if (isXml(contentType)) { return { key: 'XML', summary: 'XML', valueGenerator: getSchemaUiXml, lang: 'xml', } } return { key: 'JSON', summary: 'JSON', valueGenerator: getSchemaUiJson, lang: 'json', hideOnSchemaTabs: isFormUrlEncoded(contentType) || isMultipartFormData(contentType) || isFile(contentType), } } export function getSchemaExample(contentType: string, uiProperties: OAProperty[] | OAProperty, useExample = false): Record<string, OAExampleObject> { const contentTypeSchemaExample = getContentTypeSchemaExample(contentType) return { [contentTypeSchemaExample.key]: getSchemaExampleValue(contentTypeSchemaExample, uiProperties, useExample), } } function getSchemaExampleValue(contentTypeSchema: ContentTypeSchemaExample, uiProperties: OAProperty[] | OAProperty, useExample = false): OAExampleObject { return { summary: contentTypeSchema.summary, description: '', value: contentTypeSchema.valueGenerator(uiProperties, useExample), isAutogenerated: true, lang: contentTypeSchema.lang ?? 'json', hideOnSchemaTabs: contentTypeSchema.hideOnSchemaTabs ?? false, } as OAExampleObject }