Skip to content

fix: allow passing timeout value to sendRequest #4

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 2 commits into from
Feb 3, 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
10 changes: 9 additions & 1 deletion lib/resolvers/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import type { FileInfo } from "../types/index.js";
export const sendRequest = async ({
init,
redirects = [],
timeout = 60_000,
url,
}: {
init?: RequestInit;
redirects?: string[];
timeout?: number;
url: URL | string;
}): Promise<{
response: Response;
Expand All @@ -21,14 +23,19 @@ export const sendRequest = async ({
const controller = new AbortController();
const timeoutId = setTimeout(() => {
controller.abort();
}, 60_000);
}, timeout);
const response = await fetch(url, {
signal: controller.signal,
...init,
});
clearTimeout(timeoutId);

if (response.status >= 400) {
// gracefully handle HEAD method not allowed
if (response.status === 405 && init?.method === 'HEAD') {
return { response };
}

throw ono({ status: response.status }, `HTTP ERROR ${response.status}`);
}

Expand All @@ -49,6 +56,7 @@ export const sendRequest = async ({
return sendRequest({
init,
redirects,
timeout,
url: resolve(url.href, response.headers.location as string),
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hey-api/json-schema-ref-parser",
"version": "0.0.2",
"version": "0.0.3",
"description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
"homepage": "https://heyapi.dev/",
"repository": {
Expand Down