forked from stoplightio/json-ref-resolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
283 lines (241 loc) · 6.55 KB
/
types.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
import { Dictionary, Segment } from '@stoplight/types';
import { DepGraph } from 'dependency-graph';
import * as URI from 'urijs';
/**
* The following interfaces are the primary interaction points for json-ref-resolver.
*
* IResolverOpts
* IResolveOpts
* IResolveResult
*/
/** The options that you can pass to `new Resolver(opts)` */
export interface IResolverOpts {
/** Used to store uri lookup results. If no cache passed in, one will be created for you. */
uriCache?: ICache;
/**
* Resolvers define how to fetch pointers for different URI schemes (http, https, file, mongo, whatever).
*
* If you do not pass in any resolvers, only inline pointer resolution will work `#/foo/bar`.
*/
resolvers?: {
[scheme: string]: IResolver;
};
/**
* Hook to customize which properties are resolved.
*
* By default, only `$ref` keys are considered and processed.
*
* If defined, this hook is called for every single property in source, so it should be performant!
*
* It should return the string to be resolved, or nothing to skip.
*
* Note, this overrides the default behavior. If you would like to preserve that, call the exported `defaultGetRef`
* as part of your custom `getRef` function.
*/
getRef?: (key: string, val: any) => string | void;
/**
* Hook to transform the final ref value before it is resolved.
*
* It should return a URI object to change the value being resolved, or void to make no changes.
*/
transformRef?: (opts: IRefTransformer, ctx: any) => URI | void;
/**
* Hook to customize how the result of an uri look is parsed.
*
* For example, could use `js-yaml` to parse a yaml string returned from the uri.
*/
parseResolveResult?: (opts: IUriParser) => Promise<IUriParserResult>;
/**
* Hook to transform resolved object.
*
* For example, transform `OpenAPI` file to a `Hub Page`.
*/
transformDereferenceResult?: (opts: IDereferenceTransformer) => Promise<ITransformerResult>;
/** Should we resolve local pointers? true by default. */
dereferenceInline?: boolean;
/**
* Should we resolve remote URIs? true by default.
*
* Note, must have a resolver defined for the uri scheme in question.
*/
dereferenceRemote?: boolean;
/**
* A spot to put your own arbitrary data.
*
* This is passed through to some hook functions, such as `transformRef` and `Resolver.resolve`.
*/
ctx?: any;
}
/** The options that you can pass to `resolver.resolve(opts)` */
export interface IResolveOpts extends IResolverOpts {
// resolve a specific part of the source object
jsonPointer?: string;
// the base URI against which $ref URIs are resolved
baseUri?: string;
parentPath?: string[];
pointerStack?: string[];
}
/** The object returned from `await resolver.resolve()` */
export interface IResolveResult {
/** The original source object, with all relevant references replaced. */
result: any;
/**
* A map of every single reference in source, and where it points, ie:
*
* ```json
* {
* "#/source/user": "#/models/user",
* "#/source/card": "file:///api.json/#models/card"
* }
* ```
*/
refMap: {
[source: string]: string;
};
/**
*
* A graph of every single reference in source.
*
*/
graph: DepGraph<IGraphNodeData>;
/** Any errors that occured during the resolution process. */
errors: IResolveError[];
/** The runner itself, which can be useful in more advanced cases. */
runner: IResolveRunner;
}
/**
* The below are useful to reference, but mostly internal details.
*/
export interface IResolver {
resolve(ref: URI, ctx: any): Promise<any>;
}
export interface IUriParser {
result: any;
fragment: string;
uriResult: IUriResult;
targetAuthority: URI;
parentAuthority: URI;
parentPath: string[];
}
export interface IUriParserResult {
result?: any;
error?: Error;
}
export interface IDereferenceTransformer {
result: any;
source: any;
fragment: string;
targetAuthority: URI;
parentAuthority: URI;
parentPath: string[];
}
export interface ITransformerResult {
result?: any;
error?: Error;
}
export interface IUriResult {
pointerStack: string[];
targetPath: string[];
uri: URI;
resolved?: IResolveResult;
error?: IResolveError;
}
/** @hidden */
export interface IComputeRefOpts {
key?: any;
val: any;
pointerStack: string[];
jsonPointer?: string;
}
export interface IRefTransformer extends IComputeRefOpts {
ref?: URI;
uri: URI;
}
export type ResolverErrorCode =
| 'POINTER_MISSING'
| 'RESOLVE_URI'
| 'PARSE_URI'
| 'RESOLVE_POINTER'
| 'PARSE_POINTER'
| 'TRANSFORM_DEREFERENCED';
export interface IResolveError {
code: ResolverErrorCode;
message: string;
path: Segment[];
uri: URI;
uriStack: string[];
pointerStack: string[];
}
export interface ICache {
readonly stats: {
hits: number;
misses: number;
};
get(key: string): any;
set(key: string, val: any): void;
has(key: string): boolean;
purge(): void;
}
export interface ICacheOpts {
// maxSize?: number;
stdTTL?: number; // the ttl as number in ms for non-error cache element.
// errTTL?: number; // the ttl as number in ms for every error cache element.
}
/** @hidden */
export interface IRefHandlerOpts {
ref: URI;
val: any;
pointerStack: string[];
cacheKey: string;
resolvingPointer?: string;
parentPath: string[];
parentPointer: string;
}
export interface IGraphNodeData {
/**
* A map of the refs in this node, and where they point
*
* Example:
*
* ```json
* {
* "#/users/address": "file:///api.json/#models/card",
* }
* ```
*/
refMap: Dictionary<string>;
data?: any;
}
export interface IResolveRunner {
id: number;
source: any;
dereferenceInline: boolean;
dereferenceRemote: boolean;
uriCache: ICache;
depth: number;
uriStack: string[];
baseUri: URI;
graph: DepGraph<IGraphNodeData>;
root: string;
atMaxUriDepth: () => boolean;
resolve: (opts?: IResolveOpts) => Promise<IResolveResult>;
computeRef: (opts: IComputeRefOpts) => URI | void | undefined;
lookupAndResolveUri: (opts: IRefHandlerOpts) => Promise<IUriResult>;
}
/** @hidden */
export interface IResolveRunnerOpts extends IResolveOpts {
root?: URI;
depth?: number;
uriStack?: string[];
}
export interface ICrawler {
jsonPointer?: string;
pointerGraph: DepGraph<string>;
pointerStemGraph: DepGraph<string>;
resolvers: Array<Promise<IUriResult>>;
computeGraph: (target: any, parentPath: string[], parentPointer: string, pointerStack?: string[]) => void;
}
export interface ICrawlerResult {
result: any;
errors: IResolveError[];
}