vigor-fetch 1.0.6 → 1.0.7

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.
@@ -0,0 +1,76 @@
1
+ interface VigorErrorOptions {
2
+ url?: string | null;
3
+ status?: number;
4
+ message?: string;
5
+ data?: any;
6
+ }
7
+ interface VigorFetchConfig {
8
+ path: string;
9
+ method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | null;
10
+ offset: RequestInit;
11
+ headers: Record<string, string>;
12
+ body: any;
13
+ count: number;
14
+ max: number;
15
+ wait: number;
16
+ backoff: number;
17
+ unretry: Set<number>;
18
+ retryHeader: string[];
19
+ original: boolean;
20
+ parse: keyof Response | null;
21
+ beforeRequest: Array<(opt: RequestInit) => Promise<Partial<RequestInit> | void> | Partial<RequestInit> | void>;
22
+ afterRequest: Array<(res: Response) => Promise<Response | void> | Response | void>;
23
+ beforeResponse: Array<(res: Response) => Promise<Response> | Response>;
24
+ afterResponse: Array<(data: any) => Promise<any> | any>;
25
+ onError: Array<(err: any) => Promise<any> | any>;
26
+ query: Record<string, any>;
27
+ jitter: number;
28
+ }
29
+ declare class VigorFetch<T = any> {
30
+ private _origin;
31
+ private _config;
32
+ constructor(origin: string, config?: VigorFetchConfig);
33
+ private _next;
34
+ path(arg: string): VigorFetch<T>;
35
+ method(arg: VigorFetchConfig['method']): VigorFetch<T>;
36
+ offset(arg: RequestInit): VigorFetch<T>;
37
+ headers(arg: Record<string, string>): VigorFetch<T>;
38
+ body(arg: any): VigorFetch<T>;
39
+ count(arg: number): VigorFetch<T>;
40
+ max(arg: number): VigorFetch<T>;
41
+ wait(arg: number): VigorFetch<T>;
42
+ backoff(arg: number): VigorFetch<T>;
43
+ unretry(arg: number[]): VigorFetch<T>;
44
+ retryHeader(...arg: string[]): VigorFetch<T>;
45
+ original(arg: boolean): VigorFetch<T>;
46
+ parse(arg: keyof Response): VigorFetch<T>;
47
+ query(arg: Record<string, any>): VigorFetch<T>;
48
+ jitter(arg: number): VigorFetch<T>;
49
+ beforeRequest(...arg: VigorFetchConfig['beforeRequest']): VigorFetch<T>;
50
+ afterRequest(...arg: VigorFetchConfig['afterRequest']): VigorFetch<T>;
51
+ beforeResponse(...arg: VigorFetchConfig['beforeResponse']): VigorFetch<T>;
52
+ afterResponse(...arg: VigorFetchConfig['afterResponse']): VigorFetch<T>;
53
+ onError(...arg: VigorFetchConfig['onError']): VigorFetch<T>;
54
+ request(): Promise<T>;
55
+ }
56
+ interface VigorAllConfig {
57
+ limit: number;
58
+ jitter: number;
59
+ promises: Array<() => Promise<any>>;
60
+ }
61
+ declare class VigorAll {
62
+ private _config;
63
+ constructor(config?: VigorAllConfig);
64
+ private _next;
65
+ limit(arg: number): VigorAll;
66
+ jitter(arg: number): VigorAll;
67
+ promises(...args: Array<() => Promise<any>>): VigorAll;
68
+ request(): Promise<any[]>;
69
+ }
70
+ declare class Vigor {
71
+ fetch<T = any>(origin: string, config?: VigorFetchConfig): VigorFetch<T>;
72
+ all(config?: VigorAllConfig): VigorAll;
73
+ }
74
+ declare const vigor: Vigor;
75
+
76
+ export { type VigorAllConfig, type VigorErrorOptions, type VigorFetchConfig, vigor as default };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- export interface VigorErrorOptions {
1
+ interface VigorErrorOptions {
2
2
  url?: string | null;
3
3
  status?: number;
4
4
  message?: string;
5
5
  data?: any;
6
6
  }
7
- export interface VigorFetchConfig {
7
+ interface VigorFetchConfig {
8
8
  path: string;
9
9
  method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | null;
10
10
  offset: RequestInit;
@@ -53,7 +53,7 @@ declare class VigorFetch<T = any> {
53
53
  onError(...arg: VigorFetchConfig['onError']): VigorFetch<T>;
54
54
  request(): Promise<T>;
55
55
  }
56
- export interface VigorAllConfig {
56
+ interface VigorAllConfig {
57
57
  limit: number;
58
58
  jitter: number;
59
59
  promises: Array<() => Promise<any>>;
@@ -71,4 +71,6 @@ declare class Vigor {
71
71
  fetch<T = any>(origin: string, config?: VigorFetchConfig): VigorFetch<T>;
72
72
  all(config?: VigorAllConfig): VigorAll;
73
73
  }
74
- export default Vigor;
74
+ declare const vigor: Vigor;
75
+
76
+ export { type VigorAllConfig, type VigorErrorOptions, type VigorFetchConfig, vigor as default };
package/dist/index.js CHANGED
@@ -1,194 +1,290 @@
1
- 'use strict';
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
2
18
 
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- class VigorError extends Error {
5
- constructor(text, { url = null, status = 0, message, data = null }) {
6
- super(text);
7
- this.name = "VigorError";
8
- this.url = url;
9
- this.status = status;
10
- this.message = message || text;
11
- this.data = data;
12
- }
13
- }
14
- class VigorFetch {
15
- constructor(origin, config) {
16
- this._origin = origin;
17
- this._config = config || {
18
- path: "", method: null, offset: {}, headers: {}, body: null,
19
- count: 5, max: 5000, wait: 10000, backoff: 1.3,
20
- unretry: new Set([400, 401, 403, 404, 405, 413, 422]),
21
- retryHeader: ["retry-after", "ratelimit-reset", "x-ratelimit-reset", "x-retry-after", "x-amz-retry-after", "chrome-proxy-next-link"],
22
- original: false, parse: null, query: {}, jitter: 500,
23
- beforeRequest: [], afterRequest: [], beforeResponse: [], afterResponse: [], onError: []
24
- };
25
- }
26
- _next(changes) {
27
- return new VigorFetch(this._origin, { ...this._config, ...changes });
28
- }
29
- path(arg) { return this._next({ path: arg }); }
30
- method(arg) { return this._next({ method: arg }); }
31
- offset(arg) { return this._next({ offset: arg }); }
32
- headers(arg) { return this._next({ headers: arg }); }
33
- body(arg) { return this._next({ body: arg }); }
34
- count(arg) { return this._next({ count: arg }); }
35
- max(arg) { return this._next({ max: arg }); }
36
- wait(arg) { return this._next({ wait: arg }); }
37
- backoff(arg) { return this._next({ backoff: arg }); }
38
- unretry(arg) { return this._next({ unretry: new Set(arg) }); }
39
- retryHeader(...arg) { return this._next({ retryHeader: [...this._config.retryHeader, ...arg] }); }
40
- original(arg) { return this._next({ original: arg }); }
41
- parse(arg) { return this._next({ parse: arg }); }
42
- query(arg) { return this._next({ query: { ...this._config.query, ...arg } }); }
43
- jitter(arg) { return this._next({ jitter: arg }); }
44
- beforeRequest(...arg) { return this._next({ beforeRequest: [...this._config.beforeRequest, ...arg] }); }
45
- afterRequest(...arg) { return this._next({ afterRequest: [...this._config.afterRequest, ...arg] }); }
46
- beforeResponse(...arg) { return this._next({ beforeResponse: [...this._config.beforeResponse, ...arg] }); }
47
- afterResponse(...arg) { return this._next({ afterResponse: [...this._config.afterResponse, ...arg] }); }
48
- onError(...arg) { return this._next({ onError: [...this._config.onError, ...arg] }); }
49
- async request() {
50
- const { path, method, offset, headers, body, query, count, max, wait, backoff, unretry, jitter, original, parse, retryHeader, beforeRequest, afterRequest, beforeResponse, afterResponse, onError, } = this._config;
19
+ // src/index.ts
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ default: () => index_default
23
+ });
24
+ module.exports = __toCommonJS(index_exports);
25
+ var VigorError = class extends Error {
26
+ url;
27
+ status;
28
+ data;
29
+ constructor(text, { url = null, status = 0, message, data = null }) {
30
+ super(text);
31
+ this.name = "VigorError";
32
+ this.url = url;
33
+ this.status = status;
34
+ this.message = message || text;
35
+ this.data = data;
36
+ }
37
+ };
38
+ var VigorFetch = class _VigorFetch {
39
+ _origin;
40
+ _config;
41
+ constructor(origin, config) {
42
+ this._origin = origin;
43
+ this._config = config || {
44
+ path: "",
45
+ method: null,
46
+ offset: {},
47
+ headers: {},
48
+ body: null,
49
+ count: 5,
50
+ max: 5e3,
51
+ wait: 1e4,
52
+ backoff: 1.3,
53
+ unretry: /* @__PURE__ */ new Set([400, 401, 403, 404, 405, 413, 422]),
54
+ retryHeader: ["retry-after", "ratelimit-reset", "x-ratelimit-reset", "x-retry-after", "x-amz-retry-after", "chrome-proxy-next-link"],
55
+ original: false,
56
+ parse: null,
57
+ query: {},
58
+ jitter: 500,
59
+ beforeRequest: [],
60
+ afterRequest: [],
61
+ beforeResponse: [],
62
+ afterResponse: [],
63
+ onError: []
64
+ };
65
+ }
66
+ _next(changes) {
67
+ return new _VigorFetch(this._origin, { ...this._config, ...changes });
68
+ }
69
+ path(arg) {
70
+ return this._next({ path: arg });
71
+ }
72
+ method(arg) {
73
+ return this._next({ method: arg });
74
+ }
75
+ offset(arg) {
76
+ return this._next({ offset: arg });
77
+ }
78
+ headers(arg) {
79
+ return this._next({ headers: arg });
80
+ }
81
+ body(arg) {
82
+ return this._next({ body: arg });
83
+ }
84
+ count(arg) {
85
+ return this._next({ count: arg });
86
+ }
87
+ max(arg) {
88
+ return this._next({ max: arg });
89
+ }
90
+ wait(arg) {
91
+ return this._next({ wait: arg });
92
+ }
93
+ backoff(arg) {
94
+ return this._next({ backoff: arg });
95
+ }
96
+ unretry(arg) {
97
+ return this._next({ unretry: new Set(arg) });
98
+ }
99
+ retryHeader(...arg) {
100
+ return this._next({ retryHeader: [...this._config.retryHeader, ...arg] });
101
+ }
102
+ original(arg) {
103
+ return this._next({ original: arg });
104
+ }
105
+ parse(arg) {
106
+ return this._next({ parse: arg });
107
+ }
108
+ query(arg) {
109
+ return this._next({ query: { ...this._config.query, ...arg } });
110
+ }
111
+ jitter(arg) {
112
+ return this._next({ jitter: arg });
113
+ }
114
+ beforeRequest(...arg) {
115
+ return this._next({ beforeRequest: [...this._config.beforeRequest, ...arg] });
116
+ }
117
+ afterRequest(...arg) {
118
+ return this._next({ afterRequest: [...this._config.afterRequest, ...arg] });
119
+ }
120
+ beforeResponse(...arg) {
121
+ return this._next({ beforeResponse: [...this._config.beforeResponse, ...arg] });
122
+ }
123
+ afterResponse(...arg) {
124
+ return this._next({ afterResponse: [...this._config.afterResponse, ...arg] });
125
+ }
126
+ onError(...arg) {
127
+ return this._next({ onError: [...this._config.onError, ...arg] });
128
+ }
129
+ async request() {
130
+ const {
131
+ path,
132
+ method,
133
+ offset,
134
+ headers,
135
+ body,
136
+ query,
137
+ count,
138
+ max,
139
+ wait,
140
+ backoff,
141
+ unretry,
142
+ jitter,
143
+ original,
144
+ parse,
145
+ retryHeader,
146
+ beforeRequest,
147
+ afterRequest,
148
+ beforeResponse,
149
+ afterResponse,
150
+ onError
151
+ } = this._config;
152
+ try {
153
+ if (!/^(https?|data|blob|file|about):\/\//.test(this._origin)) {
154
+ throw new VigorError(`[vigor] ${this._origin} >> Invalid Protocol`, {
155
+ url: this._origin,
156
+ status: 0,
157
+ message: "Invalid Protocol"
158
+ });
159
+ }
160
+ const urlObj = new URL(path.replace(/^\//, ""), this._origin + "/");
161
+ Object.entries(query).forEach(([key, value]) => {
162
+ if (value !== null && value !== void 0) urlObj.searchParams.append(key, String(value));
163
+ });
164
+ const url = urlObj.href;
165
+ const isJson = Array.isArray(body) || !!body && Object.getPrototypeOf(body) === Object.prototype;
166
+ const waitTimeout = (time) => new Promise((resolve) => setTimeout(resolve, time));
167
+ let option = {
168
+ ...offset,
169
+ method: method || (body ? "POST" : "GET"),
170
+ headers: { ...isJson && { "Content-Type": "application/json" }, ...headers },
171
+ ...body && { body: isJson ? JSON.stringify(body) : body }
172
+ };
173
+ for (const hook of beforeRequest) {
174
+ const modified = await hook(option);
175
+ if (modified) option = { ...option, ...modified };
176
+ }
177
+ let req;
178
+ for (let i = 0; i < count; i++) {
179
+ const controller = new AbortController();
180
+ const abort = setTimeout(() => controller.abort(), max);
181
+ option.signal = controller.signal;
51
182
  try {
52
- if (!/^(https?|data|blob|file|about):\/\//.test(this._origin)) {
53
- throw new VigorError(`[vigor] ${this._origin} >> Invalid Protocol`, {
54
- url: this._origin, status: 0, message: "Invalid Protocol"
55
- });
56
- }
57
- const urlObj = new URL(path.replace(/^\//, ""), this._origin + "/");
58
- Object.entries(query).forEach(([key, value]) => {
59
- if (value !== null && value !== undefined)
60
- urlObj.searchParams.append(key, String(value));
61
- });
62
- const url = urlObj.href;
63
- const isJson = Array.isArray(body) || (!!body && Object.getPrototypeOf(body) === Object.prototype);
64
- const waitTimeout = (time) => new Promise(resolve => setTimeout(resolve, time));
65
- let option = {
66
- ...offset,
67
- method: method || (body ? "POST" : "GET"),
68
- headers: { ...(isJson && { "Content-Type": "application/json" }), ...headers },
69
- ...(body && { body: isJson ? JSON.stringify(body) : body }),
70
- };
71
- for (const hook of beforeRequest) {
72
- const modified = await hook(option);
73
- if (modified)
74
- option = { ...option, ...modified };
75
- }
76
- let req;
77
- for (let i = 0; i < count; i++) {
78
- const controller = new AbortController();
79
- const abort = setTimeout(() => controller.abort(), max);
80
- option.signal = controller.signal;
81
- try {
82
- req = await fetch(url, option);
83
- for (const hook of afterRequest) {
84
- req = (await hook(req)) || req;
85
- }
86
- if (req.ok) {
87
- clearTimeout(abort);
88
- break;
89
- }
90
- }
91
- catch (error) {
92
- clearTimeout(abort);
93
- if (i === count - 1)
94
- throw new VigorError(`[vigor] ${url} >> Network Error`, { url, status: 0, message: "Network Error" });
95
- }
96
- finally {
97
- clearTimeout(abort);
98
- }
99
- if (req) {
100
- const status = req.status;
101
- if (unretry.has(status))
102
- throw new VigorError(`[vigor] ${url} >> Unretry ${status}`, { url, status, message: "Unretry", data: status });
103
- const basic = Math.min(Math.pow(backoff, i) * 1000, wait) + Math.random() * jitter;
104
- if (status === 429) {
105
- const rHeader = retryHeader.map(h => req?.headers.get(h)).find(Boolean);
106
- const delay = rHeader ? (isNaN(Number(rHeader)) ? new Date(rHeader).getTime() - Date.now() : Number(rHeader) * 1000) : 0;
107
- const parsedDelay = Math.max(0, delay) + Math.random() * jitter;
108
- if (parsedDelay > wait)
109
- throw new VigorError(`[vigor] ${url} >> Timeouted ${parsedDelay}ms`, { url, status, message: "Timeouted", data: parsedDelay });
110
- await waitTimeout(parsedDelay || basic);
111
- }
112
- else {
113
- await waitTimeout(basic);
114
- }
115
- }
116
- }
117
- if (!req)
118
- throw new Error("No response");
119
- let currentReq = req;
120
- for (const hook of beforeResponse) {
121
- currentReq = await hook(currentReq);
122
- }
123
- if (!currentReq.ok)
124
- throw new VigorError(`[vigor] ${url} >> Failed`, { url, status: currentReq.status, message: "Failed" });
125
- let res = await (async () => {
126
- if (original)
127
- return currentReq;
128
- if (parse) {
129
- const target = currentReq[parse];
130
- return typeof target === 'function' ? await target.call(currentReq) : target;
131
- }
132
- const contentType = currentReq.headers.get("Content-Type") || "";
133
- if (/json/.test(contentType))
134
- return await currentReq.json();
135
- if (/(image|video|audio|pdf)/.test(contentType))
136
- return await currentReq.blob();
137
- return await currentReq.text();
138
- })();
139
- for (const hook of afterResponse) {
140
- res = await hook(res);
141
- }
142
- return res;
183
+ req = await fetch(url, option);
184
+ for (const hook of afterRequest) {
185
+ req = await hook(req) || req;
186
+ }
187
+ if (req.ok) {
188
+ clearTimeout(abort);
189
+ break;
190
+ }
191
+ } catch (error) {
192
+ clearTimeout(abort);
193
+ if (i === count - 1) throw new VigorError(`[vigor] ${url} >> Network Error`, { url, status: 0, message: "Network Error" });
194
+ } finally {
195
+ clearTimeout(abort);
143
196
  }
144
- catch (error) {
145
- let currentError = error;
146
- for (const hook of onError) {
147
- const result = await hook(currentError);
148
- if (result !== undefined && !(result instanceof Error))
149
- return result;
150
- currentError = result || currentError;
151
- }
152
- throw currentError;
197
+ if (req) {
198
+ const status = req.status;
199
+ if (unretry.has(status)) throw new VigorError(`[vigor] ${url} >> Unretry ${status}`, { url, status, message: "Unretry", data: status });
200
+ const basic = Math.min(Math.pow(backoff, i) * 1e3, wait) + Math.random() * jitter;
201
+ if (status === 429) {
202
+ const rHeader = retryHeader.map((h) => req == null ? void 0 : req.headers.get(h)).find(Boolean);
203
+ const delay = rHeader ? isNaN(Number(rHeader)) ? new Date(rHeader).getTime() - Date.now() : Number(rHeader) * 1e3 : 0;
204
+ const parsedDelay = Math.max(0, delay) + Math.random() * jitter;
205
+ if (parsedDelay > wait) throw new VigorError(`[vigor] ${url} >> Timeouted ${parsedDelay}ms`, { url, status, message: "Timeouted", data: parsedDelay });
206
+ await waitTimeout(parsedDelay || basic);
207
+ } else {
208
+ await waitTimeout(basic);
209
+ }
153
210
  }
154
- }
155
- }
156
- class VigorAll {
157
- constructor(config) {
158
- this._config = config || { limit: 10, jitter: 1000, promises: [] };
159
- }
160
- _next(changes) { return new VigorAll({ ...this._config, ...changes }); }
161
- limit(arg) { return this._next({ limit: arg }); }
162
- jitter(arg) { return this._next({ jitter: arg }); }
163
- promises(...args) { return this._next({ promises: [...this._config.promises, ...args] }); }
164
- async request() {
165
- const { limit, jitter, promises } = this._config;
166
- const results = [];
167
- const executing = new Set();
168
- for (const task of promises) {
169
- const p = Promise.resolve()
170
- .then(() => new Promise(res => setTimeout(res, Math.random() * jitter)))
171
- .then(() => task());
172
- results.push(p);
173
- executing.add(p);
174
- p.finally(() => executing.delete(p));
175
- if (executing.size >= limit)
176
- await Promise.race(executing);
211
+ }
212
+ if (!req) throw new Error("No response");
213
+ let currentReq = req;
214
+ for (const hook of beforeResponse) {
215
+ currentReq = await hook(currentReq);
216
+ }
217
+ if (!currentReq.ok) throw new VigorError(`[vigor] ${url} >> Failed`, { url, status: currentReq.status, message: "Failed" });
218
+ let res = await (async () => {
219
+ if (original) return currentReq;
220
+ if (parse) {
221
+ const target = currentReq[parse];
222
+ return typeof target === "function" ? await target.call(currentReq) : target;
177
223
  }
178
- const ready = await Promise.allSettled(results);
179
- return ready.map(i => {
180
- if (i.status === "fulfilled")
181
- return i.value;
182
- return i.reason instanceof VigorError ? i.reason : new VigorError(i.reason?.message || "Unknown", { message: i.reason?.message || "Unknown" });
183
- });
184
- }
185
- }
186
- class Vigor {
187
- fetch(origin, config) {
188
- return new VigorFetch(origin, config);
224
+ const contentType = currentReq.headers.get("Content-Type") || "";
225
+ if (/json/.test(contentType)) return await currentReq.json();
226
+ if (/(image|video|audio|pdf)/.test(contentType)) return await currentReq.blob();
227
+ return await currentReq.text();
228
+ })();
229
+ for (const hook of afterResponse) {
230
+ res = await hook(res);
231
+ }
232
+ return res;
233
+ } catch (error) {
234
+ let currentError = error;
235
+ for (const hook of onError) {
236
+ const result = await hook(currentError);
237
+ if (result !== void 0 && !(result instanceof Error)) return result;
238
+ currentError = result || currentError;
239
+ }
240
+ throw currentError;
189
241
  }
190
- all(config) {
191
- return new VigorAll(config);
242
+ }
243
+ };
244
+ var VigorAll = class _VigorAll {
245
+ _config;
246
+ constructor(config) {
247
+ this._config = config || { limit: 10, jitter: 1e3, promises: [] };
248
+ }
249
+ _next(changes) {
250
+ return new _VigorAll({ ...this._config, ...changes });
251
+ }
252
+ limit(arg) {
253
+ return this._next({ limit: arg });
254
+ }
255
+ jitter(arg) {
256
+ return this._next({ jitter: arg });
257
+ }
258
+ promises(...args) {
259
+ return this._next({ promises: [...this._config.promises, ...args] });
260
+ }
261
+ async request() {
262
+ const { limit, jitter, promises } = this._config;
263
+ const results = [];
264
+ const executing = /* @__PURE__ */ new Set();
265
+ for (const task of promises) {
266
+ const p = Promise.resolve().then(() => new Promise((res) => setTimeout(res, Math.random() * jitter))).then(() => task());
267
+ results.push(p);
268
+ executing.add(p);
269
+ p.finally(() => executing.delete(p));
270
+ if (executing.size >= limit) await Promise.race(executing);
192
271
  }
193
- }
194
- exports.default = Vigor;
272
+ const ready = await Promise.allSettled(results);
273
+ return ready.map((i) => {
274
+ var _a, _b;
275
+ if (i.status === "fulfilled") return i.value;
276
+ return i.reason instanceof VigorError ? i.reason : new VigorError(((_a = i.reason) == null ? void 0 : _a.message) || "Unknown", { message: ((_b = i.reason) == null ? void 0 : _b.message) || "Unknown" });
277
+ });
278
+ }
279
+ };
280
+ var Vigor = class {
281
+ fetch(origin, config) {
282
+ return new VigorFetch(origin, config);
283
+ }
284
+ all(config) {
285
+ return new VigorAll(config);
286
+ }
287
+ };
288
+ var vigor = new Vigor();
289
+ var index_default = vigor;
290
+ if (module.exports.default) module.exports = module.exports.default;
package/dist/index.mjs CHANGED
@@ -1,192 +1,270 @@
1
- Object.defineProperty(exports, "__esModule", { value: true });
2
- class VigorError extends Error {
3
- constructor(text, { url = null, status = 0, message, data = null }) {
4
- super(text);
5
- this.name = "VigorError";
6
- this.url = url;
7
- this.status = status;
8
- this.message = message || text;
9
- this.data = data;
10
- }
11
- }
12
- class VigorFetch {
13
- constructor(origin, config) {
14
- this._origin = origin;
15
- this._config = config || {
16
- path: "", method: null, offset: {}, headers: {}, body: null,
17
- count: 5, max: 5000, wait: 10000, backoff: 1.3,
18
- unretry: new Set([400, 401, 403, 404, 405, 413, 422]),
19
- retryHeader: ["retry-after", "ratelimit-reset", "x-ratelimit-reset", "x-retry-after", "x-amz-retry-after", "chrome-proxy-next-link"],
20
- original: false, parse: null, query: {}, jitter: 500,
21
- beforeRequest: [], afterRequest: [], beforeResponse: [], afterResponse: [], onError: []
22
- };
23
- }
24
- _next(changes) {
25
- return new VigorFetch(this._origin, { ...this._config, ...changes });
26
- }
27
- path(arg) { return this._next({ path: arg }); }
28
- method(arg) { return this._next({ method: arg }); }
29
- offset(arg) { return this._next({ offset: arg }); }
30
- headers(arg) { return this._next({ headers: arg }); }
31
- body(arg) { return this._next({ body: arg }); }
32
- count(arg) { return this._next({ count: arg }); }
33
- max(arg) { return this._next({ max: arg }); }
34
- wait(arg) { return this._next({ wait: arg }); }
35
- backoff(arg) { return this._next({ backoff: arg }); }
36
- unretry(arg) { return this._next({ unretry: new Set(arg) }); }
37
- retryHeader(...arg) { return this._next({ retryHeader: [...this._config.retryHeader, ...arg] }); }
38
- original(arg) { return this._next({ original: arg }); }
39
- parse(arg) { return this._next({ parse: arg }); }
40
- query(arg) { return this._next({ query: { ...this._config.query, ...arg } }); }
41
- jitter(arg) { return this._next({ jitter: arg }); }
42
- beforeRequest(...arg) { return this._next({ beforeRequest: [...this._config.beforeRequest, ...arg] }); }
43
- afterRequest(...arg) { return this._next({ afterRequest: [...this._config.afterRequest, ...arg] }); }
44
- beforeResponse(...arg) { return this._next({ beforeResponse: [...this._config.beforeResponse, ...arg] }); }
45
- afterResponse(...arg) { return this._next({ afterResponse: [...this._config.afterResponse, ...arg] }); }
46
- onError(...arg) { return this._next({ onError: [...this._config.onError, ...arg] }); }
47
- async request() {
48
- const { path, method, offset, headers, body, query, count, max, wait, backoff, unretry, jitter, original, parse, retryHeader, beforeRequest, afterRequest, beforeResponse, afterResponse, onError, } = this._config;
1
+ // src/index.ts
2
+ var VigorError = class extends Error {
3
+ url;
4
+ status;
5
+ data;
6
+ constructor(text, { url = null, status = 0, message, data = null }) {
7
+ super(text);
8
+ this.name = "VigorError";
9
+ this.url = url;
10
+ this.status = status;
11
+ this.message = message || text;
12
+ this.data = data;
13
+ }
14
+ };
15
+ var VigorFetch = class _VigorFetch {
16
+ _origin;
17
+ _config;
18
+ constructor(origin, config) {
19
+ this._origin = origin;
20
+ this._config = config || {
21
+ path: "",
22
+ method: null,
23
+ offset: {},
24
+ headers: {},
25
+ body: null,
26
+ count: 5,
27
+ max: 5e3,
28
+ wait: 1e4,
29
+ backoff: 1.3,
30
+ unretry: /* @__PURE__ */ new Set([400, 401, 403, 404, 405, 413, 422]),
31
+ retryHeader: ["retry-after", "ratelimit-reset", "x-ratelimit-reset", "x-retry-after", "x-amz-retry-after", "chrome-proxy-next-link"],
32
+ original: false,
33
+ parse: null,
34
+ query: {},
35
+ jitter: 500,
36
+ beforeRequest: [],
37
+ afterRequest: [],
38
+ beforeResponse: [],
39
+ afterResponse: [],
40
+ onError: []
41
+ };
42
+ }
43
+ _next(changes) {
44
+ return new _VigorFetch(this._origin, { ...this._config, ...changes });
45
+ }
46
+ path(arg) {
47
+ return this._next({ path: arg });
48
+ }
49
+ method(arg) {
50
+ return this._next({ method: arg });
51
+ }
52
+ offset(arg) {
53
+ return this._next({ offset: arg });
54
+ }
55
+ headers(arg) {
56
+ return this._next({ headers: arg });
57
+ }
58
+ body(arg) {
59
+ return this._next({ body: arg });
60
+ }
61
+ count(arg) {
62
+ return this._next({ count: arg });
63
+ }
64
+ max(arg) {
65
+ return this._next({ max: arg });
66
+ }
67
+ wait(arg) {
68
+ return this._next({ wait: arg });
69
+ }
70
+ backoff(arg) {
71
+ return this._next({ backoff: arg });
72
+ }
73
+ unretry(arg) {
74
+ return this._next({ unretry: new Set(arg) });
75
+ }
76
+ retryHeader(...arg) {
77
+ return this._next({ retryHeader: [...this._config.retryHeader, ...arg] });
78
+ }
79
+ original(arg) {
80
+ return this._next({ original: arg });
81
+ }
82
+ parse(arg) {
83
+ return this._next({ parse: arg });
84
+ }
85
+ query(arg) {
86
+ return this._next({ query: { ...this._config.query, ...arg } });
87
+ }
88
+ jitter(arg) {
89
+ return this._next({ jitter: arg });
90
+ }
91
+ beforeRequest(...arg) {
92
+ return this._next({ beforeRequest: [...this._config.beforeRequest, ...arg] });
93
+ }
94
+ afterRequest(...arg) {
95
+ return this._next({ afterRequest: [...this._config.afterRequest, ...arg] });
96
+ }
97
+ beforeResponse(...arg) {
98
+ return this._next({ beforeResponse: [...this._config.beforeResponse, ...arg] });
99
+ }
100
+ afterResponse(...arg) {
101
+ return this._next({ afterResponse: [...this._config.afterResponse, ...arg] });
102
+ }
103
+ onError(...arg) {
104
+ return this._next({ onError: [...this._config.onError, ...arg] });
105
+ }
106
+ async request() {
107
+ const {
108
+ path,
109
+ method,
110
+ offset,
111
+ headers,
112
+ body,
113
+ query,
114
+ count,
115
+ max,
116
+ wait,
117
+ backoff,
118
+ unretry,
119
+ jitter,
120
+ original,
121
+ parse,
122
+ retryHeader,
123
+ beforeRequest,
124
+ afterRequest,
125
+ beforeResponse,
126
+ afterResponse,
127
+ onError
128
+ } = this._config;
129
+ try {
130
+ if (!/^(https?|data|blob|file|about):\/\//.test(this._origin)) {
131
+ throw new VigorError(`[vigor] ${this._origin} >> Invalid Protocol`, {
132
+ url: this._origin,
133
+ status: 0,
134
+ message: "Invalid Protocol"
135
+ });
136
+ }
137
+ const urlObj = new URL(path.replace(/^\//, ""), this._origin + "/");
138
+ Object.entries(query).forEach(([key, value]) => {
139
+ if (value !== null && value !== void 0) urlObj.searchParams.append(key, String(value));
140
+ });
141
+ const url = urlObj.href;
142
+ const isJson = Array.isArray(body) || !!body && Object.getPrototypeOf(body) === Object.prototype;
143
+ const waitTimeout = (time) => new Promise((resolve) => setTimeout(resolve, time));
144
+ let option = {
145
+ ...offset,
146
+ method: method || (body ? "POST" : "GET"),
147
+ headers: { ...isJson && { "Content-Type": "application/json" }, ...headers },
148
+ ...body && { body: isJson ? JSON.stringify(body) : body }
149
+ };
150
+ for (const hook of beforeRequest) {
151
+ const modified = await hook(option);
152
+ if (modified) option = { ...option, ...modified };
153
+ }
154
+ let req;
155
+ for (let i = 0; i < count; i++) {
156
+ const controller = new AbortController();
157
+ const abort = setTimeout(() => controller.abort(), max);
158
+ option.signal = controller.signal;
49
159
  try {
50
- if (!/^(https?|data|blob|file|about):\/\//.test(this._origin)) {
51
- throw new VigorError(`[vigor] ${this._origin} >> Invalid Protocol`, {
52
- url: this._origin, status: 0, message: "Invalid Protocol"
53
- });
54
- }
55
- const urlObj = new URL(path.replace(/^\//, ""), this._origin + "/");
56
- Object.entries(query).forEach(([key, value]) => {
57
- if (value !== null && value !== undefined)
58
- urlObj.searchParams.append(key, String(value));
59
- });
60
- const url = urlObj.href;
61
- const isJson = Array.isArray(body) || (!!body && Object.getPrototypeOf(body) === Object.prototype);
62
- const waitTimeout = (time) => new Promise(resolve => setTimeout(resolve, time));
63
- let option = {
64
- ...offset,
65
- method: method || (body ? "POST" : "GET"),
66
- headers: { ...(isJson && { "Content-Type": "application/json" }), ...headers },
67
- ...(body && { body: isJson ? JSON.stringify(body) : body }),
68
- };
69
- for (const hook of beforeRequest) {
70
- const modified = await hook(option);
71
- if (modified)
72
- option = { ...option, ...modified };
73
- }
74
- let req;
75
- for (let i = 0; i < count; i++) {
76
- const controller = new AbortController();
77
- const abort = setTimeout(() => controller.abort(), max);
78
- option.signal = controller.signal;
79
- try {
80
- req = await fetch(url, option);
81
- for (const hook of afterRequest) {
82
- req = (await hook(req)) || req;
83
- }
84
- if (req.ok) {
85
- clearTimeout(abort);
86
- break;
87
- }
88
- }
89
- catch (error) {
90
- clearTimeout(abort);
91
- if (i === count - 1)
92
- throw new VigorError(`[vigor] ${url} >> Network Error`, { url, status: 0, message: "Network Error" });
93
- }
94
- finally {
95
- clearTimeout(abort);
96
- }
97
- if (req) {
98
- const status = req.status;
99
- if (unretry.has(status))
100
- throw new VigorError(`[vigor] ${url} >> Unretry ${status}`, { url, status, message: "Unretry", data: status });
101
- const basic = Math.min(Math.pow(backoff, i) * 1000, wait) + Math.random() * jitter;
102
- if (status === 429) {
103
- const rHeader = retryHeader.map(h => req?.headers.get(h)).find(Boolean);
104
- const delay = rHeader ? (isNaN(Number(rHeader)) ? new Date(rHeader).getTime() - Date.now() : Number(rHeader) * 1000) : 0;
105
- const parsedDelay = Math.max(0, delay) + Math.random() * jitter;
106
- if (parsedDelay > wait)
107
- throw new VigorError(`[vigor] ${url} >> Timeouted ${parsedDelay}ms`, { url, status, message: "Timeouted", data: parsedDelay });
108
- await waitTimeout(parsedDelay || basic);
109
- }
110
- else {
111
- await waitTimeout(basic);
112
- }
113
- }
114
- }
115
- if (!req)
116
- throw new Error("No response");
117
- let currentReq = req;
118
- for (const hook of beforeResponse) {
119
- currentReq = await hook(currentReq);
120
- }
121
- if (!currentReq.ok)
122
- throw new VigorError(`[vigor] ${url} >> Failed`, { url, status: currentReq.status, message: "Failed" });
123
- let res = await (async () => {
124
- if (original)
125
- return currentReq;
126
- if (parse) {
127
- const target = currentReq[parse];
128
- return typeof target === 'function' ? await target.call(currentReq) : target;
129
- }
130
- const contentType = currentReq.headers.get("Content-Type") || "";
131
- if (/json/.test(contentType))
132
- return await currentReq.json();
133
- if (/(image|video|audio|pdf)/.test(contentType))
134
- return await currentReq.blob();
135
- return await currentReq.text();
136
- })();
137
- for (const hook of afterResponse) {
138
- res = await hook(res);
139
- }
140
- return res;
160
+ req = await fetch(url, option);
161
+ for (const hook of afterRequest) {
162
+ req = await hook(req) || req;
163
+ }
164
+ if (req.ok) {
165
+ clearTimeout(abort);
166
+ break;
167
+ }
168
+ } catch (error) {
169
+ clearTimeout(abort);
170
+ if (i === count - 1) throw new VigorError(`[vigor] ${url} >> Network Error`, { url, status: 0, message: "Network Error" });
171
+ } finally {
172
+ clearTimeout(abort);
141
173
  }
142
- catch (error) {
143
- let currentError = error;
144
- for (const hook of onError) {
145
- const result = await hook(currentError);
146
- if (result !== undefined && !(result instanceof Error))
147
- return result;
148
- currentError = result || currentError;
149
- }
150
- throw currentError;
174
+ if (req) {
175
+ const status = req.status;
176
+ if (unretry.has(status)) throw new VigorError(`[vigor] ${url} >> Unretry ${status}`, { url, status, message: "Unretry", data: status });
177
+ const basic = Math.min(Math.pow(backoff, i) * 1e3, wait) + Math.random() * jitter;
178
+ if (status === 429) {
179
+ const rHeader = retryHeader.map((h) => req == null ? void 0 : req.headers.get(h)).find(Boolean);
180
+ const delay = rHeader ? isNaN(Number(rHeader)) ? new Date(rHeader).getTime() - Date.now() : Number(rHeader) * 1e3 : 0;
181
+ const parsedDelay = Math.max(0, delay) + Math.random() * jitter;
182
+ if (parsedDelay > wait) throw new VigorError(`[vigor] ${url} >> Timeouted ${parsedDelay}ms`, { url, status, message: "Timeouted", data: parsedDelay });
183
+ await waitTimeout(parsedDelay || basic);
184
+ } else {
185
+ await waitTimeout(basic);
186
+ }
151
187
  }
152
- }
153
- }
154
- class VigorAll {
155
- constructor(config) {
156
- this._config = config || { limit: 10, jitter: 1000, promises: [] };
157
- }
158
- _next(changes) { return new VigorAll({ ...this._config, ...changes }); }
159
- limit(arg) { return this._next({ limit: arg }); }
160
- jitter(arg) { return this._next({ jitter: arg }); }
161
- promises(...args) { return this._next({ promises: [...this._config.promises, ...args] }); }
162
- async request() {
163
- const { limit, jitter, promises } = this._config;
164
- const results = [];
165
- const executing = new Set();
166
- for (const task of promises) {
167
- const p = Promise.resolve()
168
- .then(() => new Promise(res => setTimeout(res, Math.random() * jitter)))
169
- .then(() => task());
170
- results.push(p);
171
- executing.add(p);
172
- p.finally(() => executing.delete(p));
173
- if (executing.size >= limit)
174
- await Promise.race(executing);
188
+ }
189
+ if (!req) throw new Error("No response");
190
+ let currentReq = req;
191
+ for (const hook of beforeResponse) {
192
+ currentReq = await hook(currentReq);
193
+ }
194
+ if (!currentReq.ok) throw new VigorError(`[vigor] ${url} >> Failed`, { url, status: currentReq.status, message: "Failed" });
195
+ let res = await (async () => {
196
+ if (original) return currentReq;
197
+ if (parse) {
198
+ const target = currentReq[parse];
199
+ return typeof target === "function" ? await target.call(currentReq) : target;
175
200
  }
176
- const ready = await Promise.allSettled(results);
177
- return ready.map(i => {
178
- if (i.status === "fulfilled")
179
- return i.value;
180
- return i.reason instanceof VigorError ? i.reason : new VigorError(i.reason?.message || "Unknown", { message: i.reason?.message || "Unknown" });
181
- });
182
- }
183
- }
184
- class Vigor {
185
- fetch(origin, config) {
186
- return new VigorFetch(origin, config);
201
+ const contentType = currentReq.headers.get("Content-Type") || "";
202
+ if (/json/.test(contentType)) return await currentReq.json();
203
+ if (/(image|video|audio|pdf)/.test(contentType)) return await currentReq.blob();
204
+ return await currentReq.text();
205
+ })();
206
+ for (const hook of afterResponse) {
207
+ res = await hook(res);
208
+ }
209
+ return res;
210
+ } catch (error) {
211
+ let currentError = error;
212
+ for (const hook of onError) {
213
+ const result = await hook(currentError);
214
+ if (result !== void 0 && !(result instanceof Error)) return result;
215
+ currentError = result || currentError;
216
+ }
217
+ throw currentError;
187
218
  }
188
- all(config) {
189
- return new VigorAll(config);
219
+ }
220
+ };
221
+ var VigorAll = class _VigorAll {
222
+ _config;
223
+ constructor(config) {
224
+ this._config = config || { limit: 10, jitter: 1e3, promises: [] };
225
+ }
226
+ _next(changes) {
227
+ return new _VigorAll({ ...this._config, ...changes });
228
+ }
229
+ limit(arg) {
230
+ return this._next({ limit: arg });
231
+ }
232
+ jitter(arg) {
233
+ return this._next({ jitter: arg });
234
+ }
235
+ promises(...args) {
236
+ return this._next({ promises: [...this._config.promises, ...args] });
237
+ }
238
+ async request() {
239
+ const { limit, jitter, promises } = this._config;
240
+ const results = [];
241
+ const executing = /* @__PURE__ */ new Set();
242
+ for (const task of promises) {
243
+ const p = Promise.resolve().then(() => new Promise((res) => setTimeout(res, Math.random() * jitter))).then(() => task());
244
+ results.push(p);
245
+ executing.add(p);
246
+ p.finally(() => executing.delete(p));
247
+ if (executing.size >= limit) await Promise.race(executing);
190
248
  }
191
- }
192
- exports.default = Vigor;
249
+ const ready = await Promise.allSettled(results);
250
+ return ready.map((i) => {
251
+ var _a, _b;
252
+ if (i.status === "fulfilled") return i.value;
253
+ return i.reason instanceof VigorError ? i.reason : new VigorError(((_a = i.reason) == null ? void 0 : _a.message) || "Unknown", { message: ((_b = i.reason) == null ? void 0 : _b.message) || "Unknown" });
254
+ });
255
+ }
256
+ };
257
+ var Vigor = class {
258
+ fetch(origin, config) {
259
+ return new VigorFetch(origin, config);
260
+ }
261
+ all(config) {
262
+ return new VigorAll(config);
263
+ }
264
+ };
265
+ var vigor = new Vigor();
266
+ var index_default = vigor;
267
+ export {
268
+ index_default as default
269
+ };
270
+ if (module.exports.default) module.exports = module.exports.default;
package/package.json CHANGED
@@ -1,24 +1,23 @@
1
1
  {
2
2
  "name": "vigor-fetch",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Smart, zero-dependency HTTP client with self-healing retries for rate-limited servers.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
7
7
  "types": "./dist/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
- "require": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
11
  "import": "./dist/index.mjs",
12
- "types": "./dist/index.d.ts"
12
+ "require": "./dist/index.js"
13
13
  }
14
14
  },
15
+ "scripts": {
16
+ "build": "tsup"
17
+ },
15
18
  "files": [
16
19
  "dist"
17
20
  ],
18
- "scripts": {
19
- "build": "rollup -c",
20
- "dev": "rollup -c -w"
21
- },
22
21
  "keywords": [
23
22
  "fetch",
24
23
  "PromiseAll",
@@ -34,6 +33,7 @@
34
33
  "@rollup/plugin-typescript": "^11.1.6",
35
34
  "rollup": "^4.59.0",
36
35
  "tslib": "^2.8.1",
36
+ "tsup": "^8.5.1",
37
37
  "typescript": "^5.9.3"
38
38
  },
39
39
  "engines": {