-
-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathschema-walker.ts
42 lines (36 loc) · 1.2 KB
/
schema-walker.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
import lodash from "lodash";
import type { OpenAPI } from "openapi-types";
import type { CodeGenConfig } from "./configuration.js";
import type { SwaggerSchemaResolver } from "./swagger-schema-resolver.js";
// TODO: WIP
// this class will be needed to walk by schema everywhere
export class SchemaWalker {
config: CodeGenConfig;
swaggerSchemaResolver: SwaggerSchemaResolver;
schemas = new Map<string, OpenAPI.Document>();
caches = new Map<string, OpenAPI.Document>();
constructor(
config: CodeGenConfig,
swaggerSchemaResolver: SwaggerSchemaResolver,
) {
this.config = config;
this.swaggerSchemaResolver = swaggerSchemaResolver;
}
addSchema = (name: string, schema: OpenAPI.Document) => {
this.schemas.set(name, structuredClone(schema));
};
_isLocalRef = (ref: string) => {
return ref.startsWith("#");
};
_isRemoteRef = (ref: string) => {
return ref.startsWith("http://") || ref.startsWith("https://");
};
_getRefDataFromSchema = (schema: Record<string, unknown>, ref: string) => {
const path = ref.replace("#", "").split("/");
const refData = lodash.get(schema, path);
if (refData) {
this.caches.set(ref, refData);
}
return refData;
};
}