oas-truth 0.0.1 → 0.1.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/dist/index.d.ts +49 -52
- package/dist/index.js +33 -26
- package/package.json +32 -14
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
import SwaggerParser from "@apidevtools/swagger-parser";
|
|
2
|
-
|
|
3
1
|
//#region src/openapi/index.d.ts
|
|
4
|
-
declare function parseOpenAPI(input: string): Promise<{
|
|
2
|
+
export declare function parseOpenAPI(input: string): Promise<{
|
|
5
3
|
readonly ok: true;
|
|
6
4
|
readonly value: OpenAPI;
|
|
7
5
|
readonly error?: never;
|
|
8
6
|
} | {
|
|
7
|
+
readonly value?: never;
|
|
9
8
|
readonly ok: false;
|
|
10
9
|
readonly error: string;
|
|
11
|
-
readonly value?: never;
|
|
12
10
|
}>;
|
|
13
|
-
type
|
|
14
|
-
type OpenAPI = BaseOpenAPI & {
|
|
11
|
+
export type OpenAPI = {
|
|
15
12
|
readonly openapi?: string;
|
|
16
13
|
readonly $self?: string;
|
|
17
14
|
readonly info?: {
|
|
@@ -33,7 +30,7 @@ type OpenAPI = BaseOpenAPI & {
|
|
|
33
30
|
};
|
|
34
31
|
readonly jsonSchemaDialect?: string;
|
|
35
32
|
readonly servers?: readonly Server[];
|
|
36
|
-
readonly paths:
|
|
33
|
+
readonly paths: OpenAPIPaths;
|
|
37
34
|
readonly webhooks?: {
|
|
38
35
|
readonly [k: string]: PathItem;
|
|
39
36
|
};
|
|
@@ -50,10 +47,8 @@ type OpenAPI = BaseOpenAPI & {
|
|
|
50
47
|
readonly kind?: string;
|
|
51
48
|
}[];
|
|
52
49
|
readonly externalDocs?: ExternalDocs;
|
|
53
|
-
} & {
|
|
54
|
-
paths: OpenAPIPaths;
|
|
55
50
|
};
|
|
56
|
-
type Components = {
|
|
51
|
+
export type Components = {
|
|
57
52
|
readonly schemas?: {
|
|
58
53
|
readonly [k: string]: Schema;
|
|
59
54
|
};
|
|
@@ -121,12 +116,14 @@ type OAuthFlow = {
|
|
|
121
116
|
};
|
|
122
117
|
};
|
|
123
118
|
};
|
|
124
|
-
type OpenAPIPaths = {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
type
|
|
128
|
-
type
|
|
129
|
-
type
|
|
119
|
+
export type OpenAPIPaths = {
|
|
120
|
+
readonly [k: string]: PathItem;
|
|
121
|
+
};
|
|
122
|
+
export type Type = 'string' | 'number' | 'integer' | 'date' | 'boolean' | 'array' | 'object' | 'null';
|
|
123
|
+
export type Format = FormatString | FormatNumber;
|
|
124
|
+
export type FormatString = 'email' | 'uuid' | 'uuidv4' | 'uuidv6' | 'uuidv7' | 'url' | 'uri' | 'httpUrl' | 'hostname' | 'hex' | 'jwt' | 'emoji' | 'base64' | 'base64url' | 'nanoid' | 'cuid' | 'cuid2' | 'ulid' | 'ipv4' | 'ipv6' | 'cidrv4' | 'cidrv6' | 'date' | 'time' | 'date-time' | 'duration' | 'binary' | 'byte' | 'password' | 'mac' | 'hash' | 'e164' | 'guid' | 'toLowerCase' | 'toUpperCase' | 'trim';
|
|
125
|
+
export type FormatNumber = 'int32' | 'int64' | 'bigint' | 'float' | 'float32' | 'float64' | 'double' | 'password';
|
|
126
|
+
export type Ref = `#/components/schemas/${string}` | `#/components/responses/${string}` | `#/components/parameters/${string}` | `#/components/examples/${string}` | `#/components/requestBodies/${string}` | `#/components/headers/${string}` | `#/components/securitySchemes/${string}` | `#/components/links/${string}` | `#/components/callbacks/${string}` | `#/components/pathItems/${string}` | `#/components/mediaTypes/${string}`;
|
|
130
127
|
type Server = {
|
|
131
128
|
readonly url: string;
|
|
132
129
|
readonly description?: string;
|
|
@@ -139,7 +136,7 @@ type Server = {
|
|
|
139
136
|
};
|
|
140
137
|
};
|
|
141
138
|
};
|
|
142
|
-
type Header = {
|
|
139
|
+
export type Header = {
|
|
143
140
|
readonly description?: string;
|
|
144
141
|
readonly required?: boolean;
|
|
145
142
|
readonly deprecated?: boolean;
|
|
@@ -160,7 +157,7 @@ type Header = {
|
|
|
160
157
|
schema?: Schema;
|
|
161
158
|
content?: Content;
|
|
162
159
|
};
|
|
163
|
-
type Link = {
|
|
160
|
+
export type Link = {
|
|
164
161
|
readonly operationRef?: string;
|
|
165
162
|
readonly operationId?: string;
|
|
166
163
|
readonly parameters?: {
|
|
@@ -170,12 +167,12 @@ type Link = {
|
|
|
170
167
|
readonly description?: string;
|
|
171
168
|
readonly server?: Server;
|
|
172
169
|
};
|
|
173
|
-
type Reference = {
|
|
170
|
+
export type Reference = {
|
|
174
171
|
readonly $ref?: Ref;
|
|
175
172
|
readonly summary?: string;
|
|
176
173
|
readonly description?: string;
|
|
177
174
|
};
|
|
178
|
-
type Encoding = {
|
|
175
|
+
export type Encoding = {
|
|
179
176
|
readonly contentType?: string;
|
|
180
177
|
readonly headers?: {
|
|
181
178
|
readonly [k: string]: Header | Reference;
|
|
@@ -186,10 +183,10 @@ type Encoding = {
|
|
|
186
183
|
readonly prefixEncoding?: Encoding;
|
|
187
184
|
readonly itemEncoding?: Encoding;
|
|
188
185
|
};
|
|
189
|
-
type Content = {
|
|
186
|
+
export type Content = {
|
|
190
187
|
readonly [k: string]: Media;
|
|
191
188
|
};
|
|
192
|
-
type PathItem = {
|
|
189
|
+
export type PathItem = {
|
|
193
190
|
readonly $ref?: Ref;
|
|
194
191
|
readonly summary?: string;
|
|
195
192
|
readonly description?: string;
|
|
@@ -208,7 +205,7 @@ type PathItem = {
|
|
|
208
205
|
readonly servers?: readonly Server[];
|
|
209
206
|
readonly parameters?: readonly (Parameter | Reference)[];
|
|
210
207
|
};
|
|
211
|
-
type Operation = {
|
|
208
|
+
export type Operation = {
|
|
212
209
|
readonly tags?: readonly string[];
|
|
213
210
|
readonly summary?: string;
|
|
214
211
|
readonly description?: string;
|
|
@@ -246,7 +243,7 @@ type Operation = {
|
|
|
246
243
|
}[];
|
|
247
244
|
readonly 'x-pagination'?: boolean;
|
|
248
245
|
};
|
|
249
|
-
type Responses = {
|
|
246
|
+
export type Responses = {
|
|
250
247
|
readonly $ref?: Ref;
|
|
251
248
|
readonly summary?: string;
|
|
252
249
|
readonly description?: string;
|
|
@@ -269,7 +266,7 @@ type ExternalDocs = {
|
|
|
269
266
|
readonly url: string;
|
|
270
267
|
readonly description?: string;
|
|
271
268
|
};
|
|
272
|
-
type Schema = {
|
|
269
|
+
export type Schema = {
|
|
273
270
|
readonly discriminator?: Discriminator;
|
|
274
271
|
readonly xml?: {
|
|
275
272
|
readonly nodeType?: string;
|
|
@@ -320,7 +317,8 @@ type Schema = {
|
|
|
320
317
|
* `false` = no trailing items / length cap). Boolean form is exercised when
|
|
321
318
|
* paired with `prefixItems` to enforce a strict tuple shape.
|
|
322
319
|
*/
|
|
323
|
-
readonly items?: Schema | readonly Schema[] | boolean;
|
|
320
|
+
readonly items?: Schema | readonly Schema[] | boolean;
|
|
321
|
+
/** JSON Schema 2020-12: Tuple validation */
|
|
324
322
|
readonly prefixItems?: readonly Schema[];
|
|
325
323
|
readonly enum?: readonly (string | number | boolean | null | readonly (string | number | boolean | null)[])[];
|
|
326
324
|
readonly nullable?: boolean;
|
|
@@ -445,7 +443,7 @@ type Schema = {
|
|
|
445
443
|
readonly 'x-transform'?: string;
|
|
446
444
|
readonly 'x-pipe'?: string;
|
|
447
445
|
};
|
|
448
|
-
type Parameter = {
|
|
446
|
+
export type Parameter = {
|
|
449
447
|
readonly $ref?: Ref;
|
|
450
448
|
readonly name: string;
|
|
451
449
|
readonly in: 'path' | 'query' | 'header' | 'cookie';
|
|
@@ -470,14 +468,14 @@ type Parameter = {
|
|
|
470
468
|
} | Reference;
|
|
471
469
|
};
|
|
472
470
|
};
|
|
473
|
-
type RequestBody = {
|
|
471
|
+
export type RequestBody = {
|
|
474
472
|
readonly description?: string;
|
|
475
473
|
readonly content?: {
|
|
476
474
|
readonly [k: string]: Media | Reference;
|
|
477
475
|
};
|
|
478
476
|
readonly required?: boolean;
|
|
479
477
|
};
|
|
480
|
-
type Media = {
|
|
478
|
+
export type Media = {
|
|
481
479
|
readonly schema: Schema;
|
|
482
480
|
readonly itemSchema?: Schema;
|
|
483
481
|
readonly example?: unknown;
|
|
@@ -497,13 +495,13 @@ type Media = {
|
|
|
497
495
|
readonly prefixEncoding?: Encoding;
|
|
498
496
|
readonly itemEncoding?: Encoding;
|
|
499
497
|
};
|
|
500
|
-
type Callbacks = {
|
|
498
|
+
export type Callbacks = {
|
|
501
499
|
readonly [k: string]: PathItem;
|
|
502
500
|
};
|
|
503
501
|
//#endregion
|
|
504
502
|
//#region src/adapter/index.d.ts
|
|
505
|
-
type ParamIn = 'query' | 'path';
|
|
506
|
-
type SchemaLib = 'zod' | 'valibot' | 'arktype' | 'effect' | 'typebox';
|
|
503
|
+
export type ParamIn = 'query' | 'path';
|
|
504
|
+
export type SchemaLib = 'zod' | 'valibot' | 'arktype' | 'effect' | 'typebox';
|
|
507
505
|
/**
|
|
508
506
|
* The minimal surface the component generators consume from a validator
|
|
509
507
|
* adapter. Each library's full adapter (carrying `toNamedExport`,
|
|
@@ -517,71 +515,70 @@ type SchemaLib = 'zod' | 'valibot' | 'arktype' | 'effect' | 'typebox';
|
|
|
517
515
|
* omitting it leaves the expression untouched (identity), so the shared library
|
|
518
516
|
* stays framework-agnostic.
|
|
519
517
|
*/
|
|
520
|
-
type ComponentAdapter = {
|
|
518
|
+
export type ComponentAdapter = {
|
|
521
519
|
readonly renderImport: () => string;
|
|
522
520
|
readonly toExpression: (schema: Schema, paramIn?: ParamIn) => string;
|
|
523
521
|
readonly renderTypeInfer: (constName: string) => string;
|
|
524
522
|
readonly wrapSchema?: (expr: string) => string;
|
|
525
523
|
};
|
|
526
|
-
declare function makeAdapter(lib: SchemaLib): ComponentAdapter;
|
|
524
|
+
export declare function makeAdapter(lib: SchemaLib): ComponentAdapter;
|
|
527
525
|
//#endregion
|
|
528
526
|
//#region src/helper/ref-resolver.d.ts
|
|
529
|
-
declare function valueToCode(value: unknown, codeReplacements?: ReadonlyMap<unknown, string>): string;
|
|
527
|
+
export declare function valueToCode(value: unknown, codeReplacements?: ReadonlyMap<unknown, string>): string;
|
|
530
528
|
//#endregion
|
|
531
529
|
//#region src/helper/schema-replacements.d.ts
|
|
532
|
-
declare function makeSchemaReplacements(value: unknown, adapter: ComponentAdapter): ReadonlyMap<unknown, string>;
|
|
530
|
+
export declare function makeSchemaReplacements(value: unknown, adapter: ComponentAdapter): ReadonlyMap<unknown, string>;
|
|
533
531
|
//#endregion
|
|
534
532
|
//#region src/utils/index.d.ts
|
|
535
|
-
declare function toPascalCase(str: string): string;
|
|
533
|
+
export declare function toPascalCase(str: string): string;
|
|
536
534
|
/**
|
|
537
535
|
* Extract the component name from a `$ref`, decoding percent-encoding so the
|
|
538
536
|
* result matches the (decoded) key under `components.schemas`. Pure-ASCII refs
|
|
539
537
|
* are returned unchanged. Used for component lookup, not variable naming.
|
|
540
538
|
*/
|
|
541
|
-
declare function schemaRefToName(ref: string): string;
|
|
539
|
+
export declare function schemaRefToName(ref: string): string;
|
|
542
540
|
/**
|
|
543
541
|
* Convert a name to a valid PascalCase TypeScript identifier. Mirrors
|
|
544
542
|
* schema-to-library's `toIdentifierPascalCase` so reference identifiers match
|
|
545
543
|
* the component declaration names it emits. Non-ASCII names are encoded
|
|
546
544
|
* injectively so distinct names never collide.
|
|
547
545
|
*/
|
|
548
|
-
declare function toIdentifierPascalCase(name: string): string;
|
|
546
|
+
export declare function toIdentifierPascalCase(name: string): string;
|
|
549
547
|
/**
|
|
550
548
|
* Encode a property key for object-literal output: bare identifiers stay
|
|
551
549
|
* unquoted, everything else is JSON-encoded so hyphenated/reserved/numeric
|
|
552
550
|
* keys (e.g. `X-Request-ID`) emit as valid TypeScript.
|
|
553
551
|
*/
|
|
554
|
-
declare function makeSafeKey(key: string): string;
|
|
552
|
+
export declare function makeSafeKey(key: string): string;
|
|
555
553
|
/** Narrow an unknown value to a plain object (excludes arrays and null). */
|
|
556
|
-
declare function isRecord(v: unknown): v is {
|
|
554
|
+
export declare function isRecord(v: unknown): v is {
|
|
557
555
|
readonly [k: string]: unknown;
|
|
558
556
|
};
|
|
559
557
|
//#endregion
|
|
560
558
|
//#region src/generator/components/callbacks.d.ts
|
|
561
|
-
declare function makeCallbacksCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
559
|
+
export declare function makeCallbacksCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
562
560
|
//#endregion
|
|
563
561
|
//#region src/generator/components/examples.d.ts
|
|
564
|
-
declare function makeExamplesCode(components: Components, readonly: boolean): string;
|
|
562
|
+
export declare function makeExamplesCode(components: Components, readonly: boolean): string;
|
|
565
563
|
//#endregion
|
|
566
564
|
//#region src/generator/components/headers.d.ts
|
|
567
|
-
declare function makeHeadersCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
565
|
+
export declare function makeHeadersCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
568
566
|
//#endregion
|
|
569
567
|
//#region src/generator/components/links.d.ts
|
|
570
|
-
declare function makeLinksCode(components: Components, readonly: boolean): string;
|
|
568
|
+
export declare function makeLinksCode(components: Components, readonly: boolean): string;
|
|
571
569
|
//#endregion
|
|
572
570
|
//#region src/generator/components/parameters.d.ts
|
|
573
|
-
declare function makeParametersCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
571
|
+
export declare function makeParametersCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
574
572
|
//#endregion
|
|
575
573
|
//#region src/generator/components/path-items.d.ts
|
|
576
|
-
declare function makePathItemsCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
574
|
+
export declare function makePathItemsCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
577
575
|
//#endregion
|
|
578
576
|
//#region src/generator/components/request-bodies.d.ts
|
|
579
|
-
declare function makeRequestBodiesCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
577
|
+
export declare function makeRequestBodiesCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
580
578
|
//#endregion
|
|
581
579
|
//#region src/generator/components/responses.d.ts
|
|
582
|
-
declare function makeResponsesCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
580
|
+
export declare function makeResponsesCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
583
581
|
//#endregion
|
|
584
582
|
//#region src/generator/components/security-schemes.d.ts
|
|
585
|
-
declare function makeSecuritySchemesCode(components: Components, readonly: boolean): string;
|
|
586
|
-
//#endregion
|
|
587
|
-
export { Callbacks, ComponentAdapter, Components, Content, Encoding, Format, FormatNumber, FormatString, Header, Link, Media, OpenAPI, OpenAPIPaths, Operation, ParamIn, Parameter, PathItem, Ref, Reference, RequestBody, Responses, Schema, SchemaLib, Type, isRecord, makeAdapter, makeCallbacksCode, makeExamplesCode, makeHeadersCode, makeLinksCode, makeParametersCode, makePathItemsCode, makeRequestBodiesCode, makeResponsesCode, makeSafeKey, makeSchemaReplacements, makeSecuritySchemesCode, parseOpenAPI, schemaRefToName, toIdentifierPascalCase, toPascalCase, valueToCode };
|
|
583
|
+
export declare function makeSecuritySchemesCode(components: Components, readonly: boolean): string;
|
|
584
|
+
//#endregion
|
package/dist/index.js
CHANGED
|
@@ -12,7 +12,7 @@ async function parseOpenAPI(input) {
|
|
|
12
12
|
try {
|
|
13
13
|
if (typeof input === "string" && input.endsWith(".tsp")) {
|
|
14
14
|
const program = await compile(NodeHost, path.resolve(input), { noEmit: true });
|
|
15
|
-
if (program.diagnostics.length) return {
|
|
15
|
+
if (program.diagnostics.length > 0) return {
|
|
16
16
|
ok: false,
|
|
17
17
|
error: `TypeSpec compile failed:\n${program.diagnostics.map((d) => d.message).join("\n")}`
|
|
18
18
|
};
|
|
@@ -20,17 +20,17 @@ async function parseOpenAPI(input) {
|
|
|
20
20
|
const tsp = "document" in record ? record.document : record.versions[0].document;
|
|
21
21
|
return {
|
|
22
22
|
ok: true,
|
|
23
|
-
value: await SwaggerParser.bundle(
|
|
23
|
+
value: await SwaggerParser.bundle(tsp)
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
26
|
return {
|
|
27
27
|
ok: true,
|
|
28
28
|
value: await SwaggerParser.bundle(input)
|
|
29
29
|
};
|
|
30
|
-
} catch (
|
|
30
|
+
} catch (error) {
|
|
31
31
|
return {
|
|
32
32
|
ok: false,
|
|
33
|
-
error:
|
|
33
|
+
error: error instanceof Error ? error.message : String(error)
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
36
|
}
|
|
@@ -47,21 +47,30 @@ function toJsonSchema(schema) {
|
|
|
47
47
|
/** Pull the bare expression out of a `export const X = <expr>` wrapper, then strip cross-schema refs. */
|
|
48
48
|
function extractExpr(code, stripRefs = (s) => s) {
|
|
49
49
|
const joined = code.split("\n").filter((line) => !line.startsWith("import ")).join("\n").trim();
|
|
50
|
-
const expr = joined.match(/^export const \w+ = (.+)$/
|
|
51
|
-
if (expr !== void 0) return stripRefs(expr.replace(/;?\s
|
|
50
|
+
const expr = joined.match(/^export const \w+ = (.+)$/su)?.[1];
|
|
51
|
+
if (expr !== void 0) return stripRefs(expr.replace(/;?\s*$/u, ""));
|
|
52
52
|
return stripRefs(joined);
|
|
53
53
|
}
|
|
54
|
+
/** One factory per library; the mapped type makes a missing library a compile error. */
|
|
55
|
+
const ADAPTERS = {
|
|
56
|
+
zod: makeZodAdapter,
|
|
57
|
+
valibot: makeValibotAdapter,
|
|
58
|
+
arktype: makeArktypeAdapter,
|
|
59
|
+
typebox: makeTypeboxAdapter,
|
|
60
|
+
effect: makeEffectAdapter
|
|
61
|
+
};
|
|
54
62
|
function makeAdapter(lib) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
63
|
+
return ADAPTERS[lib]();
|
|
64
|
+
}
|
|
65
|
+
/** Inline `z.lazy(() => XSchema)` back to the bare `XSchema` reference. */
|
|
66
|
+
function stripZodLazy(code) {
|
|
67
|
+
return code.replaceAll(/z\.lazy\(\(\)\s*=>\s*(\w+Schema)\)/gu, "$1");
|
|
68
|
+
}
|
|
69
|
+
/** Inline `v.lazy(() => XSchema)` back to the bare `XSchema` reference. */
|
|
70
|
+
function stripValibotLazy(code) {
|
|
71
|
+
return code.replaceAll(/v\.lazy\(\(\)\s*=>\s*(\w+Schema)\)/gu, "$1");
|
|
62
72
|
}
|
|
63
73
|
function makeZodAdapter() {
|
|
64
|
-
const stripLazy = (code) => code.replace(/z\.lazy\(\(\)\s*=>\s*(\w+Schema)\)/g, "$1");
|
|
65
74
|
return {
|
|
66
75
|
toExpression(schema, paramIn) {
|
|
67
76
|
return extractExpr(schemaToZod(toJsonSchema(schema), {
|
|
@@ -69,7 +78,7 @@ function makeZodAdapter() {
|
|
|
69
78
|
openapi: true,
|
|
70
79
|
readonly: false,
|
|
71
80
|
...paramIn && { paramIn }
|
|
72
|
-
}),
|
|
81
|
+
}), stripZodLazy);
|
|
73
82
|
},
|
|
74
83
|
renderImport() {
|
|
75
84
|
return "import * as z from 'zod'";
|
|
@@ -80,7 +89,6 @@ function makeZodAdapter() {
|
|
|
80
89
|
};
|
|
81
90
|
}
|
|
82
91
|
function makeValibotAdapter() {
|
|
83
|
-
const stripLazy = (code) => code.replace(/v\.lazy\(\(\)\s*=>\s*(\w+Schema)\)/g, "$1");
|
|
84
92
|
return {
|
|
85
93
|
toExpression(schema, paramIn) {
|
|
86
94
|
return extractExpr(schemaToValibot(toJsonSchema(schema), {
|
|
@@ -88,7 +96,7 @@ function makeValibotAdapter() {
|
|
|
88
96
|
openapi: true,
|
|
89
97
|
readonly: false,
|
|
90
98
|
...paramIn && { paramIn }
|
|
91
|
-
}),
|
|
99
|
+
}), stripValibotLazy);
|
|
92
100
|
},
|
|
93
101
|
renderImport() {
|
|
94
102
|
return "import * as v from 'valibot'";
|
|
@@ -155,7 +163,7 @@ function makeEffectAdapter() {
|
|
|
155
163
|
//#endregion
|
|
156
164
|
//#region src/utils/index.ts
|
|
157
165
|
function toPascalCase(str) {
|
|
158
|
-
return str.
|
|
166
|
+
return str.replaceAll(/[^a-zA-Z0-9]+(.)/gu, (_, c) => c.toUpperCase()).replace(/^(.)/u, (_, c) => c.toUpperCase());
|
|
159
167
|
}
|
|
160
168
|
/**
|
|
161
169
|
* Decode percent-encoding, falling back to the raw input on malformed sequences
|
|
@@ -175,15 +183,14 @@ function safeDecode(value) {
|
|
|
175
183
|
* are returned unchanged. Used for component lookup, not variable naming.
|
|
176
184
|
*/
|
|
177
185
|
function schemaRefToName(ref) {
|
|
178
|
-
|
|
179
|
-
return safeDecode(parts[parts.length - 1] ?? "");
|
|
186
|
+
return safeDecode(ref.split("/").at(-1) ?? "");
|
|
180
187
|
}
|
|
181
188
|
/**
|
|
182
189
|
* Encode non-ASCII characters as `u<hex codepoint>` so they survive identifier
|
|
183
190
|
* normalization instead of collapsing every non-ASCII name to the same value.
|
|
184
191
|
*/
|
|
185
192
|
function encodeNonAscii(name) {
|
|
186
|
-
return
|
|
193
|
+
return [...name].map((ch) => {
|
|
187
194
|
const cp = ch.codePointAt(0) ?? 0;
|
|
188
195
|
return cp > 127 ? `u${cp.toString(16)}` : ch;
|
|
189
196
|
}).join("");
|
|
@@ -195,10 +202,10 @@ function encodeNonAscii(name) {
|
|
|
195
202
|
* injectively so distinct names never collide.
|
|
196
203
|
*/
|
|
197
204
|
function toIdentifierPascalCase(name) {
|
|
198
|
-
const parts = encodeNonAscii(name).split(/[^a-zA-Z0-9]+/).filter(Boolean);
|
|
205
|
+
const parts = encodeNonAscii(name).split(/[^a-zA-Z0-9]+/u).filter(Boolean);
|
|
199
206
|
if (parts.length === 0) return "Schema";
|
|
200
207
|
const result = parts.map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
201
|
-
if (/^[0-9]
|
|
208
|
+
if (/^[0-9]/u.test(result)) return `_${result}`.replace(/([0-9])([a-z])/u, (_, d, c) => `${d}${c.toUpperCase()}`);
|
|
202
209
|
return result;
|
|
203
210
|
}
|
|
204
211
|
/**
|
|
@@ -207,7 +214,7 @@ function toIdentifierPascalCase(name) {
|
|
|
207
214
|
* component schema. Shared by ref-resolver and schema-replacements.
|
|
208
215
|
*/
|
|
209
216
|
function makeSchemaVarName(ref) {
|
|
210
|
-
const name = ref.match(/^#\/components\/schemas\/(.+)$/)?.[1];
|
|
217
|
+
const name = ref.match(/^#\/components\/schemas\/(.+)$/u)?.[1];
|
|
211
218
|
if (name === void 0) return void 0;
|
|
212
219
|
return `${toIdentifierPascalCase(safeDecode(name))}Schema`;
|
|
213
220
|
}
|
|
@@ -217,7 +224,7 @@ function makeSchemaVarName(ref) {
|
|
|
217
224
|
* keys (e.g. `X-Request-ID`) emit as valid TypeScript.
|
|
218
225
|
*/
|
|
219
226
|
function makeSafeKey(key) {
|
|
220
|
-
return /^[A-Za-z_$][A-Za-z0-9_$]
|
|
227
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/u.test(key) ? key : JSON.stringify(key);
|
|
221
228
|
}
|
|
222
229
|
/** Narrow an unknown value to a plain object (excludes arrays and null). */
|
|
223
230
|
function isRecord(v) {
|
|
@@ -252,7 +259,7 @@ function encodeValue(value, codeReplacements, seen) {
|
|
|
252
259
|
}
|
|
253
260
|
seen.add(value);
|
|
254
261
|
const entries = Object.entries(value).map(([k, v]) => {
|
|
255
|
-
return `${/^[a-zA-Z_$][a-zA-Z0-9_$]
|
|
262
|
+
return `${/^[a-zA-Z_$][a-zA-Z0-9_$]*$/u.test(k) ? k : JSON.stringify(k)}:${encodeValue(v, codeReplacements, seen)}`;
|
|
256
263
|
});
|
|
257
264
|
seen.delete(value);
|
|
258
265
|
return `{${entries.join(",")}}`;
|
package/package.json
CHANGED
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oas-truth",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "oas-truth is a parser from OpenAPI and TypeSpec to a typed OpenAPI document",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"openapi",
|
|
7
|
+
"typespec"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/nakita628/oas-truth",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/nakita628/oas-truth/issues"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/nakita628/oas-truth.git",
|
|
17
|
+
"directory": "packages/oas-truth"
|
|
18
|
+
},
|
|
5
19
|
"files": [
|
|
6
20
|
"dist"
|
|
7
21
|
],
|
|
8
22
|
"type": "module",
|
|
23
|
+
"sideEffects": false,
|
|
9
24
|
"exports": {
|
|
10
25
|
".": {
|
|
11
26
|
"types": "./dist/index.d.ts",
|
|
@@ -13,25 +28,28 @@
|
|
|
13
28
|
}
|
|
14
29
|
},
|
|
15
30
|
"dependencies": {
|
|
16
|
-
"schema-to-library": "^0.
|
|
31
|
+
"schema-to-library": "^0.4.0"
|
|
17
32
|
},
|
|
18
33
|
"devDependencies": {
|
|
19
|
-
"@apidevtools/swagger-parser": "^
|
|
20
|
-
"@types/node": "^
|
|
21
|
-
"@typespec/compiler": "
|
|
22
|
-
"@typespec/http": "
|
|
23
|
-
"@typespec/
|
|
24
|
-
"@typespec/
|
|
25
|
-
"@typespec/
|
|
26
|
-
"
|
|
27
|
-
"typescript": "^
|
|
34
|
+
"@apidevtools/swagger-parser": "^13.0.0",
|
|
35
|
+
"@types/node": "^26.5.1",
|
|
36
|
+
"@typespec/compiler": "1.16.0",
|
|
37
|
+
"@typespec/http": "1.16.0",
|
|
38
|
+
"@typespec/openapi": "1.16.0",
|
|
39
|
+
"@typespec/openapi3": "1.16.0",
|
|
40
|
+
"@typespec/rest": "0.86.0",
|
|
41
|
+
"@typespec/versioning": "0.86.0",
|
|
42
|
+
"typescript": "^7.0.2"
|
|
28
43
|
},
|
|
29
44
|
"peerDependencies": {
|
|
30
|
-
"@apidevtools/swagger-parser": ">=12.0.0 <
|
|
45
|
+
"@apidevtools/swagger-parser": ">=12.0.0 <14.0.0",
|
|
31
46
|
"@typespec/compiler": ">=1.11.0 <2.0.0",
|
|
32
47
|
"@typespec/openapi3": ">=1.11.0 <2.0.0"
|
|
33
48
|
},
|
|
34
49
|
"scripts": {
|
|
35
|
-
"build": "
|
|
50
|
+
"build": "vp pack",
|
|
51
|
+
"check": "vp check",
|
|
52
|
+
"fix": "vp check --fix",
|
|
53
|
+
"test": "vp test"
|
|
36
54
|
}
|
|
37
55
|
}
|