@json-schema-tools/dereferencer
Version:
Dereference (aka parse refs) from JSON Schemas
75 lines (74 loc) • 2.26 kB
TypeScript
import { JSONSchema } from "@json-schema-tools/meta-schema";
import { ProtocolHandlerMap } from "@json-schema-tools/reference-resolver/build/reference-resolver";
export interface RefCache {
[k: string]: JSONSchema;
}
/**
* Options that can be passed to the derefencer constructor.
*/
export interface DereferencerOptions {
/**
* If true, resolved non-local references will also be dereferenced using the same options.
*/
recursive?: boolean;
/**
* If true, the schema passed in will be dereferenced in-place, mutating the original. Default is false.
*/
mutate?: boolean;
/**
* Preseed the dereferencer with resolved refs
*/
refCache?: RefCache;
rootSchema?: JSONSchema;
/**
* Setup custom protocol handlers. See https://github.com/json-schema-tools/reference-resolver for details
*/
protocolHandlerMap?: ProtocolHandlerMap;
}
/**
* Error thrown by the constructor when given a ref that isn't a string
*
*
* @example
* ```typescript
*
* import Dereferencer, { NonStringRefError } from "@json-schema-tools/dereferencer";
*
* try { const dereffer = new Dereferencer({}); }
* catch(e) {
* if (e instanceof NonStringRefError) { ... }
* }
* ```
*
*/
export declare class NonStringRefError implements Error {
message: string;
name: string;
constructor(s: JSONSchema);
}
/**
* When instantiated, represents a fully configured dereferencer. When constructed, references are pulled out.
* No references are fetched until .resolve is called.
*/
export default class Dereferencer {
private options;
refs: string[];
private schema;
refCache: RefCache;
constructor(schema: JSONSchema, options?: DereferencerOptions);
/**
* Fetches the schemas for all the refs in the configured input schema(s)
*
* @returns a promise that will resolve a fully dereferenced schema, where all the
* promises for each ref has been resolved as well.
*
*
*/
resolve(): Promise<JSONSchema>;
_resolve(): Promise<JSONSchema>;
/**
* First-pass traversal to collect all the refs that we can find. This allows us to
* optimize the async work required as well.
*/
collectRefs(): string[];
}