This repository was archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathresolver.ts
More file actions
57 lines (50 loc) · 2.11 KB
/
resolver.ts
File metadata and controls
57 lines (50 loc) · 2.11 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
import { DepGraph } from 'dependency-graph';
import * as URI from 'urijs';
import { Cache } from './cache';
import { ResolveRunner } from './runner';
import * as Types from './types';
/**
* This is the primary class.
*
* See IResolverOptions for available options that you can pass in.
*/
export class Resolver {
public readonly uriCache: Types.ICache;
protected dereferenceInline: boolean;
protected dereferenceRemote: boolean;
protected ctx: any = {};
protected resolvers: {
[scheme: string]: Types.IResolver;
};
protected getRef?: (key: string, val: any) => string | void;
protected transformRef?: (opts: Types.IRefTransformer, ctx: any) => URI | any;
protected parseResolveResult?: (opts: Types.IUriParser) => Promise<Types.IUriParserResult>;
protected transformDereferenceResult?: (opts: Types.IDereferenceTransformer) => Promise<Types.ITransformerResult>;
constructor(opts: Types.IResolverOpts = {}) {
this.uriCache = opts.uriCache || new Cache();
this.resolvers = opts.resolvers || {};
this.getRef = opts.getRef;
this.transformRef = opts.transformRef;
this.dereferenceInline = typeof opts.dereferenceInline !== 'undefined' ? opts.dereferenceInline : true;
this.dereferenceRemote = typeof opts.dereferenceRemote !== 'undefined' ? opts.dereferenceRemote : true;
this.parseResolveResult = opts.parseResolveResult;
this.transformDereferenceResult = opts.transformDereferenceResult;
this.ctx = opts.ctx;
}
public resolve(source: any, opts: Types.IResolveOpts = {}): Promise<Types.IResolveResult> {
const graph = new DepGraph<any>({ circular: true });
const runner = new ResolveRunner(source, graph, {
uriCache: this.uriCache,
resolvers: this.resolvers,
getRef: this.getRef,
transformRef: this.transformRef,
dereferenceInline: this.dereferenceInline,
dereferenceRemote: this.dereferenceRemote,
parseResolveResult: this.parseResolveResult,
transformDereferenceResult: this.transformDereferenceResult,
...opts,
ctx: Object.assign({}, this.ctx || {}, opts.ctx || {}),
});
return runner.resolve(opts);
}
}