|
1 |
| -import { OperationObject, PathItemObject, SchemaFormatter } from "../types"; |
| 1 | +import { GlobalContext, OperationObject, PathItemObject } from "../types"; |
2 | 2 | import { comment, tsReadonly } from "../utils";
|
3 | 3 | import { transformHeaderObjMap } from "./headers";
|
4 | 4 | import { transformOperationObj } from "./operation";
|
5 | 5 | import { transformPathsObj } from "./paths";
|
6 |
| -import { transformResponsesObj, transformRequestBodies } from "./responses"; |
| 6 | +import { transformRequestBodies } from "./request"; |
| 7 | +import { transformResponsesObj } from "./responses"; |
7 | 8 | import { transformSchemaObjMap } from "./schema";
|
8 | 9 |
|
9 |
| -interface TransformOptions { |
10 |
| - formatter?: SchemaFormatter; |
11 |
| - immutableTypes: boolean; |
12 |
| - rawSchema?: boolean; |
13 |
| - version: number; |
14 |
| -} |
15 |
| - |
16 |
| -export function transformAll(schema: any, { formatter, immutableTypes, rawSchema, version }: TransformOptions): string { |
17 |
| - const readonly = tsReadonly(immutableTypes); |
| 10 | +export function transformAll(schema: any, ctx: GlobalContext): Record<string, string> { |
| 11 | + const readonly = tsReadonly(ctx.immutableTypes); |
18 | 12 |
|
19 |
| - let output = ""; |
| 13 | + let output: Record<string, string> = {}; |
20 | 14 |
|
21 | 15 | let operations: Record<string, { operation: OperationObject; pathItem: PathItemObject }> = {};
|
22 | 16 |
|
23 | 17 | // --raw-schema mode
|
24 |
| - if (rawSchema) { |
25 |
| - switch (version) { |
| 18 | + if (ctx.rawSchema) { |
| 19 | + const required = new Set(Object.keys(schema)); |
| 20 | + switch (ctx.version) { |
26 | 21 | case 2: {
|
27 |
| - return `export interface definitions {\n ${transformSchemaObjMap(schema, { |
28 |
| - formatter, |
29 |
| - immutableTypes, |
30 |
| - required: Object.keys(schema), |
31 |
| - version, |
32 |
| - })}\n}`; |
| 22 | + output.definitions = transformSchemaObjMap(schema, { ...ctx, required }); |
| 23 | + return output; |
33 | 24 | }
|
34 | 25 | case 3: {
|
35 |
| - return `export interface schemas {\n ${transformSchemaObjMap(schema, { |
36 |
| - formatter, |
37 |
| - immutableTypes, |
38 |
| - required: Object.keys(schema), |
39 |
| - version, |
40 |
| - })}\n }\n\n`; |
| 26 | + output.schemas = transformSchemaObjMap(schema, { ...ctx, required }); |
| 27 | + return output; |
41 | 28 | }
|
42 | 29 | }
|
43 | 30 | }
|
44 | 31 |
|
45 | 32 | // #/paths (V2 & V3)
|
46 |
| - output += `export interface paths {\n`; // open paths |
| 33 | + output.paths = ""; // open paths |
47 | 34 | if (schema.paths) {
|
48 |
| - output += transformPathsObj(schema.paths, { |
| 35 | + output.paths += transformPathsObj(schema.paths, { |
| 36 | + ...ctx, |
49 | 37 | globalParameters: (schema.components && schema.components.parameters) || schema.parameters,
|
50 |
| - immutableTypes, |
51 | 38 | operations,
|
52 |
| - version, |
53 | 39 | });
|
54 | 40 | }
|
55 |
| - output += `}\n\n`; // close paths |
56 | 41 |
|
57 |
| - switch (version) { |
| 42 | + switch (ctx.version) { |
58 | 43 | case 2: {
|
59 | 44 | // #/definitions
|
60 | 45 | if (schema.definitions) {
|
61 |
| - output += `export interface definitions {\n ${transformSchemaObjMap(schema.definitions, { |
62 |
| - formatter, |
63 |
| - immutableTypes, |
64 |
| - required: Object.keys(schema.definitions), |
65 |
| - version, |
66 |
| - })}\n}\n\n`; |
| 46 | + output.definitions = transformSchemaObjMap(schema.definitions, { |
| 47 | + ...ctx, |
| 48 | + required: new Set(Object.keys(schema.definitions)), |
| 49 | + }); |
67 | 50 | }
|
68 | 51 |
|
69 | 52 | // #/parameters
|
70 | 53 | if (schema.parameters) {
|
71 |
| - const required = Object.keys(schema.parameters); |
72 |
| - output += `export interface parameters {\n ${transformSchemaObjMap(schema.parameters, { |
73 |
| - formatter, |
74 |
| - immutableTypes, |
75 |
| - required, |
76 |
| - version, |
77 |
| - })}\n }\n\n`; |
| 54 | + output.parameters = transformSchemaObjMap(schema.parameters, { |
| 55 | + ...ctx, |
| 56 | + required: new Set(Object.keys(schema.parameters)), |
| 57 | + }); |
78 | 58 | }
|
79 | 59 |
|
80 | 60 | // #/parameters
|
81 | 61 | if (schema.responses) {
|
82 |
| - output += `export interface responses {\n ${transformResponsesObj(schema.responses, { |
83 |
| - formatter, |
84 |
| - immutableTypes, |
85 |
| - version, |
86 |
| - })}\n }\n\n`; |
| 62 | + output.responses = transformResponsesObj(schema.responses, ctx); |
87 | 63 | }
|
88 | 64 | break;
|
89 | 65 | }
|
90 | 66 | case 3: {
|
91 | 67 | // #/components
|
92 |
| - output += `export interface components {\n`; // open components |
| 68 | + output.components = ""; |
93 | 69 |
|
94 | 70 | if (schema.components) {
|
95 | 71 | // #/components/schemas
|
96 | 72 | if (schema.components.schemas) {
|
97 |
| - const required = Object.keys(schema.components.schemas); |
98 |
| - output += ` ${readonly}schemas: {\n ${transformSchemaObjMap(schema.components.schemas, { |
99 |
| - formatter, |
100 |
| - immutableTypes, |
101 |
| - required, |
102 |
| - version, |
| 73 | + output.components += ` ${readonly}schemas: {\n ${transformSchemaObjMap(schema.components.schemas, { |
| 74 | + ...ctx, |
| 75 | + required: new Set(Object.keys(schema.components.schemas)), |
103 | 76 | })}\n }\n`;
|
104 | 77 | }
|
105 | 78 |
|
106 | 79 | // #/components/responses
|
107 | 80 | if (schema.components.responses) {
|
108 |
| - output += ` ${readonly}responses: {\n ${transformResponsesObj(schema.components.responses, { |
109 |
| - formatter, |
110 |
| - immutableTypes, |
111 |
| - version, |
112 |
| - })}\n }\n`; |
| 81 | + output.components += ` ${readonly}responses: {\n ${transformResponsesObj( |
| 82 | + schema.components.responses, |
| 83 | + ctx |
| 84 | + )}\n }\n`; |
113 | 85 | }
|
114 | 86 |
|
115 | 87 | // #/components/parameters
|
116 | 88 | if (schema.components.parameters) {
|
117 |
| - const required = Object.keys(schema.components.parameters); |
118 |
| - output += ` ${readonly}parameters: {\n ${transformSchemaObjMap(schema.components.parameters, { |
119 |
| - formatter, |
120 |
| - immutableTypes, |
121 |
| - required, |
122 |
| - version, |
| 89 | + output.components += ` ${readonly}parameters: {\n ${transformSchemaObjMap(schema.components.parameters, { |
| 90 | + ...ctx, |
| 91 | + required: new Set(Object.keys(schema.components.parameters)), |
123 | 92 | })}\n }\n`;
|
124 | 93 | }
|
125 | 94 |
|
126 | 95 | // #/components/requestBodies
|
127 | 96 | if (schema.components.requestBodies) {
|
128 |
| - output += ` ${readonly}requestBodies: {\n ${transformRequestBodies(schema.components.requestBodies, { |
129 |
| - formatter, |
130 |
| - immutableTypes, |
131 |
| - version, |
132 |
| - })}\n }\n`; |
| 97 | + output.components += ` ${readonly}requestBodies: {\n ${transformRequestBodies( |
| 98 | + schema.components.requestBodies, |
| 99 | + ctx |
| 100 | + )}\n }\n`; |
133 | 101 | }
|
134 | 102 |
|
135 | 103 | // #/components/headers
|
136 | 104 | if (schema.components.headers) {
|
137 |
| - output += ` ${readonly}headers: {\n ${transformHeaderObjMap(schema.components.headers, { |
138 |
| - formatter, |
139 |
| - immutableTypes, |
140 |
| - version, |
141 |
| - })} }\n`; |
| 105 | + output.components += ` ${readonly}headers: {\n ${transformHeaderObjMap(schema.components.headers, { |
| 106 | + ...ctx, |
| 107 | + required: new Set<string>(), |
| 108 | + })}\n }\n`; |
142 | 109 | }
|
143 | 110 | }
|
144 |
| - |
145 |
| - output += `}\n\n`; // close components |
146 | 111 | break;
|
147 | 112 | }
|
148 | 113 | }
|
149 | 114 |
|
150 |
| - output += `export interface operations {\n`; // open operations |
| 115 | + // #/operations |
| 116 | + output.operations = ""; |
151 | 117 | if (Object.keys(operations).length) {
|
152 |
| - Object.entries(operations).forEach(([operationId, { operation, pathItem }]) => { |
153 |
| - if (operation.description) output += comment(operation.description); // handle comment |
154 |
| - output += ` ${readonly}"${operationId}": {\n ${transformOperationObj(operation, { |
| 118 | + for (const id of Object.keys(operations)) { |
| 119 | + const { operation, pathItem } = operations[id]; |
| 120 | + if (operation.description) output.operations += comment(operation.description); // handle comment |
| 121 | + output.operations += ` ${readonly}"${id}": {\n ${transformOperationObj(operation, { |
| 122 | + ...ctx, |
155 | 123 | pathItem,
|
156 | 124 | globalParameters: (schema.components && schema.components.parameters) || schema.parameters,
|
157 |
| - immutableTypes, |
158 |
| - version, |
159 | 125 | })}\n }\n`;
|
160 |
| - }); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // cleanup: trim whitespace |
| 130 | + for (const k of Object.keys(output)) { |
| 131 | + if (typeof output[k] === "string") { |
| 132 | + output[k] = output[k].trim(); |
| 133 | + } |
161 | 134 | }
|
162 |
| - output += `}\n`; // close operations |
163 | 135 |
|
164 |
| - return output.trim(); |
| 136 | + return output; |
165 | 137 | }
|
0 commit comments