oas-truth 0.7.1 → 0.8.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 +2 -12
- package/dist/index.d.ts +77 -31
- package/dist/index.js +39 -47
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -29,21 +29,11 @@ if (result.ok) {
|
|
|
29
29
|
with `@apidevtools/swagger-parser` to resolve external `$ref`s
|
|
30
30
|
- TypeSpec files (`.tsp`) are compiled with `@typespec/compiler` and then bundled; the compiler is
|
|
31
31
|
loaded only for a `.tsp` input
|
|
32
|
-
- Returns
|
|
32
|
+
- Returns `{ ok: true, value } | { ok: false, error }` and never throws: an
|
|
33
33
|
unsupported input, a missing file, an unreachable URL and a TypeSpec diagnostic all come back as
|
|
34
34
|
`error`
|
|
35
35
|
|
|
36
|
-
The input is typed as `
|
|
37
|
-
to it:
|
|
38
|
-
|
|
39
|
-
```ts
|
|
40
|
-
import { isOpenAPIInput, parseOpenAPI } from 'oas-truth'
|
|
41
|
-
|
|
42
|
-
const input = process.argv[2]
|
|
43
|
-
if (isOpenAPIInput(input)) {
|
|
44
|
-
const result = await parseOpenAPI(input)
|
|
45
|
-
}
|
|
46
|
-
```
|
|
36
|
+
The input is typed as a template literal union of these documents (`Parameters<typeof parseOpenAPI>[0]`). A string that names none of them is checked at runtime too and comes back as `{ ok: false, error }` before anything is read.
|
|
47
37
|
|
|
48
38
|
## Component schemas
|
|
49
39
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The outcome of an operation that can fail: the value, or the reason it failed. The public
|
|
3
|
-
* API never throws; every failure is folded into `{ ok: false, error }`.
|
|
4
|
-
*/
|
|
5
|
-
export type Result<T, E = string> = {
|
|
6
|
-
readonly ok: true;
|
|
7
|
-
readonly value: T;
|
|
8
|
-
} | {
|
|
9
|
-
readonly ok: false;
|
|
10
|
-
readonly error: E;
|
|
11
|
-
};
|
|
12
|
-
/**
|
|
13
|
-
* A document `parseOpenAPI` reads: an OpenAPI file by its extension, a TypeSpec file, or an
|
|
14
|
-
* `http(s)` URL whatever its path (a document served without an extension is OpenAPI by
|
|
15
|
-
* content).
|
|
16
|
-
*/
|
|
17
|
-
export type OpenAPIInput = `${string}.yaml` | `${string}.yml` | `${string}.json` | `${string}.tsp` | `http://${string}` | `https://${string}`;
|
|
18
|
-
/** Whether a value names a document `parseOpenAPI` reads, for narrowing a CLI or config value. */
|
|
19
|
-
export declare function isOpenAPIInput(value: unknown): value is OpenAPIInput;
|
|
20
1
|
/**
|
|
21
2
|
* Read an OpenAPI or TypeSpec document into a typed OpenAPI document.
|
|
22
3
|
*
|
|
@@ -25,8 +6,23 @@ export declare function isOpenAPIInput(value: unknown): value is OpenAPIInput;
|
|
|
25
6
|
* the code builders can emit schema variables instead of inline copies. A local `.tsp` file
|
|
26
7
|
* is compiled with `@typespec/compiler` and emitted with `@typespec/openapi3`, both loaded
|
|
27
8
|
* only for TypeSpec, then bundled the same way.
|
|
9
|
+
*
|
|
10
|
+
* The input type lists what it reads: an OpenAPI file by its extension, a TypeSpec file, or an
|
|
11
|
+
* `http(s)` URL whatever its path (a document served without an extension is OpenAPI by
|
|
12
|
+
* content).
|
|
13
|
+
*
|
|
14
|
+
* It never throws: the document comes back as `{ ok: true, value }` and every failure as
|
|
15
|
+
* `{ ok: false, error }` with a message.
|
|
28
16
|
*/
|
|
29
|
-
export declare function parseOpenAPI(input:
|
|
17
|
+
export declare function parseOpenAPI(input: `${string}.yaml` | `${string}.yml` | `${string}.json` | `${string}.tsp` | `http://${string}` | `https://${string}`): Promise<{
|
|
18
|
+
readonly ok: true;
|
|
19
|
+
readonly value: OpenAPI;
|
|
20
|
+
readonly error?: never;
|
|
21
|
+
} | {
|
|
22
|
+
readonly value?: never;
|
|
23
|
+
readonly ok: false;
|
|
24
|
+
readonly error: string;
|
|
25
|
+
}>;
|
|
30
26
|
export type OpenAPI = {
|
|
31
27
|
readonly openapi?: string;
|
|
32
28
|
readonly $self?: string;
|
|
@@ -627,7 +623,7 @@ type SchemaReplacementsOptions = {
|
|
|
627
623
|
readonly slot?: SchemaSlot;
|
|
628
624
|
readonly identifiers?: ReadonlyMap<string, string>;
|
|
629
625
|
};
|
|
630
|
-
export declare function makeSchemaReplacements(value: unknown, adapter: ComponentAdapter, options?: SchemaReplacementsOptions):
|
|
626
|
+
export declare function makeSchemaReplacements(value: unknown, adapter: ComponentAdapter, options?: SchemaReplacementsOptions): Map<unknown, string>;
|
|
631
627
|
export declare function toPascalCase(str: string): string;
|
|
632
628
|
/**
|
|
633
629
|
* Extract the component name from a `$ref`, decoding percent-encoding so the
|
|
@@ -681,23 +677,68 @@ type ComponentCodeOptions = {
|
|
|
681
677
|
readonly readonly?: boolean;
|
|
682
678
|
readonly identifiers?: ReadonlyMap<string, string>;
|
|
683
679
|
};
|
|
684
|
-
export declare function makeCallbacksDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions):
|
|
680
|
+
export declare function makeCallbacksDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): {
|
|
681
|
+
name: string;
|
|
682
|
+
varName: string;
|
|
683
|
+
fileName: string;
|
|
684
|
+
code: string;
|
|
685
|
+
}[];
|
|
685
686
|
export declare function makeCallbacksCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
686
|
-
export declare function makeExamplesDeclarations(components: Components, options?: ComponentCodeOptions):
|
|
687
|
+
export declare function makeExamplesDeclarations(components: Components, options?: ComponentCodeOptions): {
|
|
688
|
+
name: string;
|
|
689
|
+
varName: string;
|
|
690
|
+
fileName: string;
|
|
691
|
+
code: string;
|
|
692
|
+
}[];
|
|
687
693
|
export declare function makeExamplesCode(components: Components, readonly: boolean): string;
|
|
688
|
-
export declare function makeHeadersDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions):
|
|
694
|
+
export declare function makeHeadersDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): {
|
|
695
|
+
name: string;
|
|
696
|
+
varName: string;
|
|
697
|
+
fileName: string;
|
|
698
|
+
code: string;
|
|
699
|
+
}[];
|
|
689
700
|
export declare function makeHeadersCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
690
|
-
export declare function makeLinksDeclarations(components: Components, options?: ComponentCodeOptions):
|
|
701
|
+
export declare function makeLinksDeclarations(components: Components, options?: ComponentCodeOptions): {
|
|
702
|
+
name: string;
|
|
703
|
+
varName: string;
|
|
704
|
+
fileName: string;
|
|
705
|
+
code: string;
|
|
706
|
+
}[];
|
|
691
707
|
export declare function makeLinksCode(components: Components, readonly: boolean): string;
|
|
692
|
-
export declare function makeMediaTypesDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions):
|
|
708
|
+
export declare function makeMediaTypesDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): {
|
|
709
|
+
name: string;
|
|
710
|
+
varName: string;
|
|
711
|
+
fileName: string;
|
|
712
|
+
code: string;
|
|
713
|
+
}[];
|
|
693
714
|
export declare function makeMediaTypesCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
694
|
-
export declare function makeParametersDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions):
|
|
715
|
+
export declare function makeParametersDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): {
|
|
716
|
+
name: string;
|
|
717
|
+
varName: string;
|
|
718
|
+
fileName: string;
|
|
719
|
+
code: string;
|
|
720
|
+
}[];
|
|
695
721
|
export declare function makeParametersCode(components: Components, adapter: ComponentAdapter, exportTypes?: boolean): string;
|
|
696
|
-
export declare function makePathItemsDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions):
|
|
722
|
+
export declare function makePathItemsDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): {
|
|
723
|
+
name: string;
|
|
724
|
+
varName: string;
|
|
725
|
+
fileName: string;
|
|
726
|
+
code: string;
|
|
727
|
+
}[];
|
|
697
728
|
export declare function makePathItemsCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
698
|
-
export declare function makeRequestBodiesDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions):
|
|
729
|
+
export declare function makeRequestBodiesDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): {
|
|
730
|
+
name: string;
|
|
731
|
+
varName: string;
|
|
732
|
+
fileName: string;
|
|
733
|
+
code: string;
|
|
734
|
+
}[];
|
|
699
735
|
export declare function makeRequestBodiesCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
700
|
-
export declare function makeResponsesDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions):
|
|
736
|
+
export declare function makeResponsesDeclarations(components: Components, adapter: ComponentAdapter, options?: ComponentCodeOptions): {
|
|
737
|
+
name: string;
|
|
738
|
+
varName: string;
|
|
739
|
+
fileName: string;
|
|
740
|
+
code: string;
|
|
741
|
+
}[];
|
|
701
742
|
export declare function makeResponsesCode(components: Components, adapter: ComponentAdapter, readonly: boolean): string;
|
|
702
743
|
type SchemasOptions = {
|
|
703
744
|
readonly exportTypes?: boolean;
|
|
@@ -767,6 +808,11 @@ export declare function makeSchemaDeclarations(schemas: {
|
|
|
767
808
|
* order. Returns `''` when schemas are missing or empty.
|
|
768
809
|
*/
|
|
769
810
|
export declare function makeSchemasCode(components: Components, adapter: ComponentAdapter, options?: SchemasOptions): string;
|
|
770
|
-
export declare function makeSecuritySchemesDeclarations(components: Components, options?: ComponentCodeOptions):
|
|
811
|
+
export declare function makeSecuritySchemesDeclarations(components: Components, options?: ComponentCodeOptions): {
|
|
812
|
+
name: string;
|
|
813
|
+
varName: string;
|
|
814
|
+
fileName: string;
|
|
815
|
+
code: string;
|
|
816
|
+
}[];
|
|
771
817
|
export declare function makeSecuritySchemesCode(components: Components, readonly: boolean): string;
|
|
772
818
|
export type { ComponentCodeOptions, ComponentDeclaration, SchemaDeclaration, SchemasOptions };
|
package/dist/index.js
CHANGED
|
@@ -6,19 +6,6 @@ import { schemaToTypebox } from "schema-truth/typebox";
|
|
|
6
6
|
import { schemaToValibot } from "schema-truth/valibot";
|
|
7
7
|
import { schemaToZod } from "schema-truth/zod";
|
|
8
8
|
// src/openapi/index.ts
|
|
9
|
-
const INPUT_EXTENSIONS = [
|
|
10
|
-
".yaml",
|
|
11
|
-
".yml",
|
|
12
|
-
".json",
|
|
13
|
-
".tsp"
|
|
14
|
-
];
|
|
15
|
-
/** Whether a value names a document `parseOpenAPI` reads, for narrowing a CLI or config value. */
|
|
16
|
-
function isOpenAPIInput(value) {
|
|
17
|
-
return typeof value === "string" && (isURL(value) || INPUT_EXTENSIONS.some((extension) => value.endsWith(extension)));
|
|
18
|
-
}
|
|
19
|
-
function isURL(value) {
|
|
20
|
-
return /^https?:\/\//iu.test(value);
|
|
21
|
-
}
|
|
22
9
|
/**
|
|
23
10
|
* Read an OpenAPI or TypeSpec document into a typed OpenAPI document.
|
|
24
11
|
*
|
|
@@ -27,53 +14,58 @@ function isURL(value) {
|
|
|
27
14
|
* the code builders can emit schema variables instead of inline copies. A local `.tsp` file
|
|
28
15
|
* is compiled with `@typespec/compiler` and emitted with `@typespec/openapi3`, both loaded
|
|
29
16
|
* only for TypeSpec, then bundled the same way.
|
|
17
|
+
*
|
|
18
|
+
* The input type lists what it reads: an OpenAPI file by its extension, a TypeSpec file, or an
|
|
19
|
+
* `http(s)` URL whatever its path (a document served without an extension is OpenAPI by
|
|
20
|
+
* content).
|
|
21
|
+
*
|
|
22
|
+
* It never throws: the document comes back as `{ ok: true, value }` and every failure as
|
|
23
|
+
* `{ ok: false, error }` with a message.
|
|
30
24
|
*/
|
|
31
25
|
async function parseOpenAPI(input) {
|
|
32
|
-
|
|
26
|
+
const isURL = typeof input === "string" && /^https?:\/\//iu.test(input);
|
|
27
|
+
if (typeof input === "string" && !isURL && ![
|
|
28
|
+
".yaml",
|
|
29
|
+
".yml",
|
|
30
|
+
".json",
|
|
31
|
+
".tsp"
|
|
32
|
+
].some((extension) => input.endsWith(extension))) return {
|
|
33
33
|
ok: false,
|
|
34
|
-
error: `Unsupported input: ${
|
|
34
|
+
error: `Unsupported input: ${input} (expected a .yaml, .yml, .json or .tsp file, or an http(s) URL)`
|
|
35
35
|
};
|
|
36
36
|
try {
|
|
37
|
-
if (typeof input
|
|
38
|
-
return {
|
|
37
|
+
if (typeof input !== "string" || isURL || !input.endsWith(".tsp")) return {
|
|
39
38
|
ok: true,
|
|
40
39
|
value: await SwaggerParser.bundle(input)
|
|
41
40
|
};
|
|
41
|
+
const [{ compile, NodeHost }, { getOpenAPI3 }] = await Promise.all([import("@typespec/compiler"), import("@typespec/openapi3")]);
|
|
42
|
+
const program = await compile(NodeHost, path.resolve(input), { noEmit: true });
|
|
43
|
+
if (program.diagnostics.length > 0) return {
|
|
44
|
+
ok: false,
|
|
45
|
+
error: `TypeSpec compile failed:\n${program.diagnostics.map((d) => d.message).join("\n")}`
|
|
46
|
+
};
|
|
47
|
+
const [record] = await getOpenAPI3(program);
|
|
48
|
+
const document = record === void 0 ? void 0 : "document" in record ? record.document : record.versions[0]?.document;
|
|
49
|
+
if (document === void 0) return {
|
|
50
|
+
ok: false,
|
|
51
|
+
error: `TypeSpec emitted no OpenAPI document: ${input}`
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
ok: true,
|
|
55
|
+
value: await SwaggerParser.bundle(document)
|
|
56
|
+
};
|
|
42
57
|
} catch (error) {
|
|
58
|
+
if (!(error instanceof Error)) return {
|
|
59
|
+
ok: false,
|
|
60
|
+
error: String(error)
|
|
61
|
+
};
|
|
62
|
+
const code = "ioErrorCode" in error ? error.ioErrorCode : void 0;
|
|
43
63
|
return {
|
|
44
64
|
ok: false,
|
|
45
|
-
error:
|
|
65
|
+
error: (error.message.trim() === "" || error.message.trimEnd().endsWith(":")) && typeof code === "string" && code !== "" ? `${error.message.trimEnd()} ${code}`.trim() : error.message
|
|
46
66
|
};
|
|
47
67
|
}
|
|
48
68
|
}
|
|
49
|
-
async function parseTypeSpec(input) {
|
|
50
|
-
const [{ compile, NodeHost }, { getOpenAPI3 }] = await Promise.all([import("@typespec/compiler"), import("@typespec/openapi3")]);
|
|
51
|
-
const program = await compile(NodeHost, path.resolve(input), { noEmit: true });
|
|
52
|
-
if (program.diagnostics.length > 0) return {
|
|
53
|
-
ok: false,
|
|
54
|
-
error: `TypeSpec compile failed:\n${program.diagnostics.map((d) => d.message).join("\n")}`
|
|
55
|
-
};
|
|
56
|
-
const [record] = await getOpenAPI3(program);
|
|
57
|
-
const document = record === void 0 ? void 0 : "document" in record ? record.document : record.versions[0]?.document;
|
|
58
|
-
if (document === void 0) return {
|
|
59
|
-
ok: false,
|
|
60
|
-
error: `TypeSpec emitted no OpenAPI document: ${input}`
|
|
61
|
-
};
|
|
62
|
-
return {
|
|
63
|
-
ok: true,
|
|
64
|
-
value: await SwaggerParser.bundle(document)
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* The message of a thrown value. swagger-parser reports some I/O failures (a URL that does
|
|
69
|
-
* not resolve, a missing file) with a message that ends before the reason and the reason in
|
|
70
|
-
* `ioErrorCode`; it is appended so the message says what went wrong.
|
|
71
|
-
*/
|
|
72
|
-
function errorMessage(error) {
|
|
73
|
-
if (!(error instanceof Error)) return String(error);
|
|
74
|
-
const code = "ioErrorCode" in error ? error.ioErrorCode : void 0;
|
|
75
|
-
return (error.message.trim() === "" || error.message.trimEnd().endsWith(":")) && typeof code === "string" && code !== "" ? `${error.message.trimEnd()} ${code}`.trim() : error.message;
|
|
76
|
-
}
|
|
77
69
|
// src/adapter/index.ts
|
|
78
70
|
const PARAM_STYLES = [
|
|
79
71
|
"matrix",
|
|
@@ -1036,4 +1028,4 @@ function makeSecuritySchemesDeclarations(components, options) {
|
|
|
1036
1028
|
function makeSecuritySchemesCode(components, readonly) {
|
|
1037
1029
|
return joinDeclarations(makeSecuritySchemesDeclarations(components, { readonly }), { trailingNewline: true });
|
|
1038
1030
|
}
|
|
1039
|
-
export { declarationFileName,
|
|
1031
|
+
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.8.0",
|
|
4
4
|
"description": "oas-truth is a parser from OpenAPI and TypeSpec to a typed OpenAPI document",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"openapi",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"provenance": true
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"schema-truth": "^0.8.
|
|
35
|
+
"schema-truth": "^0.8.8"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@apidevtools/swagger-parser": "^13.1.0",
|