-
-
Notifications
You must be signed in to change notification settings - Fork 637
Expand file tree
/
Copy pathindex.ts
More file actions
108 lines (99 loc) · 4.76 KB
/
index.ts
File metadata and controls
108 lines (99 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { performance } from "node:perf_hooks";
import type { Readable } from "node:stream";
import { createConfig } from "@redocly/openapi-core";
import type ts from "typescript";
import { validateAndBundle } from "./lib/redoc.js";
import { debug, resolveRef, scanDiscriminators } from "./lib/utils.js";
import transformSchema from "./transform/index.js";
import type { GlobalContext, OpenAPI3, OpenAPITSOptions } from "./types.js";
export * from "./lib/ts.js";
export * from "./lib/utils.js";
export { default as transformComponentsObject } from "./transform/components-object.js";
export { default as transformHeaderObject } from "./transform/header-object.js";
export { default as transformSchema } from "./transform/index.js";
export { default as transformMediaTypeObject } from "./transform/media-type-object.js";
export * from "./transform/operation-object.js";
export { default as transformOperationObject } from "./transform/operation-object.js";
export { default as transformParameterObject } from "./transform/parameter-object.js";
export * from "./transform/path-item-object.js";
export { default as transformPathItemObject } from "./transform/path-item-object.js";
export { default as transformPathsObject } from "./transform/paths-object.js";
export { default as transformRequestBodyObject } from "./transform/request-body-object.js";
export { default as transformResponseObject } from "./transform/response-object.js";
export { default as transformResponsesObject } from "./transform/responses-object.js";
export * from "./transform/schema-object.js";
export { default as transformSchemaObject } from "./transform/schema-object.js";
export * from "./types.js";
export const COMMENT_HEADER = `/**
* This file was auto-generated by openapi-typescript.
* Do not make direct changes to the file.
*/
`;
/**
* Convert an OpenAPI schema to TypesScript AST
* @param {string|URL|object|Readable} source OpenAPI schema source:
* - YAML: string
* - JSON: parsed object
* - URL: URL to a YAML or JSON file (local or remote)
* - Readable: Readable stream of YAML or JSON
*/
export default async function openapiTS(
source: string | URL | OpenAPI3 | Buffer | Readable,
options: OpenAPITSOptions = {} as Partial<OpenAPITSOptions>,
): Promise<ts.Node[]> {
if (!source) {
throw new Error("Empty schema. Please specify a URL, file path, or Redocly Config");
}
const redoc =
options.redocly ??
(await createConfig({
extends: ["minimal"],
rules: {
"operation-operationId-unique": { severity: "error" }, // throw error on duplicate operationIDs
struct: "warn", // downgrade struct rule to warning to allow incomplete schemas
"no-server-trailing-slash": "warn",
},
}));
const schema = await validateAndBundle(source, {
redoc,
cwd: options.cwd instanceof URL ? options.cwd : new URL(`file://${options.cwd ?? process.cwd()}/`),
silent: options.silent ?? false,
});
const ctx: GlobalContext = {
additionalProperties: options.additionalProperties ?? false,
alphabetize: options.alphabetize ?? false,
arrayLength: options.arrayLength ?? false,
defaultNonNullable: options.defaultNonNullable ?? true,
discriminators: scanDiscriminators(schema, options),
emptyObjectsUnknown: options.emptyObjectsUnknown ?? false,
enum: options.enum ?? false,
enumValues: options.enumValues ?? false,
conditionalEnums: options.conditionalEnums ?? false,
dedupeEnums: options.dedupeEnums ?? false,
excludeDeprecated: options.excludeDeprecated ?? false,
exportType: options.exportType ?? false,
immutable: options.immutable ?? false,
rootTypes: options.rootTypes ?? false,
rootTypesNoSchemaPrefix: options.rootTypesNoSchemaPrefix ?? false,
rootTypesKeepCasing: options.rootTypesKeepCasing ?? false,
injectFooter: [],
pathParamsAsTypes: options.pathParamsAsTypes ?? false,
postTransform: typeof options.postTransform === "function" ? options.postTransform : undefined,
propertiesRequiredByDefault: options.propertiesRequiredByDefault ?? false,
redoc,
silent: options.silent ?? false,
inject: options.inject ?? undefined,
transform: typeof options.transform === "function" ? options.transform : undefined,
transformProperty: typeof options.transformProperty === "function" ? options.transformProperty : undefined,
makePathsEnum: options.makePathsEnum ?? false,
generatePathParams: options.generatePathParams ?? false,
readWriteMarkers: options.readWriteMarkers ?? false,
resolve($ref) {
return resolveRef(schema, $ref, { silent: options.silent ?? false });
},
};
const transformT = performance.now();
const result = transformSchema(schema, ctx);
debug("Completed AST transformation for entire document", "ts", performance.now() - transformT);
return result;
}