Skip to content

fix: allow passing fetch options to request #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,21 @@ export class $RefParser {
*/
public async bundle({
arrayBuffer,
fetch,
pathOrUrlOrSchema,
resolvedInput,
}: {
arrayBuffer?: ArrayBuffer;
fetch?: RequestInit;
pathOrUrlOrSchema: JSONSchema | string | unknown;
resolvedInput?: ResolvedInput;
}): Promise<JSONSchema> {
await this.parse({ arrayBuffer, pathOrUrlOrSchema, resolvedInput });
await this.parse({
arrayBuffer,
fetch,
pathOrUrlOrSchema,
resolvedInput,
});
await resolveExternal(this, this.options);
const errors = JSONParserErrorGroup.getParserErrors(this);
if (errors.length > 0) {
Expand All @@ -125,11 +132,16 @@ export class $RefParser {
* @param pathOrUrlOrSchema A JSON Schema object, or the file path or URL of a JSON Schema file.
*/
public async dereference({
fetch,
pathOrUrlOrSchema,
}: {
fetch?: RequestInit;
pathOrUrlOrSchema: JSONSchema | string | unknown;
}): Promise<JSONSchema> {
await this.parse({ pathOrUrlOrSchema });
await this.parse({
fetch,
pathOrUrlOrSchema,
});
await resolveExternal(this, this.options);
const errors = JSONParserErrorGroup.getParserErrors(this);
if (errors.length > 0) {
Expand All @@ -153,10 +165,12 @@ export class $RefParser {
*/
public async parse({
arrayBuffer,
fetch,
pathOrUrlOrSchema,
resolvedInput: _resolvedInput,
}: {
arrayBuffer?: ArrayBuffer;
fetch?: RequestInit;
pathOrUrlOrSchema: JSONSchema | string | unknown;
resolvedInput?: ResolvedInput;
}): Promise<{ schema: JSONSchema }> {
Expand All @@ -182,7 +196,11 @@ export class $RefParser {
$refAdded.pathType = type;
try {
const resolver = type === 'file' ? fileResolver : urlResolver;
await resolver.handler(file, arrayBuffer);
await resolver.handler({
arrayBuffer,
fetch,
file,
});
const parseResult = await parseFile(file, this.options);
$refAdded.value = parseResult.result;
schema = parseResult.result;
Expand Down
2 changes: 1 addition & 1 deletion lib/resolve-external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ async function resolve$Ref<S extends object = JSONSchema>(

if (resolvedInput.type !== 'json') {
const resolver = resolvedInput.type === 'file' ? fileResolver : urlResolver;
await resolver.handler(file);
await resolver.handler({ file });
const parseResult = await parseFile(file, options);
$refAdded.value = parseResult.result;
promises = crawl(parseResult.result, `${withoutHash}#`, $refs, options, new Set(), true);
Expand Down
6 changes: 5 additions & 1 deletion lib/resolvers/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { ResolverError } from "../util/errors.js";
import type { FileInfo } from "../types/index.js";

export const fileResolver = {
handler: async (file: FileInfo): Promise<void> => {
handler: async ({
file,
}: {
file: FileInfo;
}): Promise<void> => {
let path: string | undefined;

try {
Expand Down
29 changes: 19 additions & 10 deletions lib/resolvers/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { ResolverError } from "../util/errors.js";
import type { FileInfo } from "../types/index.js";

export const sendRequest = async ({
init,
fetchOptions,
redirects = [],
timeout = 60_000,
url,
}: {
init?: RequestInit;
fetchOptions?: RequestInit;
redirects?: string[];
timeout?: number;
url: URL | string;
}): Promise<{
init?: RequestInit;
fetchOptions?: RequestInit;
response: Response;
}> => {
url = new URL(url);
Expand All @@ -26,7 +26,7 @@ export const sendRequest = async ({
}, timeout);
const response = await fetch(url, {
signal: controller.signal,
...init,
...fetchOptions,
});
clearTimeout(timeoutId);

Expand All @@ -45,32 +45,41 @@ export const sendRequest = async ({
}

return sendRequest({
init,
fetchOptions,
redirects,
timeout,
url: resolve(url.href, response.headers.location as string),
});
}

return { init, response };
return { fetchOptions, response };
}

export const urlResolver = {
handler: async (file: FileInfo, arrayBuffer?: ArrayBuffer): Promise<void> => {
handler: async ({
arrayBuffer,
fetch: _fetch,
file,
}: {
arrayBuffer?: ArrayBuffer;
fetch?: RequestInit;
file: FileInfo;
}): Promise<void> => {
let data = arrayBuffer;

if (!data) {
try {
const { init, response } = await sendRequest({
init: {
const { fetchOptions, response } = await sendRequest({
fetchOptions: {
method: 'GET',
..._fetch,
},
url: file.url,
});

if (response.status >= 400) {
// gracefully handle HEAD method not allowed
if (response.status !== 405 || init?.method !== 'HEAD') {
if (response.status !== 405 || fetchOptions?.method !== 'HEAD') {
throw ono({ status: response.status }, `HTTP ERROR ${response.status}`);
}

Expand Down