Skip to content

Commit 482dff1

Browse files
committed
fix(types): fix types of options, inheritance globally
1 parent a3d1801 commit 482dff1

13 files changed

+143
-127
lines changed

lib/bundle.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { JSONSchema } from "./index";
1414
* @param parser
1515
* @param options
1616
*/
17-
function bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
17+
function bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
1818
parser: $RefParser<S, O>,
1919
options: O,
2020
) {
@@ -40,14 +40,14 @@ function bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = Par
4040
* @param $refs
4141
* @param options
4242
*/
43-
function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
43+
function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
4444
parent: any,
4545
key: string | null,
4646
path: string,
4747
pathFromRoot: string,
4848
indirections: number,
4949
inventory: unknown[],
50-
$refs: $Refs<S>,
50+
$refs: $Refs<S, O>,
5151
options: O,
5252
) {
5353
const obj = key === null ? parent : parent[key];
@@ -102,14 +102,14 @@ function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions = Pars
102102
* @param $refs
103103
* @param options
104104
*/
105-
function inventory$Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
105+
function inventory$Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
106106
$refParent: any,
107107
$refKey: any,
108108
path: string,
109109
pathFromRoot: any,
110110
indirections: any,
111111
inventory: any,
112-
$refs: $Refs<S>,
112+
$refs: $Refs<S, O>,
113113
options: O,
114114
) {
115115
const $ref = $refKey === null ? $refParent : $refParent[$refKey];

lib/dereference.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default dereference;
1616
* @param parser
1717
* @param options
1818
*/
19-
function dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
19+
function dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
2020
parser: $RefParser<S, O>,
2121
options: O,
2222
) {
@@ -48,14 +48,14 @@ function dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions
4848
* @param options
4949
* @returns
5050
*/
51-
function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
51+
function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
5252
obj: any,
5353
path: string,
5454
pathFromRoot: string,
5555
parents: Set<any>,
5656
processedObjects: Set<any>,
5757
dereferencedCache: any,
58-
$refs: $Refs<S>,
58+
$refs: $Refs<S, O>,
5959
options: O,
6060
) {
6161
let dereferenced;
@@ -161,14 +161,14 @@ function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions = Pars
161161
* @param options
162162
* @returns
163163
*/
164-
function dereference$Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
164+
function dereference$Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
165165
$ref: any,
166166
path: string,
167167
pathFromRoot: string,
168168
parents: Set<any>,
169169
processedObjects: any,
170170
dereferencedCache: any,
171-
$refs: $Refs<S>,
171+
$refs: $Refs<S, O>,
172172
options: O,
173173
) {
174174
const isExternalRef = $Ref.isExternal$Ref($ref);

lib/index.ts

+46-42
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export type RefParserSchema = string | JSONSchema;
3737
*
3838
* @class
3939
*/
40-
export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions> {
40+
export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
4141
/**
4242
* The parsed (and possibly dereferenced) JSON schema object
4343
*
@@ -52,7 +52,7 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
5252
* @type {$Refs}
5353
* @readonly
5454
*/
55-
$refs = new $Refs<S>();
55+
$refs = new $Refs<S, O>();
5656

5757
/**
5858
* Parses the given JSON schema.
@@ -143,32 +143,34 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
143143
}
144144
}
145145

146-
public static parse<S extends JSONSchema = JSONSchema>(schema: S | string): Promise<S>;
147-
public static parse<S extends JSONSchema = JSONSchema>(
146+
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
147+
schema: S | string,
148+
): Promise<S>;
149+
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
148150
schema: S | string,
149151
callback: SchemaCallback<S>,
150152
): Promise<void>;
151-
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
153+
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
152154
schema: S | string,
153155
options: O,
154156
): Promise<S>;
155-
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
157+
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
156158
schema: S | string,
157159
options: O,
158160
callback: SchemaCallback<S>,
159161
): Promise<void>;
160-
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
162+
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
161163
baseUrl: string,
162164
schema: S | string,
163165
options: O,
164166
): Promise<S>;
165-
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
167+
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
166168
baseUrl: string,
167169
schema: S | string,
168170
options: O,
169171
callback: SchemaCallback<S>,
170172
): Promise<void>;
171-
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>():
173+
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
172174
| Promise<S>
173175
| Promise<void> {
174176
const parser = new $RefParser<S, O>();
@@ -186,12 +188,12 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
186188
* @param options (optional)
187189
* @param callback (optional) A callback that will receive a `$Refs` object
188190
*/
189-
public resolve(schema: S | string): Promise<$Refs<S>>;
190-
public resolve(schema: S | string, callback: $RefsCallback<S>): Promise<void>;
191-
public resolve(schema: S | string, options: O): Promise<$Refs<S>>;
192-
public resolve(schema: S | string, options: O, callback: $RefsCallback<S>): Promise<void>;
193-
public resolve(baseUrl: string, schema: S | string, options: O): Promise<$Refs<S>>;
194-
public resolve(baseUrl: string, schema: S | string, options: O, callback: $RefsCallback<S>): Promise<void>;
191+
public resolve(schema: S | string): Promise<$Refs<S, O>>;
192+
public resolve(schema: S | string, callback: $RefsCallback<S, O>): Promise<void>;
193+
public resolve(schema: S | string, options: O): Promise<$Refs<S, O>>;
194+
public resolve(schema: S | string, options: O, callback: $RefsCallback<S, O>): Promise<void>;
195+
public resolve(baseUrl: string, schema: S | string, options: O): Promise<$Refs<S, O>>;
196+
public resolve(baseUrl: string, schema: S | string, options: O, callback: $RefsCallback<S, O>): Promise<void>;
195197
async resolve() {
196198
const args = normalizeArgs<S, O>(arguments);
197199

@@ -216,32 +218,34 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
216218
* @param options (optional)
217219
* @param callback (optional) A callback that will receive a `$Refs` object
218220
*/
219-
public static resolve<S extends JSONSchema = JSONSchema>(schema: S | string): Promise<$Refs<S>>;
220-
public static resolve<S extends JSONSchema = JSONSchema>(
221+
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
222+
schema: S | string,
223+
): Promise<$Refs<S, O>>;
224+
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
221225
schema: S | string,
222-
callback: $RefsCallback<S>,
226+
callback: $RefsCallback<S, O>,
223227
): Promise<void>;
224-
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
228+
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
225229
schema: S | string,
226230
options: O,
227-
): Promise<$Refs<S>>;
228-
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
231+
): Promise<$Refs<S, O>>;
232+
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
229233
schema: S | string,
230234
options: O,
231-
callback: $RefsCallback<S>,
235+
callback: $RefsCallback<S, O>,
232236
): Promise<void>;
233-
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
237+
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
234238
baseUrl: string,
235239
schema: S | string,
236240
options: O,
237-
): Promise<$Refs<S>>;
238-
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
241+
): Promise<$Refs<S, O>>;
242+
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
239243
baseUrl: string,
240244
schema: S | string,
241245
options: O,
242-
callback: $RefsCallback<S>,
246+
callback: $RefsCallback<S, O>,
243247
): Promise<void>;
244-
static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>():
248+
static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
245249
| Promise<S>
246250
| Promise<void> {
247251
const instance = new $RefParser<S, O>();
@@ -259,34 +263,34 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
259263
* @param options (optional)
260264
* @param callback (optional) A callback that will receive the bundled schema object
261265
*/
262-
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
266+
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
263267
schema: S | string,
264268
): Promise<S>;
265-
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
269+
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
266270
schema: S | string,
267271
callback: SchemaCallback<S>,
268272
): Promise<void>;
269-
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
273+
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
270274
schema: S | string,
271275
options: O,
272276
): Promise<S>;
273-
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
277+
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
274278
schema: S | string,
275279
options: O,
276280
callback: SchemaCallback<S>,
277281
): Promise<void>;
278-
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
282+
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
279283
baseUrl: string,
280284
schema: S | string,
281285
options: O,
282286
): Promise<S>;
283-
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
287+
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
284288
baseUrl: string,
285289
schema: S | string,
286290
options: O,
287291
callback: SchemaCallback<S>,
288292
): Promise<S>;
289-
static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>():
293+
static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
290294
| Promise<S>
291295
| Promise<void> {
292296
const instance = new $RefParser<S, O>();
@@ -333,34 +337,34 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
333337
* @param options (optional)
334338
* @param callback (optional) A callback that will receive the dereferenced schema object
335339
*/
336-
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
340+
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
337341
schema: S | string,
338342
): Promise<S>;
339-
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
343+
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
340344
schema: S | string,
341345
callback: SchemaCallback<S>,
342346
): Promise<void>;
343-
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
347+
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
344348
schema: S | string,
345349
options: O,
346350
): Promise<S>;
347-
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
351+
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
348352
schema: S | string,
349353
options: O,
350354
callback: SchemaCallback<S>,
351355
): Promise<void>;
352-
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
356+
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
353357
baseUrl: string,
354358
schema: S | string,
355359
options: O,
356360
): Promise<S>;
357-
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
361+
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
358362
baseUrl: string,
359363
schema: S | string,
360364
options: O,
361365
callback: SchemaCallback<S>,
362366
): Promise<void>;
363-
static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>():
367+
static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
364368
| Promise<S>
365369
| Promise<void> {
366370
const instance = new $RefParser<S, O>();
@@ -400,7 +404,7 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
400404
}
401405
export default $RefParser;
402406

