Skip to content

Commit dbde5d1

Browse files
authored
Merge pull request #7 from hey-api/fix/fetch-options
fix: allow passing fetch options to request
2 parents 92dbaac + 5e5ccf1 commit dbde5d1

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

Diff for: lib/index.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,21 @@ export class $RefParser {
9494
*/
9595
public async bundle({
9696
arrayBuffer,
97+
fetch,
9798
pathOrUrlOrSchema,
9899
resolvedInput,
99100
}: {
100101
arrayBuffer?: ArrayBuffer;
102+
fetch?: RequestInit;
101103
pathOrUrlOrSchema: JSONSchema | string | unknown;
102104
resolvedInput?: ResolvedInput;
103105
}): Promise<JSONSchema> {
104-
await this.parse({ arrayBuffer, pathOrUrlOrSchema, resolvedInput });
106+
await this.parse({
107+
arrayBuffer,
108+
fetch,
109+
pathOrUrlOrSchema,
110+
resolvedInput,
111+
});
105112
await resolveExternal(this, this.options);
106113
const errors = JSONParserErrorGroup.getParserErrors(this);
107114
if (errors.length > 0) {
@@ -125,11 +132,16 @@ export class $RefParser {
125132
* @param pathOrUrlOrSchema A JSON Schema object, or the file path or URL of a JSON Schema file.
126133
*/
127134
public async dereference({
135+
fetch,
128136
pathOrUrlOrSchema,
129137
}: {
138+
fetch?: RequestInit;
130139
pathOrUrlOrSchema: JSONSchema | string | unknown;
131140
}): Promise<JSONSchema> {
132-
await this.parse({ pathOrUrlOrSchema });
141+
await this.parse({
142+
fetch,
143+
pathOrUrlOrSchema,
144+
});
133145
await resolveExternal(this, this.options);
134146
const errors = JSONParserErrorGroup.getParserErrors(this);
135147
if (errors.length > 0) {
@@ -153,10 +165,12 @@ export class $RefParser {
153165
*/
154166
public async parse({
155167
arrayBuffer,
168+
fetch,
156169
pathOrUrlOrSchema,
157170
resolvedInput: _resolvedInput,
158171
}: {
159172
arrayBuffer?: ArrayBuffer;
173+
fetch?: RequestInit;
160174
pathOrUrlOrSchema: JSONSchema | string | unknown;
161175
resolvedInput?: ResolvedInput;
162176
}): Promise<{ schema: JSONSchema }> {
@@ -182,7 +196,11 @@ export class $RefParser {
182196
$refAdded.pathType = type;
183197
try {
184198
const resolver = type === 'file' ? fileResolver : urlResolver;
185-
await resolver.handler(file, arrayBuffer);
199+
await resolver.handler({
200+
arrayBuffer,
201+
fetch,
202+
file,
203+
});
186204
const parseResult = await parseFile(file, this.options);
187205
$refAdded.value = parseResult.result;
188206
schema = parseResult.result;

Diff for: lib/resolve-external.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ async function resolve$Ref<S extends object = JSONSchema>(
124124

125125
if (resolvedInput.type !== 'json') {
126126
const resolver = resolvedInput.type === 'file' ? fileResolver : urlResolver;
127-
await resolver.handler(file);
127+
await resolver.handler({ file });
128128
const parseResult = await parseFile(file, options);
129129
$refAdded.value = parseResult.result;
130130
promises = crawl(parseResult.result, `${withoutHash}#`, $refs, options, new Set(), true);

Diff for: lib/resolvers/file.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { ResolverError } from "../util/errors.js";
55
import type { FileInfo } from "../types/index.js";
66

77
export const fileResolver = {
8-
handler: async (file: FileInfo): Promise<void> => {
8+
handler: async ({
9+
file,
10+
}: {
11+
file: FileInfo;
12+
}): Promise<void> => {
913
let path: string | undefined;
1014

1115
try {

Diff for: lib/resolvers/url.ts

+19-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import { ResolverError } from "../util/errors.js";
44
import type { FileInfo } from "../types/index.js";
55

66
export const sendRequest = async ({
7-
init,
7+
fetchOptions,
88
redirects = [],
99
timeout = 60_000,
1010
url,
1111
}: {
12-
init?: RequestInit;
12+
fetchOptions?: RequestInit;
1313
redirects?: string[];
1414
timeout?: number;
1515
url: URL | string;
1616
}): Promise<{
17-
init?: RequestInit;
17+
fetchOptions?: RequestInit;
1818
response: Response;
1919
}> => {
2020
url = new URL(url);
@@ -26,7 +26,7 @@ export const sendRequest = async ({
2626
}, timeout);
2727
const response = await fetch(url, {
2828
signal: controller.signal,
29-
...init,
29+
...fetchOptions,
3030
});
3131
clearTimeout(timeoutId);
3232

@@ -45,32 +45,41 @@ export const sendRequest = async ({
4545
}
4646

4747
return sendRequest({
48-
init,
48+
fetchOptions,
4949
redirects,
5050
timeout,
5151
url: resolve(url.href, response.headers.location as string),
5252
});
5353
}
5454

55-
return { init, response };
55+
return { fetchOptions, response };
5656
}
5757

5858
export const urlResolver = {
59-
handler: async (file: FileInfo, arrayBuffer?: ArrayBuffer): Promise<void> => {
59+
handler: async ({
60+
arrayBuffer,
61+
fetch: _fetch,
62+
file,
63+
}: {
64+
arrayBuffer?: ArrayBuffer;
65+
fetch?: RequestInit;
66+
file: FileInfo;
67+
}): Promise<void> => {
6068
let data = arrayBuffer;
6169

6270
if (!data) {
6371
try {
64-
const { init, response } = await sendRequest({
65-
init: {
72+
const { fetchOptions, response } = await sendRequest({
73+
fetchOptions: {
6674
method: 'GET',
75+
..._fetch,
6776
},
6877
url: file.url,
6978
});
7079

7180
if (response.status >= 400) {
7281
// gracefully handle HEAD method not allowed
73-
if (response.status !== 405 || init?.method !== 'HEAD') {
82+
if (response.status !== 405 || fetchOptions?.method !== 'HEAD') {
7483
throw ono({ status: response.status }, `HTTP ERROR ${response.status}`);
7584
}
7685

0 commit comments

Comments
 (0)