|
| 1 | +import { OperationObject } from "../types"; |
| 2 | +import { comment } from "../utils"; |
| 3 | +import { transformHeaderObjMap } from "./headers"; |
| 4 | +import { transformOperationObj } from "./operation"; |
| 5 | +import { transformPathsObj } from "./paths"; |
| 6 | +import { transformResponsesObj } from "./responses"; |
| 7 | +import { transformSchemaObjMap } from "./schema"; |
| 8 | + |
| 9 | +interface TransformOptions { |
| 10 | + rawSchema?: boolean; |
| 11 | + version: number; |
| 12 | +} |
| 13 | + |
| 14 | +export function transformAll(schema: any, { version, rawSchema }: TransformOptions): string { |
| 15 | + let output = ""; |
| 16 | + |
| 17 | + let operations: Record<string, OperationObject> = {}; |
| 18 | + |
| 19 | + // --raw-schema mode |
| 20 | + if (rawSchema) { |
| 21 | + switch (version) { |
| 22 | + case 2: { |
| 23 | + return `export interface definitions {\n ${transformSchemaObjMap(schema, { |
| 24 | + required: Object.keys(schema), |
| 25 | + })}\n}`; |
| 26 | + } |
| 27 | + case 3: { |
| 28 | + return `export interface schemas {\n ${transformSchemaObjMap(schema, { |
| 29 | + required: Object.keys(schema), |
| 30 | + })}\n }\n\n`; |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // #/paths (V2 & V3) |
| 36 | + output += `export interface paths {\n`; // open paths |
| 37 | + if (schema.paths) { |
| 38 | + output += transformPathsObj(schema.paths, { |
| 39 | + operations, |
| 40 | + parameters: (schema.components && schema.components.parameters) || schema.parameters, |
| 41 | + }); |
| 42 | + } |
| 43 | + output += `}\n\n`; // close paths |
| 44 | + |
| 45 | + switch (version) { |
| 46 | + case 2: { |
| 47 | + // #/definitions |
| 48 | + output += `export interface definitions {\n ${transformSchemaObjMap(schema.definitions || {}, { |
| 49 | + required: Object.keys(schema.definitions), |
| 50 | + })}\n}\n\n`; |
| 51 | + |
| 52 | + // #/parameters |
| 53 | + if (schema.parameters) { |
| 54 | + const required = Object.keys(schema.parameters); |
| 55 | + output += `export interface parameters {\n ${transformSchemaObjMap(schema.parameters, { |
| 56 | + required, |
| 57 | + })}\n }\n\n`; |
| 58 | + } |
| 59 | + |
| 60 | + // #/parameters |
| 61 | + if (schema.responses) { |
| 62 | + output += `export interface responses {\n ${transformResponsesObj(schema.responses)}\n }\n\n`; |
| 63 | + } |
| 64 | + break; |
| 65 | + } |
| 66 | + case 3: { |
| 67 | + // #/components |
| 68 | + output += `export interface components {\n`; // open components |
| 69 | + |
| 70 | + if (schema.components) { |
| 71 | + // #/components/schemas |
| 72 | + if (schema.components.schemas) { |
| 73 | + const required = Object.keys(schema.components.schemas); |
| 74 | + output += ` schemas: {\n ${transformSchemaObjMap(schema.components.schemas, { required })}\n }\n`; |
| 75 | + } |
| 76 | + |
| 77 | + // #/components/responses |
| 78 | + if (schema.components.responses) { |
| 79 | + output += ` responses: {\n ${transformResponsesObj(schema.components.responses)}\n }\n`; |
| 80 | + } |
| 81 | + |
| 82 | + // #/components/parameters |
| 83 | + if (schema.components.parameters) { |
| 84 | + const required = Object.keys(schema.components.parameters); |
| 85 | + output += ` parameters: {\n ${transformSchemaObjMap(schema.components.parameters, { |
| 86 | + required, |
| 87 | + })}\n }\n`; |
| 88 | + } |
| 89 | + |
| 90 | + // #/components/requestBodies |
| 91 | + if (schema.components.requestBodies) { |
| 92 | + const required = Object.keys(schema.components.requestBodies); |
| 93 | + output += ` requestBodies: {\n ${transformSchemaObjMap(schema.components.requestBodies, { |
| 94 | + required, |
| 95 | + })}\n }\n`; |
| 96 | + } |
| 97 | + |
| 98 | + // #/components/headers |
| 99 | + if (schema.components.headers) { |
| 100 | + output += ` headers: {\n ${transformHeaderObjMap(schema.components.headers)} }\n`; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + output += `}\n\n`; // close components |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + output += `export interface operations {\n`; // open operations |
| 110 | + if (Object.keys(operations).length) { |
| 111 | + Object.entries(operations).forEach(([operationId, operation]) => { |
| 112 | + if (operation.description) output += comment(operation.description); // handle comment |
| 113 | + output += ` "${operationId}": {\n ${transformOperationObj( |
| 114 | + operation, |
| 115 | + schema.components && schema.components.parameters |
| 116 | + )};\n }\n`; |
| 117 | + }); |
| 118 | + } |
| 119 | + output += `}\n`; // close operations |
| 120 | + |
| 121 | + return output.trim(); |
| 122 | +} |
0 commit comments