oas-truth 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +33 -0
- package/dist/index.d.ts +587 -0
- package/dist/index.js +412 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 - present, OAS Truth Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# oas-truth
|
|
2
|
+
|
|
3
|
+
Read OpenAPI / TypeSpec as the single source of truth and get a typed `OpenAPI` document back.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install oas-truth
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { parseOpenAPI } from 'oas-truth'
|
|
15
|
+
|
|
16
|
+
const result = await parseOpenAPI('openapi.yaml') // supports .json / .yaml / .tsp
|
|
17
|
+
|
|
18
|
+
if (result.ok) {
|
|
19
|
+
result.value // resolved, typed OpenAPI document
|
|
20
|
+
} else {
|
|
21
|
+
console.error(result.error)
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`parseOpenAPI` does the following:
|
|
26
|
+
|
|
27
|
+
- OpenAPI files (`.json` / `.yaml`) are bundled with `@apidevtools/swagger-parser` to resolve `$ref`
|
|
28
|
+
- TypeSpec files (`.tsp`) are compiled with `@typespec/compiler` and then bundled
|
|
29
|
+
- Returns `{ ok: true, value } | { ok: false, error }` (never throws)
|
|
30
|
+
|
|
31
|
+
## License
|
|
32
|
+
|
|
33
|
+
[MIT](https://github.com/nakita628/oas-truth/blob/main/LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
import SwaggerParser from "@apidevtools/swagger-parser";
|
|
2
|
+
|
|
3
|
+
//#region src/openapi/index.d.ts
|
|
4
|
+
declare function parseOpenAPI(input: string): Promise<{
|
|
5
|
+
readonly ok: true;
|
|
6
|
+
readonly value: OpenAPI;
|
|
7
|
+
readonly error?: never;
|
|
8
|
+
} | {
|
|
9
|
+
readonly ok: false;
|
|
10
|
+
readonly error: string;
|
|
11
|
+
readonly value?: never;
|
|
12
|
+
}>;
|
|
13
|
+
type BaseOpenAPI = Awaited<ReturnType<typeof SwaggerParser.bundle>>;
|
|
14
|
+
type OpenAPI = BaseOpenAPI & {
|
|
15
|
+
readonly openapi?: string;
|
|
16
|
+
readonly $self?: string;
|
|
17
|
+
readonly info?: {
|
|
18
|
+
readonly title?: string;
|
|
19
|
+
readonly summary?: string;
|
|
20
|
+
readonly description?: string;
|
|
21
|
+
readonly termsOfService?: string;
|
|
22
|
+
readonly contact?: {
|
|
23
|
+
readonly name?: string;
|
|
24
|
+
readonly url?: string;
|
|
25
|
+
readonly email?: string;
|
|
26
|
+
};
|
|
27
|
+
readonly license?: {
|
|
28
|
+
readonly name?: string;
|
|
29
|
+
readonly identifier?: string;
|
|
30
|
+
readonly url?: string;
|
|
31
|
+
};
|
|
32
|
+
readonly version?: string;
|
|
33
|
+
};
|
|
34
|
+
readonly jsonSchemaDialect?: string;
|
|
35
|
+
readonly servers?: readonly Server[];
|
|
36
|
+
readonly paths: PathItem;
|
|
37
|
+
readonly webhooks?: {
|
|
38
|
+
readonly [k: string]: PathItem;
|
|
39
|
+
};
|
|
40
|
+
readonly components?: Components;
|
|
41
|
+
readonly security?: readonly {
|
|
42
|
+
readonly [k: string]: readonly string[];
|
|
43
|
+
}[];
|
|
44
|
+
readonly tags?: {
|
|
45
|
+
readonly name: string;
|
|
46
|
+
readonly summary?: string;
|
|
47
|
+
readonly description?: string;
|
|
48
|
+
readonly externalDocs?: ExternalDocs;
|
|
49
|
+
readonly parent?: string;
|
|
50
|
+
readonly kind?: string;
|
|
51
|
+
}[];
|
|
52
|
+
readonly externalDocs?: ExternalDocs;
|
|
53
|
+
} & {
|
|
54
|
+
paths: OpenAPIPaths;
|
|
55
|
+
};
|
|
56
|
+
type Components = {
|
|
57
|
+
readonly schemas?: {
|
|
58
|
+
readonly [k: string]: Schema;
|
|
59
|
+
};
|
|
60
|
+
readonly responses?: {
|
|
61
|
+
readonly [k: string]: Responses;
|
|
62
|
+
};
|
|
63
|
+
readonly parameters?: {
|
|
64
|
+
readonly [k: string]: Parameter;
|
|
65
|
+
};
|
|
66
|
+
readonly examples?: {
|
|
67
|
+
readonly [k: string]: {
|
|
68
|
+
readonly summary?: string;
|
|
69
|
+
readonly description?: string;
|
|
70
|
+
readonly dataValue?: unknown;
|
|
71
|
+
readonly serializedValue?: string;
|
|
72
|
+
readonly externalValue?: string;
|
|
73
|
+
readonly value?: unknown;
|
|
74
|
+
} | Reference;
|
|
75
|
+
};
|
|
76
|
+
readonly requestBodies?: {
|
|
77
|
+
readonly [k: string]: RequestBody;
|
|
78
|
+
};
|
|
79
|
+
readonly headers?: {
|
|
80
|
+
readonly [k: string]: Header | Reference;
|
|
81
|
+
};
|
|
82
|
+
readonly securitySchemes?: {
|
|
83
|
+
readonly [k: string]: {
|
|
84
|
+
readonly type?: string;
|
|
85
|
+
readonly description?: string;
|
|
86
|
+
readonly name?: string;
|
|
87
|
+
readonly in?: string;
|
|
88
|
+
readonly scheme?: string;
|
|
89
|
+
readonly bearerFormat?: string;
|
|
90
|
+
readonly flows?: OAuthFlow;
|
|
91
|
+
readonly password?: OAuthFlow;
|
|
92
|
+
readonly clientCredentials?: OAuthFlow;
|
|
93
|
+
readonly authorizationCode?: OAuthFlow;
|
|
94
|
+
readonly deviceAuthorization?: OAuthFlow;
|
|
95
|
+
readonly openIdConnectUrl?: string;
|
|
96
|
+
readonly oauth2MetadataUrl?: string;
|
|
97
|
+
readonly deprecated?: boolean;
|
|
98
|
+
} | Reference;
|
|
99
|
+
};
|
|
100
|
+
readonly links?: {
|
|
101
|
+
readonly [k: string]: Link | Reference;
|
|
102
|
+
};
|
|
103
|
+
readonly callbacks?: {
|
|
104
|
+
readonly [k: string]: Callbacks | Reference;
|
|
105
|
+
};
|
|
106
|
+
readonly pathItems?: {
|
|
107
|
+
readonly [k: string]: PathItem;
|
|
108
|
+
};
|
|
109
|
+
readonly mediaTypes?: {
|
|
110
|
+
readonly [k: string]: Media | Reference;
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
type OAuthFlow = {
|
|
114
|
+
readonly implicit?: {
|
|
115
|
+
readonly authorizationUrl: string;
|
|
116
|
+
readonly deviceAuthorizationUrl: string;
|
|
117
|
+
readonly tokenUrl: string;
|
|
118
|
+
readonly refreshUrl: string;
|
|
119
|
+
readonly scopes: {
|
|
120
|
+
readonly [k: string]: string;
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
type OpenAPIPaths = { readonly [P in keyof NonNullable<BaseOpenAPI['paths']>]: PathItem };
|
|
125
|
+
type Type = 'string' | 'number' | 'integer' | 'date' | 'boolean' | 'array' | 'object' | 'null';
|
|
126
|
+
type Format = FormatString | FormatNumber;
|
|
127
|
+
type FormatString = 'email' | 'uuid' | 'uuidv4' | 'uuidv6' | 'uuidv7' | 'url' | 'uri' | 'httpUrl' | 'hostname' | 'hex' | 'jwt' | 'emoji' | 'base64' | 'base64url' | 'nanoid' | 'cuid' | 'cuid2' | 'ulid' | 'ipv4' | 'ipv6' | 'cidrv4' | 'cidrv6' | 'date' | 'time' | 'date-time' | 'duration' | 'binary' | 'byte' | 'password' | 'mac' | 'hash' | 'e164' | 'guid' | 'toLowerCase' | 'toUpperCase' | 'trim';
|
|
128
|
+
type FormatNumber = 'int32' | 'int64' | 'bigint' | 'float' | 'float32' | 'float64' | 'double' | 'password';
|
|
129
|
+
type Ref = `#/components/schemas/${string}` | `#/components/responses/${string}` | `#/components/parameters/${string}` | `#/components/examples/${string}` | `#/components/requestBodies/${string}` | `#/components/headers/${string}` | `#/components/securitySchemes/${string}` | `#/components/links/${string}` | `#/components/callbacks/${string}` | `#/components/pathItems/${string}` | `#/components/mediaTypes/${string}`;
|
|
130
|
+
type Server = {
|
|
131
|
+
readonly url: string;
|
|
132
|
+
readonly description?: string;
|
|
133
|
+
readonly name?: string;
|
|
134
|
+
readonly variables?: {
|
|
135
|
+
readonly [k: string]: {
|
|
136
|
+
readonly enum?: readonly string[];
|
|
137
|
+
readonly default?: string;
|
|
138
|
+
readonly description?: string;
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
type Header = {
|
|
143
|
+
readonly description?: string;
|
|
144
|
+
readonly required?: boolean;
|
|
145
|
+
readonly deprecated?: boolean;
|
|
146
|
+
readonly example?: unknown;
|
|
147
|
+
readonly examples?: {
|
|
148
|
+
readonly [k: string]: {
|
|
149
|
+
readonly summary?: string;
|
|
150
|
+
readonly description?: string;
|
|
151
|
+
readonly defaultValue?: unknown;
|
|
152
|
+
readonly serializedValue?: string;
|
|
153
|
+
readonly externalValue?: string;
|
|
154
|
+
readonly value?: unknown;
|
|
155
|
+
} | Reference;
|
|
156
|
+
};
|
|
157
|
+
style?: string;
|
|
158
|
+
explode?: boolean;
|
|
159
|
+
allowReserved?: boolean;
|
|
160
|
+
schema?: Schema;
|
|
161
|
+
content?: Content;
|
|
162
|
+
};
|
|
163
|
+
type Link = {
|
|
164
|
+
readonly operationRef?: string;
|
|
165
|
+
readonly operationId?: string;
|
|
166
|
+
readonly parameters?: {
|
|
167
|
+
readonly [k: string]: unknown;
|
|
168
|
+
};
|
|
169
|
+
readonly requestBody?: unknown;
|
|
170
|
+
readonly description?: string;
|
|
171
|
+
readonly server?: Server;
|
|
172
|
+
};
|
|
173
|
+
type Reference = {
|
|
174
|
+
readonly $ref?: Ref;
|
|
175
|
+
readonly summary?: string;
|
|
176
|
+
readonly description?: string;
|
|
177
|
+
};
|
|
178
|
+
type Encoding = {
|
|
179
|
+
readonly contentType?: string;
|
|
180
|
+
readonly headers?: {
|
|
181
|
+
readonly [k: string]: Header | Reference;
|
|
182
|
+
};
|
|
183
|
+
readonly encoding?: {
|
|
184
|
+
readonly [k: string]: Encoding;
|
|
185
|
+
};
|
|
186
|
+
readonly prefixEncoding?: Encoding;
|
|
187
|
+
readonly itemEncoding?: Encoding;
|
|
188
|
+
};
|
|
189
|
+
type Content = {
|
|
190
|
+
readonly [k: string]: Media;
|
|
191
|
+
};
|
|
192
|
+
type PathItem = {
|
|
193
|
+
readonly $ref?: Ref;
|
|
194
|
+
readonly summary?: string;
|
|
195
|
+
readonly description?: string;
|
|
196
|
+
readonly get?: Operation;
|
|
197
|
+
readonly put?: Operation;
|
|
198
|
+
readonly post?: Operation;
|
|
199
|
+
readonly delete?: Operation;
|
|
200
|
+
readonly options?: Operation;
|
|
201
|
+
readonly head?: Operation;
|
|
202
|
+
readonly patch?: Operation;
|
|
203
|
+
readonly trace?: Operation;
|
|
204
|
+
readonly query?: Operation;
|
|
205
|
+
readonly additionalOperations?: {
|
|
206
|
+
readonly [k: string]: Operation;
|
|
207
|
+
};
|
|
208
|
+
readonly servers?: readonly Server[];
|
|
209
|
+
readonly parameters?: readonly (Parameter | Reference)[];
|
|
210
|
+
};
|
|
211
|
+
type Operation = {
|
|
212
|
+
readonly tags?: readonly string[];
|
|
213
|
+
readonly summary?: string;
|
|
214
|
+
readonly description?: string;
|
|
215
|
+
readonly externalDocs?: {
|
|
216
|
+
readonly description?: string;
|
|
217
|
+
readonly url: string;
|
|
218
|
+
};
|
|
219
|
+
readonly operationId?: string;
|
|
220
|
+
readonly parameters?: readonly (Parameter | Reference)[];
|
|
221
|
+
readonly requestBody?: RequestBody | Reference;
|
|
222
|
+
readonly responses: {
|
|
223
|
+
readonly [k: string]: Responses;
|
|
224
|
+
};
|
|
225
|
+
readonly callbacks?: {
|
|
226
|
+
readonly [k: string]: {
|
|
227
|
+
readonly $ref?: string;
|
|
228
|
+
readonly summary?: string;
|
|
229
|
+
readonly description?: string;
|
|
230
|
+
};
|
|
231
|
+
};
|
|
232
|
+
readonly deprecated?: boolean;
|
|
233
|
+
readonly security?: readonly {
|
|
234
|
+
readonly [scheme: string]: readonly string[];
|
|
235
|
+
}[];
|
|
236
|
+
readonly servers?: readonly {
|
|
237
|
+
readonly url: string;
|
|
238
|
+
readonly description?: string;
|
|
239
|
+
readonly variables?: {
|
|
240
|
+
readonly [k: string]: {
|
|
241
|
+
readonly enum?: readonly string[];
|
|
242
|
+
readonly default?: string;
|
|
243
|
+
readonly description?: string;
|
|
244
|
+
};
|
|
245
|
+
};
|
|
246
|
+
}[];
|
|
247
|
+
readonly 'x-pagination'?: boolean;
|
|
248
|
+
};
|
|
249
|
+
type Responses = {
|
|
250
|
+
readonly $ref?: Ref;
|
|
251
|
+
readonly summary?: string;
|
|
252
|
+
readonly description?: string;
|
|
253
|
+
readonly content?: Content;
|
|
254
|
+
readonly headers?: {
|
|
255
|
+
readonly [k: string]: Header | Reference;
|
|
256
|
+
};
|
|
257
|
+
readonly links?: {
|
|
258
|
+
readonly [k: string]: Link | Reference;
|
|
259
|
+
};
|
|
260
|
+
};
|
|
261
|
+
type Discriminator = {
|
|
262
|
+
readonly propertyName?: string;
|
|
263
|
+
readonly mapping?: {
|
|
264
|
+
readonly [k: string]: string;
|
|
265
|
+
};
|
|
266
|
+
readonly defaultMapping?: string;
|
|
267
|
+
};
|
|
268
|
+
type ExternalDocs = {
|
|
269
|
+
readonly url: string;
|
|
270
|
+
readonly description?: string;
|
|
271
|
+
};
|
|
272
|
+
type Schema = {
|
|
273
|
+
readonly discriminator?: Discriminator;
|
|
274
|
+
readonly xml?: {
|
|
275
|
+
readonly nodeType?: string;
|
|
276
|
+
readonly name?: string;
|
|
277
|
+
readonly namespace?: string;
|
|
278
|
+
readonly prefix?: string;
|
|
279
|
+
readonly attribute?: boolean;
|
|
280
|
+
readonly wrapped?: boolean;
|
|
281
|
+
};
|
|
282
|
+
readonly externalDocs?: ExternalDocs;
|
|
283
|
+
readonly example?: unknown;
|
|
284
|
+
readonly examples?: {
|
|
285
|
+
readonly [k: string]: {
|
|
286
|
+
readonly summary?: string;
|
|
287
|
+
readonly description?: string;
|
|
288
|
+
readonly defaultValue?: unknown;
|
|
289
|
+
readonly serializedValue?: string;
|
|
290
|
+
readonly externalValue?: string;
|
|
291
|
+
readonly value?: unknown;
|
|
292
|
+
} | Reference;
|
|
293
|
+
};
|
|
294
|
+
readonly title?: string;
|
|
295
|
+
readonly name?: string;
|
|
296
|
+
readonly description?: string;
|
|
297
|
+
readonly type?: Type | [Type, ...Type[]];
|
|
298
|
+
readonly format?: Format;
|
|
299
|
+
readonly pattern?: string;
|
|
300
|
+
readonly minLength?: number;
|
|
301
|
+
readonly maxLength?: number;
|
|
302
|
+
readonly minimum?: number;
|
|
303
|
+
readonly maximum?: number;
|
|
304
|
+
readonly exclusiveMinimum?: number | boolean;
|
|
305
|
+
readonly exclusiveMaximum?: number | boolean;
|
|
306
|
+
readonly multipleOf?: number;
|
|
307
|
+
readonly minItems?: number;
|
|
308
|
+
readonly maxItems?: number;
|
|
309
|
+
readonly uniqueItems?: boolean;
|
|
310
|
+
readonly minProperties?: number;
|
|
311
|
+
readonly maxProperties?: number;
|
|
312
|
+
readonly default?: unknown;
|
|
313
|
+
readonly properties?: {
|
|
314
|
+
readonly [k: string]: Schema;
|
|
315
|
+
};
|
|
316
|
+
readonly required?: readonly string[];
|
|
317
|
+
/**
|
|
318
|
+
* JSON Schema 2020-12 §10.3.1.2: `items` may be a schema, a tuple of schemas
|
|
319
|
+
* (Draft-04 compat), or a boolean schema (`true` = any item allowed,
|
|
320
|
+
* `false` = no trailing items / length cap). Boolean form is exercised when
|
|
321
|
+
* paired with `prefixItems` to enforce a strict tuple shape.
|
|
322
|
+
*/
|
|
323
|
+
readonly items?: Schema | readonly Schema[] | boolean; /** JSON Schema 2020-12: Tuple validation */
|
|
324
|
+
readonly prefixItems?: readonly Schema[];
|
|
325
|
+
readonly enum?: readonly (string | number | boolean | null | readonly (string | number | boolean | null)[])[];
|
|
326
|
+
readonly nullable?: boolean;
|
|
327
|
+
readonly readOnly?: boolean;
|
|
328
|
+
readonly writeOnly?: boolean;
|
|
329
|
+
readonly deprecated?: boolean;
|
|
330
|
+
readonly additionalProperties?: Schema | boolean;
|
|
331
|
+
readonly $ref?: Ref;
|
|
332
|
+
readonly oneOf?: readonly Schema[];
|
|
333
|
+
readonly allOf?: readonly Schema[];
|
|
334
|
+
readonly anyOf?: readonly Schema[];
|
|
335
|
+
readonly not?: Schema;
|
|
336
|
+
readonly const?: unknown;
|
|
337
|
+
readonly patternProperties?: {
|
|
338
|
+
readonly [k: string]: Schema;
|
|
339
|
+
};
|
|
340
|
+
readonly propertyNames?: Schema;
|
|
341
|
+
readonly dependentRequired?: {
|
|
342
|
+
readonly [k: string]: readonly string[];
|
|
343
|
+
};
|
|
344
|
+
readonly 'x-error-message'?: string;
|
|
345
|
+
readonly 'x-length-message'?: string;
|
|
346
|
+
readonly 'x-pattern-message'?: string;
|
|
347
|
+
readonly 'x-minimum-message'?: string;
|
|
348
|
+
readonly 'x-maximum-message'?: string;
|
|
349
|
+
readonly 'x-exclusiveMinimum-message'?: string;
|
|
350
|
+
readonly 'x-exclusiveMaximum-message'?: string;
|
|
351
|
+
readonly 'x-multipleOf-message'?: string;
|
|
352
|
+
readonly 'x-dependentRequired-message'?: string;
|
|
353
|
+
readonly 'x-dependentSchemas-message'?: string;
|
|
354
|
+
readonly 'x-propertyNames-message'?: string;
|
|
355
|
+
readonly 'x-allOf-message'?: string;
|
|
356
|
+
readonly 'x-anyOf-message'?: string;
|
|
357
|
+
readonly 'x-oneOf-message'?: string;
|
|
358
|
+
readonly 'x-not-message'?: string;
|
|
359
|
+
readonly 'x-implication-message'?: string;
|
|
360
|
+
readonly 'x-required-message'?: string;
|
|
361
|
+
readonly 'x-additionalProperties-message'?: string;
|
|
362
|
+
readonly 'x-uniqueItems-message'?: string;
|
|
363
|
+
readonly 'x-const-message'?: string;
|
|
364
|
+
readonly 'x-enum-message'?: string;
|
|
365
|
+
readonly 'x-minLength-message'?: string;
|
|
366
|
+
readonly 'x-maxLength-message'?: string;
|
|
367
|
+
readonly 'x-minItems-message'?: string;
|
|
368
|
+
readonly 'x-maxItems-message'?: string;
|
|
369
|
+
readonly 'x-minProperties-message'?: string;
|
|
370
|
+
readonly 'x-maxProperties-message'?: string;
|
|
371
|
+
readonly 'x-patternProperties-message'?: string;
|
|
372
|
+
readonly 'x-contains-message'?: string;
|
|
373
|
+
readonly 'x-minContains-message'?: string;
|
|
374
|
+
readonly 'x-maxContains-message'?: string;
|
|
375
|
+
readonly 'x-properties-message'?: string;
|
|
376
|
+
readonly 'x-prefixItems-message'?: string;
|
|
377
|
+
readonly 'x-items-message'?: string;
|
|
378
|
+
readonly 'x-unevaluatedProperties-message'?: string;
|
|
379
|
+
readonly 'x-unevaluatedItems-message'?: string;
|
|
380
|
+
readonly 'x-if-message'?: string;
|
|
381
|
+
readonly 'x-then-message'?: string;
|
|
382
|
+
readonly 'x-else-message'?: string;
|
|
383
|
+
readonly $comment?: string;
|
|
384
|
+
readonly contains?: Schema;
|
|
385
|
+
readonly minContains?: number;
|
|
386
|
+
readonly maxContains?: number;
|
|
387
|
+
readonly contentEncoding?: 'base64' | 'base64url' | 'binary' | '7bit' | '8bit' | 'quoted-printable';
|
|
388
|
+
readonly contentMediaType?: string;
|
|
389
|
+
readonly contentSchema?: Schema;
|
|
390
|
+
readonly dependentSchemas?: {
|
|
391
|
+
readonly [k: string]: Schema;
|
|
392
|
+
};
|
|
393
|
+
readonly if?: Schema;
|
|
394
|
+
readonly then?: Schema;
|
|
395
|
+
readonly else?: Schema;
|
|
396
|
+
readonly unevaluatedProperties?: boolean | Schema;
|
|
397
|
+
readonly unevaluatedItems?: boolean | Schema;
|
|
398
|
+
readonly $schema?: string;
|
|
399
|
+
readonly $id?: string;
|
|
400
|
+
readonly $anchor?: string;
|
|
401
|
+
readonly $dynamicAnchor?: string;
|
|
402
|
+
readonly $dynamicRef?: string;
|
|
403
|
+
readonly $vocabulary?: {
|
|
404
|
+
readonly [k: string]: boolean;
|
|
405
|
+
};
|
|
406
|
+
readonly $defs?: {
|
|
407
|
+
readonly [k: string]: Schema;
|
|
408
|
+
};
|
|
409
|
+
readonly 'x-brand'?: string;
|
|
410
|
+
readonly 'x-trim'?: boolean;
|
|
411
|
+
readonly 'x-toLowerCase'?: boolean;
|
|
412
|
+
readonly 'x-toUpperCase'?: boolean;
|
|
413
|
+
readonly 'x-lowercase'?: boolean;
|
|
414
|
+
readonly 'x-uppercase'?: boolean;
|
|
415
|
+
readonly 'x-normalize'?: 'NFC' | 'NFD' | 'NFKC' | 'NFKD';
|
|
416
|
+
readonly 'x-coerce'?: boolean;
|
|
417
|
+
readonly 'x-stringbool'?: true | {
|
|
418
|
+
readonly truthy?: readonly string[];
|
|
419
|
+
readonly falsy?: readonly string[];
|
|
420
|
+
readonly case?: 'sensitive' | 'insensitive';
|
|
421
|
+
};
|
|
422
|
+
readonly 'x-emailPattern'?: 'html5' | 'rfc5322' | 'unicode';
|
|
423
|
+
readonly 'x-emailRegex'?: string;
|
|
424
|
+
readonly 'x-uuidVersion'?: 'v1' | 'v2' | 'v3' | 'v4' | 'v5' | 'v6' | 'v7' | 'v8';
|
|
425
|
+
readonly 'x-urlHostname'?: string;
|
|
426
|
+
readonly 'x-urlProtocol'?: string;
|
|
427
|
+
readonly 'x-urlNormalize'?: boolean;
|
|
428
|
+
readonly 'x-isoPrecision'?: number;
|
|
429
|
+
readonly 'x-isoOffset'?: boolean;
|
|
430
|
+
readonly 'x-isoLocal'?: boolean;
|
|
431
|
+
readonly 'x-macDelimiter'?: string;
|
|
432
|
+
readonly 'x-jwtAlg'?: string;
|
|
433
|
+
readonly 'x-hashAlg'?: 'sha1' | 'sha256' | 'sha384' | 'sha512' | 'md5';
|
|
434
|
+
readonly 'x-hashEnc'?: 'hex' | 'base64' | 'base64url';
|
|
435
|
+
readonly 'x-catch'?: unknown;
|
|
436
|
+
readonly 'x-prefault'?: unknown;
|
|
437
|
+
readonly 'x-readonly'?: boolean;
|
|
438
|
+
readonly 'x-includes'?: string;
|
|
439
|
+
readonly 'x-startsWith'?: string;
|
|
440
|
+
readonly 'x-endsWith'?: string;
|
|
441
|
+
readonly 'x-refine'?: string;
|
|
442
|
+
readonly 'x-superRefine'?: string;
|
|
443
|
+
readonly 'x-codec'?: string;
|
|
444
|
+
readonly 'x-preprocess'?: string;
|
|
445
|
+
readonly 'x-transform'?: string;
|
|
446
|
+
readonly 'x-pipe'?: string;
|
|
447
|
+
};
|
|
448
|
+
type Parameter = {
|
|
449
|
+
readonly $ref?: Ref;
|
|
450
|
+
readonly name: string;
|
|
451
|
+
readonly in: 'path' | 'query' | 'header' | 'cookie';
|
|
452
|
+
readonly description?: string;
|
|
453
|
+
readonly required?: boolean;
|
|
454
|
+
readonly deprecated?: boolean;
|
|
455
|
+
readonly allowEmptyValue?: boolean;
|
|
456
|
+
readonly style?: string;
|
|
457
|
+
readonly explode?: boolean;
|
|
458
|
+
readonly allowReserved?: boolean;
|
|
459
|
+
readonly schema?: Schema;
|
|
460
|
+
readonly content?: Content;
|
|
461
|
+
readonly example?: unknown;
|
|
462
|
+
readonly examples?: {
|
|
463
|
+
readonly [k: string]: {
|
|
464
|
+
readonly summary?: string;
|
|
465
|
+
readonly description?: string;
|
|
466
|
+
readonly defaultValue?: unknown;
|
|
467
|
+
readonly serializedValue?: string;
|
|
468
|
+
readonly externalValue?: string;
|
|
469
|
+
readonly value?: unknown;
|
|
470
|
+
} | Reference;
|
|
471
|
+
};
|
|
472
|
+
};
|
|
473
|
+
type RequestBody = {
|
|
474
|
+
readonly description?: string;
|
|
475
|
+
readonly content?: {
|
|
476
|
+
readonly [k: string]: Media | Reference;
|
|
477
|
+
};
|
|
478
|
+
readonly required?: boolean;
|
|
479
|
+
};
|
|
480
|
+
type Media = {
|
|
481
|
+
readonly schema: Schema;
|
|
482
|
+
readonly itemSchema?: Schema;
|
|
483
|
+
readonly example?: unknown;
|
|
484
|
+
readonly examples?: {
|
|
485
|
+
readonly [k: string]: {
|
|
486
|
+
readonly summary?: string;
|
|
487
|
+
readonly description?: string;
|
|
488
|
+
readonly defaultValue?: unknown;
|
|
489
|
+
readonly serializedValue?: string;
|
|
490
|
+
readonly externalValue?: string;
|
|
491
|
+
readonly value?: unknown;
|
|
492
|
+
} | Reference;
|
|
493
|
+
};
|
|
494
|
+
readonly encoding?: {
|
|
495
|
+
readonly [k: string]: Encoding;
|
|
496
|
+
};
|
|
497
|
+
readonly prefixEncoding?: Encoding;
|
|
498
|
+
readonly itemEncoding?: Encoding;
|
|
499
|
+
};
|
|
500
|
+
type Callbacks = {
|
|
501
|
+
readonly [k: string]: PathItem;
|
|
502
|
+
};
|
|
503
|
+
//#endregion
|
|
504
|
+
//#region src/adapter/index.d.ts
|
|
505
|
+
type ParamIn = 'query' | 'path';
|
|
506
|
+
type SchemaLib = 'zod' | 'valibot' | 'arktype' | 'effect' | 'typebox';
|
|
507
|
+
/**
|
|
508
|
+
* The minimal surface the component generators consume from a validator
|
|
509
|
+
* adapter. Each library's full adapter (carrying `toNamedExport`,
|
|
510
|
+
* `annotateCyclic`, `objectField`, … for schema/contract generation) is a
|
|
511
|
+
* structural supertype, so it can be passed wherever a `ComponentAdapter` is
|
|
512
|
+
* required without a cast.
|
|
513
|
+
*
|
|
514
|
+
* `wrapSchema` is an optional host hook applied to every `schema:` slot
|
|
515
|
+
* expression (inline and `$ref` alike). A host that needs to transform the
|
|
516
|
+
* expression — e.g. hono-openapi wrapping it in `resolver(...)` — supplies it;
|
|
517
|
+
* omitting it leaves the expression untouched (identity), so the shared library
|
|
518
|
+
* stays framework-agnostic.
|
|
519
|
+
*/
|
|
520
|
+
type ComponentAdapter = {
|
|
521
|
+
readonly renderImport: () => string;
|
|
522
|
+
readonly toExpression: (schema: Schema, paramIn?: ParamIn) => string;
|
|
523
|
+
readonly renderTypeInfer: (constName: string) => string;
|
|
524
|
+
readonly wrapSchema?: (expr: string) => string;
|
|
525
|
+
};
|
|
526
|
+
declare function makeAdapter(lib: SchemaLib): ComponentAdapter;
|
|
527
|
+
//#endregion
|
|
528
|
+
//#region src/helper/ref-resolver.d.ts
|
|
529
|
+
declare function valueToCode(value: unknown, codeReplacements?: ReadonlyMap<unknown, string>): string;
|
|
530
|
+
//#endregion
|
|
531
|
+
//#region src/helper/schema-replacements.d.ts
|
|
532
|
+
declare function makeSchemaReplacements(value: unknown, adapter: ComponentAdapter): ReadonlyMap<unknown, string>;
|
|
533
|
+
//#endregion
|
|
534
|
+
//#region src/utils/index.d.ts
|
|
535
|
+
declare function toPascalCase(str: string): string;
|
|
536
|
+
/**
|
|
537
|
+
* Extract the component name from a `$ref`, decoding percent-encoding so the
|
|
538
|
+
* result matches the (decoded) key under `components.schemas`. Pure-ASCII refs
|
|
539
|
+
* are returned unchanged. Used for component lookup, not variable naming.
|
|
540
|
+
*/
|
|
541
|
+
declare function schemaRefToName(ref: string): string;
|
|
542
|
+
/**
|
|
543
|
+
* Convert a name to a valid PascalCase TypeScript identifier. Mirrors
|
|
544
|
+
* schema-to-library's `toIdentifierPascalCase` so reference identifiers match
|
|
545
|
+
* the component declaration names it emits. Non-ASCII names are encoded
|
|
546
|
+
* injectively so distinct names never collide.
|
|
547
|
+
*/
|
|
548
|
+
declare function toIdentifierPascalCase(name: string): string;
|
|
549
|
+
/**
|
|
550
|
+
* Encode a property key for object-literal output: bare identifiers stay
|
|
551
|
+
* unquoted, everything else is JSON-encoded so hyphenated/reserved/numeric
|
|
552
|
+
* keys (e.g. `X-Request-ID`) emit as valid TypeScript.
|
|
553
|
+
*/
|
|
554
|
+
declare function makeSafeKey(key: string): string;
|
|
555
|
+
/** Narrow an unknown value to a plain object (excludes arrays and null). */
|
|
556
|
+
declare function isRecord(v: unknown): v is {
|
|
557
|
+
readonly [k: string]: unknown;
|
|
558
|
+
};
|
|
559
|
+
//#endregion
|
|
560
|
+
//#region src/generator/components/callbacks.d.ts
|
|
561
|
+
declare function makeCallbacksCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
562
|
+
//#endregion
|
|
563
|
+
//#region src/generator/components/examples.d.ts
|
|
564
|
+
declare function makeExamplesCode(components: Components, readonly: boolean): string;
|
|
565
|
+
//#endregion
|
|
566
|
+
//#region src/generator/components/headers.d.ts
|
|
567
|
+
declare function makeHeadersCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
568
|
+
//#endregion
|
|
569
|
+
//#region src/generator/components/links.d.ts
|
|
570
|
+
declare function makeLinksCode(components: Components, readonly: boolean): string;
|
|
571
|
+
//#endregion
|
|
572
|
+
//#region src/generator/components/parameters.d.ts
|
|
573
|
+
declare function makeParametersCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
574
|
+
//#endregion
|
|
575
|
+
//#region src/generator/components/path-items.d.ts
|
|
576
|
+
declare function makePathItemsCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
577
|
+
//#endregion
|
|
578
|
+
//#region src/generator/components/request-bodies.d.ts
|
|
579
|
+
declare function makeRequestBodiesCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
580
|
+
//#endregion
|
|
581
|
+
//#region src/generator/components/responses.d.ts
|
|
582
|
+
declare function makeResponsesCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
583
|
+
//#endregion
|
|
584
|
+
//#region src/generator/components/security-schemes.d.ts
|
|
585
|
+
declare function makeSecuritySchemesCode(components: Components, readonly: boolean): string;
|
|
586
|
+
//#endregion
|
|
587
|
+
export { Callbacks, ComponentAdapter, Components, Content, Encoding, Format, FormatNumber, FormatString, Header, Link, Media, OpenAPI, OpenAPIPaths, Operation, ParamIn, Parameter, PathItem, Ref, Reference, RequestBody, Responses, Schema, SchemaLib, Type, isRecord, makeAdapter, makeCallbacksCode, makeExamplesCode, makeHeadersCode, makeLinksCode, makeParametersCode, makePathItemsCode, makeRequestBodiesCode, makeResponsesCode, makeSafeKey, makeSchemaReplacements, makeSecuritySchemesCode, parseOpenAPI, schemaRefToName, toIdentifierPascalCase, toPascalCase, valueToCode };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import SwaggerParser from "@apidevtools/swagger-parser";
|
|
3
|
+
import { NodeHost, compile } from "@typespec/compiler";
|
|
4
|
+
import { getOpenAPI3 } from "@typespec/openapi3";
|
|
5
|
+
import { schemaToArktype } from "schema-to-library/arktype";
|
|
6
|
+
import { schemaToEffect } from "schema-to-library/effect";
|
|
7
|
+
import { schemaToTypebox } from "schema-to-library/typebox";
|
|
8
|
+
import { schemaToValibot } from "schema-to-library/valibot";
|
|
9
|
+
import { schemaToZod } from "schema-to-library/zod";
|
|
10
|
+
//#region src/openapi/index.ts
|
|
11
|
+
async function parseOpenAPI(input) {
|
|
12
|
+
try {
|
|
13
|
+
if (typeof input === "string" && input.endsWith(".tsp")) {
|
|
14
|
+
const program = await compile(NodeHost, path.resolve(input), { noEmit: true });
|
|
15
|
+
if (program.diagnostics.length) return {
|
|
16
|
+
ok: false,
|
|
17
|
+
error: `TypeSpec compile failed:\n${program.diagnostics.map((d) => d.message).join("\n")}`
|
|
18
|
+
};
|
|
19
|
+
const [record] = await getOpenAPI3(program);
|
|
20
|
+
const tsp = "document" in record ? record.document : record.versions[0].document;
|
|
21
|
+
return {
|
|
22
|
+
ok: true,
|
|
23
|
+
value: await SwaggerParser.bundle(JSON.parse(JSON.stringify(tsp)))
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
ok: true,
|
|
28
|
+
value: await SwaggerParser.bundle(input)
|
|
29
|
+
};
|
|
30
|
+
} catch (e) {
|
|
31
|
+
return {
|
|
32
|
+
ok: false,
|
|
33
|
+
error: e instanceof Error ? e.message : String(e)
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/adapter/index.ts
|
|
39
|
+
/**
|
|
40
|
+
* Widen the rich `Schema` type to a bare JSON-Schema object for schema-to-library.
|
|
41
|
+
* `Schema` is not structurally assignable to its `JSONSchema` (e.g. `x-emailPattern`
|
|
42
|
+
* literal unions differ), so this `as`-free bridge erases the incompatible members.
|
|
43
|
+
*/
|
|
44
|
+
function toJsonSchema(schema) {
|
|
45
|
+
return schema;
|
|
46
|
+
}
|
|
47
|
+
/** Pull the bare expression out of a `export const X = <expr>` wrapper, then strip cross-schema refs. */
|
|
48
|
+
function extractExpr(code, stripRefs = (s) => s) {
|
|
49
|
+
const joined = code.split("\n").filter((line) => !line.startsWith("import ")).join("\n").trim();
|
|
50
|
+
const expr = joined.match(/^export const \w+ = (.+)$/s)?.[1];
|
|
51
|
+
if (expr !== void 0) return stripRefs(expr.replace(/;?\s*$/, ""));
|
|
52
|
+
return stripRefs(joined);
|
|
53
|
+
}
|
|
54
|
+
function makeAdapter(lib) {
|
|
55
|
+
switch (lib) {
|
|
56
|
+
case "zod": return makeZodAdapter();
|
|
57
|
+
case "valibot": return makeValibotAdapter();
|
|
58
|
+
case "arktype": return makeArktypeAdapter();
|
|
59
|
+
case "typebox": return makeTypeboxAdapter();
|
|
60
|
+
case "effect": return makeEffectAdapter();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function makeZodAdapter() {
|
|
64
|
+
const stripLazy = (code) => code.replace(/z\.lazy\(\(\)\s*=>\s*(\w+Schema)\)/g, "$1");
|
|
65
|
+
return {
|
|
66
|
+
toExpression(schema, paramIn) {
|
|
67
|
+
return extractExpr(schemaToZod(toJsonSchema(schema), {
|
|
68
|
+
exportType: false,
|
|
69
|
+
openapi: true,
|
|
70
|
+
readonly: false,
|
|
71
|
+
...paramIn && { paramIn }
|
|
72
|
+
}), stripLazy);
|
|
73
|
+
},
|
|
74
|
+
renderImport() {
|
|
75
|
+
return "import * as z from 'zod'";
|
|
76
|
+
},
|
|
77
|
+
renderTypeInfer(constName) {
|
|
78
|
+
return `export type ${constName}=z.infer<typeof ${constName}>`;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function makeValibotAdapter() {
|
|
83
|
+
const stripLazy = (code) => code.replace(/v\.lazy\(\(\)\s*=>\s*(\w+Schema)\)/g, "$1");
|
|
84
|
+
return {
|
|
85
|
+
toExpression(schema, paramIn) {
|
|
86
|
+
return extractExpr(schemaToValibot(toJsonSchema(schema), {
|
|
87
|
+
exportType: false,
|
|
88
|
+
openapi: true,
|
|
89
|
+
readonly: false,
|
|
90
|
+
...paramIn && { paramIn }
|
|
91
|
+
}), stripLazy);
|
|
92
|
+
},
|
|
93
|
+
renderImport() {
|
|
94
|
+
return "import * as v from 'valibot'";
|
|
95
|
+
},
|
|
96
|
+
renderTypeInfer(constName) {
|
|
97
|
+
return `export type ${constName}=v.InferOutput<typeof ${constName}>`;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function makeArktypeAdapter() {
|
|
102
|
+
return {
|
|
103
|
+
toExpression(schema, paramIn) {
|
|
104
|
+
return extractExpr(schemaToArktype(toJsonSchema(schema), {
|
|
105
|
+
exportType: false,
|
|
106
|
+
openapi: true,
|
|
107
|
+
readonly: false,
|
|
108
|
+
...paramIn && { paramIn }
|
|
109
|
+
}));
|
|
110
|
+
},
|
|
111
|
+
renderImport() {
|
|
112
|
+
return "import { type } from 'arktype'";
|
|
113
|
+
},
|
|
114
|
+
renderTypeInfer(constName) {
|
|
115
|
+
return `export type ${constName}=typeof ${constName}.infer`;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function makeTypeboxAdapter() {
|
|
120
|
+
return {
|
|
121
|
+
toExpression(schema, paramIn) {
|
|
122
|
+
return extractExpr(schemaToTypebox(toJsonSchema(schema), {
|
|
123
|
+
exportType: false,
|
|
124
|
+
openapi: true,
|
|
125
|
+
readonly: false,
|
|
126
|
+
...paramIn && { paramIn }
|
|
127
|
+
}));
|
|
128
|
+
},
|
|
129
|
+
renderImport() {
|
|
130
|
+
return "import { Type } from '@sinclair/typebox'";
|
|
131
|
+
},
|
|
132
|
+
renderTypeInfer(constName) {
|
|
133
|
+
return `export type ${constName}=Static<typeof ${constName}>`;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function makeEffectAdapter() {
|
|
138
|
+
return {
|
|
139
|
+
toExpression(schema, paramIn) {
|
|
140
|
+
return extractExpr(schemaToEffect(toJsonSchema(schema), {
|
|
141
|
+
exportType: false,
|
|
142
|
+
openapi: true,
|
|
143
|
+
readonly: false,
|
|
144
|
+
...paramIn && { paramIn }
|
|
145
|
+
}));
|
|
146
|
+
},
|
|
147
|
+
renderImport() {
|
|
148
|
+
return "import { Schema } from 'effect'";
|
|
149
|
+
},
|
|
150
|
+
renderTypeInfer(constName) {
|
|
151
|
+
return `export type ${constName}=Schema.Schema.Type<typeof ${constName}>`;
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/utils/index.ts
|
|
157
|
+
function toPascalCase(str) {
|
|
158
|
+
return str.replace(/[^a-zA-Z0-9]+(.)/g, (_, c) => c.toUpperCase()).replace(/^(.)/, (_, c) => c.toUpperCase());
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Decode percent-encoding, falling back to the raw input on malformed sequences
|
|
162
|
+
* (e.g. `%ZZ`, a lone `%`) so a single bad `$ref` never throws and aborts the
|
|
163
|
+
* whole generation run.
|
|
164
|
+
*/
|
|
165
|
+
function safeDecode(value) {
|
|
166
|
+
try {
|
|
167
|
+
return decodeURIComponent(value);
|
|
168
|
+
} catch {
|
|
169
|
+
return value;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Extract the component name from a `$ref`, decoding percent-encoding so the
|
|
174
|
+
* result matches the (decoded) key under `components.schemas`. Pure-ASCII refs
|
|
175
|
+
* are returned unchanged. Used for component lookup, not variable naming.
|
|
176
|
+
*/
|
|
177
|
+
function schemaRefToName(ref) {
|
|
178
|
+
const parts = ref.split("/");
|
|
179
|
+
return safeDecode(parts[parts.length - 1] ?? "");
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Encode non-ASCII characters as `u<hex codepoint>` so they survive identifier
|
|
183
|
+
* normalization instead of collapsing every non-ASCII name to the same value.
|
|
184
|
+
*/
|
|
185
|
+
function encodeNonAscii(name) {
|
|
186
|
+
return Array.from(name).map((ch) => {
|
|
187
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
188
|
+
return cp > 127 ? `u${cp.toString(16)}` : ch;
|
|
189
|
+
}).join("");
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Convert a name to a valid PascalCase TypeScript identifier. Mirrors
|
|
193
|
+
* schema-to-library's `toIdentifierPascalCase` so reference identifiers match
|
|
194
|
+
* the component declaration names it emits. Non-ASCII names are encoded
|
|
195
|
+
* injectively so distinct names never collide.
|
|
196
|
+
*/
|
|
197
|
+
function toIdentifierPascalCase(name) {
|
|
198
|
+
const parts = encodeNonAscii(name).split(/[^a-zA-Z0-9]+/).filter(Boolean);
|
|
199
|
+
if (parts.length === 0) return "Schema";
|
|
200
|
+
const result = parts.map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
201
|
+
if (/^[0-9]/.test(result)) return `_${result}`.replace(/([0-9])([a-z])/, (_, d, c) => `${d}${c.toUpperCase()}`);
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Resolve a `#/components/schemas/...` `$ref` to its generated schema variable
|
|
206
|
+
* identifier (`UserSchema`), or `undefined` when the ref does not point at a
|
|
207
|
+
* component schema. Shared by ref-resolver and schema-replacements.
|
|
208
|
+
*/
|
|
209
|
+
function makeSchemaVarName(ref) {
|
|
210
|
+
const name = ref.match(/^#\/components\/schemas\/(.+)$/)?.[1];
|
|
211
|
+
if (name === void 0) return void 0;
|
|
212
|
+
return `${toIdentifierPascalCase(safeDecode(name))}Schema`;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Encode a property key for object-literal output: bare identifiers stay
|
|
216
|
+
* unquoted, everything else is JSON-encoded so hyphenated/reserved/numeric
|
|
217
|
+
* keys (e.g. `X-Request-ID`) emit as valid TypeScript.
|
|
218
|
+
*/
|
|
219
|
+
function makeSafeKey(key) {
|
|
220
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : JSON.stringify(key);
|
|
221
|
+
}
|
|
222
|
+
/** Narrow an unknown value to a plain object (excludes arrays and null). */
|
|
223
|
+
function isRecord(v) {
|
|
224
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
225
|
+
}
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/helper/ref-resolver.ts
|
|
228
|
+
function valueToCode(value, codeReplacements) {
|
|
229
|
+
return encodeValue(value, codeReplacements, /* @__PURE__ */ new Set());
|
|
230
|
+
}
|
|
231
|
+
function encodeValue(value, codeReplacements, seen) {
|
|
232
|
+
const replacement = codeReplacements?.get(value);
|
|
233
|
+
if (replacement !== void 0) return replacement;
|
|
234
|
+
if (value === null) return "null";
|
|
235
|
+
if (value === void 0) return "undefined";
|
|
236
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
237
|
+
if (typeof value === "number") return Number.isFinite(value) ? String(value) : "null";
|
|
238
|
+
if (typeof value === "boolean") return String(value);
|
|
239
|
+
if (typeof value === "bigint") return value.toString();
|
|
240
|
+
if (Array.isArray(value)) {
|
|
241
|
+
if (seen.has(value)) return "null";
|
|
242
|
+
seen.add(value);
|
|
243
|
+
const code = `[${value.map((v) => encodeValue(v, codeReplacements, seen)).join(",")}]`;
|
|
244
|
+
seen.delete(value);
|
|
245
|
+
return code;
|
|
246
|
+
}
|
|
247
|
+
if (isRecord(value)) {
|
|
248
|
+
if (seen.has(value)) return "null";
|
|
249
|
+
if ("$ref" in value && typeof value.$ref === "string") {
|
|
250
|
+
const varName = makeSchemaVarName(value.$ref);
|
|
251
|
+
if (varName) return varName;
|
|
252
|
+
}
|
|
253
|
+
seen.add(value);
|
|
254
|
+
const entries = Object.entries(value).map(([k, v]) => {
|
|
255
|
+
return `${/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(k) ? k : JSON.stringify(k)}:${encodeValue(v, codeReplacements, seen)}`;
|
|
256
|
+
});
|
|
257
|
+
seen.delete(value);
|
|
258
|
+
return `{${entries.join(",")}}`;
|
|
259
|
+
}
|
|
260
|
+
return "null";
|
|
261
|
+
}
|
|
262
|
+
//#endregion
|
|
263
|
+
//#region src/helper/schema-replacements.ts
|
|
264
|
+
function isInlineSchema(value) {
|
|
265
|
+
if (!isRecord(value)) return false;
|
|
266
|
+
if ("$ref" in value) return false;
|
|
267
|
+
return "type" in value || "properties" in value || "items" in value || "oneOf" in value || "anyOf" in value || "allOf" in value || "enum" in value || "const" in value || "not" in value;
|
|
268
|
+
}
|
|
269
|
+
function makeSchemaReplacements(value, adapter) {
|
|
270
|
+
return new Map(makeEntries(value, adapter, /* @__PURE__ */ new Set()));
|
|
271
|
+
}
|
|
272
|
+
function makeEntries(v, adapter, seen, parentKey) {
|
|
273
|
+
if (v === null || v === void 0 || typeof v !== "object") return [];
|
|
274
|
+
if (seen.has(v)) return [];
|
|
275
|
+
const wrap = adapter.wrapSchema ?? ((expr) => expr);
|
|
276
|
+
if (parentKey === "schema") {
|
|
277
|
+
if (isInlineSchema(v)) return [[v, wrap(adapter.toExpression(v))]];
|
|
278
|
+
if (isRecord(v) && typeof v.$ref === "string") {
|
|
279
|
+
const varName = makeSchemaVarName(v.$ref);
|
|
280
|
+
if (varName) return [[v, wrap(varName)]];
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
seen.add(v);
|
|
284
|
+
const entries = Array.isArray(v) ? v.flatMap((item) => makeEntries(item, adapter, seen)) : isRecord(v) ? Object.entries(v).flatMap(([k, child]) => makeEntries(child, adapter, seen, k)) : [];
|
|
285
|
+
seen.delete(v);
|
|
286
|
+
return entries;
|
|
287
|
+
}
|
|
288
|
+
//#endregion
|
|
289
|
+
//#region src/generator/components/callbacks.ts
|
|
290
|
+
function makeCallbacksCode(components, adapter, readonly) {
|
|
291
|
+
const { callbacks } = components;
|
|
292
|
+
if (!callbacks) return "";
|
|
293
|
+
const entries = Object.entries(callbacks);
|
|
294
|
+
if (entries.length === 0) return "";
|
|
295
|
+
const asConst = readonly ? " as const" : "";
|
|
296
|
+
const exports = entries.filter(([, callback]) => !("$ref" in callback && callback.$ref)).map(([name, callback]) => {
|
|
297
|
+
return `export const ${`${toIdentifierPascalCase(name)}Callback`}=${valueToCode(callback, makeSchemaReplacements(callback, adapter))}${asConst}`;
|
|
298
|
+
}).filter(Boolean);
|
|
299
|
+
if (exports.length === 0) return "";
|
|
300
|
+
return exports.join(";");
|
|
301
|
+
}
|
|
302
|
+
//#endregion
|
|
303
|
+
//#region src/generator/components/examples.ts
|
|
304
|
+
function makeExamplesCode(components, readonly) {
|
|
305
|
+
const { examples } = components;
|
|
306
|
+
if (!examples) return "";
|
|
307
|
+
const entries = Object.entries(examples);
|
|
308
|
+
if (entries.length === 0) return "";
|
|
309
|
+
const asConst = readonly ? " as const" : "";
|
|
310
|
+
return `${entries.map(([name, example]) => {
|
|
311
|
+
return `export const ${`${toIdentifierPascalCase(name)}Example`}=${valueToCode(example)}${asConst}`;
|
|
312
|
+
}).join(";")}\n`;
|
|
313
|
+
}
|
|
314
|
+
//#endregion
|
|
315
|
+
//#region src/generator/components/headers.ts
|
|
316
|
+
function makeHeadersCode(components, adapter, exportTypes) {
|
|
317
|
+
const { headers } = components;
|
|
318
|
+
if (!headers) return "";
|
|
319
|
+
const entries = Object.entries(headers);
|
|
320
|
+
if (entries.length === 0) return "";
|
|
321
|
+
const imports = adapter.renderImport();
|
|
322
|
+
const exports = entries.map(([name, header]) => {
|
|
323
|
+
if (!("schema" in header) || !header.schema) return "";
|
|
324
|
+
const constName = `${toIdentifierPascalCase(name)}HeaderSchema`;
|
|
325
|
+
return `export const ${constName}=${adapter.toExpression(header.schema)}${exportTypes ? `\n\n${adapter.renderTypeInfer(constName)}` : ""}`;
|
|
326
|
+
}).filter(Boolean).join(";");
|
|
327
|
+
return exports ? `${imports}\n\n${exports}\n` : "";
|
|
328
|
+
}
|
|
329
|
+
//#endregion
|
|
330
|
+
//#region src/generator/components/links.ts
|
|
331
|
+
function makeLinksCode(components, readonly) {
|
|
332
|
+
const { links } = components;
|
|
333
|
+
if (!links) return "";
|
|
334
|
+
const entries = Object.entries(links);
|
|
335
|
+
if (entries.length === 0) return "";
|
|
336
|
+
const asConst = readonly ? " as const" : "";
|
|
337
|
+
return `${entries.map(([name, link]) => {
|
|
338
|
+
return `export const ${`${toIdentifierPascalCase(name)}Link`}=${valueToCode(link)}${asConst}`;
|
|
339
|
+
}).join(";")}\n`;
|
|
340
|
+
}
|
|
341
|
+
//#endregion
|
|
342
|
+
//#region src/generator/components/parameters.ts
|
|
343
|
+
function makeParametersCode(components, adapter, exportTypes) {
|
|
344
|
+
const { parameters } = components;
|
|
345
|
+
if (!parameters) return "";
|
|
346
|
+
const entries = Object.entries(parameters);
|
|
347
|
+
if (entries.length === 0) return "";
|
|
348
|
+
const imports = adapter.renderImport();
|
|
349
|
+
const exports = entries.map(([name, param]) => {
|
|
350
|
+
if (!param.schema) return "";
|
|
351
|
+
const paramIn = param.in === "query" || param.in === "path" ? param.in : void 0;
|
|
352
|
+
const constName = `${toIdentifierPascalCase(name)}ParamsSchema`;
|
|
353
|
+
return `export const ${constName}=${adapter.toExpression(param.schema, paramIn)}${exportTypes ? `\n\n${adapter.renderTypeInfer(constName)}` : ""}`;
|
|
354
|
+
}).filter(Boolean).join(";");
|
|
355
|
+
return exports ? `${imports}\n\n${exports}\n` : "";
|
|
356
|
+
}
|
|
357
|
+
//#endregion
|
|
358
|
+
//#region src/generator/components/path-items.ts
|
|
359
|
+
function makePathItemsCode(components, adapter, readonly) {
|
|
360
|
+
const { pathItems } = components;
|
|
361
|
+
if (!pathItems) return "";
|
|
362
|
+
const entries = Object.entries(pathItems);
|
|
363
|
+
if (entries.length === 0) return "";
|
|
364
|
+
const asConst = readonly ? " as const" : "";
|
|
365
|
+
const exports = entries.map(([name, pathItem]) => {
|
|
366
|
+
return `export const ${`${toIdentifierPascalCase(name)}PathItem`}=${valueToCode(pathItem, makeSchemaReplacements(pathItem, adapter))}${asConst}`;
|
|
367
|
+
}).filter(Boolean);
|
|
368
|
+
if (exports.length === 0) return "";
|
|
369
|
+
return exports.join(";");
|
|
370
|
+
}
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/generator/components/request-bodies.ts
|
|
373
|
+
function makeRequestBodiesCode(components, adapter, readonly) {
|
|
374
|
+
const { requestBodies } = components;
|
|
375
|
+
if (!requestBodies) return "";
|
|
376
|
+
const entries = Object.entries(requestBodies);
|
|
377
|
+
if (entries.length === 0) return "";
|
|
378
|
+
const asConst = readonly ? " as const" : "";
|
|
379
|
+
const exports = entries.map(([name, body]) => {
|
|
380
|
+
return `export const ${`${toIdentifierPascalCase(name)}RequestBody`}=${valueToCode(body, makeSchemaReplacements(body, adapter))}${asConst}`;
|
|
381
|
+
}).filter(Boolean);
|
|
382
|
+
if (exports.length === 0) return "";
|
|
383
|
+
return exports.join(";");
|
|
384
|
+
}
|
|
385
|
+
//#endregion
|
|
386
|
+
//#region src/generator/components/responses.ts
|
|
387
|
+
function makeResponsesCode(components, adapter, readonly) {
|
|
388
|
+
const { responses } = components;
|
|
389
|
+
if (!responses) return "";
|
|
390
|
+
const entries = Object.entries(responses);
|
|
391
|
+
if (entries.length === 0) return "";
|
|
392
|
+
const asConst = readonly ? " as const" : "";
|
|
393
|
+
const exports = entries.filter(([, response]) => !("$ref" in response && response.$ref)).map(([name, response]) => {
|
|
394
|
+
return `export const ${`${toIdentifierPascalCase(name)}Response`}=${valueToCode(response, makeSchemaReplacements(response, adapter))}${asConst}`;
|
|
395
|
+
}).filter(Boolean);
|
|
396
|
+
if (exports.length === 0) return "";
|
|
397
|
+
return exports.join(";");
|
|
398
|
+
}
|
|
399
|
+
//#endregion
|
|
400
|
+
//#region src/generator/components/security-schemes.ts
|
|
401
|
+
function makeSecuritySchemesCode(components, readonly) {
|
|
402
|
+
const { securitySchemes } = components;
|
|
403
|
+
if (!securitySchemes) return "";
|
|
404
|
+
const entries = Object.entries(securitySchemes);
|
|
405
|
+
if (entries.length === 0) return "";
|
|
406
|
+
const asConst = readonly ? " as const" : "";
|
|
407
|
+
return `${entries.map(([name, scheme]) => {
|
|
408
|
+
return `export const ${`${toIdentifierPascalCase(name)}SecurityScheme`}=${valueToCode(scheme)}${asConst}`;
|
|
409
|
+
}).join(";")}\n`;
|
|
410
|
+
}
|
|
411
|
+
//#endregion
|
|
412
|
+
export { isRecord, makeAdapter, makeCallbacksCode, makeExamplesCode, makeHeadersCode, makeLinksCode, makeParametersCode, makePathItemsCode, makeRequestBodiesCode, makeResponsesCode, makeSafeKey, makeSchemaReplacements, makeSecuritySchemesCode, parseOpenAPI, schemaRefToName, toIdentifierPascalCase, toPascalCase, valueToCode };
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "oas-truth",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Read OpenAPI / TypeSpec as the single source of truth and get a typed OpenAPI document back.",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"schema-to-library": "^0.3.5"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@apidevtools/swagger-parser": "^12.1.0",
|
|
20
|
+
"@types/node": "^25.9.3",
|
|
21
|
+
"@typespec/compiler": "^1.13.0",
|
|
22
|
+
"@typespec/http": "^1.13.0",
|
|
23
|
+
"@typespec/openapi3": "^1.13.0",
|
|
24
|
+
"@typespec/rest": "^0.83.0",
|
|
25
|
+
"@typespec/versioning": "^0.83.0",
|
|
26
|
+
"tsdown": "^0.22.2",
|
|
27
|
+
"typescript": "^6.0.3"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@apidevtools/swagger-parser": ">=12.0.0 <13.0.0",
|
|
31
|
+
"@typespec/compiler": ">=1.11.0 <2.0.0",
|
|
32
|
+
"@typespec/openapi3": ">=1.11.0 <2.0.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsdown"
|
|
36
|
+
}
|
|
37
|
+
}
|