oas-truth 0.3.0 → 0.4.0
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/README.md +13 -1
- package/dist/index.d.ts +78 -10
- package/dist/index.js +311 -137
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -42,9 +42,21 @@ type-checks under `exactOptionalPropertyTypes`. Arktype cycles become
|
|
|
42
42
|
`import { type, scope } from 'arktype'`. `schemaImportLine` is the same function
|
|
43
43
|
`makeSchemasCode` uses when the declarations stay in one file.
|
|
44
44
|
|
|
45
|
+
Every other component kind has the same split: `make<Kind>Declarations` returns `{ name,
|
|
46
|
+
varName, fileName, code }` with no import line, and `make<Kind>Code` joins those bodies.
|
|
47
|
+
`makeMediaTypesCode` covers `components.mediaTypes`. `makeSchemaIdentifiers` is the collision
|
|
48
|
+
map (`user` / `User` → `User` / `User2`); `$ref`s resolve through it, so a later collider is
|
|
49
|
+
`User2Schema`.
|
|
50
|
+
|
|
51
|
+
`wrapSchema(expr, slot)` is slot-aware (`response-content`, `request-content`, `header`,
|
|
52
|
+
`parameter`, `media-type`). TypeBox imports from `typebox` and reserves `Compile`. Pass
|
|
53
|
+
`{ ref: true }` to `makeSchemaDeclarations` to register the OpenAPI key — TypeBox writes it
|
|
54
|
+
into the outermost builder options; the other libraries use an outer wrap.
|
|
55
|
+
|
|
45
56
|
```ts
|
|
46
|
-
import { makeAdapter, makeSchemaDeclarations } from 'oas-truth'
|
|
57
|
+
import { makeAdapter, makeSchemaDeclarations, makeSchemaIdentifiers } from 'oas-truth'
|
|
47
58
|
|
|
59
|
+
const identifiers = makeSchemaIdentifiers(schemas)
|
|
48
60
|
const declarations = makeSchemaDeclarations(schemas, makeAdapter('arktype'))
|
|
49
61
|
// a cyclic file: import { type, scope } from 'arktype'
|
|
50
62
|
// a non-cyclic file: import { type } from 'arktype'
|
package/dist/index.d.ts
CHANGED
|
@@ -502,6 +502,15 @@ export type Callbacks = {
|
|
|
502
502
|
//#region src/adapter/index.d.ts
|
|
503
503
|
export type ParamIn = 'query' | 'path';
|
|
504
504
|
export type SchemaLib = 'zod' | 'valibot' | 'arktype' | 'effect' | 'typebox';
|
|
505
|
+
/**
|
|
506
|
+
* Which `schema:` slot a host `wrapSchema` hook is looking at. The second
|
|
507
|
+
* argument is optional so an existing one-argument hook keeps compiling.
|
|
508
|
+
*/
|
|
509
|
+
export type SchemaSlot = 'response-content' | 'request-content' | 'header' | 'parameter' | 'media-type';
|
|
510
|
+
/** Optional third argument to `toExpression` — TypeBox puts `ref` in builder options. */
|
|
511
|
+
export type ToExpressionOptions = {
|
|
512
|
+
readonly ref?: string;
|
|
513
|
+
};
|
|
505
514
|
/**
|
|
506
515
|
* Flags `renderImport` uses when the schemas builder needs more than the
|
|
507
516
|
* library's default line — Arktype `scope` for a `$ref` cycle, TypeBox
|
|
@@ -528,10 +537,14 @@ export type CyclicTypeStyle = {
|
|
|
528
537
|
* required without a cast.
|
|
529
538
|
*
|
|
530
539
|
* `wrapSchema` is an optional host hook applied to every `schema:` slot
|
|
531
|
-
* expression (inline and `$ref` alike).
|
|
532
|
-
*
|
|
533
|
-
*
|
|
534
|
-
*
|
|
540
|
+
* expression (inline and `$ref` alike). The second argument is the slot so a
|
|
541
|
+
* host can wrap response content and leave header schemas plain. Omitting the
|
|
542
|
+
* hook leaves the expression untouched (identity), so the shared library stays
|
|
543
|
+
* framework-agnostic.
|
|
544
|
+
*
|
|
545
|
+
* `withRef` is the library-specific outer wrap for a component name
|
|
546
|
+
* (`.meta({ref})`, `v.metadata`, `.configure({ref})`, `.annotate({identifier})`).
|
|
547
|
+
* TypeBox has no post-construction wrap: pass `ref` to `toExpression` instead.
|
|
535
548
|
*
|
|
536
549
|
* Cycle handling lives next to each library: `wrapLazy` re-wraps a `$ref`
|
|
537
550
|
* inside a strongly connected component (Zod, Valibot, Effect);
|
|
@@ -542,9 +555,10 @@ export type CyclicTypeStyle = {
|
|
|
542
555
|
*/
|
|
543
556
|
export type ComponentAdapter = {
|
|
544
557
|
readonly renderImport: (options?: RenderImportOptions) => string;
|
|
545
|
-
readonly toExpression: (schema: Schema, paramIn?: ParamIn) => string;
|
|
558
|
+
readonly toExpression: (schema: Schema, paramIn?: ParamIn, options?: ToExpressionOptions) => string;
|
|
546
559
|
readonly renderTypeInfer: (constName: string) => string;
|
|
547
|
-
readonly wrapSchema?: (expr: string) => string;
|
|
560
|
+
readonly wrapSchema?: (expr: string, slot?: SchemaSlot) => string;
|
|
561
|
+
readonly withRef?: (expr: string, ref: string) => string;
|
|
548
562
|
readonly wrapLazy?: (varName: string) => string;
|
|
549
563
|
readonly cyclicAnnotation?: (typeName: string) => string;
|
|
550
564
|
readonly cyclicTypeStyle?: CyclicTypeStyle;
|
|
@@ -554,10 +568,14 @@ export type ComponentAdapter = {
|
|
|
554
568
|
export declare function makeAdapter(lib: SchemaLib): ComponentAdapter;
|
|
555
569
|
//#endregion
|
|
556
570
|
//#region src/helper/ref-resolver.d.ts
|
|
557
|
-
export declare function valueToCode(value: unknown, codeReplacements?: ReadonlyMap<unknown, string>): string;
|
|
571
|
+
export declare function valueToCode(value: unknown, codeReplacements?: ReadonlyMap<unknown, string>, identifiers?: ReadonlyMap<string, string>): string;
|
|
558
572
|
//#endregion
|
|
559
573
|
//#region src/helper/schema-replacements.d.ts
|
|
560
|
-
|
|
574
|
+
type SchemaReplacementsOptions = {
|
|
575
|
+
readonly slot?: SchemaSlot;
|
|
576
|
+
readonly identifiers?: ReadonlyMap<string, string>;
|
|
577
|
+
};
|
|
578
|
+
export declare function makeSchemaReplacements(value: unknown, adapter: ComponentAdapter, options?: SchemaReplacementsOptions): ReadonlyMap<unknown, string>;
|
|
561
579
|
//#endregion
|
|
562
580
|
//#region src/utils/index.d.ts
|
|
563
581
|
export declare function toPascalCase(str: string): string;
|
|
@@ -574,6 +592,24 @@ export declare function schemaRefToName(ref: string): string;
|
|
|
574
592
|
* injectively so distinct names never collide.
|
|
575
593
|
*/
|
|
576
594
|
export declare function toIdentifierPascalCase(name: string): string;
|
|
595
|
+
/**
|
|
596
|
+
* `toIdentifierPascalCase` folds `user` and `User` into one identifier; later
|
|
597
|
+
* colliders get a numeric suffix so no declaration is lost. The map is
|
|
598
|
+
* `OpenAPI key → identifier` (`user` → `User`, `User` → `User2`).
|
|
599
|
+
*/
|
|
600
|
+
export declare function makeSchemaIdentifiers(schemas: {
|
|
601
|
+
readonly [k: string]: unknown;
|
|
602
|
+
}): Map<string, string>;
|
|
603
|
+
/** Lowercase the first character for a split-file name (`User` → `user`). */
|
|
604
|
+
export declare function declarationFileName(ident: string): string;
|
|
605
|
+
/**
|
|
606
|
+
* Resolve a `#/components/schemas/...` `$ref` to its generated schema variable
|
|
607
|
+
* identifier (`UserSchema`), or `undefined` when the ref does not point at a
|
|
608
|
+
* component schema. Shared by ref-resolver and schema-replacements. Pass the
|
|
609
|
+
* collision map from `makeSchemaIdentifiers` so a later collider resolves to
|
|
610
|
+
* the suffixed name (`User` → `User2Schema`).
|
|
611
|
+
*/
|
|
612
|
+
export declare function makeSchemaVarName(ref: string, identifiers?: ReadonlyMap<string, string>): string | undefined;
|
|
577
613
|
/**
|
|
578
614
|
* Encode a property key for object-literal output: bare identifiers stay
|
|
579
615
|
* unquoted, everything else is JSON-encoded so hyphenated/reserved/numeric
|
|
@@ -585,28 +621,53 @@ export declare function isRecord(v: unknown): v is {
|
|
|
585
621
|
readonly [k: string]: unknown;
|
|
586
622
|
};
|
|
587
623
|
//#endregion
|
|
624
|
+
//#region src/generator/components/declaration.d.ts
|
|
625
|
+
type ComponentDeclaration = {
|
|
626
|
+
readonly name: string;
|
|
627
|
+
readonly varName: string;
|
|
628
|
+
readonly fileName: string;
|
|
629
|
+
readonly code: string;
|
|
630
|
+
};
|
|
631
|
+
type ComponentCodeOptions = {
|
|
632
|
+
readonly exportTypes?: boolean;
|
|
633
|
+
readonly readonly?: boolean;
|
|
634
|
+
readonly identifiers?: ReadonlyMap<string, string>;
|
|
635
|
+
};
|
|
636
|
+
//#endregion
|
|
588
637
|
//#region src/generator/components/callbacks.d.ts
|
|
638
|
+
export declare function makeCallbacksDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
589
639
|
export declare function makeCallbacksCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
590
640
|
//#endregion
|
|
591
641
|
//#region src/generator/components/examples.d.ts
|
|
642
|
+
export declare function makeExamplesDeclarations(components: Components, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
592
643
|
export declare function makeExamplesCode(components: Components, readonly: boolean): string;
|
|
593
644
|
//#endregion
|
|
594
645
|
//#region src/generator/components/headers.d.ts
|
|
646
|
+
export declare function makeHeadersDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
595
647
|
export declare function makeHeadersCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
596
648
|
//#endregion
|
|
597
649
|
//#region src/generator/components/links.d.ts
|
|
650
|
+
export declare function makeLinksDeclarations(components: Components, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
598
651
|
export declare function makeLinksCode(components: Components, readonly: boolean): string;
|
|
599
652
|
//#endregion
|
|
653
|
+
//#region src/generator/components/media-types.d.ts
|
|
654
|
+
export declare function makeMediaTypesDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
655
|
+
export declare function makeMediaTypesCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
656
|
+
//#endregion
|
|
600
657
|
//#region src/generator/components/parameters.d.ts
|
|
658
|
+
export declare function makeParametersDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
601
659
|
export declare function makeParametersCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
602
660
|
//#endregion
|
|
603
661
|
//#region src/generator/components/path-items.d.ts
|
|
662
|
+
export declare function makePathItemsDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
604
663
|
export declare function makePathItemsCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
605
664
|
//#endregion
|
|
606
665
|
//#region src/generator/components/request-bodies.d.ts
|
|
666
|
+
export declare function makeRequestBodiesDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
607
667
|
export declare function makeRequestBodiesCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
608
668
|
//#endregion
|
|
609
669
|
//#region src/generator/components/responses.d.ts
|
|
670
|
+
export declare function makeResponsesDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
610
671
|
export declare function makeResponsesCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
611
672
|
//#endregion
|
|
612
673
|
//#region src/generator/components/schemas.d.ts
|
|
@@ -627,9 +688,15 @@ type SchemasOptions = {
|
|
|
627
688
|
readonly cyclicTypeStyle?: 'library' | 'literal';
|
|
628
689
|
/**
|
|
629
690
|
* Host hook around each declaration expression — e.g. hono-openapi ref
|
|
630
|
-
* registration. Applied after cycle handling.
|
|
691
|
+
* registration. Applied after cycle handling. Wins over `ref`.
|
|
631
692
|
*/
|
|
632
693
|
readonly wrapDeclaration?: (expr: string, name: string) => string;
|
|
694
|
+
/**
|
|
695
|
+
* Register the OpenAPI key on each declaration. TypeBox puts it in the
|
|
696
|
+
* outermost builder options via schema-to-library; the other libraries use
|
|
697
|
+
* `adapter.withRef` (an outer wrap). A bare identifier is left unchanged.
|
|
698
|
+
*/
|
|
699
|
+
readonly ref?: boolean;
|
|
633
700
|
};
|
|
634
701
|
type SchemaDeclaration = {
|
|
635
702
|
readonly name: string;
|
|
@@ -673,6 +740,7 @@ export declare function makeSchemaDeclarations(schemas: {
|
|
|
673
740
|
export declare function makeSchemasCode(components: Components, adapter: ComponentAdapter, options?: SchemasOptions): string;
|
|
674
741
|
//#endregion
|
|
675
742
|
//#region src/generator/components/security-schemes.d.ts
|
|
743
|
+
export declare function makeSecuritySchemesDeclarations(components: Components, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
676
744
|
export declare function makeSecuritySchemesCode(components: Components, readonly: boolean): string;
|
|
677
745
|
//#endregion
|
|
678
|
-
export type { SchemaDeclaration, SchemasOptions };
|
|
746
|
+
export type { ComponentCodeOptions, ComponentDeclaration, SchemaDeclaration, SchemasOptions };
|
package/dist/index.js
CHANGED
|
@@ -36,6 +36,10 @@ async function parseOpenAPI(input) {
|
|
|
36
36
|
}
|
|
37
37
|
//#endregion
|
|
38
38
|
//#region src/adapter/index.ts
|
|
39
|
+
/** A generated identifier (`TagSchema`) is already a registered name — do not wrap it. */
|
|
40
|
+
function isBareIdent(expr) {
|
|
41
|
+
return /^[A-Za-z_$][\w$]*$/u.test(expr);
|
|
42
|
+
}
|
|
39
43
|
/**
|
|
40
44
|
* Widen the rich `Schema` type to a bare JSON-Schema object for schema-to-library.
|
|
41
45
|
* `Schema` is not structurally assignable to its `JSONSchema` (e.g. `x-emailPattern`
|
|
@@ -105,6 +109,9 @@ function makeZodAdapter() {
|
|
|
105
109
|
},
|
|
106
110
|
cyclicAnnotation(typeName) {
|
|
107
111
|
return `z.ZodType<${typeName}>`;
|
|
112
|
+
},
|
|
113
|
+
withRef(expr, ref) {
|
|
114
|
+
return isBareIdent(expr) ? expr : `${expr}.meta({ref:${JSON.stringify(ref)}})`;
|
|
108
115
|
}
|
|
109
116
|
};
|
|
110
117
|
}
|
|
@@ -130,7 +137,10 @@ function makeValibotAdapter() {
|
|
|
130
137
|
cyclicAnnotation(typeName) {
|
|
131
138
|
return `v.GenericSchema<${typeName}>`;
|
|
132
139
|
},
|
|
133
|
-
cyclicTypeStyle: { optionalUndefined: true }
|
|
140
|
+
cyclicTypeStyle: { optionalUndefined: true },
|
|
141
|
+
withRef(expr, ref) {
|
|
142
|
+
return isBareIdent(expr) ? expr : `v.pipe(${expr},v.metadata({ref:${JSON.stringify(ref)}}))`;
|
|
143
|
+
}
|
|
134
144
|
};
|
|
135
145
|
}
|
|
136
146
|
function makeArktypeAdapter() {
|
|
@@ -150,21 +160,25 @@ function makeArktypeAdapter() {
|
|
|
150
160
|
return `export type ${constName}=typeof ${constName}.infer`;
|
|
151
161
|
},
|
|
152
162
|
renderCyclic: renderArktypeCyclic,
|
|
153
|
-
reservedTypeNames: ["type", "scope"]
|
|
163
|
+
reservedTypeNames: ["type", "scope"],
|
|
164
|
+
withRef(expr, ref) {
|
|
165
|
+
return isBareIdent(expr) ? expr : `${expr}.configure({ref:${JSON.stringify(ref)}})`;
|
|
166
|
+
}
|
|
154
167
|
};
|
|
155
168
|
}
|
|
156
169
|
function makeTypeboxAdapter() {
|
|
157
170
|
return {
|
|
158
|
-
toExpression(schema, paramIn) {
|
|
171
|
+
toExpression(schema, paramIn, options) {
|
|
159
172
|
return extractExpr(schemaToTypebox(toJsonSchema(schema), {
|
|
160
173
|
exportType: false,
|
|
161
174
|
openapi: true,
|
|
162
175
|
readonly: false,
|
|
163
|
-
...paramIn && { paramIn }
|
|
176
|
+
...paramIn && { paramIn },
|
|
177
|
+
...options?.ref !== void 0 && { ref: options.ref }
|
|
164
178
|
}));
|
|
165
179
|
},
|
|
166
180
|
renderImport(options) {
|
|
167
|
-
return options?.exportTypes === true ? "import { Type, type Static } from '
|
|
181
|
+
return options?.exportTypes === true ? "import { Type, type Static } from 'typebox'" : "import { Type } from 'typebox'";
|
|
168
182
|
},
|
|
169
183
|
renderTypeInfer(constName) {
|
|
170
184
|
return `export type ${constName}=Static<typeof ${constName}>`;
|
|
@@ -172,7 +186,8 @@ function makeTypeboxAdapter() {
|
|
|
172
186
|
reservedTypeNames: [
|
|
173
187
|
"Type",
|
|
174
188
|
"Static",
|
|
175
|
-
"Codec"
|
|
189
|
+
"Codec",
|
|
190
|
+
"Compile"
|
|
176
191
|
]
|
|
177
192
|
};
|
|
178
193
|
}
|
|
@@ -202,7 +217,10 @@ function makeEffectAdapter() {
|
|
|
202
217
|
optionalUndefined: true,
|
|
203
218
|
readonlyArrays: true
|
|
204
219
|
},
|
|
205
|
-
reservedTypeNames: ["Schema", "Effect"]
|
|
220
|
+
reservedTypeNames: ["Schema", "Effect"],
|
|
221
|
+
withRef(expr, ref) {
|
|
222
|
+
return isBareIdent(expr) ? expr : `${expr}.annotate({identifier:${JSON.stringify(ref)}})`;
|
|
223
|
+
}
|
|
206
224
|
};
|
|
207
225
|
}
|
|
208
226
|
//#endregion
|
|
@@ -254,14 +272,36 @@ function toIdentifierPascalCase(name) {
|
|
|
254
272
|
return result;
|
|
255
273
|
}
|
|
256
274
|
/**
|
|
275
|
+
* `name`, or the first of `name2`, `name3`, … not in `used`.
|
|
276
|
+
*/
|
|
277
|
+
function claimName(name, used, i = 2) {
|
|
278
|
+
if (!used.has(name)) return name;
|
|
279
|
+
return used.has(`${name}${i}`) ? claimName(name, used, i + 1) : `${name}${i}`;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* `toIdentifierPascalCase` folds `user` and `User` into one identifier; later
|
|
283
|
+
* colliders get a numeric suffix so no declaration is lost. The map is
|
|
284
|
+
* `OpenAPI key → identifier` (`user` → `User`, `User` → `User2`).
|
|
285
|
+
*/
|
|
286
|
+
function makeSchemaIdentifiers(schemas) {
|
|
287
|
+
return Object.keys(schemas).reduce((acc, key) => acc.set(key, claimName(toIdentifierPascalCase(key), new Set(acc.values()))), /* @__PURE__ */ new Map());
|
|
288
|
+
}
|
|
289
|
+
/** Lowercase the first character for a split-file name (`User` → `user`). */
|
|
290
|
+
function declarationFileName(ident) {
|
|
291
|
+
return `${ident.charAt(0).toLowerCase()}${ident.slice(1)}`;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
257
294
|
* Resolve a `#/components/schemas/...` `$ref` to its generated schema variable
|
|
258
295
|
* identifier (`UserSchema`), or `undefined` when the ref does not point at a
|
|
259
|
-
* component schema. Shared by ref-resolver and schema-replacements.
|
|
296
|
+
* component schema. Shared by ref-resolver and schema-replacements. Pass the
|
|
297
|
+
* collision map from `makeSchemaIdentifiers` so a later collider resolves to
|
|
298
|
+
* the suffixed name (`User` → `User2Schema`).
|
|
260
299
|
*/
|
|
261
|
-
function makeSchemaVarName(ref) {
|
|
300
|
+
function makeSchemaVarName(ref, identifiers) {
|
|
262
301
|
const name = ref.match(/^#\/components\/schemas\/(.+)$/u)?.[1];
|
|
263
302
|
if (name === void 0) return void 0;
|
|
264
|
-
|
|
303
|
+
const key = safeDecode(name);
|
|
304
|
+
return `${identifiers?.get(key) ?? toIdentifierPascalCase(key)}Schema`;
|
|
265
305
|
}
|
|
266
306
|
/**
|
|
267
307
|
* Encode a property key for object-literal output: bare identifiers stay
|
|
@@ -277,10 +317,10 @@ function isRecord(v) {
|
|
|
277
317
|
}
|
|
278
318
|
//#endregion
|
|
279
319
|
//#region src/helper/ref-resolver.ts
|
|
280
|
-
function valueToCode(value, codeReplacements) {
|
|
281
|
-
return encodeValue(value, codeReplacements, /* @__PURE__ */ new Set());
|
|
320
|
+
function valueToCode(value, codeReplacements, identifiers) {
|
|
321
|
+
return encodeValue(value, codeReplacements, identifiers, /* @__PURE__ */ new Set());
|
|
282
322
|
}
|
|
283
|
-
function encodeValue(value, codeReplacements, seen) {
|
|
323
|
+
function encodeValue(value, codeReplacements, identifiers, seen) {
|
|
284
324
|
const replacement = codeReplacements?.get(value);
|
|
285
325
|
if (replacement !== void 0) return replacement;
|
|
286
326
|
if (value === null) return "null";
|
|
@@ -292,19 +332,19 @@ function encodeValue(value, codeReplacements, seen) {
|
|
|
292
332
|
if (Array.isArray(value)) {
|
|
293
333
|
if (seen.has(value)) return "null";
|
|
294
334
|
seen.add(value);
|
|
295
|
-
const code = `[${value.map((v) => encodeValue(v, codeReplacements, seen)).join(",")}]`;
|
|
335
|
+
const code = `[${value.map((v) => encodeValue(v, codeReplacements, identifiers, seen)).join(",")}]`;
|
|
296
336
|
seen.delete(value);
|
|
297
337
|
return code;
|
|
298
338
|
}
|
|
299
339
|
if (isRecord(value)) {
|
|
300
340
|
if (seen.has(value)) return "null";
|
|
301
341
|
if ("$ref" in value && typeof value.$ref === "string") {
|
|
302
|
-
const varName = makeSchemaVarName(value.$ref);
|
|
342
|
+
const varName = makeSchemaVarName(value.$ref, identifiers);
|
|
303
343
|
if (varName) return varName;
|
|
304
344
|
}
|
|
305
345
|
seen.add(value);
|
|
306
346
|
const entries = Object.entries(value).map(([k, v]) => {
|
|
307
|
-
return `${/^[a-zA-Z_$][a-zA-Z0-9_$]*$/u.test(k) ? k : JSON.stringify(k)}:${encodeValue(v, codeReplacements, seen)}`;
|
|
347
|
+
return `${/^[a-zA-Z_$][a-zA-Z0-9_$]*$/u.test(k) ? k : JSON.stringify(k)}:${encodeValue(v, codeReplacements, identifiers, seen)}`;
|
|
308
348
|
});
|
|
309
349
|
seen.delete(value);
|
|
310
350
|
return `{${entries.join(",")}}`;
|
|
@@ -318,135 +358,261 @@ function isInlineSchema(value) {
|
|
|
318
358
|
if ("$ref" in value) return false;
|
|
319
359
|
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;
|
|
320
360
|
}
|
|
321
|
-
function
|
|
322
|
-
|
|
361
|
+
function slotAt(path, fallback) {
|
|
362
|
+
if (path.includes("headers")) return "header";
|
|
363
|
+
if (path.includes("parameters")) return "parameter";
|
|
364
|
+
if (path.includes("content")) return fallback ?? "response-content";
|
|
365
|
+
return fallback ?? "media-type";
|
|
366
|
+
}
|
|
367
|
+
function makeSchemaReplacements(value, adapter, options) {
|
|
368
|
+
return new Map(makeEntries(value, adapter, /* @__PURE__ */ new Set(), [], options));
|
|
323
369
|
}
|
|
324
|
-
function makeEntries(v, adapter, seen,
|
|
370
|
+
function makeEntries(v, adapter, seen, path, options) {
|
|
325
371
|
if (v === null || v === void 0 || typeof v !== "object") return [];
|
|
326
372
|
if (seen.has(v)) return [];
|
|
327
|
-
const wrap = adapter.wrapSchema
|
|
328
|
-
if (
|
|
373
|
+
const wrap = (expr) => adapter.wrapSchema ? adapter.wrapSchema(expr, slotAt(path, options?.slot)) : expr;
|
|
374
|
+
if (path.at(-1) === "schema") {
|
|
329
375
|
if (isInlineSchema(v)) return [[v, wrap(adapter.toExpression(v))]];
|
|
330
376
|
if (isRecord(v) && typeof v.$ref === "string") {
|
|
331
|
-
const varName = makeSchemaVarName(v.$ref);
|
|
377
|
+
const varName = makeSchemaVarName(v.$ref, options?.identifiers);
|
|
332
378
|
if (varName) return [[v, wrap(varName)]];
|
|
333
379
|
}
|
|
334
380
|
}
|
|
335
381
|
seen.add(v);
|
|
336
|
-
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)) : [];
|
|
382
|
+
const entries = Array.isArray(v) ? v.flatMap((item) => makeEntries(item, adapter, seen, path, options)) : isRecord(v) ? Object.entries(v).flatMap(([k, child]) => makeEntries(child, adapter, seen, [...path, k], options)) : [];
|
|
337
383
|
seen.delete(v);
|
|
338
384
|
return entries;
|
|
339
385
|
}
|
|
340
386
|
//#endregion
|
|
387
|
+
//#region src/generator/components/declaration.ts
|
|
388
|
+
/**
|
|
389
|
+
* Join declaration bodies the way `make<Kind>Code` already does: `;` between
|
|
390
|
+
* entries, no `import ` line. `trailingNewline` matches examples / links /
|
|
391
|
+
* security-schemes / parameters / headers.
|
|
392
|
+
*/
|
|
393
|
+
function joinDeclarations(declarations, options) {
|
|
394
|
+
if (declarations.length === 0) return "";
|
|
395
|
+
const body = declarations.map((d) => d.code).join(";");
|
|
396
|
+
const text = options?.trailingNewline === true ? `${body}\n` : body;
|
|
397
|
+
return options?.importLine === void 0 ? text : `${options.importLine}\n\n${text}`;
|
|
398
|
+
}
|
|
399
|
+
//#endregion
|
|
341
400
|
//#region src/generator/components/callbacks.ts
|
|
342
|
-
function
|
|
401
|
+
function makeCallbacksDeclarations(components, adapter, options) {
|
|
343
402
|
const { callbacks } = components;
|
|
344
|
-
if (!callbacks) return
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
403
|
+
if (!callbacks) return [];
|
|
404
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
405
|
+
return Object.entries(callbacks).flatMap(([name, callback]) => {
|
|
406
|
+
if ("$ref" in callback && callback.$ref) return [];
|
|
407
|
+
const ident = toIdentifierPascalCase(name);
|
|
408
|
+
const varName = `${ident}Callback`;
|
|
409
|
+
const replacements = makeSchemaReplacements(callback, adapter, {
|
|
410
|
+
slot: "response-content",
|
|
411
|
+
...options?.identifiers && { identifiers: options.identifiers }
|
|
412
|
+
});
|
|
413
|
+
return [{
|
|
414
|
+
name,
|
|
415
|
+
varName,
|
|
416
|
+
fileName: declarationFileName(ident),
|
|
417
|
+
code: `export const ${varName}=${valueToCode(callback, replacements, options?.identifiers)}${asConst}`
|
|
418
|
+
}];
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
function makeCallbacksCode(components, adapter, readonly) {
|
|
422
|
+
return joinDeclarations(makeCallbacksDeclarations(components, adapter, { readonly }));
|
|
353
423
|
}
|
|
354
424
|
//#endregion
|
|
355
425
|
//#region src/generator/components/examples.ts
|
|
356
|
-
function
|
|
426
|
+
function makeExamplesDeclarations(components, options) {
|
|
357
427
|
const { examples } = components;
|
|
358
|
-
if (!examples) return
|
|
359
|
-
const
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
return
|
|
364
|
-
|
|
428
|
+
if (!examples) return [];
|
|
429
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
430
|
+
return Object.entries(examples).map(([name, example]) => {
|
|
431
|
+
const ident = toIdentifierPascalCase(name);
|
|
432
|
+
const varName = `${ident}Example`;
|
|
433
|
+
return {
|
|
434
|
+
name,
|
|
435
|
+
varName,
|
|
436
|
+
fileName: declarationFileName(ident),
|
|
437
|
+
code: `export const ${varName}=${valueToCode(example)}${asConst}`
|
|
438
|
+
};
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
function makeExamplesCode(components, readonly) {
|
|
442
|
+
return joinDeclarations(makeExamplesDeclarations(components, { readonly }), { trailingNewline: true });
|
|
365
443
|
}
|
|
366
444
|
//#endregion
|
|
367
445
|
//#region src/generator/components/headers.ts
|
|
368
|
-
function
|
|
446
|
+
function makeHeadersDeclarations(components, adapter, options) {
|
|
369
447
|
const { headers } = components;
|
|
370
|
-
if (!headers) return
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
448
|
+
if (!headers) return [];
|
|
449
|
+
return Object.entries(headers).flatMap(([name, header]) => {
|
|
450
|
+
if (!("schema" in header) || !header.schema) return [];
|
|
451
|
+
const ident = toIdentifierPascalCase(name);
|
|
452
|
+
const varName = `${ident}HeaderSchema`;
|
|
453
|
+
const raw = adapter.toExpression(header.schema);
|
|
454
|
+
const expr = adapter.wrapSchema ? adapter.wrapSchema(raw, "header") : raw;
|
|
455
|
+
const typeExport = options?.exportTypes === true ? `\n\n${adapter.renderTypeInfer(varName)}` : "";
|
|
456
|
+
return [{
|
|
457
|
+
name,
|
|
458
|
+
varName,
|
|
459
|
+
fileName: declarationFileName(ident),
|
|
460
|
+
code: `export const ${varName}=${expr}${typeExport}`
|
|
461
|
+
}];
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
function makeHeadersCode(components, adapter, exportTypes) {
|
|
465
|
+
return joinDeclarations(makeHeadersDeclarations(components, adapter, exportTypes === true ? { exportTypes: true } : void 0), {
|
|
466
|
+
trailingNewline: true,
|
|
467
|
+
importLine: adapter.renderImport()
|
|
468
|
+
});
|
|
380
469
|
}
|
|
381
470
|
//#endregion
|
|
382
471
|
//#region src/generator/components/links.ts
|
|
383
|
-
function
|
|
472
|
+
function makeLinksDeclarations(components, options) {
|
|
384
473
|
const { links } = components;
|
|
385
|
-
if (!links) return
|
|
386
|
-
const
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
return
|
|
391
|
-
|
|
474
|
+
if (!links) return [];
|
|
475
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
476
|
+
return Object.entries(links).map(([name, link]) => {
|
|
477
|
+
const ident = toIdentifierPascalCase(name);
|
|
478
|
+
const varName = `${ident}Link`;
|
|
479
|
+
return {
|
|
480
|
+
name,
|
|
481
|
+
varName,
|
|
482
|
+
fileName: declarationFileName(ident),
|
|
483
|
+
code: `export const ${varName}=${valueToCode(link)}${asConst}`
|
|
484
|
+
};
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
function makeLinksCode(components, readonly) {
|
|
488
|
+
return joinDeclarations(makeLinksDeclarations(components, { readonly }), { trailingNewline: true });
|
|
489
|
+
}
|
|
490
|
+
//#endregion
|
|
491
|
+
//#region src/generator/components/media-types.ts
|
|
492
|
+
function makeMediaTypesDeclarations(components, adapter, options) {
|
|
493
|
+
const { mediaTypes } = components;
|
|
494
|
+
if (!mediaTypes) return [];
|
|
495
|
+
return Object.entries(mediaTypes).flatMap(([name, media]) => {
|
|
496
|
+
if ("$ref" in media && media.$ref) return [];
|
|
497
|
+
if (!("schema" in media) || !media.schema) return [];
|
|
498
|
+
const ident = toIdentifierPascalCase(name);
|
|
499
|
+
const varName = `${ident}MediaTypeSchema`;
|
|
500
|
+
const raw = adapter.toExpression(media.schema);
|
|
501
|
+
const expr = adapter.wrapSchema ? adapter.wrapSchema(raw, "media-type") : raw;
|
|
502
|
+
const typeExport = options?.exportTypes === true ? `\n\n${adapter.renderTypeInfer(varName)}` : "";
|
|
503
|
+
return [{
|
|
504
|
+
name,
|
|
505
|
+
varName,
|
|
506
|
+
fileName: declarationFileName(ident),
|
|
507
|
+
code: `export const ${varName}=${expr}${typeExport}`
|
|
508
|
+
}];
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
function makeMediaTypesCode(components, adapter, exportTypes) {
|
|
512
|
+
return joinDeclarations(makeMediaTypesDeclarations(components, adapter, exportTypes === true ? { exportTypes: true } : void 0), {
|
|
513
|
+
trailingNewline: true,
|
|
514
|
+
importLine: adapter.renderImport()
|
|
515
|
+
});
|
|
392
516
|
}
|
|
393
517
|
//#endregion
|
|
394
518
|
//#region src/generator/components/parameters.ts
|
|
395
|
-
function
|
|
519
|
+
function makeParametersDeclarations(components, adapter, options) {
|
|
396
520
|
const { parameters } = components;
|
|
397
|
-
if (!parameters) return
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
const imports = adapter.renderImport();
|
|
401
|
-
const exports = entries.map(([name, param]) => {
|
|
402
|
-
if (!param.schema) return "";
|
|
521
|
+
if (!parameters) return [];
|
|
522
|
+
return Object.entries(parameters).flatMap(([name, param]) => {
|
|
523
|
+
if (!param.schema) return [];
|
|
403
524
|
const paramIn = param.in === "query" || param.in === "path" ? param.in : void 0;
|
|
404
|
-
const
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
525
|
+
const ident = toIdentifierPascalCase(name);
|
|
526
|
+
const varName = `${ident}ParamsSchema`;
|
|
527
|
+
const raw = adapter.toExpression(param.schema, paramIn);
|
|
528
|
+
const expr = adapter.wrapSchema ? adapter.wrapSchema(raw, "parameter") : raw;
|
|
529
|
+
const typeExport = options?.exportTypes === true ? `\n\n${adapter.renderTypeInfer(varName)}` : "";
|
|
530
|
+
return [{
|
|
531
|
+
name,
|
|
532
|
+
varName,
|
|
533
|
+
fileName: declarationFileName(ident),
|
|
534
|
+
code: `export const ${varName}=${expr}${typeExport}`
|
|
535
|
+
}];
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
function makeParametersCode(components, adapter, exportTypes) {
|
|
539
|
+
return joinDeclarations(makeParametersDeclarations(components, adapter, exportTypes === true ? { exportTypes: true } : void 0), {
|
|
540
|
+
trailingNewline: true,
|
|
541
|
+
importLine: adapter.renderImport()
|
|
542
|
+
});
|
|
408
543
|
}
|
|
409
544
|
//#endregion
|
|
410
545
|
//#region src/generator/components/path-items.ts
|
|
411
|
-
function
|
|
546
|
+
function makePathItemsDeclarations(components, adapter, options) {
|
|
412
547
|
const { pathItems } = components;
|
|
413
|
-
if (!pathItems) return
|
|
414
|
-
const
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
548
|
+
if (!pathItems) return [];
|
|
549
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
550
|
+
return Object.entries(pathItems).map(([name, pathItem]) => {
|
|
551
|
+
const ident = toIdentifierPascalCase(name);
|
|
552
|
+
const varName = `${ident}PathItem`;
|
|
553
|
+
const replacements = makeSchemaReplacements(pathItem, adapter, {
|
|
554
|
+
slot: "response-content",
|
|
555
|
+
...options?.identifiers && { identifiers: options.identifiers }
|
|
556
|
+
});
|
|
557
|
+
return {
|
|
558
|
+
name,
|
|
559
|
+
varName,
|
|
560
|
+
fileName: declarationFileName(ident),
|
|
561
|
+
code: `export const ${varName}=${valueToCode(pathItem, replacements, options?.identifiers)}${asConst}`
|
|
562
|
+
};
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
function makePathItemsCode(components, adapter, readonly) {
|
|
566
|
+
return joinDeclarations(makePathItemsDeclarations(components, adapter, { readonly }));
|
|
422
567
|
}
|
|
423
568
|
//#endregion
|
|
424
569
|
//#region src/generator/components/request-bodies.ts
|
|
425
|
-
function
|
|
570
|
+
function makeRequestBodiesDeclarations(components, adapter, options) {
|
|
426
571
|
const { requestBodies } = components;
|
|
427
|
-
if (!requestBodies) return
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
572
|
+
if (!requestBodies) return [];
|
|
573
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
574
|
+
return Object.entries(requestBodies).map(([name, body]) => {
|
|
575
|
+
const ident = toIdentifierPascalCase(name);
|
|
576
|
+
const varName = `${ident}RequestBody`;
|
|
577
|
+
const replacements = makeSchemaReplacements(body, adapter, {
|
|
578
|
+
slot: "request-content",
|
|
579
|
+
...options?.identifiers && { identifiers: options.identifiers }
|
|
580
|
+
});
|
|
581
|
+
return {
|
|
582
|
+
name,
|
|
583
|
+
varName,
|
|
584
|
+
fileName: declarationFileName(ident),
|
|
585
|
+
code: `export const ${varName}=${valueToCode(body, replacements, options?.identifiers)}${asConst}`
|
|
586
|
+
};
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
function makeRequestBodiesCode(components, adapter, readonly) {
|
|
590
|
+
return joinDeclarations(makeRequestBodiesDeclarations(components, adapter, { readonly }));
|
|
436
591
|
}
|
|
437
592
|
//#endregion
|
|
438
593
|
//#region src/generator/components/responses.ts
|
|
439
|
-
function
|
|
594
|
+
function makeResponsesDeclarations(components, adapter, options) {
|
|
440
595
|
const { responses } = components;
|
|
441
|
-
if (!responses) return
|
|
442
|
-
const
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
596
|
+
if (!responses) return [];
|
|
597
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
598
|
+
return Object.entries(responses).flatMap(([name, response]) => {
|
|
599
|
+
if ("$ref" in response && response.$ref) return [];
|
|
600
|
+
const ident = toIdentifierPascalCase(name);
|
|
601
|
+
const varName = `${ident}Response`;
|
|
602
|
+
const replacements = makeSchemaReplacements(response, adapter, {
|
|
603
|
+
slot: "response-content",
|
|
604
|
+
...options?.identifiers && { identifiers: options.identifiers }
|
|
605
|
+
});
|
|
606
|
+
return [{
|
|
607
|
+
name,
|
|
608
|
+
varName,
|
|
609
|
+
fileName: declarationFileName(ident),
|
|
610
|
+
code: `export const ${varName}=${valueToCode(response, replacements, options?.identifiers)}${asConst}`
|
|
611
|
+
}];
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
function makeResponsesCode(components, adapter, readonly) {
|
|
615
|
+
return joinDeclarations(makeResponsesDeclarations(components, adapter, { readonly }));
|
|
450
616
|
}
|
|
451
617
|
//#endregion
|
|
452
618
|
//#region src/helper/graph.ts
|
|
@@ -606,19 +772,6 @@ function wrapReferences(expr, names, wrap) {
|
|
|
606
772
|
}
|
|
607
773
|
//#endregion
|
|
608
774
|
//#region src/generator/components/schemas.ts
|
|
609
|
-
/** `name`, or the first of `name2`, `name3`, … not in `used`. */
|
|
610
|
-
function claimName(name, used, i = 2) {
|
|
611
|
-
if (!used.has(name)) return name;
|
|
612
|
-
return used.has(`${name}${i}`) ? claimName(name, used, i + 1) : `${name}${i}`;
|
|
613
|
-
}
|
|
614
|
-
/**
|
|
615
|
-
* `toIdentifierPascalCase` folds `user` and `User` into one identifier; later
|
|
616
|
-
* colliders get a numeric suffix so no declaration is lost. A `$ref` to a later
|
|
617
|
-
* collider still resolves to the first declaration.
|
|
618
|
-
*/
|
|
619
|
-
function makeSchemaIdentifiers(schemas) {
|
|
620
|
-
return Object.keys(schemas).reduce((acc, key) => acc.set(key, claimName(toIdentifierPascalCase(key), new Set(acc.values()))), /* @__PURE__ */ new Map());
|
|
621
|
-
}
|
|
622
775
|
/** Keys whose value is a subschema (or a list of them) and keys whose value maps names to subschemas. */
|
|
623
776
|
const SUBSCHEMA_KEYS = /* @__PURE__ */ new Set([
|
|
624
777
|
"items",
|
|
@@ -674,19 +827,33 @@ function markReadonly(schema) {
|
|
|
674
827
|
* becomes one `Type.Cyclic(...)` / `scope(...)` container and each member selects
|
|
675
828
|
* its entry. schema-to-library builds the container from a `$defs` document.
|
|
676
829
|
*/
|
|
677
|
-
function
|
|
830
|
+
function rewriteCollisionRef(node, identifiers) {
|
|
831
|
+
if (typeof node.$ref !== "string" || !node.$ref.startsWith("#/components/schemas/")) return node;
|
|
832
|
+
const key = schemaRefToName(node.$ref);
|
|
833
|
+
const ident = identifiers.get(key);
|
|
834
|
+
if (ident === void 0 || ident === toIdentifierPascalCase(key)) return node;
|
|
835
|
+
return {
|
|
836
|
+
...node,
|
|
837
|
+
$ref: `#/components/schemas/${ident}`
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
function makeCyclicContainer(varName, group, schemas, identifiers, adapter, ref) {
|
|
678
841
|
const groupIdentifiers = new Map(group.map((name) => [name, identifiers.get(name) ?? name]));
|
|
679
842
|
const localize = (node) => {
|
|
680
|
-
const
|
|
681
|
-
if (
|
|
682
|
-
const
|
|
683
|
-
|
|
843
|
+
const key = node.$ref ? schemaRefToName(node.$ref) : void 0;
|
|
844
|
+
if (key === void 0) return node;
|
|
845
|
+
const groupIdent = groupIdentifiers.get(key);
|
|
846
|
+
if (groupIdent !== void 0) {
|
|
847
|
+
const local = Object.fromEntries([...Object.entries(node), ["$ref", `#/$defs/${groupIdent}Schema`]]);
|
|
848
|
+
return isSchema(local) ? local : node;
|
|
849
|
+
}
|
|
850
|
+
return rewriteCollisionRef(node, identifiers);
|
|
684
851
|
};
|
|
685
852
|
const $defs = Object.fromEntries(group.map((name) => [`${groupIdentifiers.get(name)}Schema`, mapSchema(schemas[name] ?? {}, localize)]));
|
|
686
853
|
return adapter.toExpression({
|
|
687
854
|
title: varName,
|
|
688
855
|
$defs
|
|
689
|
-
});
|
|
856
|
+
}, void 0, ref === void 0 ? void 0 : { ref });
|
|
690
857
|
}
|
|
691
858
|
/**
|
|
692
859
|
* The import line a schemas file needs. `cyclic` is true when this file (or
|
|
@@ -706,9 +873,6 @@ function exportedTypeName(ident, varName, adapter, options) {
|
|
|
706
873
|
if (options?.typeAlias !== "key") return varName;
|
|
707
874
|
return (adapter.reservedTypeNames ?? []).includes(ident) ? `${ident}Type` : ident;
|
|
708
875
|
}
|
|
709
|
-
function fileNameOf(ident) {
|
|
710
|
-
return `${ident.charAt(0).toLowerCase()}${ident.slice(1)}`;
|
|
711
|
-
}
|
|
712
876
|
/**
|
|
713
877
|
* One `export const <X>Schema=...` declaration per `components.schemas` entry,
|
|
714
878
|
* in dependency-first order. Only references inside a `$ref` cycle are lazy
|
|
@@ -726,21 +890,23 @@ function makeSchemaDeclarations(schemas, adapter, options) {
|
|
|
726
890
|
]);
|
|
727
891
|
const infer = (varName) => adapter.renderTypeInfer(varName).replace(`export type ${varName}=`, "");
|
|
728
892
|
const style = options?.cyclicTypeStyle === "literal" ? {} : adapter.cyclicTypeStyle ?? {};
|
|
893
|
+
const declarationRef = options?.ref === true;
|
|
729
894
|
return order.map((name) => {
|
|
730
|
-
const schema = prepared[name] ?? {};
|
|
895
|
+
const schema = mapSchema(prepared[name] ?? {}, (node) => rewriteCollisionRef(node, identifiers));
|
|
731
896
|
const ident = identifiers.get(name) ?? toIdentifierPascalCase(name);
|
|
732
897
|
const varName = `${ident}Schema`;
|
|
733
898
|
const group = cycles.get(name);
|
|
734
899
|
const arktypeReadonly = adapter.renderCyclic !== void 0 && options?.readonly === true;
|
|
735
|
-
const
|
|
900
|
+
const hostRef = declarationRef ? name : void 0;
|
|
901
|
+
const container = group && adapter.wrapLazy === void 0 ? makeCyclicContainer(varName, group, arktypeReadonly ? schemas : prepared, identifiers, adapter, adapter.withRef === void 0 ? hostRef : void 0) : void 0;
|
|
736
902
|
const peers = group ?? [];
|
|
737
903
|
const rendered = container === void 0 ? void 0 : adapter.renderCyclic?.(varName, container, [...new Set(peers.flatMap((peer) => collectSchemaRefs(schemas[peer])))].flatMap((ref) => peers.includes(ref) || !identifiers.has(ref) ? [] : [`${identifiers.get(ref)}Schema`])) ?? container;
|
|
738
904
|
const expression = (rendered && arktypeReadonly ? `${rendered}.readonly()` : rendered) ?? adapter.toExpression({
|
|
739
905
|
...schema,
|
|
740
906
|
title: varName
|
|
741
|
-
});
|
|
907
|
+
}, void 0, adapter.withRef === void 0 && hostRef !== void 0 ? { ref: hostRef } : void 0);
|
|
742
908
|
const body = adapter.wrapLazy ? wrapReferences(expression, new Set((group ?? []).map((peer) => `${identifiers.get(peer)}Schema`)), adapter.wrapLazy) : expression;
|
|
743
|
-
const value = options?.wrapDeclaration ? options.wrapDeclaration(body, name) : body;
|
|
909
|
+
const value = options?.wrapDeclaration ? options.wrapDeclaration(body, name) : hostRef !== void 0 && adapter.withRef ? adapter.withRef(body, name) : body;
|
|
744
910
|
const helper = group && adapter.cyclicAnnotation ? claimName(`${ident}Type`, taken) : void 0;
|
|
745
911
|
const annotationText = helper ? adapter.cyclicAnnotation?.(helper) : void 0;
|
|
746
912
|
const usesHelper = helper !== void 0 && annotationText?.includes(helper) === true;
|
|
@@ -752,7 +918,7 @@ function makeSchemaDeclarations(schemas, adapter, options) {
|
|
|
752
918
|
return {
|
|
753
919
|
name,
|
|
754
920
|
varName,
|
|
755
|
-
fileName:
|
|
921
|
+
fileName: declarationFileName(ident),
|
|
756
922
|
importLine: schemaImportLine(adapter, options, containerCycle),
|
|
757
923
|
code: `${typeDef}export const ${varName}${annotation}=${value}${typeExport}`
|
|
758
924
|
};
|
|
@@ -771,15 +937,23 @@ function makeSchemasCode(components, adapter, options) {
|
|
|
771
937
|
}
|
|
772
938
|
//#endregion
|
|
773
939
|
//#region src/generator/components/security-schemes.ts
|
|
774
|
-
function
|
|
940
|
+
function makeSecuritySchemesDeclarations(components, options) {
|
|
775
941
|
const { securitySchemes } = components;
|
|
776
|
-
if (!securitySchemes) return
|
|
777
|
-
const
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
return
|
|
782
|
-
|
|
942
|
+
if (!securitySchemes) return [];
|
|
943
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
944
|
+
return Object.entries(securitySchemes).map(([name, scheme]) => {
|
|
945
|
+
const ident = toIdentifierPascalCase(name);
|
|
946
|
+
const varName = `${ident}SecurityScheme`;
|
|
947
|
+
return {
|
|
948
|
+
name,
|
|
949
|
+
varName,
|
|
950
|
+
fileName: declarationFileName(ident),
|
|
951
|
+
code: `export const ${varName}=${valueToCode(scheme)}${asConst}`
|
|
952
|
+
};
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
function makeSecuritySchemesCode(components, readonly) {
|
|
956
|
+
return joinDeclarations(makeSecuritySchemesDeclarations(components, { readonly }), { trailingNewline: true });
|
|
783
957
|
}
|
|
784
958
|
//#endregion
|
|
785
|
-
export { isRecord, makeAdapter, makeCallbacksCode, makeExamplesCode, makeHeadersCode, makeLinksCode, makeParametersCode, makePathItemsCode, makeRequestBodiesCode, makeResponsesCode, makeSafeKey, makeSchemaDeclarations, makeSchemaReplacements, makeSchemasCode, makeSecuritySchemesCode, parseOpenAPI, schemaImportLine, schemaRefToName, toIdentifierPascalCase, toPascalCase, valueToCode };
|
|
959
|
+
export { declarationFileName, isRecord, makeAdapter, makeCallbacksCode, makeCallbacksDeclarations, makeExamplesCode, makeExamplesDeclarations, makeHeadersCode, makeHeadersDeclarations, makeLinksCode, makeLinksDeclarations, makeMediaTypesCode, makeMediaTypesDeclarations, makeParametersCode, makeParametersDeclarations, makePathItemsCode, makePathItemsDeclarations, makeRequestBodiesCode, makeRequestBodiesDeclarations, makeResponsesCode, makeResponsesDeclarations, makeSafeKey, makeSchemaDeclarations, makeSchemaIdentifiers, makeSchemaReplacements, makeSchemaVarName, makeSchemasCode, makeSecuritySchemesCode, makeSecuritySchemesDeclarations, parseOpenAPI, schemaImportLine, schemaRefToName, toIdentifierPascalCase, toPascalCase, valueToCode };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oas-truth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "oas-truth is a parser from OpenAPI and TypeSpec to a typed OpenAPI document",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"openapi",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"schema-to-library": "^0.
|
|
31
|
+
"schema-to-library": "^0.5.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@apidevtools/swagger-parser": "^13.0.0",
|