403-
function finalize<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
407+
function finalize<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
404408
parser: $RefParser<S, O>,
405409
) {
406410
const errors = JSONParserErrorGroup.getParserErrors(parser);

lib/normalize-args.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ import type { JSONSchema, SchemaCallback } from "./types";
44

55
// I really dislike this function and the way it's written. It's not clear what it's doing, and it's way too flexible
66
// In the future, I'd like to deprecate the api and accept only named parameters in index.ts
7-
export interface NormalizedArguments<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions> {
7+
export interface NormalizedArguments<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
88
path: string;
99
schema: S;
10-
options: O & Options;
10+
options: O & Options<S>;
1111
callback: SchemaCallback<S>;
1212
}
1313
/**
1414
* Normalizes the given arguments, accounting for optional args.
1515
*/
16-
export function normalizeArgs<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
16+
export function normalizeArgs<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
1717
_args: Partial<IArguments>,
1818
): NormalizedArguments<S, O> {
1919
let path;
2020
let schema;
21-
let options: Options & O;
21+
let options: Options<S> & O;
2222
let callback;
2323
const args = Array.prototype.slice.call(_args) as any[];
2424

lib/options.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export const getJsonSchemaRefParserDefaultOptions = () => {
174174
return defaults;
175175
};
176176

177-
export const getNewOptions = <S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
177+
export const getNewOptions = <S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
178178
options: O | undefined,
179179
): O & $RefParserOptions<S> => {
180180
const newOptions = getJsonSchemaRefParserDefaultOptions();
@@ -183,8 +183,9 @@ export const getNewOptions = <S extends JSONSchema = JSONSchema, O extends Parse
183183
}
184184
return newOptions as O & $RefParserOptions<S>;
185185
};
186-
export type Options = $RefParserOptions<JSONSchema>;
187-
export type ParserOptions = DeepPartial<$RefParserOptions<JSONSchema>>;
186+
187+
export type Options<S extends JSONSchema = JSONSchema> = $RefParserOptions<S>;
188+
export type ParserOptions<S extends JSONSchema = JSONSchema> = DeepPartial<$RefParserOptions<S>>;
188189
/**
189190
* Merges the properties of the source object into the target object.
190191
*

0 commit comments

Comments
 (0)