oas-truth 0.3.0 → 0.5.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 +19 -1
- package/dist/index.d.ts +94 -41
- package/dist/index.js +369 -189
- package/package.json +14 -14
package/README.md
CHANGED
|
@@ -42,9 +42,27 @@ 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
|
+
`makeParametersCode` reads every parameter as text: `query`, `path`, `header` and `cookie`
|
|
52
|
+
values are coerced, and the parameter's `style`, `explode` and `allowEmptyValue` decide how the
|
|
53
|
+
text is split (`form` with `explode: false` reads `1,2,3`, `deepObject` gathers `filter[key]`).
|
|
54
|
+
`toExpression(schema, paramIn, { style, explode, allowEmptyValue })` takes the same options, and
|
|
55
|
+
`isParamStyle` narrows a free-form `style` to one it reads. TypeBox ignores the three options.
|
|
56
|
+
|
|
57
|
+
`wrapSchema(expr, slot)` is slot-aware (`response-content`, `request-content`, `header`,
|
|
58
|
+
`parameter`, `media-type`). TypeBox imports from `typebox` and reserves `Compile`. Pass
|
|
59
|
+
`{ ref: true }` to `makeSchemaDeclarations` to register the OpenAPI key — TypeBox writes it
|
|
60
|
+
into the outermost builder options; the other libraries use an outer wrap.
|
|
61
|
+
|
|
45
62
|
```ts
|
|
46
|
-
import { makeAdapter, makeSchemaDeclarations } from 'oas-truth'
|
|
63
|
+
import { makeAdapter, makeSchemaDeclarations, makeSchemaIdentifiers } from 'oas-truth'
|
|
47
64
|
|
|
65
|
+
const identifiers = makeSchemaIdentifiers(schemas)
|
|
48
66
|
const declarations = makeSchemaDeclarations(schemas, makeAdapter('arktype'))
|
|
49
67
|
// a cyclic file: import { type, scope } from 'arktype'
|
|
50
68
|
// a non-cyclic file: import { type } from 'arktype'
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
//#region src/openapi/index.d.ts
|
|
2
1
|
export declare function parseOpenAPI(input: string): Promise<{
|
|
3
2
|
readonly ok: true;
|
|
4
3
|
readonly value: OpenAPI;
|
|
@@ -498,10 +497,36 @@ export type Media = {
|
|
|
498
497
|
export type Callbacks = {
|
|
499
498
|
readonly [k: string]: PathItem;
|
|
500
499
|
};
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
500
|
+
/**
|
|
501
|
+
* Where a value arrives as text: an OpenAPI parameter location, or `formData` for a field of
|
|
502
|
+
* a form body. schema-to-library coerces the value from text for every one of them.
|
|
503
|
+
*/
|
|
504
|
+
export type ParamIn = 'query' | 'path' | 'header' | 'cookie' | 'formData';
|
|
505
|
+
/**
|
|
506
|
+
* A Parameter Object's `style`: how its text is split into an array or an object.
|
|
507
|
+
*
|
|
508
|
+
* @see https://spec.openapis.org/oas/v3.2.0.html#style-values
|
|
509
|
+
*/
|
|
510
|
+
export type ParamStyle = 'matrix' | 'label' | 'simple' | 'form' | 'spaceDelimited' | 'pipeDelimited' | 'deepObject';
|
|
511
|
+
/** Narrow a Parameter Object's free-form `style` to one schema-to-library reads. */
|
|
512
|
+
export declare function isParamStyle(style: string): style is ParamStyle;
|
|
504
513
|
export type SchemaLib = 'zod' | 'valibot' | 'arktype' | 'effect' | 'typebox';
|
|
514
|
+
/**
|
|
515
|
+
* Which `schema:` slot a host `wrapSchema` hook is looking at. The second
|
|
516
|
+
* argument is optional so an existing one-argument hook keeps compiling.
|
|
517
|
+
*/
|
|
518
|
+
export type SchemaSlot = 'response-content' | 'request-content' | 'header' | 'parameter' | 'media-type';
|
|
519
|
+
/**
|
|
520
|
+
* Optional third argument to `toExpression`. TypeBox puts `ref` in builder options. `style`,
|
|
521
|
+
* `explode` and `allowEmptyValue` are the parameter's own and apply with a `paramIn`; Zod,
|
|
522
|
+
* Valibot, Effect and Arktype read them, TypeBox ignores them.
|
|
523
|
+
*/
|
|
524
|
+
export type ToExpressionOptions = {
|
|
525
|
+
readonly ref?: string;
|
|
526
|
+
readonly style?: ParamStyle;
|
|
527
|
+
readonly explode?: boolean;
|
|
528
|
+
readonly allowEmptyValue?: boolean;
|
|
529
|
+
};
|
|
505
530
|
/**
|
|
506
531
|
* Flags `renderImport` uses when the schemas builder needs more than the
|
|
507
532
|
* library's default line — Arktype `scope` for a `$ref` cycle, TypeBox
|
|
@@ -528,10 +553,14 @@ export type CyclicTypeStyle = {
|
|
|
528
553
|
* required without a cast.
|
|
529
554
|
*
|
|
530
555
|
* `wrapSchema` is an optional host hook applied to every `schema:` slot
|
|
531
|
-
* expression (inline and `$ref` alike).
|
|
532
|
-
*
|
|
533
|
-
*
|
|
534
|
-
*
|
|
556
|
+
* expression (inline and `$ref` alike). The second argument is the slot so a
|
|
557
|
+
* host can wrap response content and leave header schemas plain. Omitting the
|
|
558
|
+
* hook leaves the expression untouched (identity), so the shared library stays
|
|
559
|
+
* framework-agnostic.
|
|
560
|
+
*
|
|
561
|
+
* `withRef` is the library-specific outer wrap for a component name
|
|
562
|
+
* (`.meta({ref})`, `v.metadata`, `.configure({ref})`, `.annotate({identifier})`).
|
|
563
|
+
* TypeBox has no post-construction wrap: pass `ref` to `toExpression` instead.
|
|
535
564
|
*
|
|
536
565
|
* Cycle handling lives next to each library: `wrapLazy` re-wraps a `$ref`
|
|
537
566
|
* inside a strongly connected component (Zod, Valibot, Effect);
|
|
@@ -542,9 +571,10 @@ export type CyclicTypeStyle = {
|
|
|
542
571
|
*/
|
|
543
572
|
export type ComponentAdapter = {
|
|
544
573
|
readonly renderImport: (options?: RenderImportOptions) => string;
|
|
545
|
-
readonly toExpression: (schema: Schema, paramIn?: ParamIn) => string;
|
|
574
|
+
readonly toExpression: (schema: Schema, paramIn?: ParamIn, options?: ToExpressionOptions) => string;
|
|
546
575
|
readonly renderTypeInfer: (constName: string) => string;
|
|
547
|
-
readonly wrapSchema?: (expr: string) => string;
|
|
576
|
+
readonly wrapSchema?: (expr: string, slot?: SchemaSlot) => string;
|
|
577
|
+
readonly withRef?: (expr: string, ref: string) => string;
|
|
548
578
|
readonly wrapLazy?: (varName: string) => string;
|
|
549
579
|
readonly cyclicAnnotation?: (typeName: string) => string;
|
|
550
580
|
readonly cyclicTypeStyle?: CyclicTypeStyle;
|
|
@@ -552,14 +582,12 @@ export type ComponentAdapter = {
|
|
|
552
582
|
readonly reservedTypeNames?: readonly string[];
|
|
553
583
|
};
|
|
554
584
|
export declare function makeAdapter(lib: SchemaLib): ComponentAdapter;
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
export declare function makeSchemaReplacements(value: unknown, adapter: ComponentAdapter): ReadonlyMap<unknown, string>;
|
|
561
|
-
//#endregion
|
|
562
|
-
//#region src/utils/index.d.ts
|
|
585
|
+
export declare function valueToCode(value: unknown, codeReplacements?: ReadonlyMap<unknown, string>, identifiers?: ReadonlyMap<string, string>): string;
|
|
586
|
+
type SchemaReplacementsOptions = {
|
|
587
|
+
readonly slot?: SchemaSlot;
|
|
588
|
+
readonly identifiers?: ReadonlyMap<string, string>;
|
|
589
|
+
};
|
|
590
|
+
export declare function makeSchemaReplacements(value: unknown, adapter: ComponentAdapter, options?: SchemaReplacementsOptions): ReadonlyMap<unknown, string>;
|
|
563
591
|
export declare function toPascalCase(str: string): string;
|
|
564
592
|
/**
|
|
565
593
|
* Extract the component name from a `$ref`, decoding percent-encoding so the
|
|
@@ -574,6 +602,24 @@ export declare function schemaRefToName(ref: string): string;
|
|
|
574
602
|
* injectively so distinct names never collide.
|
|
575
603
|
*/
|
|
576
604
|
export declare function toIdentifierPascalCase(name: string): string;
|
|
605
|
+
/**
|
|
606
|
+
* `toIdentifierPascalCase` folds `user` and `User` into one identifier; later
|
|
607
|
+
* colliders get a numeric suffix so no declaration is lost. The map is
|
|
608
|
+
* `OpenAPI key → identifier` (`user` → `User`, `User` → `User2`).
|
|
609
|
+
*/
|
|
610
|
+
export declare function makeSchemaIdentifiers(schemas: {
|
|
611
|
+
readonly [k: string]: unknown;
|
|
612
|
+
}): Map<string, string>;
|
|
613
|
+
/** Lowercase the first character for a split-file name (`User` → `user`). */
|
|
614
|
+
export declare function declarationFileName(ident: string): string;
|
|
615
|
+
/**
|
|
616
|
+
* Resolve a `#/components/schemas/...` `$ref` to its generated schema variable
|
|
617
|
+
* identifier (`UserSchema`), or `undefined` when the ref does not point at a
|
|
618
|
+
* component schema. Shared by ref-resolver and schema-replacements. Pass the
|
|
619
|
+
* collision map from `makeSchemaIdentifiers` so a later collider resolves to
|
|
620
|
+
* the suffixed name (`User` → `User2Schema`).
|
|
621
|
+
*/
|
|
622
|
+
export declare function makeSchemaVarName(ref: string, identifiers?: ReadonlyMap<string, string>): string | undefined;
|
|
577
623
|
/**
|
|
578
624
|
* Encode a property key for object-literal output: bare identifiers stay
|
|
579
625
|
* unquoted, everything else is JSON-encoded so hyphenated/reserved/numeric
|
|
@@ -584,32 +630,35 @@ export declare function makeSafeKey(key: string): string;
|
|
|
584
630
|
export declare function isRecord(v: unknown): v is {
|
|
585
631
|
readonly [k: string]: unknown;
|
|
586
632
|
};
|
|
587
|
-
|
|
588
|
-
|
|
633
|
+
type ComponentDeclaration = {
|
|
634
|
+
readonly name: string;
|
|
635
|
+
readonly varName: string;
|
|
636
|
+
readonly fileName: string;
|
|
637
|
+
readonly code: string;
|
|
638
|
+
};
|
|
639
|
+
type ComponentCodeOptions = {
|
|
640
|
+
readonly exportTypes?: boolean;
|
|
641
|
+
readonly readonly?: boolean;
|
|
642
|
+
readonly identifiers?: ReadonlyMap<string, string>;
|
|
643
|
+
};
|
|
644
|
+
export declare function makeCallbacksDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
589
645
|
export declare function makeCallbacksCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
590
|
-
|
|
591
|
-
//#region src/generator/components/examples.d.ts
|
|
646
|
+
export declare function makeExamplesDeclarations(components: Components, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
592
647
|
export declare function makeExamplesCode(components: Components, readonly: boolean): string;
|
|
593
|
-
|
|
594
|
-
//#region src/generator/components/headers.d.ts
|
|
648
|
+
export declare function makeHeadersDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
595
649
|
export declare function makeHeadersCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
596
|
-
|
|
597
|
-
//#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
|
-
|
|
600
|
-
|
|
652
|
+
export declare function makeMediaTypesDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
653
|
+
export declare function makeMediaTypesCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
654
|
+
export declare function makeParametersDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
601
655
|
export declare function makeParametersCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
602
|
-
|
|
603
|
-
//#region src/generator/components/path-items.d.ts
|
|
656
|
+
export declare function makePathItemsDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
604
657
|
export declare function makePathItemsCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
605
|
-
|
|
606
|
-
//#region src/generator/components/request-bodies.d.ts
|
|
658
|
+
export declare function makeRequestBodiesDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
607
659
|
export declare function makeRequestBodiesCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
608
|
-
|
|
609
|
-
//#region src/generator/components/responses.d.ts
|
|
660
|
+
export declare function makeResponsesDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
610
661
|
export declare function makeResponsesCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
611
|
-
//#endregion
|
|
612
|
-
//#region src/generator/components/schemas.d.ts
|
|
613
662
|
type SchemasOptions = {
|
|
614
663
|
readonly exportTypes?: boolean;
|
|
615
664
|
readonly readonly?: boolean;
|
|
@@ -627,9 +676,15 @@ type SchemasOptions = {
|
|
|
627
676
|
readonly cyclicTypeStyle?: 'library' | 'literal';
|
|
628
677
|
/**
|
|
629
678
|
* Host hook around each declaration expression — e.g. hono-openapi ref
|
|
630
|
-
* registration. Applied after cycle handling.
|
|
679
|
+
* registration. Applied after cycle handling. Wins over `ref`.
|
|
631
680
|
*/
|
|
632
681
|
readonly wrapDeclaration?: (expr: string, name: string) => string;
|
|
682
|
+
/**
|
|
683
|
+
* Register the OpenAPI key on each declaration. TypeBox puts it in the
|
|
684
|
+
* outermost builder options via schema-to-library; the other libraries use
|
|
685
|
+
* `adapter.withRef` (an outer wrap). A bare identifier is left unchanged.
|
|
686
|
+
*/
|
|
687
|
+
readonly ref?: boolean;
|
|
633
688
|
};
|
|
634
689
|
type SchemaDeclaration = {
|
|
635
690
|
readonly name: string;
|
|
@@ -671,8 +726,6 @@ export declare function makeSchemaDeclarations(schemas: {
|
|
|
671
726
|
* order. Returns `''` when schemas are missing or empty.
|
|
672
727
|
*/
|
|
673
728
|
export declare function makeSchemasCode(components: Components, adapter: ComponentAdapter, options?: SchemasOptions): string;
|
|
674
|
-
|
|
675
|
-
//#region src/generator/components/security-schemes.d.ts
|
|
729
|
+
export declare function makeSecuritySchemesDeclarations(components: Components, options?: ComponentCodeOptions): readonly ComponentDeclaration[];
|
|
676
730
|
export declare function makeSecuritySchemesCode(components: Components, readonly: boolean): string;
|
|
677
|
-
|
|
678
|
-
export type { SchemaDeclaration, SchemasOptions };
|
|
731
|
+
export type { ComponentCodeOptions, ComponentDeclaration, SchemaDeclaration, SchemasOptions };
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { schemaToEffect } from "schema-to-library/effect";
|
|
|
7
7
|
import { schemaToTypebox } from "schema-to-library/typebox";
|
|
8
8
|
import { schemaToValibot } from "schema-to-library/valibot";
|
|
9
9
|
import { schemaToZod } from "schema-to-library/zod";
|
|
10
|
-
|
|
10
|
+
// src/openapi/index.ts
|
|
11
11
|
async function parseOpenAPI(input) {
|
|
12
12
|
try {
|
|
13
13
|
if (typeof input === "string" && input.endsWith(".tsp")) {
|
|
@@ -34,8 +34,24 @@ async function parseOpenAPI(input) {
|
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
// src/adapter/index.ts
|
|
38
|
+
const PARAM_STYLES = [
|
|
39
|
+
"matrix",
|
|
40
|
+
"label",
|
|
41
|
+
"simple",
|
|
42
|
+
"form",
|
|
43
|
+
"spaceDelimited",
|
|
44
|
+
"pipeDelimited",
|
|
45
|
+
"deepObject"
|
|
46
|
+
];
|
|
47
|
+
/** Narrow a Parameter Object's free-form `style` to one schema-to-library reads. */
|
|
48
|
+
function isParamStyle(style) {
|
|
49
|
+
return PARAM_STYLES.includes(style);
|
|
50
|
+
}
|
|
51
|
+
/** A generated identifier (`TagSchema`) is already a registered name — do not wrap it. */
|
|
52
|
+
function isBareIdent(expr) {
|
|
53
|
+
return /^[A-Za-z_$][\w$]*$/u.test(expr);
|
|
54
|
+
}
|
|
39
55
|
/**
|
|
40
56
|
* Widen the rich `Schema` type to a bare JSON-Schema object for schema-to-library.
|
|
41
57
|
* `Schema` is not structurally assignable to its `JSONSchema` (e.g. `x-emailPattern`
|
|
@@ -44,6 +60,15 @@ async function parseOpenAPI(input) {
|
|
|
44
60
|
function toJsonSchema(schema) {
|
|
45
61
|
return schema;
|
|
46
62
|
}
|
|
63
|
+
/** The wire options schema-to-library reads, without an `undefined` member. */
|
|
64
|
+
function wireOptions(paramIn, options) {
|
|
65
|
+
return {
|
|
66
|
+
...paramIn && { paramIn },
|
|
67
|
+
...options?.style !== void 0 && { style: options.style },
|
|
68
|
+
...options?.explode !== void 0 && { explode: options.explode },
|
|
69
|
+
...options?.allowEmptyValue !== void 0 && { allowEmptyValue: options.allowEmptyValue }
|
|
70
|
+
};
|
|
71
|
+
}
|
|
47
72
|
/** Pull the bare expression out of a `export const X = <expr>` wrapper, then strip cross-schema refs. */
|
|
48
73
|
function extractExpr(code, stripRefs = (s) => s) {
|
|
49
74
|
const joined = code.split("\n").filter((line) => !line.startsWith("import ")).join("\n").trim();
|
|
@@ -86,12 +111,12 @@ function renderArktypeCyclic(varName, containerExpr, externals) {
|
|
|
86
111
|
}
|
|
87
112
|
function makeZodAdapter() {
|
|
88
113
|
return {
|
|
89
|
-
toExpression(schema, paramIn) {
|
|
114
|
+
toExpression(schema, paramIn, options) {
|
|
90
115
|
return extractExpr(schemaToZod(toJsonSchema(schema), {
|
|
91
116
|
exportType: false,
|
|
92
117
|
openapi: true,
|
|
93
118
|
readonly: false,
|
|
94
|
-
...paramIn
|
|
119
|
+
...wireOptions(paramIn, options)
|
|
95
120
|
}), stripZodLazy);
|
|
96
121
|
},
|
|
97
122
|
renderImport() {
|
|
@@ -105,17 +130,20 @@ function makeZodAdapter() {
|
|
|
105
130
|
},
|
|
106
131
|
cyclicAnnotation(typeName) {
|
|
107
132
|
return `z.ZodType<${typeName}>`;
|
|
133
|
+
},
|
|
134
|
+
withRef(expr, ref) {
|
|
135
|
+
return isBareIdent(expr) ? expr : `${expr}.meta({ref:${JSON.stringify(ref)}})`;
|
|
108
136
|
}
|
|
109
137
|
};
|
|
110
138
|
}
|
|
111
139
|
function makeValibotAdapter() {
|
|
112
140
|
return {
|
|
113
|
-
toExpression(schema, paramIn) {
|
|
141
|
+
toExpression(schema, paramIn, options) {
|
|
114
142
|
return extractExpr(schemaToValibot(toJsonSchema(schema), {
|
|
115
143
|
exportType: false,
|
|
116
144
|
openapi: true,
|
|
117
145
|
readonly: false,
|
|
118
|
-
...paramIn
|
|
146
|
+
...wireOptions(paramIn, options)
|
|
119
147
|
}), stripValibotLazy);
|
|
120
148
|
},
|
|
121
149
|
renderImport() {
|
|
@@ -130,17 +158,20 @@ function makeValibotAdapter() {
|
|
|
130
158
|
cyclicAnnotation(typeName) {
|
|
131
159
|
return `v.GenericSchema<${typeName}>`;
|
|
132
160
|
},
|
|
133
|
-
cyclicTypeStyle: { optionalUndefined: true }
|
|
161
|
+
cyclicTypeStyle: { optionalUndefined: true },
|
|
162
|
+
withRef(expr, ref) {
|
|
163
|
+
return isBareIdent(expr) ? expr : `v.pipe(${expr},v.metadata({ref:${JSON.stringify(ref)}}))`;
|
|
164
|
+
}
|
|
134
165
|
};
|
|
135
166
|
}
|
|
136
167
|
function makeArktypeAdapter() {
|
|
137
168
|
return {
|
|
138
|
-
toExpression(schema, paramIn) {
|
|
169
|
+
toExpression(schema, paramIn, options) {
|
|
139
170
|
return extractExpr(schemaToArktype(toJsonSchema(schema), {
|
|
140
171
|
exportType: false,
|
|
141
172
|
openapi: true,
|
|
142
173
|
readonly: false,
|
|
143
|
-
...paramIn
|
|
174
|
+
...wireOptions(paramIn, options)
|
|
144
175
|
}));
|
|
145
176
|
},
|
|
146
177
|
renderImport(options) {
|
|
@@ -150,21 +181,25 @@ function makeArktypeAdapter() {
|
|
|
150
181
|
return `export type ${constName}=typeof ${constName}.infer`;
|
|
151
182
|
},
|
|
152
183
|
renderCyclic: renderArktypeCyclic,
|
|
153
|
-
reservedTypeNames: ["type", "scope"]
|
|
184
|
+
reservedTypeNames: ["type", "scope"],
|
|
185
|
+
withRef(expr, ref) {
|
|
186
|
+
return isBareIdent(expr) ? expr : `${expr}.configure({ref:${JSON.stringify(ref)}})`;
|
|
187
|
+
}
|
|
154
188
|
};
|
|
155
189
|
}
|
|
156
190
|
function makeTypeboxAdapter() {
|
|
157
191
|
return {
|
|
158
|
-
toExpression(schema, paramIn) {
|
|
192
|
+
toExpression(schema, paramIn, options) {
|
|
159
193
|
return extractExpr(schemaToTypebox(toJsonSchema(schema), {
|
|
160
194
|
exportType: false,
|
|
161
195
|
openapi: true,
|
|
162
196
|
readonly: false,
|
|
163
|
-
...paramIn
|
|
197
|
+
...wireOptions(paramIn, options),
|
|
198
|
+
...options?.ref !== void 0 && { ref: options.ref }
|
|
164
199
|
}));
|
|
165
200
|
},
|
|
166
201
|
renderImport(options) {
|
|
167
|
-
return options?.exportTypes === true ? "import { Type, type Static } from '
|
|
202
|
+
return options?.exportTypes === true ? "import { Type, type Static } from 'typebox'" : "import { Type } from 'typebox'";
|
|
168
203
|
},
|
|
169
204
|
renderTypeInfer(constName) {
|
|
170
205
|
return `export type ${constName}=Static<typeof ${constName}>`;
|
|
@@ -172,18 +207,19 @@ function makeTypeboxAdapter() {
|
|
|
172
207
|
reservedTypeNames: [
|
|
173
208
|
"Type",
|
|
174
209
|
"Static",
|
|
175
|
-
"Codec"
|
|
210
|
+
"Codec",
|
|
211
|
+
"Compile"
|
|
176
212
|
]
|
|
177
213
|
};
|
|
178
214
|
}
|
|
179
215
|
function makeEffectAdapter() {
|
|
180
216
|
return {
|
|
181
|
-
toExpression(schema, paramIn) {
|
|
217
|
+
toExpression(schema, paramIn, options) {
|
|
182
218
|
return extractExpr(schemaToEffect(toJsonSchema(schema), {
|
|
183
219
|
exportType: false,
|
|
184
220
|
openapi: true,
|
|
185
221
|
readonly: false,
|
|
186
|
-
...paramIn
|
|
222
|
+
...wireOptions(paramIn, options)
|
|
187
223
|
}), stripEffectSuspend);
|
|
188
224
|
},
|
|
189
225
|
renderImport() {
|
|
@@ -202,11 +238,13 @@ function makeEffectAdapter() {
|
|
|
202
238
|
optionalUndefined: true,
|
|
203
239
|
readonlyArrays: true
|
|
204
240
|
},
|
|
205
|
-
reservedTypeNames: ["Schema", "Effect"]
|
|
241
|
+
reservedTypeNames: ["Schema", "Effect"],
|
|
242
|
+
withRef(expr, ref) {
|
|
243
|
+
return isBareIdent(expr) ? expr : `${expr}.annotate({identifier:${JSON.stringify(ref)}})`;
|
|
244
|
+
}
|
|
206
245
|
};
|
|
207
246
|
}
|
|
208
|
-
|
|
209
|
-
//#region src/utils/index.ts
|
|
247
|
+
// src/utils/index.ts
|
|
210
248
|
function toPascalCase(str) {
|
|
211
249
|
return str.replaceAll(/[^a-zA-Z0-9]+(.)/gu, (_, c) => c.toUpperCase()).replace(/^(.)/u, (_, c) => c.toUpperCase());
|
|
212
250
|
}
|
|
@@ -254,14 +292,36 @@ function toIdentifierPascalCase(name) {
|
|
|
254
292
|
return result;
|
|
255
293
|
}
|
|
256
294
|
/**
|
|
295
|
+
* `name`, or the first of `name2`, `name3`, … not in `used`.
|
|
296
|
+
*/
|
|
297
|
+
function claimName(name, used, i = 2) {
|
|
298
|
+
if (!used.has(name)) return name;
|
|
299
|
+
return used.has(`${name}${i}`) ? claimName(name, used, i + 1) : `${name}${i}`;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* `toIdentifierPascalCase` folds `user` and `User` into one identifier; later
|
|
303
|
+
* colliders get a numeric suffix so no declaration is lost. The map is
|
|
304
|
+
* `OpenAPI key → identifier` (`user` → `User`, `User` → `User2`).
|
|
305
|
+
*/
|
|
306
|
+
function makeSchemaIdentifiers(schemas) {
|
|
307
|
+
return Object.keys(schemas).reduce((acc, key) => acc.set(key, claimName(toIdentifierPascalCase(key), new Set(acc.values()))), /* @__PURE__ */ new Map());
|
|
308
|
+
}
|
|
309
|
+
/** Lowercase the first character for a split-file name (`User` → `user`). */
|
|
310
|
+
function declarationFileName(ident) {
|
|
311
|
+
return `${ident.charAt(0).toLowerCase()}${ident.slice(1)}`;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
257
314
|
* Resolve a `#/components/schemas/...` `$ref` to its generated schema variable
|
|
258
315
|
* identifier (`UserSchema`), or `undefined` when the ref does not point at a
|
|
259
|
-
* component schema. Shared by ref-resolver and schema-replacements.
|
|
316
|
+
* component schema. Shared by ref-resolver and schema-replacements. Pass the
|
|
317
|
+
* collision map from `makeSchemaIdentifiers` so a later collider resolves to
|
|
318
|
+
* the suffixed name (`User` → `User2Schema`).
|
|
260
319
|
*/
|
|
261
|
-
function makeSchemaVarName(ref) {
|
|
320
|
+
function makeSchemaVarName(ref, identifiers) {
|
|
262
321
|
const name = ref.match(/^#\/components\/schemas\/(.+)$/u)?.[1];
|
|
263
322
|
if (name === void 0) return void 0;
|
|
264
|
-
|
|
323
|
+
const key = safeDecode(name);
|
|
324
|
+
return `${identifiers?.get(key) ?? toIdentifierPascalCase(key)}Schema`;
|
|
265
325
|
}
|
|
266
326
|
/**
|
|
267
327
|
* Encode a property key for object-literal output: bare identifiers stay
|
|
@@ -275,12 +335,11 @@ function makeSafeKey(key) {
|
|
|
275
335
|
function isRecord(v) {
|
|
276
336
|
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
277
337
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
return encodeValue(value, codeReplacements, /* @__PURE__ */ new Set());
|
|
338
|
+
// src/helper/ref-resolver.ts
|
|
339
|
+
function valueToCode(value, codeReplacements, identifiers) {
|
|
340
|
+
return encodeValue(value, codeReplacements, identifiers, /* @__PURE__ */ new Set());
|
|
282
341
|
}
|
|
283
|
-
function encodeValue(value, codeReplacements, seen) {
|
|
342
|
+
function encodeValue(value, codeReplacements, identifiers, seen) {
|
|
284
343
|
const replacement = codeReplacements?.get(value);
|
|
285
344
|
if (replacement !== void 0) return replacement;
|
|
286
345
|
if (value === null) return "null";
|
|
@@ -292,164 +351,281 @@ function encodeValue(value, codeReplacements, seen) {
|
|
|
292
351
|
if (Array.isArray(value)) {
|
|
293
352
|
if (seen.has(value)) return "null";
|
|
294
353
|
seen.add(value);
|
|
295
|
-
const code = `[${value.map((v) => encodeValue(v, codeReplacements, seen)).join(",")}]`;
|
|
354
|
+
const code = `[${value.map((v) => encodeValue(v, codeReplacements, identifiers, seen)).join(",")}]`;
|
|
296
355
|
seen.delete(value);
|
|
297
356
|
return code;
|
|
298
357
|
}
|
|
299
358
|
if (isRecord(value)) {
|
|
300
359
|
if (seen.has(value)) return "null";
|
|
301
360
|
if ("$ref" in value && typeof value.$ref === "string") {
|
|
302
|
-
const varName = makeSchemaVarName(value.$ref);
|
|
361
|
+
const varName = makeSchemaVarName(value.$ref, identifiers);
|
|
303
362
|
if (varName) return varName;
|
|
304
363
|
}
|
|
305
364
|
seen.add(value);
|
|
306
365
|
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)}`;
|
|
366
|
+
return `${/^[a-zA-Z_$][a-zA-Z0-9_$]*$/u.test(k) ? k : JSON.stringify(k)}:${encodeValue(v, codeReplacements, identifiers, seen)}`;
|
|
308
367
|
});
|
|
309
368
|
seen.delete(value);
|
|
310
369
|
return `{${entries.join(",")}}`;
|
|
311
370
|
}
|
|
312
371
|
return "null";
|
|
313
372
|
}
|
|
314
|
-
|
|
315
|
-
//#region src/helper/schema-replacements.ts
|
|
373
|
+
// src/helper/schema-replacements.ts
|
|
316
374
|
function isInlineSchema(value) {
|
|
317
375
|
if (!isRecord(value)) return false;
|
|
318
376
|
if ("$ref" in value) return false;
|
|
319
377
|
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
378
|
}
|
|
321
|
-
function
|
|
322
|
-
|
|
379
|
+
function slotAt(path, fallback) {
|
|
380
|
+
if (path.includes("headers")) return "header";
|
|
381
|
+
if (path.includes("parameters")) return "parameter";
|
|
382
|
+
if (path.includes("content")) return fallback ?? "response-content";
|
|
383
|
+
return fallback ?? "media-type";
|
|
384
|
+
}
|
|
385
|
+
function makeSchemaReplacements(value, adapter, options) {
|
|
386
|
+
return new Map(makeEntries(value, adapter, /* @__PURE__ */ new Set(), [], options));
|
|
323
387
|
}
|
|
324
|
-
function makeEntries(v, adapter, seen,
|
|
388
|
+
function makeEntries(v, adapter, seen, path, options) {
|
|
325
389
|
if (v === null || v === void 0 || typeof v !== "object") return [];
|
|
326
390
|
if (seen.has(v)) return [];
|
|
327
|
-
const wrap = adapter.wrapSchema
|
|
328
|
-
if (
|
|
391
|
+
const wrap = (expr) => adapter.wrapSchema ? adapter.wrapSchema(expr, slotAt(path, options?.slot)) : expr;
|
|
392
|
+
if (path.at(-1) === "schema") {
|
|
329
393
|
if (isInlineSchema(v)) return [[v, wrap(adapter.toExpression(v))]];
|
|
330
394
|
if (isRecord(v) && typeof v.$ref === "string") {
|
|
331
|
-
const varName = makeSchemaVarName(v.$ref);
|
|
395
|
+
const varName = makeSchemaVarName(v.$ref, options?.identifiers);
|
|
332
396
|
if (varName) return [[v, wrap(varName)]];
|
|
333
397
|
}
|
|
334
398
|
}
|
|
335
399
|
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)) : [];
|
|
400
|
+
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
401
|
seen.delete(v);
|
|
338
402
|
return entries;
|
|
339
403
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
404
|
+
// src/generator/components/declaration.ts
|
|
405
|
+
/**
|
|
406
|
+
* Join declaration bodies the way `make<Kind>Code` already does: `;` between
|
|
407
|
+
* entries, no `import ` line. `trailingNewline` matches examples / links /
|
|
408
|
+
* security-schemes / parameters / headers.
|
|
409
|
+
*/
|
|
410
|
+
function joinDeclarations(declarations, options) {
|
|
411
|
+
if (declarations.length === 0) return "";
|
|
412
|
+
const body = declarations.map((d) => d.code).join(";");
|
|
413
|
+
const text = options?.trailingNewline === true ? `${body}\n` : body;
|
|
414
|
+
return options?.importLine === void 0 ? text : `${options.importLine}\n\n${text}`;
|
|
415
|
+
}
|
|
416
|
+
// src/generator/components/callbacks.ts
|
|
417
|
+
function makeCallbacksDeclarations(components, adapter, options) {
|
|
343
418
|
const { callbacks } = components;
|
|
344
|
-
if (!callbacks) return
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
419
|
+
if (!callbacks) return [];
|
|
420
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
421
|
+
return Object.entries(callbacks).flatMap(([name, callback]) => {
|
|
422
|
+
if ("$ref" in callback && callback.$ref) return [];
|
|
423
|
+
const ident = toIdentifierPascalCase(name);
|
|
424
|
+
const varName = `${ident}Callback`;
|
|
425
|
+
const replacements = makeSchemaReplacements(callback, adapter, {
|
|
426
|
+
slot: "response-content",
|
|
427
|
+
...options?.identifiers && { identifiers: options.identifiers }
|
|
428
|
+
});
|
|
429
|
+
return [{
|
|
430
|
+
name,
|
|
431
|
+
varName,
|
|
432
|
+
fileName: declarationFileName(ident),
|
|
433
|
+
code: `export const ${varName}=${valueToCode(callback, replacements, options?.identifiers)}${asConst}`
|
|
434
|
+
}];
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
function makeCallbacksCode(components, adapter, readonly) {
|
|
438
|
+
return joinDeclarations(makeCallbacksDeclarations(components, adapter, { readonly }));
|
|
439
|
+
}
|
|
440
|
+
// src/generator/components/examples.ts
|
|
441
|
+
function makeExamplesDeclarations(components, options) {
|
|
357
442
|
const { examples } = components;
|
|
358
|
-
if (!examples) return
|
|
359
|
-
const
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
return
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
443
|
+
if (!examples) return [];
|
|
444
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
445
|
+
return Object.entries(examples).map(([name, example]) => {
|
|
446
|
+
const ident = toIdentifierPascalCase(name);
|
|
447
|
+
const varName = `${ident}Example`;
|
|
448
|
+
return {
|
|
449
|
+
name,
|
|
450
|
+
varName,
|
|
451
|
+
fileName: declarationFileName(ident),
|
|
452
|
+
code: `export const ${varName}=${valueToCode(example)}${asConst}`
|
|
453
|
+
};
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
function makeExamplesCode(components, readonly) {
|
|
457
|
+
return joinDeclarations(makeExamplesDeclarations(components, { readonly }), { trailingNewline: true });
|
|
458
|
+
}
|
|
459
|
+
// src/generator/components/headers.ts
|
|
460
|
+
function makeHeadersDeclarations(components, adapter, options) {
|
|
369
461
|
const { headers } = components;
|
|
370
|
-
if (!headers) return
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
462
|
+
if (!headers) return [];
|
|
463
|
+
return Object.entries(headers).flatMap(([name, header]) => {
|
|
464
|
+
if (!("schema" in header) || !header.schema) return [];
|
|
465
|
+
const ident = toIdentifierPascalCase(name);
|
|
466
|
+
const varName = `${ident}HeaderSchema`;
|
|
467
|
+
const raw = adapter.toExpression(header.schema);
|
|
468
|
+
const expr = adapter.wrapSchema ? adapter.wrapSchema(raw, "header") : raw;
|
|
469
|
+
const typeExport = options?.exportTypes === true ? `\n\n${adapter.renderTypeInfer(varName)}` : "";
|
|
470
|
+
return [{
|
|
471
|
+
name,
|
|
472
|
+
varName,
|
|
473
|
+
fileName: declarationFileName(ident),
|
|
474
|
+
code: `export const ${varName}=${expr}${typeExport}`
|
|
475
|
+
}];
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
function makeHeadersCode(components, adapter, exportTypes) {
|
|
479
|
+
return joinDeclarations(makeHeadersDeclarations(components, adapter, exportTypes === true ? { exportTypes: true } : void 0), {
|
|
480
|
+
trailingNewline: true,
|
|
481
|
+
importLine: adapter.renderImport()
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
// src/generator/components/links.ts
|
|
485
|
+
function makeLinksDeclarations(components, options) {
|
|
384
486
|
const { links } = components;
|
|
385
|
-
if (!links) return
|
|
386
|
-
const
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
return
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
487
|
+
if (!links) return [];
|
|
488
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
489
|
+
return Object.entries(links).map(([name, link]) => {
|
|
490
|
+
const ident = toIdentifierPascalCase(name);
|
|
491
|
+
const varName = `${ident}Link`;
|
|
492
|
+
return {
|
|
493
|
+
name,
|
|
494
|
+
varName,
|
|
495
|
+
fileName: declarationFileName(ident),
|
|
496
|
+
code: `export const ${varName}=${valueToCode(link)}${asConst}`
|
|
497
|
+
};
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
function makeLinksCode(components, readonly) {
|
|
501
|
+
return joinDeclarations(makeLinksDeclarations(components, { readonly }), { trailingNewline: true });
|
|
502
|
+
}
|
|
503
|
+
// src/generator/components/media-types.ts
|
|
504
|
+
function makeMediaTypesDeclarations(components, adapter, options) {
|
|
505
|
+
const { mediaTypes } = components;
|
|
506
|
+
if (!mediaTypes) return [];
|
|
507
|
+
return Object.entries(mediaTypes).flatMap(([name, media]) => {
|
|
508
|
+
if ("$ref" in media && media.$ref) return [];
|
|
509
|
+
if (!("schema" in media) || !media.schema) return [];
|
|
510
|
+
const ident = toIdentifierPascalCase(name);
|
|
511
|
+
const varName = `${ident}MediaTypeSchema`;
|
|
512
|
+
const raw = adapter.toExpression(media.schema);
|
|
513
|
+
const expr = adapter.wrapSchema ? adapter.wrapSchema(raw, "media-type") : raw;
|
|
514
|
+
const typeExport = options?.exportTypes === true ? `\n\n${adapter.renderTypeInfer(varName)}` : "";
|
|
515
|
+
return [{
|
|
516
|
+
name,
|
|
517
|
+
varName,
|
|
518
|
+
fileName: declarationFileName(ident),
|
|
519
|
+
code: `export const ${varName}=${expr}${typeExport}`
|
|
520
|
+
}];
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
function makeMediaTypesCode(components, adapter, exportTypes) {
|
|
524
|
+
return joinDeclarations(makeMediaTypesDeclarations(components, adapter, exportTypes === true ? { exportTypes: true } : void 0), {
|
|
525
|
+
trailingNewline: true,
|
|
526
|
+
importLine: adapter.renderImport()
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
// src/generator/components/parameters.ts
|
|
530
|
+
function makeParametersDeclarations(components, adapter, options) {
|
|
396
531
|
const { parameters } = components;
|
|
397
|
-
if (!parameters) return
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
532
|
+
if (!parameters) return [];
|
|
533
|
+
return Object.entries(parameters).flatMap(([name, param]) => {
|
|
534
|
+
if (!param.schema) return [];
|
|
535
|
+
const ident = toIdentifierPascalCase(name);
|
|
536
|
+
const varName = `${ident}ParamsSchema`;
|
|
537
|
+
const raw = adapter.toExpression(param.schema, param.in, {
|
|
538
|
+
...param.style !== void 0 && isParamStyle(param.style) && { style: param.style },
|
|
539
|
+
...param.explode !== void 0 && { explode: param.explode },
|
|
540
|
+
...param.allowEmptyValue !== void 0 && { allowEmptyValue: param.allowEmptyValue }
|
|
541
|
+
});
|
|
542
|
+
const expr = adapter.wrapSchema ? adapter.wrapSchema(raw, "parameter") : raw;
|
|
543
|
+
const typeExport = options?.exportTypes === true ? `\n\n${adapter.renderTypeInfer(varName)}` : "";
|
|
544
|
+
return [{
|
|
545
|
+
name,
|
|
546
|
+
varName,
|
|
547
|
+
fileName: declarationFileName(ident),
|
|
548
|
+
code: `export const ${varName}=${expr}${typeExport}`
|
|
549
|
+
}];
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
function makeParametersCode(components, adapter, exportTypes) {
|
|
553
|
+
return joinDeclarations(makeParametersDeclarations(components, adapter, exportTypes === true ? { exportTypes: true } : void 0), {
|
|
554
|
+
trailingNewline: true,
|
|
555
|
+
importLine: adapter.renderImport()
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
// src/generator/components/path-items.ts
|
|
559
|
+
function makePathItemsDeclarations(components, adapter, options) {
|
|
412
560
|
const { pathItems } = components;
|
|
413
|
-
if (!pathItems) return
|
|
414
|
-
const
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
561
|
+
if (!pathItems) return [];
|
|
562
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
563
|
+
return Object.entries(pathItems).map(([name, pathItem]) => {
|
|
564
|
+
const ident = toIdentifierPascalCase(name);
|
|
565
|
+
const varName = `${ident}PathItem`;
|
|
566
|
+
const replacements = makeSchemaReplacements(pathItem, adapter, {
|
|
567
|
+
slot: "response-content",
|
|
568
|
+
...options?.identifiers && { identifiers: options.identifiers }
|
|
569
|
+
});
|
|
570
|
+
return {
|
|
571
|
+
name,
|
|
572
|
+
varName,
|
|
573
|
+
fileName: declarationFileName(ident),
|
|
574
|
+
code: `export const ${varName}=${valueToCode(pathItem, replacements, options?.identifiers)}${asConst}`
|
|
575
|
+
};
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
function makePathItemsCode(components, adapter, readonly) {
|
|
579
|
+
return joinDeclarations(makePathItemsDeclarations(components, adapter, { readonly }));
|
|
580
|
+
}
|
|
581
|
+
// src/generator/components/request-bodies.ts
|
|
582
|
+
function makeRequestBodiesDeclarations(components, adapter, options) {
|
|
426
583
|
const { requestBodies } = components;
|
|
427
|
-
if (!requestBodies) return
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
584
|
+
if (!requestBodies) return [];
|
|
585
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
586
|
+
return Object.entries(requestBodies).map(([name, body]) => {
|
|
587
|
+
const ident = toIdentifierPascalCase(name);
|
|
588
|
+
const varName = `${ident}RequestBody`;
|
|
589
|
+
const replacements = makeSchemaReplacements(body, adapter, {
|
|
590
|
+
slot: "request-content",
|
|
591
|
+
...options?.identifiers && { identifiers: options.identifiers }
|
|
592
|
+
});
|
|
593
|
+
return {
|
|
594
|
+
name,
|
|
595
|
+
varName,
|
|
596
|
+
fileName: declarationFileName(ident),
|
|
597
|
+
code: `export const ${varName}=${valueToCode(body, replacements, options?.identifiers)}${asConst}`
|
|
598
|
+
};
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
function makeRequestBodiesCode(components, adapter, readonly) {
|
|
602
|
+
return joinDeclarations(makeRequestBodiesDeclarations(components, adapter, { readonly }));
|
|
603
|
+
}
|
|
604
|
+
// src/generator/components/responses.ts
|
|
605
|
+
function makeResponsesDeclarations(components, adapter, options) {
|
|
440
606
|
const { responses } = components;
|
|
441
|
-
if (!responses) return
|
|
442
|
-
const
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
|
|
607
|
+
if (!responses) return [];
|
|
608
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
609
|
+
return Object.entries(responses).flatMap(([name, response]) => {
|
|
610
|
+
if ("$ref" in response && response.$ref) return [];
|
|
611
|
+
const ident = toIdentifierPascalCase(name);
|
|
612
|
+
const varName = `${ident}Response`;
|
|
613
|
+
const replacements = makeSchemaReplacements(response, adapter, {
|
|
614
|
+
slot: "response-content",
|
|
615
|
+
...options?.identifiers && { identifiers: options.identifiers }
|
|
616
|
+
});
|
|
617
|
+
return [{
|
|
618
|
+
name,
|
|
619
|
+
varName,
|
|
620
|
+
fileName: declarationFileName(ident),
|
|
621
|
+
code: `export const ${varName}=${valueToCode(response, replacements, options?.identifiers)}${asConst}`
|
|
622
|
+
}];
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
function makeResponsesCode(components, adapter, readonly) {
|
|
626
|
+
return joinDeclarations(makeResponsesDeclarations(components, adapter, { readonly }));
|
|
627
|
+
}
|
|
628
|
+
// src/helper/graph.ts
|
|
453
629
|
/** Every `components.schemas` name a schema reaches through `$ref`, at any depth. */
|
|
454
630
|
function collectSchemaRefs(node) {
|
|
455
631
|
if (Array.isArray(node)) return node.flatMap(collectSchemaRefs);
|
|
@@ -543,8 +719,7 @@ function makeCyclicType(name, typeName, schema, infer, readonly, style = {}) {
|
|
|
543
719
|
typeName
|
|
544
720
|
}, infer, readonly, style)}`;
|
|
545
721
|
}
|
|
546
|
-
|
|
547
|
-
//#region src/helper/identifiers.ts
|
|
722
|
+
// src/helper/identifiers.ts
|
|
548
723
|
/**
|
|
549
724
|
* Identifier rewriting on generated source without a parser: skip quoted
|
|
550
725
|
* strings so identifier-shaped text inside descriptions and `Type.Ref('X')`
|
|
@@ -604,21 +779,7 @@ function wrapReferences(expr, names, wrap) {
|
|
|
604
779
|
}
|
|
605
780
|
return result;
|
|
606
781
|
}
|
|
607
|
-
|
|
608
|
-
//#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
|
-
}
|
|
782
|
+
// src/generator/components/schemas.ts
|
|
622
783
|
/** Keys whose value is a subschema (or a list of them) and keys whose value maps names to subschemas. */
|
|
623
784
|
const SUBSCHEMA_KEYS = /* @__PURE__ */ new Set([
|
|
624
785
|
"items",
|
|
@@ -674,19 +835,33 @@ function markReadonly(schema) {
|
|
|
674
835
|
* becomes one `Type.Cyclic(...)` / `scope(...)` container and each member selects
|
|
675
836
|
* its entry. schema-to-library builds the container from a `$defs` document.
|
|
676
837
|
*/
|
|
677
|
-
function
|
|
838
|
+
function rewriteCollisionRef(node, identifiers) {
|
|
839
|
+
if (typeof node.$ref !== "string" || !node.$ref.startsWith("#/components/schemas/")) return node;
|
|
840
|
+
const key = schemaRefToName(node.$ref);
|
|
841
|
+
const ident = identifiers.get(key);
|
|
842
|
+
if (ident === void 0 || ident === toIdentifierPascalCase(key)) return node;
|
|
843
|
+
return {
|
|
844
|
+
...node,
|
|
845
|
+
$ref: `#/components/schemas/${ident}`
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
function makeCyclicContainer(varName, group, schemas, identifiers, adapter, ref) {
|
|
678
849
|
const groupIdentifiers = new Map(group.map((name) => [name, identifiers.get(name) ?? name]));
|
|
679
850
|
const localize = (node) => {
|
|
680
|
-
const
|
|
681
|
-
if (
|
|
682
|
-
const
|
|
683
|
-
|
|
851
|
+
const key = node.$ref ? schemaRefToName(node.$ref) : void 0;
|
|
852
|
+
if (key === void 0) return node;
|
|
853
|
+
const groupIdent = groupIdentifiers.get(key);
|
|
854
|
+
if (groupIdent !== void 0) {
|
|
855
|
+
const local = Object.fromEntries([...Object.entries(node), ["$ref", `#/$defs/${groupIdent}Schema`]]);
|
|
856
|
+
return isSchema(local) ? local : node;
|
|
857
|
+
}
|
|
858
|
+
return rewriteCollisionRef(node, identifiers);
|
|
684
859
|
};
|
|
685
860
|
const $defs = Object.fromEntries(group.map((name) => [`${groupIdentifiers.get(name)}Schema`, mapSchema(schemas[name] ?? {}, localize)]));
|
|
686
861
|
return adapter.toExpression({
|
|
687
862
|
title: varName,
|
|
688
863
|
$defs
|
|
689
|
-
});
|
|
864
|
+
}, void 0, ref === void 0 ? void 0 : { ref });
|
|
690
865
|
}
|
|
691
866
|
/**
|
|
692
867
|
* The import line a schemas file needs. `cyclic` is true when this file (or
|
|
@@ -706,9 +881,6 @@ function exportedTypeName(ident, varName, adapter, options) {
|
|
|
706
881
|
if (options?.typeAlias !== "key") return varName;
|
|
707
882
|
return (adapter.reservedTypeNames ?? []).includes(ident) ? `${ident}Type` : ident;
|
|
708
883
|
}
|
|
709
|
-
function fileNameOf(ident) {
|
|
710
|
-
return `${ident.charAt(0).toLowerCase()}${ident.slice(1)}`;
|
|
711
|
-
}
|
|
712
884
|
/**
|
|
713
885
|
* One `export const <X>Schema=...` declaration per `components.schemas` entry,
|
|
714
886
|
* in dependency-first order. Only references inside a `$ref` cycle are lazy
|
|
@@ -726,21 +898,23 @@ function makeSchemaDeclarations(schemas, adapter, options) {
|
|
|
726
898
|
]);
|
|
727
899
|
const infer = (varName) => adapter.renderTypeInfer(varName).replace(`export type ${varName}=`, "");
|
|
728
900
|
const style = options?.cyclicTypeStyle === "literal" ? {} : adapter.cyclicTypeStyle ?? {};
|
|
901
|
+
const declarationRef = options?.ref === true;
|
|
729
902
|
return order.map((name) => {
|
|
730
|
-
const schema = prepared[name] ?? {};
|
|
903
|
+
const schema = mapSchema(prepared[name] ?? {}, (node) => rewriteCollisionRef(node, identifiers));
|
|
731
904
|
const ident = identifiers.get(name) ?? toIdentifierPascalCase(name);
|
|
732
905
|
const varName = `${ident}Schema`;
|
|
733
906
|
const group = cycles.get(name);
|
|
734
907
|
const arktypeReadonly = adapter.renderCyclic !== void 0 && options?.readonly === true;
|
|
735
|
-
const
|
|
908
|
+
const hostRef = declarationRef ? name : void 0;
|
|
909
|
+
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
910
|
const peers = group ?? [];
|
|
737
911
|
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
912
|
const expression = (rendered && arktypeReadonly ? `${rendered}.readonly()` : rendered) ?? adapter.toExpression({
|
|
739
913
|
...schema,
|
|
740
914
|
title: varName
|
|
741
|
-
});
|
|
915
|
+
}, void 0, adapter.withRef === void 0 && hostRef !== void 0 ? { ref: hostRef } : void 0);
|
|
742
916
|
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;
|
|
917
|
+
const value = options?.wrapDeclaration ? options.wrapDeclaration(body, name) : hostRef !== void 0 && adapter.withRef ? adapter.withRef(body, name) : body;
|
|
744
918
|
const helper = group && adapter.cyclicAnnotation ? claimName(`${ident}Type`, taken) : void 0;
|
|
745
919
|
const annotationText = helper ? adapter.cyclicAnnotation?.(helper) : void 0;
|
|
746
920
|
const usesHelper = helper !== void 0 && annotationText?.includes(helper) === true;
|
|
@@ -752,7 +926,7 @@ function makeSchemaDeclarations(schemas, adapter, options) {
|
|
|
752
926
|
return {
|
|
753
927
|
name,
|
|
754
928
|
varName,
|
|
755
|
-
fileName:
|
|
929
|
+
fileName: declarationFileName(ident),
|
|
756
930
|
importLine: schemaImportLine(adapter, options, containerCycle),
|
|
757
931
|
code: `${typeDef}export const ${varName}${annotation}=${value}${typeExport}`
|
|
758
932
|
};
|
|
@@ -769,17 +943,23 @@ function makeSchemasCode(components, adapter, options) {
|
|
|
769
943
|
if (declarations.length === 0) return "";
|
|
770
944
|
return `${schemaImportLine(adapter, options, declarations.some((d) => d.importLine !== schemaImportLine(adapter, options)))}\n\n${declarations.map((d) => d.code).join(";")}\n`;
|
|
771
945
|
}
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
function makeSecuritySchemesCode(components, readonly) {
|
|
946
|
+
// src/generator/components/security-schemes.ts
|
|
947
|
+
function makeSecuritySchemesDeclarations(components, options) {
|
|
775
948
|
const { securitySchemes } = components;
|
|
776
|
-
if (!securitySchemes) return
|
|
777
|
-
const
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
return
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
export {
|
|
949
|
+
if (!securitySchemes) return [];
|
|
950
|
+
const asConst = options?.readonly === true ? " as const" : "";
|
|
951
|
+
return Object.entries(securitySchemes).map(([name, scheme]) => {
|
|
952
|
+
const ident = toIdentifierPascalCase(name);
|
|
953
|
+
const varName = `${ident}SecurityScheme`;
|
|
954
|
+
return {
|
|
955
|
+
name,
|
|
956
|
+
varName,
|
|
957
|
+
fileName: declarationFileName(ident),
|
|
958
|
+
code: `export const ${varName}=${valueToCode(scheme)}${asConst}`
|
|
959
|
+
};
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
function makeSecuritySchemesCode(components, readonly) {
|
|
963
|
+
return joinDeclarations(makeSecuritySchemesDeclarations(components, { readonly }), { trailingNewline: true });
|
|
964
|
+
}
|
|
965
|
+
export { declarationFileName, isParamStyle, 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.5.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,23 +28,23 @@
|
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"schema-to-library": "^0.
|
|
31
|
+
"schema-to-library": "^0.7.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@apidevtools/swagger-parser": "^13.
|
|
35
|
-
"@types/node": "^26.5
|
|
36
|
-
"@typespec/compiler": "1.
|
|
37
|
-
"@typespec/http": "1.
|
|
38
|
-
"@typespec/openapi": "1.
|
|
39
|
-
"@typespec/openapi3": "1.
|
|
40
|
-
"@typespec/rest": "0.
|
|
41
|
-
"@typespec/versioning": "0.
|
|
42
|
-
"arktype": "^2.2.
|
|
43
|
-
"effect": "4.0.
|
|
44
|
-
"typebox": "^1.3
|
|
34
|
+
"@apidevtools/swagger-parser": "^13.1.0",
|
|
35
|
+
"@types/node": "^26.6.5",
|
|
36
|
+
"@typespec/compiler": "1.17.0",
|
|
37
|
+
"@typespec/http": "1.17.0",
|
|
38
|
+
"@typespec/openapi": "1.17.0",
|
|
39
|
+
"@typespec/openapi3": "1.17.0",
|
|
40
|
+
"@typespec/rest": "0.87.0",
|
|
41
|
+
"@typespec/versioning": "0.87.0",
|
|
42
|
+
"arktype": "^2.2.9",
|
|
43
|
+
"effect": "4.0.6",
|
|
44
|
+
"typebox": "^1.4.3",
|
|
45
45
|
"typescript": "^7.0.2",
|
|
46
46
|
"valibot": "^1.5.0",
|
|
47
|
-
"zod": "^4.6.
|
|
47
|
+
"zod": "^4.6.7"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"@apidevtools/swagger-parser": ">=12.0.0 <14.0.0",
|