-
Notifications
You must be signed in to change notification settings - Fork 64
/
index.d.ts
321 lines (299 loc) · 13.3 KB
/
index.d.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* Various utilities for JSON References *(http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03)* and
* JSON Pointers *(https://tools.ietf.org/html/rfc6901)*.
*/
declare module 'json-refs' {
/**
* Clears the internal cache of remote documents, reference details, etc.
*/
export function clearCache(): void;
/**
* Takes an array of path segments and decodes the JSON Pointer tokens in them.
* @param path - The array of path segments
* @returns the array of path segments with their JSON Pointer tokens decoded
* @throws if the path is not an `Array`
* @see
*/
export function decodePath(path: string[]): string[];
/**
* Takes an array of path segments and encodes the special JSON Pointer characters in them.
* @param path - The array of path segments
* @returns the array of path segments with their JSON Pointer tokens encoded
* @throws if the path is not an `Array`
* @see
*/
export function encodePath(path: string[]): string[];
/**
* Finds JSON References defined within the provided array/object.
* @param obj - The structure to find JSON References within
* @param options - The JsonRefs options
* @returns an object whose keys are JSON Pointers
* *(fragment version)* to where the JSON Reference is defined and whose values are {@link UnresolvedRefDetails}.
* @throws when the input arguments fail validation or if `options.subDocPath` points to an invalid location
*/
export function findRefs(obj: any[] | object, options?: JsonRefsOptions): { [key: string]: (UnresolvedRefDetails|undefined) };
/**
* Finds JSON References defined within the document at the provided location.
*
* This API is identical to {@link findRefs} except this API will retrieve a remote document and then
* return the result of {@link findRefs} on the retrieved document.
* @param location - The location to retrieve *(Can be relative or absolute, just make sure you look at the
* {@link JsonRefsOptions|options documentation} to see how relative references are handled.)*
* @param options - The JsonRefs options
* @returns a promise that resolves a
* {@link RetrievedRefsResults} and rejects with an `Error` when the input arguments fail validation,
* when `options.subDocPath` points to an invalid location or when the location argument points to an unloadable
* resource
*/
export function findRefsAt(location: string, options?: JsonRefsOptions): Promise<RetrievedRefsResults>;
/**
* Returns detailed information about the JSON Reference.
* @param obj - The JSON Reference definition
* @returns the detailed information
*/
export function getRefDetails(obj: object): UnresolvedRefDetails;
/**
* Returns whether the argument represents a JSON Pointer.
*
* A string is a JSON Pointer if the following are all true:
*
* * The string is of type `String`
* * The string must be empty, `#` or start with a `/` or `#/`
* @param ptr - The string to check
* @param throwWithDetails - Whether or not to throw an `Error` with the details as to why the value
* provided is invalid
* @returns the result of the check
* @throws when the provided value is invalid and the `throwWithDetails` argument is `true`
* @see
*/
export function isPtr(ptr: string, throwWithDetails?: boolean): boolean;
/**
* Returns whether the argument represents a JSON Reference.
*
* An object is a JSON Reference only if the following are all true:
*
* * The object is of type `Object`
* * The object has a `$ref` property
* * The `$ref` property is a valid URI *(We do not require 100% strict URIs and will handle unescaped special
* characters.)*
* @param obj - The object to check
* @param throwWithDetails - Whether or not to throw an `Error` with the details as to why the value
* provided is invalid
* @returns the result of the check
* @throws when the provided value is invalid and the `throwWithDetails` argument is `true`
* @see
*/
export function isRef(obj: object, throwWithDetails?: boolean): boolean;
/**
* Returns an array of path segments for the provided JSON Pointer.
* @param ptr - The JSON Pointer
* @returns the path segments
* @throws if the provided `ptr` argument is not a JSON Pointer
*/
export function pathFromPtr(ptr: string): string[];
/**
* Returns a JSON Pointer for the provided array of path segments.
*
* **Note:** If a path segment in `path` is not a `String`, it will be converted to one using `JSON.stringify`.
* @param path - The array of path segments
* @param hashPrefix - Whether or not create a hash-prefixed JSON Pointer
* @returns the corresponding JSON Pointer
* @throws if the `path` argument is not an array
*/
export function pathToPtr(path: string[], hashPrefix?: boolean): string;
/**
* Finds JSON References defined within the provided array/object and resolves them.
* @param obj - The structure to find JSON References within
* @param options - The JsonRefs options
* @returns a promise that resolves a
* {@link ResolvedRefsResults} and rejects with an `Error` when the input arguments fail validation,
* when `options.subDocPath` points to an invalid location or when the location argument points to an unloadable
* resource
*/
export function resolveRefs(obj: any[] | object, options?: JsonRefsOptions): Promise<ResolvedRefsResults>;
/**
* Resolves JSON References defined within the document at the provided location.
*
* This API is identical to {@link resolveRefs} except this API will retrieve a remote document and
* then return the result of {@link resolveRefs} on the retrieved document.
* @param location - The location to retrieve *(Can be relative or absolute, just make sure you look at the
* {@link JsonRefsOptions|options documentation} to see how relative references are handled.)*
* @param options - The JsonRefs options
* @returns a promise that resolves a
* {@link RetrievedResolvedRefsResults} and rejects with an `Error` when the input arguments fail
* validation, when `options.subDocPath` points to an invalid location or when the location argument points to an
* unloadable resource
*/
export function resolveRefsAt(location: string, options?: JsonRefsOptions): Promise<RetrievedResolvedRefsResults>;
/**
* The options used for various JsonRefs APIs.
*/
interface JsonRefsOptions {
/**
* The filter to use when gathering JSON
* References *(If this value is a single string or an array of strings, the value(s) are expected to be the `type(s)`
* you are interested in collecting as described in {@link getRefDetails}. If it is a function, it is
* expected that the function behaves like {@link RefDetailsFilter}.)*
*/
filter?: string | string[] | Function;
/**
* Whether or not to include invalid JSON Reference details *(This will
* make it so that objects that are like JSON Reference objects, as in they are an `Object` and the have a `$ref`
* property, but fail validation will be included. This is very useful for when you want to know if you have invalid
* JSON Reference definitions. This will not mean that APIs will process invalid JSON References but the reasons as to
* why the JSON References are invalid will be included in the returned metadata.)*
*/
includeInvalid?: boolean;
/**
* The options to pass to
* {@link https://github.com/whitlockjc/path-loader/blob/master/docs/API.md#module_PathLoader.load|PathLoader~load}
*/
loaderOptions?: object;
/**
* The location of the document being processed *(This property is only
* useful when resolving references as it will be used to locate relative references found within the document being
* resolved. If this value is relative, {@link https://github.com/whitlockjc/path-loader|path-loader} will use
* `window.location.href` for the browser and `process.cwd()` for Node.js.)*
*/
location?: string;
/**
* The callback used to pre-process a JSON Reference like
* object *(This is called prior to validating the JSON Reference like object and getting its details)*
*/
refPreProcessor?: RefPreProcessor;
/**
* The callback used to post-process the JSON Reference
* metadata *(This is called prior filtering the references)*
*/
refPostProcessor?: RefPostProcessor;
/**
* Whether to resolve circular references
*/
resolveCirculars?: boolean;
/**
* The JSON Pointer or array of path segments to the sub document
* location to search from
*/
subDocPath?: string | string[];
}
/**
* Simple function used to filter out JSON References.
* @param refDetails - The JSON Reference details to test
* @param path - The path to the JSON Reference
* @returns whether the JSON Reference should be filtered *(out)* or not
*/
export type RefDetailsFilter = (refDetails: UnresolvedRefDetails, path: string[])=>boolean;
/**
* Simple function used to pre-process a JSON Reference like object.
* @param obj - The JSON Reference like object
* @param path - The path to the JSON Reference like object
* @returns the processed JSON Reference like object
*/
export type RefPreProcessor = (obj: object, path: string[])=>object;
/**
* Simple function used to post-process a JSON Reference details.
* @param refDetails - The JSON Reference details to test
* @param path - The path to the JSON Reference
* @returns the processed JSON Reference details object
*/
export type RefPostProcessor = (refDetails: UnresolvedRefDetails, path: string[])=>object;
/**
* Detailed information about resolved JSON References.
*/
interface ResolvedRefDetails {
/**
* Whether or not the JSON Reference is circular *(Will not be set if the JSON
* Reference is not circular)*
*/
circular?: boolean;
/**
* The fully-qualified version of the `uri` property for
* {@link UnresolvedRefDetails} but with the value being relative to the root document
*/
fqURI: string;
/**
* Whether or not the referenced value was missing or not *(Will not be set if the
* referenced value is not missing)*
*/
missing?: boolean;
/**
* The referenced value *(Will not be set if the referenced value is missing)*
*/
value?: any;
}
/**
* The results of resolving the JSON References of an array/object.
*/
interface ResolvedRefsResults {
/**
* An object whose keys are JSON Pointers *(fragment version)*
* to where the JSON Reference is defined and whose values are {@link ResolvedRefDetails}
*/
refs: ResolvedRefDetails;
/**
* The array/object with its JSON References fully resolved
*/
resolved: object;
}
/**
* An object containing the retrieved document and detailed information about its JSON References.
*/
interface RetrievedRefsResults {
/**
* The retrieved document
*/
value: object;
}
/**
* An object containing the retrieved document, the document with its references resolved and detailed information
* about its JSON References.
*/
interface RetrievedResolvedRefsResults {
/**
* An object whose keys are JSON Pointers *(fragment version)*
* to where the JSON Reference is defined and whose values are {@link UnresolvedRefDetails}
*/
refs: UnresolvedRefDetails;
/**
* The array/object with its JSON References fully resolved
*/
resolved: object;
/**
* The retrieved document
*/
value: object;
}
/**
* Detailed information about unresolved JSON References.
*/
interface UnresolvedRefDetails {
/**
* The JSON Reference definition
*/
def: object;
/**
* The error information for invalid JSON Reference definition *(Only present when the
* JSON Reference definition is invalid or there was a problem retrieving a remote reference during resolution)*
*/
error?: string;
/**
* The URI portion of the JSON Reference
*/
uri: string;
/**
* Detailed information about the URI as provided by
* {@link https://github.com/garycourt/uri-js|URI.parse}.
*/
uriDetails: object;
/**
* The JSON Reference type *(This value can be one of the following: `invalid`, `local`,
* `relative` or `remote`.)*
*/
type: string;
/**
* The warning information *(Only present when the JSON Reference definition produces a
* warning)*
*/
warning?: string;
}
}