-
Notifications
You must be signed in to change notification settings - Fork 378
/
resolver.ts
24 lines (22 loc) · 946 Bytes
/
resolver.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
import {$RefParser, ParserOptions as $RefOptions} from '@apidevtools/json-schema-ref-parser'
import {JSONSchema} from './types/JSONSchema'
import {log} from './utils'
export type DereferencedPaths = WeakMap<JSONSchema, string>
export async function dereference(
schema: JSONSchema,
{cwd, $refOptions}: {cwd: string; $refOptions: $RefOptions},
): Promise<{dereferencedPaths: DereferencedPaths; dereferencedSchema: JSONSchema}> {
log('green', 'dereferencer', 'Dereferencing input schema:', cwd, schema)
const parser = new $RefParser()
const dereferencedPaths: DereferencedPaths = new WeakMap()
const dereferencedSchema = (await parser.dereference(cwd, schema, {
...$refOptions,
dereference: {
...$refOptions.dereference,
onDereference($ref: string, schema: JSONSchema) {
dereferencedPaths.set(schema, $ref)
},
},
})) as any // TODO: fix types
return {dereferencedPaths, dereferencedSchema}
}