Skip to content

Commit 24d46b4

Browse files
- Removed unecessary redundancy
- Added definitions for static methods - Removed some invalid method signatures
1 parent bc385b7 commit 24d46b4

1 file changed

Lines changed: 103 additions & 21 deletions

File tree

lib/index.d.ts

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ declare class $RefParser {
1414
*
1515
* See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/ref-parser.md#schema
1616
*/
17-
schema: JSONSchema4 | JSONSchema6
17+
schema: $RefParser.JSONSchema
1818

1919
/**
2020
* The $refs property is a `$Refs` object, which lets you access all of the externally-referenced files in the schema, as well as easily get and set specific values in the schema using JSON pointers.
@@ -36,9 +36,30 @@ declare class $RefParser {
3636
* @param options (optional)
3737
* @param callback (optional) A callback that will receive the dereferenced schema object
3838
*/
39-
dereference(path: string, schema: string | JSONSchema4 | JSONSchema6, options?: $RefParser.Options, callback?: (err: Error | null, schema: JSONSchema4 | JSONSchema6 | null) => any): Promise<JSONSchema4 | JSONSchema6>
40-
dereference(path: string, options?: $RefParser.Options, callback?: (err: Error | null, schema: JSONSchema4 | JSONSchema6 | null) => any): Promise<JSONSchema4 | JSONSchema6>
41-
dereference(schema: JSONSchema4 | JSONSchema6, options?: $RefParser.Options, callback?: (err: Error | null, schema: JSONSchema4 | JSONSchema6 | null) => any): Promise<JSONSchema4 | JSONSchema6>
39+
dereference(schema: string | $RefParser.JSONSchema, callback: $RefParser.SchemaCallback): void;
40+
dereference(schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.SchemaCallback): void;
41+
dereference(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.SchemaCallback): void;
42+
dereference(schema: string | $RefParser.JSONSchema): Promise<$RefParser.JSONSchema>;
43+
dereference(schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.JSONSchema>;
44+
dereference(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.JSONSchema>;
45+
46+
/**
47+
* Dereferences all `$ref` pointers in the JSON Schema, replacing each reference with its resolved value. This results in a schema object that does not contain any `$ref` pointers. Instead, it's a normal JavaScript object tree that can easily be crawled and used just like any other JavaScript object. This is great for programmatic usage, especially when using tools that don't understand JSON references.
48+
*
49+
* The dereference method maintains object reference equality, meaning that all `$ref` pointers that point to the same object will be replaced with references to the same object. Again, this is great for programmatic usage, but it does introduce the risk of circular references, so be careful if you intend to serialize the schema using `JSON.stringify()`. Consider using the bundle method instead, which does not create circular references.
50+
*
51+
* See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/ref-parser.md#dereferenceschema-options-callback
52+
*
53+
* @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
54+
* @param options (optional)
55+
* @param callback (optional) A callback that will receive the dereferenced schema object
56+
*/
57+
static dereference(schema: string | $RefParser.JSONSchema, callback: $RefParser.SchemaCallback): void;
58+
static dereference(schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.SchemaCallback): void;
59+
static dereference(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.SchemaCallback): void;
60+
static dereference(schema: string | $RefParser.JSONSchema): Promise<$RefParser.JSONSchema>;
61+
static dereference(schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.JSONSchema>;
62+
static dereference(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.JSONSchema>;
4263

4364
/**
4465
* Bundles all referenced files/URLs into a single schema that only has internal `$ref` pointers. This lets you split-up your schema however you want while you're building it, but easily combine all those files together when it's time to package or distribute the schema to other people. The resulting schema size will be small, since it will still contain internal JSON references rather than being fully-dereferenced.
@@ -51,11 +72,30 @@ declare class $RefParser {
5172
* @param options (optional)
5273
* @param callback (optional) A callback that will receive the bundled schema object
5374
*/
54-
bundle(
55-
schema: string | JSONSchema4 | JSONSchema6,
56-
options?: $RefParser.Options,
57-
callback?: (err: Error | null, schema: JSONSchema4 | JSONSchema6 | null) => any
58-
): Promise<JSONSchema4 | JSONSchema6>
75+
bundle(schema: string | $RefParser.JSONSchema, callback: $RefParser.SchemaCallback): void;
76+
bundle(schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.SchemaCallback): void;
77+
bundle(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.SchemaCallback): void;
78+
bundle(schema: string | $RefParser.JSONSchema): Promise<$RefParser.JSONSchema>;
79+
bundle(schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.JSONSchema>;
80+
bundle(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.JSONSchema>;
81+
82+
/**
83+
* Bundles all referenced files/URLs into a single schema that only has internal `$ref` pointers. This lets you split-up your schema however you want while you're building it, but easily combine all those files together when it's time to package or distribute the schema to other people. The resulting schema size will be small, since it will still contain internal JSON references rather than being fully-dereferenced.
84+
*
85+
* This also eliminates the risk of circular references, so the schema can be safely serialized using `JSON.stringify()`.
86+
*
87+
* See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/ref-parser.md#bundleschema-options-callback
88+
*
89+
* @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
90+
* @param options (optional)
91+
* @param callback (optional) A callback that will receive the bundled schema object
92+
*/
93+
static bundle(schema: string | $RefParser.JSONSchema, callback: $RefParser.SchemaCallback): void;
94+
static bundle(schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.SchemaCallback): void;
95+
static bundle(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.SchemaCallback): void;
96+
static bundle(schema: string | $RefParser.JSONSchema): Promise<$RefParser.JSONSchema>;
97+
static bundle(schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.JSONSchema>;
98+
static bundle(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.JSONSchema>;
5999

60100
/**
61101
* *This method is used internally by other methods, such as `bundle` and `dereference`. You probably won't need to call this method yourself.*
@@ -68,11 +108,48 @@ declare class $RefParser {
68108
* @param options (optional)
69109
* @param callback (optional) A callback that will receive the parsed schema object, or an error
70110
*/
71-
parse(
72-
schema: string | JSONSchema4 | JSONSchema6,
73-
options?: $RefParser.Options,
74-
callback?: (err: Error | null, schema: JSONSchema4 | JSONSchema6 | null) => any
75-
): Promise<JSONSchema4 | JSONSchema6>
111+
parse(schema: string | $RefParser.JSONSchema, callback: $RefParser.SchemaCallback): void;
112+
parse(schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.SchemaCallback): void;
113+
parse(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.SchemaCallback): void;
114+
parse(schema: string | $RefParser.JSONSchema): Promise<$RefParser.JSONSchema>;
115+
parse(schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.JSONSchema>;
116+
parse(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.JSONSchema>;
117+
118+
/**
119+
* *This method is used internally by other methods, such as `bundle` and `dereference`. You probably won't need to call this method yourself.*
120+
*
121+
* Parses the given JSON Schema file (in JSON or YAML format), and returns it as a JavaScript object. This method `does not` resolve `$ref` pointers or dereference anything. It simply parses one file and returns it.
122+
*
123+
* See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/ref-parser.md#parseschema-options-callback
124+
*
125+
* @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. The path can be absolute or relative. In Node, the path is relative to `process.cwd()`. In the browser, it's relative to the URL of the page.
126+
* @param options (optional)
127+
* @param callback (optional) A callback that will receive the parsed schema object, or an error
128+
*/
129+
static parse(schema: string | $RefParser.JSONSchema, callback: $RefParser.SchemaCallback): void;
130+
static parse(schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.SchemaCallback): void;
131+
static parse(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.SchemaCallback): void;
132+
static parse(schema: string | $RefParser.JSONSchema): Promise<$RefParser.JSONSchema>;
133+
static parse(schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.JSONSchema>;
134+
static parse(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.JSONSchema>;
135+
136+
/**
137+
* *This method is used internally by other methods, such as `bundle` and `dereference`. You probably won't need to call this method yourself.*
138+
*
139+
* Resolves all JSON references (`$ref` pointers) in the given JSON Schema file. If it references any other files/URLs, then they will be downloaded and resolved as well. This method **does not** dereference anything. It simply gives you a `$Refs` object, which is a map of all the resolved references and their values.
140+
*
141+
* See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/ref-parser.md#resolveschema-options-callback
142+
*
143+
* @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
144+
* @param options (optional)
145+
* @param callback (optional) A callback that will receive a `$Refs` object
146+
*/
147+
resolve(schema: string | $RefParser.JSONSchema, callback: $RefParser.$RefsCallback): void;
148+
resolve(schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.$RefsCallback): void;
149+
resolve(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.$RefsCallback): void;
150+
resolve(schema: string | $RefParser.JSONSchema): Promise<$RefParser.$Refs>;
151+
resolve(schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.$Refs>;
152+
resolve(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.$Refs>;
76153

77154
/**
78155
* *This method is used internally by other methods, such as `bundle` and `dereference`. You probably won't need to call this method yourself.*
@@ -85,19 +162,24 @@ declare class $RefParser {
85162
* @param options (optional)
86163
* @param callback (optional) A callback that will receive a `$Refs` object
87164
*/
88-
resolve(
89-
schema: string | JSONSchema4 | JSONSchema6,
90-
options?: $RefParser.Options,
91-
callback?: (err: Error | null, $refs: $RefParser.$Refs | null) => any
92-
): Promise<$RefParser.$Refs>
165+
static resolve(schema: string | $RefParser.JSONSchema, callback: $RefParser.$RefsCallback): void;
166+
static resolve(schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.$RefsCallback): void;
167+
static resolve(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options, callback: $RefParser.$RefsCallback): void;
168+
static resolve(schema: string | $RefParser.JSONSchema): Promise<$RefParser.$Refs>;
169+
static resolve(schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.$Refs>;
170+
static resolve(baseUrl: string, schema: string | $RefParser.JSONSchema, options: $RefParser.Options): Promise<$RefParser.$Refs>;
93171
}
94172

95173
declare namespace $RefParser {
96174

175+
export type JSONSchema = JSONSchema4 | JSONSchema6;
176+
export type SchemaCallback = (err: Error | null, schema?: JSONSchema) => any;
177+
export type $RefsCallback = (err: Error | null, $refs?: $Refs) => any;
178+
97179
/**
98180
* See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/options.md
99181
*/
100-
export type Options = object & {
182+
export type Options = {
101183

102184
/**
103185
* The `parse` options determine how different types of files will be parsed.
@@ -274,7 +356,7 @@ declare namespace $RefParser {
274356
*
275357
* @param types (optional) Optionally only return values from certain locations ("file", "http", etc.)
276358
*/
277-
values(...types: string[]): { [url: string]: JSONSchema4 | JSONSchema6 }
359+
values(...types: string[]): { [url: string]: $RefParser.JSONSchema }
278360

279361
/**
280362
* Returns `true` if the given path exists in the schema; otherwise, returns `false`

0 commit comments

Comments
 (0)