Skip to content

Commit

Permalink
Merge pull request #4 from hey-api/fix/request-timeout
Browse files Browse the repository at this point in the history
fix: allow passing timeout value to sendRequest
  • Loading branch information
mrlubos authored Feb 3, 2025
2 parents 4743034 + e5c19c0 commit 1a3f1c0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
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

0 comments on commit 1a3f1c0

Please sign in to comment.