@hey-api/json-schema-ref-parser 0.0.2 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { FileInfo } from "../types/index.js";
|
|
2
|
-
export declare const sendRequest: ({ init, redirects, url, }: {
|
|
2
|
+
export declare const sendRequest: ({ init, redirects, timeout, url, }: {
|
|
3
3
|
init?: RequestInit;
|
|
4
4
|
redirects?: string[];
|
|
5
|
+
timeout?: number;
|
|
5
6
|
url: URL | string;
|
|
6
7
|
}) => Promise<{
|
|
7
8
|
response: Response;
|
|
@@ -4,20 +4,24 @@ exports.urlResolver = exports.sendRequest = void 0;
|
|
|
4
4
|
const ono_1 = require("@jsdevtools/ono");
|
|
5
5
|
const url_js_1 = require("../util/url.js");
|
|
6
6
|
const errors_js_1 = require("../util/errors.js");
|
|
7
|
-
const sendRequest = async ({ init, redirects = [], url, }) => {
|
|
7
|
+
const sendRequest = async ({ init, redirects = [], timeout = 60000, url, }) => {
|
|
8
8
|
url = new URL(url);
|
|
9
9
|
redirects.push(url.href);
|
|
10
10
|
try {
|
|
11
11
|
const controller = new AbortController();
|
|
12
12
|
const timeoutId = setTimeout(() => {
|
|
13
13
|
controller.abort();
|
|
14
|
-
},
|
|
14
|
+
}, timeout);
|
|
15
15
|
const response = await fetch(url, {
|
|
16
16
|
signal: controller.signal,
|
|
17
17
|
...init,
|
|
18
18
|
});
|
|
19
19
|
clearTimeout(timeoutId);
|
|
20
20
|
if (response.status >= 400) {
|
|
21
|
+
// gracefully handle HEAD method not allowed
|
|
22
|
+
if (response.status === 405 && init?.method === 'HEAD') {
|
|
23
|
+
return { response };
|
|
24
|
+
}
|
|
21
25
|
throw (0, ono_1.ono)({ status: response.status }, `HTTP ERROR ${response.status}`);
|
|
22
26
|
}
|
|
23
27
|
if (response.status >= 300) {
|
|
@@ -30,6 +34,7 @@ const sendRequest = async ({ init, redirects = [], url, }) => {
|
|
|
30
34
|
return (0, exports.sendRequest)({
|
|
31
35
|
init,
|
|
32
36
|
redirects,
|
|
37
|
+
timeout,
|
|
33
38
|
url: (0, url_js_1.resolve)(url.href, response.headers.location),
|
|
34
39
|
});
|
|
35
40
|
}
|
package/lib/resolvers/url.ts
CHANGED
|
@@ -6,10 +6,12 @@ import type { FileInfo } from "../types/index.js";
|
|
|
6
6
|
export const sendRequest = async ({
|
|
7
7
|
init,
|
|
8
8
|
redirects = [],
|
|
9
|
+
timeout = 60_000,
|
|
9
10
|
url,
|
|
10
11
|
}: {
|
|
11
12
|
init?: RequestInit;
|
|
12
13
|
redirects?: string[];
|
|
14
|
+
timeout?: number;
|
|
13
15
|
url: URL | string;
|
|
14
16
|
}): Promise<{
|
|
15
17
|
response: Response;
|
|
@@ -21,7 +23,7 @@ export const sendRequest = async ({
|
|
|
21
23
|
const controller = new AbortController();
|
|
22
24
|
const timeoutId = setTimeout(() => {
|
|
23
25
|
controller.abort();
|
|
24
|
-
},
|
|
26
|
+
}, timeout);
|
|
25
27
|
const response = await fetch(url, {
|
|
26
28
|
signal: controller.signal,
|
|
27
29
|
...init,
|
|
@@ -29,6 +31,11 @@ export const sendRequest = async ({
|
|
|
29
31
|
clearTimeout(timeoutId);
|
|
30
32
|
|
|
31
33
|
if (response.status >= 400) {
|
|
34
|
+
// gracefully handle HEAD method not allowed
|
|
35
|
+
if (response.status === 405 && init?.method === 'HEAD') {
|
|
36
|
+
return { response };
|
|
37
|
+
}
|
|
38
|
+
|
|
32
39
|
throw ono({ status: response.status }, `HTTP ERROR ${response.status}`);
|
|
33
40
|
}
|
|
34
41
|
|
|
@@ -49,6 +56,7 @@ export const sendRequest = async ({
|
|
|
49
56
|
return sendRequest({
|
|
50
57
|
init,
|
|
51
58
|
redirects,
|
|
59
|
+
timeout,
|
|
52
60
|
url: resolve(url.href, response.headers.location as string),
|
|
53
61
|
});
|
|
54
62
|
}
|