UNPKG

openapi-zod-client-yohoji

Version:

[![Screenshot 2022-11-12 at 18 52 25](https://user-images.githubusercontent.com/47224540/201487856-ffc4c862-6f31-4de1-8ef1-3981fabf3416.png)](https://openapi-zod-client.vercel.app/)

46 lines (40 loc) 2.07 kB
import type { SchemaObject } from "openapi3-ts"; export default function generateJSDocArray(schema: SchemaObject, withTypesAndFormat = false): string[] { const comments: string[] = []; const mapping = { description: (value: string) => `${value}`, example: (value: any) => `@example ${JSON.stringify(value)}`, examples: (value: any[]) => value.map((example, index) => `@example Example ${index + 1}: ${JSON.stringify(example)}`), deprecated: (value: boolean) => (value ? "@deprecated" : ""), default: (value: any) => `@default ${JSON.stringify(value)}`, externalDocs: (value: { url: string }) => `@see ${value.url}`, // Additional attributes that depend on `withTypesAndFormat` type: withTypesAndFormat ? (value: string | string[]) => `@type {${Array.isArray(value) ? value.join("|") : value}}` : undefined, format: withTypesAndFormat ? (value: string) => `@format ${value}` : undefined, minimum: (value: number) => `@minimum ${value}`, maximum: (value: number) => `@maximum ${value}`, minLength: (value: number) => `@minLength ${value}`, maxLength: (value: number) => `@maxLength ${value}`, pattern: (value: string) => `@pattern ${value}`, enum: (value: string[]) => `@enum ${value.join(", ")}`, }; Object.entries(mapping).forEach(([key, mappingFunction]) => { const schemaValue = schema[key as keyof SchemaObject]; if (schemaValue !== undefined && mappingFunction) { const result = mappingFunction(schemaValue); if (Array.isArray(result)) { result.forEach((subResult) => comments.push(subResult)); } else if (result) { comments.push(result); } } }); // Add a space line after description if there are other comments if (comments.length > 1 && !!schema.description) { comments.splice(1, 0, ""); } return comments; }