-
-
Notifications
You must be signed in to change notification settings - Fork 528
/
Copy pathload.ts
410 lines (378 loc) · 14.8 KB
/
load.ts
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import type { ComponentsObject, Fetch, GlobalContext, OpenAPI3, OperationObject, ParameterObject, PathItemObject, ReferenceObject, RequestBodyObject, ResponseObject, SchemaObject, Subschema } from "./types.js";
import fs from "node:fs";
import path from "node:path";
import { Readable } from "node:stream";
import { URL } from "node:url";
import yaml from "js-yaml";
import type { Dispatcher } from "undici";
import { parseRef, error, makeTSIndex, walk, isRemoteURL, isFilepath } from "./utils.js";
interface SchemaMap {
[id: string]: Subschema;
}
export const VIRTUAL_JSON_URL = `file:///_json`; // fake URL reserved for dynamic JSON
function parseYAML(schema: any): any {
try {
return yaml.load(schema);
} catch (err: any) {
error(`YAML: ${err.toString()}`);
process.exit(1);
}
}
function parseJSON(schema: any): any {
try {
return JSON.parse(schema);
} catch (err: any) {
error(`JSON: ${err.toString()}`);
process.exit(1);
}
}
export function resolveSchema(filename: string): URL {
// option 1: remote
if (isRemoteURL(filename)) return new URL(filename.startsWith("//") ? `https:${filename}` : filename);
// option 2: local
const localPath = path.isAbsolute(filename) ? new URL(`file://${filename}`) : new URL(filename, `file://${process.cwd()}/`);
if (!fs.existsSync(localPath)) {
error(`Could not locate ${filename}`);
process.exit(1);
} else if (fs.statSync(localPath).isDirectory()) {
error(`${localPath} is a directory not a file`);
process.exit(1);
}
return localPath;
}
/**
* Accepts income HTTP headers and appends them to
* the fetch request for the schema.
*
* @param {HTTPHeaderMap} httpHeaders
* @return {Record<string, string>} {Record<string, string>} Final HTTP headers outcome.
*/
function parseHttpHeaders(httpHeaders: Record<string, any>): Record<string, any> {
const finalHeaders: Record<string, string> = {};
// Obtain the header key
for (const [k, v] of Object.entries(httpHeaders)) {
// If the value of the header is already a string, we can move on, otherwise we have to parse it
if (typeof v === "string") {
finalHeaders[k] = v;
} else {
try {
const stringVal = JSON.stringify(v);
finalHeaders[k] = stringVal;
} catch (err) {
error(`Cannot parse key: ${k} into JSON format. Continuing with the next HTTP header that is specified`);
}
}
}
return finalHeaders;
}
export interface LoadOptions extends GlobalContext {
/** Subschemas may be any type; this helps transform correctly */
hint?: Subschema["hint"];
auth?: string;
rootURL: URL;
schemas: SchemaMap;
urlCache: Set<string>;
httpHeaders?: Record<string, any>;
httpMethod?: string;
fetch: Fetch;
parameters: Record<string, ParameterObject>;
}
/** Load a schema from local path or remote URL */
export default async function load(schema: URL | Subschema | Readable, options: LoadOptions): Promise<{ [url: string]: Subschema }> {
let schemaID = ".";
// 1. load contents
// 1a. URL
if (schema instanceof URL) {
const hint = options.hint ?? "OpenAPI3";
// normalize ID
if (schema.href !== options.rootURL.href) schemaID = relativePath(options.rootURL, schema);
if (options.urlCache.has(schemaID)) return options.schemas; // exit early if already indexed
options.urlCache.add(schemaID);
const ext = path.extname(schema.pathname).toLowerCase();
// remote
if (schema.protocol.startsWith("http")) {
const headers: Record<string, any> = { "User-Agent": "openapi-typescript" };
if (options.auth) headers.Authorization = options.auth;
// Add custom parsed HTTP headers
if (options.httpHeaders) {
const parsedHeaders = parseHttpHeaders(options.httpHeaders);
for (const [k, v] of Object.entries(parsedHeaders)) {
headers[k] = v;
}
}
const res = await options.fetch(schema, {
method: (options.httpMethod as Dispatcher.HttpMethod) || "GET",
headers,
});
const contentType = res.headers.get("content-type");
if (ext === ".json" || contentType?.includes("json")) {
options.schemas[schemaID] = {
hint,
schema: parseJSON(await res.text()),
};
} else if (ext === ".yaml" || ext === ".yml" || contentType?.includes("yaml")) {
options.schemas[schemaID] = {
hint,
schema: parseYAML(await res.text()),
};
}
}
// local file
else {
const contents = fs.readFileSync(schema, "utf8");
if (ext === ".yaml" || ext === ".yml")
options.schemas[schemaID] = {
hint,
schema: parseYAML(contents),
};
else if (ext === ".json")
options.schemas[schemaID] = {
hint,
schema: parseJSON(contents),
};
}
}
// 1b. Readable stream
else if (schema instanceof Readable) {
const readable = schema;
const contents = await new Promise<string>((resolve) => {
readable.resume();
readable.setEncoding("utf8");
let content = "";
readable.on("data", (chunk: string) => {
content += chunk;
});
readable.on("end", () => {
resolve(content.trim());
});
});
// if file starts with '{' assume JSON
options.schemas[schemaID] = {
hint: "OpenAPI3",
schema: contents.startsWith("{") ? parseJSON(contents) : parseYAML(contents),
};
}
// 1c. inline
else if (typeof schema === "object") {
options.schemas[schemaID] = {
hint: "OpenAPI3",
schema: schema as any,
};
}
// 1d. failsafe
else {
error(`Invalid schema`);
process.exit(1);
}
// 2. resolve $refs
const currentSchema = options.schemas[schemaID].schema;
// 2a. remove "components.examples" first
if (options.schemas[schemaID].hint === "OpenAPI3") {
if ("components" in currentSchema && currentSchema.components && "examples" in currentSchema.components) delete currentSchema.components.examples;
}
const refPromises: Promise<any>[] = [];
walk(currentSchema, (rawNode, nodePath) => {
// filter custom properties from allOf, anyOf, oneOf
for (const k of ["allOf", "anyOf", "oneOf"]) {
if (Array.isArray(rawNode[k])) {
rawNode[k] = (rawNode as any)[k].filter((o: SchemaObject | ReferenceObject) => {
if (!o || typeof o !== "object" || Array.isArray(o)) throw new Error(`${nodePath}.${k}: Expected array of objects. Is your schema valid?`);
if (!("$ref" in o) || typeof o.$ref !== "string") return true;
const ref = parseRef(o.$ref);
return !ref.path.some((i) => i.startsWith("x-")); // ignore all custom "x-*" properties
});
}
}
if (!rawNode || typeof rawNode !== "object" || Array.isArray(rawNode)) throw new Error(`${nodePath}: Expected object, got ${Array.isArray(rawNode) ? "array" : typeof rawNode}. Is your schema valid?`);
if (!("$ref" in rawNode) || typeof rawNode.$ref !== "string") return;
const node = rawNode as unknown as ReferenceObject;
const ref = parseRef(node.$ref);
if (ref.filename === ".") return; // local $ref; ignore
// $ref with custom "x-*" property
if (ref.path.some((i) => i.startsWith("x-"))) {
delete (node as any).$ref;
return;
}
// hints help external partial schemas pick up where the root left off (for external complete/valid schemas, skip this)
const isRemoteFullSchema = ref.path[0] === "paths" || ref.path[0] === "components"; // if the initial ref is "paths" or "components" this must be a full schema
const hint = isRemoteFullSchema ? "OpenAPI3" : getHint([...nodePath, ...ref.path], options.hint);
// if root schema is remote and this is a relative reference, treat as remote
if (schema instanceof URL) {
const nextURL = new URL(ref.filename, schema);
const nextID = relativePath(schema, nextURL);
if (options.urlCache.has(nextID)) return;
refPromises.push(load(nextURL, { ...options, hint }));
node.$ref = node.$ref.replace(ref.filename, nextID);
return;
}
// otherwise, if $ref is remote use that
if (isRemoteURL(ref.filename) || isFilepath(ref.filename)) {
const nextURL = new URL(ref.filename.startsWith("//") ? `https://${ref.filename}` : ref.filename);
if (options.urlCache.has(nextURL.href)) return;
refPromises.push(load(nextURL, { ...options, hint }));
node.$ref = node.$ref.replace(ref.filename, nextURL.href);
return;
}
// if this is dynamic JSON, we have no idea how to resolve external URLs, so throw here
if (options.rootURL.href === VIRTUAL_JSON_URL) {
error(`Can’t resolve "${ref.filename}" from dynamic JSON. Load this schema from a URL instead.`);
process.exit(1);
}
error(`Can’t resolve "${ref.filename}"`);
process.exit(1);
});
await Promise.all(refPromises);
// 3. transform $refs once, at the root schema, after all have been scanned & downloaded (much easier to do here when we have the context)
if (schemaID === ".") {
for (const subschemaID of Object.keys(options.schemas)) {
walk(options.schemas[subschemaID].schema, (rawNode, nodePath) => {
if (!("$ref" in rawNode) || typeof rawNode.$ref !== "string") return;
const node = rawNode as unknown as ReferenceObject;
const ref = parseRef(node.$ref);
// local $ref: convert into TS path
if (ref.filename === ".") {
node.$ref = makeTSIndex(ref.path);
}
// external $ref
else {
const refURL = new URL(ref.filename, new URL(subschemaID, options.rootURL));
node.$ref = makeTSIndex(["external", relativePath(options.rootURL, refURL), ...ref.path]);
}
});
}
}
// 4. collect parameters (which must be hoisted to the top)
for (const k of Object.keys(options.schemas)) {
walk(options.schemas[k].schema, (rawNode, nodePath) => {
// note: 'in' is a unique required property of parameters. and parameters can live in subschemas (i.e. "parameters" doesn’t have to be part of the traceable path)
if (typeof rawNode === "object" && "in" in rawNode) {
const key = k === "." ? makeTSIndex(nodePath) : makeTSIndex(["external", k, ...nodePath]);
options.parameters[key] = rawNode as any;
}
});
}
// 5. scan for discriminators (after everything’s resolved in one file)
for (const k of Object.keys(options.schemas)) {
// 4a. lazy stringification check is faster than deep-scanning a giant object for discriminators
// since most schemas don’t use them
if (JSON.stringify(options.schemas[k].schema).includes('"discriminator"')) {
walk(options.schemas[k].schema, (rawNode, nodePath) => {
const node = rawNode as unknown as SchemaObject;
if (!node.discriminator) return;
options.discriminators[schemaID === "." ? makeTSIndex(nodePath) : makeTSIndex(["external", k, ...nodePath])] = node.discriminator;
});
}
}
return options.schemas;
}
/** relative path from 2 URLs */
function relativePath(src: URL, dest: URL): string {
const isSameOrigin = dest.protocol.startsWith("http") && src.protocol.startsWith("http") && dest.origin === src.origin;
const isSameDisk = dest.protocol === "file:" && src.protocol === "file:";
if (isSameOrigin || isSameDisk) {
return path.posix.relative(path.posix.dirname(src.pathname), dest.pathname);
}
return dest.href;
}
/** given a path array (an array of indices), what type of object is this? */
export function getHint(path: (string | number)[], startFrom?: Subschema["hint"]): Subschema["hint"] | undefined {
if (startFrom && startFrom !== "OpenAPI3") {
switch (startFrom) {
case "OperationObject":
return getHintFromOperationObject(path);
case "RequestBodyObject":
return getHintFromRequestBodyObject(path);
case "ResponseObject":
return getHintFromResponseObject(path);
default:
return startFrom;
}
}
switch (path[0] as keyof OpenAPI3) {
case "paths":
return getHintFromPathItemObject(path.slice(2)); // skip URL at [1]
case "components":
return getHintFromComponentsObject(path.slice(1));
}
return undefined;
}
function getHintFromComponentsObject(path: (string | number)[]): Subschema["hint"] | undefined {
switch (path[0] as keyof ComponentsObject) {
case "schemas":
case "headers":
return getHintFromSchemaObject(path.slice(2));
case "parameters":
return getHintFromParameterObject(path.slice(2));
case "responses":
return getHintFromResponseObject(path.slice(2));
case "requestBodies":
return getHintFromRequestBodyObject(path.slice(2));
case "pathItems":
return getHintFromPathItemObject(path.slice(2));
}
return "SchemaObject";
}
function getHintFromMediaTypeObject(path: (string | number)[]): Subschema["hint"] {
switch (path[0]) {
case "schema":
return getHintFromSchemaObject(path.slice(1));
}
return "MediaTypeObject";
}
function getHintFromOperationObject(path: (string | number)[]): Subschema["hint"] {
switch (path[0] as keyof OperationObject) {
case "parameters":
return "ParameterObject[]";
case "requestBody":
return getHintFromRequestBodyObject(path.slice(1));
case "responses":
return getHintFromResponseObject(path.slice(2)); // skip the response code at [1]
}
return "OperationObject";
}
function getHintFromParameterObject(path: (string | number)[]): Subschema["hint"] {
switch (path[0]) {
case "content":
return getHintFromMediaTypeObject(path.slice(2)); // skip content type at [1]
case "schema":
return getHintFromSchemaObject(path.slice(1));
}
return "ParameterObject";
}
function getHintFromPathItemObject(path: (string | number)[]): Subschema["hint"] | undefined {
switch (path[0] as keyof PathItemObject) {
case "parameters": {
if (typeof path[1] === "number") {
return "ParameterObject[]";
}
return getHintFromParameterObject(path.slice(1));
}
default:
return getHintFromOperationObject(path.slice(1));
}
}
function getHintFromRequestBodyObject(path: (string | number)[]): Subschema["hint"] {
switch (path[0] as keyof RequestBodyObject) {
case "content":
return getHintFromMediaTypeObject(path.slice(2)); // skip content type at [1]
}
return "RequestBodyObject";
}
function getHintFromResponseObject(path: (string | number)[]): Subschema["hint"] {
switch (path[0] as keyof ResponseObject) {
case "headers":
return getHintFromSchemaObject(path.slice(2)); // skip name at [1]
case "content":
return getHintFromMediaTypeObject(path.slice(2)); // skip content type at [1]
}
return "ResponseObject";
}
function getHintFromSchemaObject(path: (string | number)[]): Subschema["hint"] {
switch (path[0]) {
case "allOf":
case "anyOf":
case "oneOf":
return getHintFromSchemaObject(path.slice(2)); // skip array index at [1]
}
return "SchemaObject";
}