@naturalcycles/js-lib 14.169.0 → 14.171.0
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.
- package/dist/error/error.util.d.ts +5 -3
- package/dist/error/error.util.js +6 -3
- package/dist/http/fetcher.d.ts +8 -0
- package/dist/http/fetcher.js +13 -3
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -3
- package/dist/promise/pRetry.js +1 -2
- package/dist/promise/pTimeout.js +1 -2
- package/dist-esm/error/error.util.js +6 -3
- package/dist-esm/http/fetcher.js +38 -26
- package/dist-esm/index.js +1 -2
- package/dist-esm/promise/pRetry.js +1 -2
- package/dist-esm/promise/pTimeout.js +1 -2
- package/package.json +1 -1
- package/src/error/error.util.ts +10 -7
- package/src/http/fetcher.ts +16 -3
- package/src/index.ts +1 -2
- package/src/promise/pRetry.ts +1 -2
- package/src/promise/pTimeout.ts +1 -2
- package/dist/vendor/is.d.ts +0 -226
- package/dist/vendor/is.js +0 -460
- package/dist-esm/vendor/is.js +0 -457
- package/src/vendor/is.ts +0 -833
|
@@ -27,14 +27,16 @@ export declare function _isErrorObject<DATA_TYPE extends ErrorData = ErrorData>(
|
|
|
27
27
|
export declare function _isErrorLike(o: any): o is ErrorLike;
|
|
28
28
|
/**
|
|
29
29
|
* Convenience function to safely add properties to Error's `data` object
|
|
30
|
-
* (even if it wasn't previously existing)
|
|
30
|
+
* (even if it wasn't previously existing).
|
|
31
|
+
* Mutates err.
|
|
32
|
+
* Returns err for convenience, so you can re-throw it directly.
|
|
31
33
|
*
|
|
32
34
|
* @example
|
|
33
35
|
*
|
|
34
36
|
* try {} catch (err) {
|
|
35
|
-
* _errorDataAppend(err, {
|
|
37
|
+
* throw _errorDataAppend(err, {
|
|
36
38
|
* httpStatusCode: 401,
|
|
37
39
|
* })
|
|
38
40
|
* }
|
|
39
41
|
*/
|
|
40
|
-
export declare function _errorDataAppend(err:
|
|
42
|
+
export declare function _errorDataAppend<ERR>(err: ERR, data?: ErrorData): ERR;
|
package/dist/error/error.util.js
CHANGED
|
@@ -154,22 +154,25 @@ function _isErrorLike(o) {
|
|
|
154
154
|
exports._isErrorLike = _isErrorLike;
|
|
155
155
|
/**
|
|
156
156
|
* Convenience function to safely add properties to Error's `data` object
|
|
157
|
-
* (even if it wasn't previously existing)
|
|
157
|
+
* (even if it wasn't previously existing).
|
|
158
|
+
* Mutates err.
|
|
159
|
+
* Returns err for convenience, so you can re-throw it directly.
|
|
158
160
|
*
|
|
159
161
|
* @example
|
|
160
162
|
*
|
|
161
163
|
* try {} catch (err) {
|
|
162
|
-
* _errorDataAppend(err, {
|
|
164
|
+
* throw _errorDataAppend(err, {
|
|
163
165
|
* httpStatusCode: 401,
|
|
164
166
|
* })
|
|
165
167
|
* }
|
|
166
168
|
*/
|
|
167
169
|
function _errorDataAppend(err, data) {
|
|
168
170
|
if (!data)
|
|
169
|
-
return;
|
|
171
|
+
return err;
|
|
170
172
|
err.data = {
|
|
171
173
|
...err.data,
|
|
172
174
|
...data,
|
|
173
175
|
};
|
|
176
|
+
return err;
|
|
174
177
|
}
|
|
175
178
|
exports._errorDataAppend = _errorDataAppend;
|
package/dist/http/fetcher.d.ts
CHANGED
|
@@ -8,6 +8,14 @@ import type { FetcherAfterResponseHook, FetcherBeforeRequestHook, FetcherBeforeR
|
|
|
8
8
|
* Works in both Browser and Node, using `globalThis.fetch`.
|
|
9
9
|
*/
|
|
10
10
|
export declare class Fetcher {
|
|
11
|
+
/**
|
|
12
|
+
* Included in UserAgent when run in Node.
|
|
13
|
+
* In the browser it's not included, as we want "browser own" UserAgent to be included instead.
|
|
14
|
+
*
|
|
15
|
+
* Version is to be incremented every time a difference in behaviour (or a bugfix) is done.
|
|
16
|
+
*/
|
|
17
|
+
static readonly VERSION = 1;
|
|
18
|
+
static readonly userAgent: string | undefined;
|
|
11
19
|
private constructor();
|
|
12
20
|
/**
|
|
13
21
|
* Add BeforeRequest hook at the end of the hooks list.
|
package/dist/http/fetcher.js
CHANGED
|
@@ -34,6 +34,14 @@ const defRetryOptions = {
|
|
|
34
34
|
* Works in both Browser and Node, using `globalThis.fetch`.
|
|
35
35
|
*/
|
|
36
36
|
class Fetcher {
|
|
37
|
+
/**
|
|
38
|
+
* Included in UserAgent when run in Node.
|
|
39
|
+
* In the browser it's not included, as we want "browser own" UserAgent to be included instead.
|
|
40
|
+
*
|
|
41
|
+
* Version is to be incremented every time a difference in behaviour (or a bugfix) is done.
|
|
42
|
+
*/
|
|
43
|
+
static { this.VERSION = 1; }
|
|
44
|
+
static { this.userAgent = (0, env_1.isServerSide)() ? `fetcher${this.VERSION}` : undefined; }
|
|
37
45
|
constructor(cfg = {}) {
|
|
38
46
|
if (typeof globalThis.fetch !== 'function') {
|
|
39
47
|
throw new TypeError(`globalThis.fetch is not available`);
|
|
@@ -487,10 +495,10 @@ class Fetcher {
|
|
|
487
495
|
retry: { ...defRetryOptions },
|
|
488
496
|
init: {
|
|
489
497
|
method: cfg.method || 'GET',
|
|
490
|
-
headers: {
|
|
491
|
-
'user-agent':
|
|
498
|
+
headers: (0, object_util_1._filterNullishValues)({
|
|
499
|
+
'user-agent': Fetcher.userAgent,
|
|
492
500
|
...cfg.headers,
|
|
493
|
-
},
|
|
501
|
+
}),
|
|
494
502
|
credentials: cfg.credentials,
|
|
495
503
|
redirect: cfg.redirect,
|
|
496
504
|
},
|
|
@@ -532,6 +540,8 @@ class Fetcher {
|
|
|
532
540
|
headers: (0, object_util_1._mapKeys)(opt.headers || {}, k => k.toLowerCase()),
|
|
533
541
|
}),
|
|
534
542
|
};
|
|
543
|
+
// Because all header values are stringified, so `a: undefined` becomes `undefined` as a string
|
|
544
|
+
(0, object_util_1._filterNullishValues)(req.init.headers, true);
|
|
535
545
|
// setup url
|
|
536
546
|
const baseUrl = opt.baseUrl || this.cfg.baseUrl;
|
|
537
547
|
if (baseUrl) {
|
package/dist/index.d.ts
CHANGED
|
@@ -86,6 +86,5 @@ export * from './web';
|
|
|
86
86
|
export * from './zod/zod.util';
|
|
87
87
|
export * from './zod/zod.shared.schemas';
|
|
88
88
|
import { z, ZodSchema, ZodError, ZodIssue } from 'zod';
|
|
89
|
-
|
|
90
|
-
export { is, z, ZodSchema, ZodError };
|
|
89
|
+
export { z, ZodSchema, ZodError };
|
|
91
90
|
export type { ZodIssue };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ZodError = exports.ZodSchema = exports.z =
|
|
3
|
+
exports.ZodError = exports.ZodSchema = exports.z = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
tslib_1.__exportStar(require("./array/array.util"), exports);
|
|
6
6
|
tslib_1.__exportStar(require("./define"), exports);
|
|
@@ -93,5 +93,3 @@ const zod_1 = require("zod");
|
|
|
93
93
|
Object.defineProperty(exports, "z", { enumerable: true, get: function () { return zod_1.z; } });
|
|
94
94
|
Object.defineProperty(exports, "ZodSchema", { enumerable: true, get: function () { return zod_1.ZodSchema; } });
|
|
95
95
|
Object.defineProperty(exports, "ZodError", { enumerable: true, get: function () { return zod_1.ZodError; } });
|
|
96
|
-
const is_1 = require("./vendor/is");
|
|
97
|
-
Object.defineProperty(exports, "is", { enumerable: true, get: function () { return is_1.is; } });
|
package/dist/promise/pRetry.js
CHANGED
|
@@ -56,8 +56,7 @@ async function pRetry(fn, opt = {}) {
|
|
|
56
56
|
}
|
|
57
57
|
if (attempt >= maxAttempts || (predicate && !predicate(err, attempt, maxAttempts))) {
|
|
58
58
|
// Give up
|
|
59
|
-
(0, __1._errorDataAppend)(err, opt.errorData);
|
|
60
|
-
throw err;
|
|
59
|
+
throw (0, __1._errorDataAppend)(err, opt.errorData);
|
|
61
60
|
}
|
|
62
61
|
// Retry after delay
|
|
63
62
|
delay *= delayMultiplier;
|
package/dist/promise/pTimeout.js
CHANGED
|
@@ -46,8 +46,7 @@ async function pTimeout(fn, opt) {
|
|
|
46
46
|
catch (err) {
|
|
47
47
|
// keep original stack
|
|
48
48
|
err.stack = fakeError.stack.replace('Error: TimeoutError', err.name + ': ' + err.message);
|
|
49
|
-
(0, error_util_1._errorDataAppend)(err, opt.errorData);
|
|
50
|
-
reject(err);
|
|
49
|
+
reject((0, error_util_1._errorDataAppend)(err, opt.errorData));
|
|
51
50
|
}
|
|
52
51
|
return;
|
|
53
52
|
}
|
|
@@ -141,18 +141,21 @@ export function _isErrorLike(o) {
|
|
|
141
141
|
}
|
|
142
142
|
/**
|
|
143
143
|
* Convenience function to safely add properties to Error's `data` object
|
|
144
|
-
* (even if it wasn't previously existing)
|
|
144
|
+
* (even if it wasn't previously existing).
|
|
145
|
+
* Mutates err.
|
|
146
|
+
* Returns err for convenience, so you can re-throw it directly.
|
|
145
147
|
*
|
|
146
148
|
* @example
|
|
147
149
|
*
|
|
148
150
|
* try {} catch (err) {
|
|
149
|
-
* _errorDataAppend(err, {
|
|
151
|
+
* throw _errorDataAppend(err, {
|
|
150
152
|
* httpStatusCode: 401,
|
|
151
153
|
* })
|
|
152
154
|
* }
|
|
153
155
|
*/
|
|
154
156
|
export function _errorDataAppend(err, data) {
|
|
155
157
|
if (!data)
|
|
156
|
-
return;
|
|
158
|
+
return err;
|
|
157
159
|
err.data = Object.assign(Object.assign({}, err.data), data);
|
|
160
|
+
return err;
|
|
158
161
|
}
|
package/dist-esm/http/fetcher.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference lib="dom"/>
|
|
2
2
|
/// <reference lib="dom.iterable"/>
|
|
3
|
+
var _a;
|
|
3
4
|
import { isServerSide } from '../env';
|
|
4
5
|
import { _assertErrorClassOrRethrow } from '../error/assert';
|
|
5
6
|
import { _anyToError, _anyToErrorObject, _errorLikeToErrorObject } from '../error/error.util';
|
|
@@ -60,21 +61,21 @@ export class Fetcher {
|
|
|
60
61
|
* Add BeforeRequest hook at the end of the hooks list.
|
|
61
62
|
*/
|
|
62
63
|
onBeforeRequest(hook) {
|
|
63
|
-
var
|
|
64
|
+
var _b;
|
|
64
65
|
;
|
|
65
|
-
((
|
|
66
|
+
((_b = this.cfg.hooks).beforeRequest || (_b.beforeRequest = [])).push(hook);
|
|
66
67
|
return this;
|
|
67
68
|
}
|
|
68
69
|
onAfterResponse(hook) {
|
|
69
|
-
var
|
|
70
|
+
var _b;
|
|
70
71
|
;
|
|
71
|
-
((
|
|
72
|
+
((_b = this.cfg.hooks).afterResponse || (_b.afterResponse = [])).push(hook);
|
|
72
73
|
return this;
|
|
73
74
|
}
|
|
74
75
|
onBeforeRetry(hook) {
|
|
75
|
-
var
|
|
76
|
+
var _b;
|
|
76
77
|
;
|
|
77
|
-
((
|
|
78
|
+
((_b = this.cfg.hooks).beforeRetry || (_b.beforeRetry = [])).push(hook);
|
|
78
79
|
return this;
|
|
79
80
|
}
|
|
80
81
|
static create(cfg = {}) {
|
|
@@ -120,7 +121,7 @@ export class Fetcher {
|
|
|
120
121
|
* Note: responseType defaults to `void`, so, override it if you expect different.
|
|
121
122
|
*/
|
|
122
123
|
async doFetch(opt) {
|
|
123
|
-
var
|
|
124
|
+
var _b, _c;
|
|
124
125
|
const req = this.normalizeOptions(opt);
|
|
125
126
|
const { logger } = this.cfg;
|
|
126
127
|
const { timeoutSeconds, init: { method }, } = req;
|
|
@@ -182,8 +183,8 @@ export class Fetcher {
|
|
|
182
183
|
// Separate Timeout will be introduced to "download and parse the body"
|
|
183
184
|
}
|
|
184
185
|
res.statusFamily = this.getStatusFamily(res);
|
|
185
|
-
res.statusCode = (
|
|
186
|
-
if ((
|
|
186
|
+
res.statusCode = (_b = res.fetchResponse) === null || _b === void 0 ? void 0 : _b.status;
|
|
187
|
+
if ((_c = res.fetchResponse) === null || _c === void 0 ? void 0 : _c.ok) {
|
|
187
188
|
try {
|
|
188
189
|
// We are applying a separate Timeout (as long as original Timeout for now) to "download and parse the body"
|
|
189
190
|
await pTimeout(async () => await this.onOkResponse(res), {
|
|
@@ -274,7 +275,7 @@ export class Fetcher {
|
|
|
274
275
|
return await globalThis.fetch(url, init);
|
|
275
276
|
}
|
|
276
277
|
async onNotOkResponse(res) {
|
|
277
|
-
var
|
|
278
|
+
var _b, _c;
|
|
278
279
|
let cause;
|
|
279
280
|
if (res.err) {
|
|
280
281
|
// This is only possible on JSON.parse error, or CORS error,
|
|
@@ -293,10 +294,10 @@ export class Fetcher {
|
|
|
293
294
|
message: 'Fetch failed',
|
|
294
295
|
data: {},
|
|
295
296
|
});
|
|
296
|
-
const message = [(
|
|
297
|
+
const message = [(_b = res.fetchResponse) === null || _b === void 0 ? void 0 : _b.status, res.signature].filter(Boolean).join(' ');
|
|
297
298
|
res.err = new HttpRequestError(message, _filterNullishValues({
|
|
298
299
|
response: res.fetchResponse,
|
|
299
|
-
responseStatusCode: ((
|
|
300
|
+
responseStatusCode: ((_c = res.fetchResponse) === null || _c === void 0 ? void 0 : _c.status) || 0,
|
|
300
301
|
// These properties are provided to be used in e.g custom Sentry error grouping
|
|
301
302
|
// Actually, disabled now, to avoid unnecessary error printing when both msg and data are printed
|
|
302
303
|
// Enabled, cause `data` is not printed by default when error is HttpError
|
|
@@ -313,7 +314,7 @@ export class Fetcher {
|
|
|
313
314
|
await this.processRetry(res);
|
|
314
315
|
}
|
|
315
316
|
async processRetry(res) {
|
|
316
|
-
var
|
|
317
|
+
var _b;
|
|
317
318
|
const { retryStatus } = res;
|
|
318
319
|
if (!this.shouldRetry(res)) {
|
|
319
320
|
retryStatus.retryStopped = true;
|
|
@@ -333,7 +334,7 @@ export class Fetcher {
|
|
|
333
334
|
if (res.err && (!retryStatus.retryStopped || res.req.logResponse)) {
|
|
334
335
|
this.cfg.logger.error([
|
|
335
336
|
' <<',
|
|
336
|
-
((
|
|
337
|
+
((_b = res.fetchResponse) === null || _b === void 0 ? void 0 : _b.status) || 0,
|
|
337
338
|
res.signature,
|
|
338
339
|
count &&
|
|
339
340
|
(retryStatus.retryAttempt || !retryStatus.retryStopped) &&
|
|
@@ -356,12 +357,12 @@ export class Fetcher {
|
|
|
356
357
|
await pDelay(timeout);
|
|
357
358
|
}
|
|
358
359
|
getRetryTimeout(res) {
|
|
359
|
-
var
|
|
360
|
+
var _b;
|
|
360
361
|
let timeout = 0;
|
|
361
362
|
// Handling http 429 with specific retry headers
|
|
362
363
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
|
|
363
364
|
if (res.fetchResponse && [429, 503].includes(res.fetchResponse.status)) {
|
|
364
|
-
const retryAfterStr = (
|
|
365
|
+
const retryAfterStr = (_b = res.fetchResponse.headers.get('retry-after')) !== null && _b !== void 0 ? _b : res.fetchResponse.headers.get('x-ratelimit-reset');
|
|
365
366
|
if (retryAfterStr) {
|
|
366
367
|
if (Number(retryAfterStr)) {
|
|
367
368
|
timeout = Number(retryAfterStr) * 1000;
|
|
@@ -391,13 +392,13 @@ export class Fetcher {
|
|
|
391
392
|
* statusCode of 0 (or absense of it) will BE retried.
|
|
392
393
|
*/
|
|
393
394
|
shouldRetry(res) {
|
|
394
|
-
var
|
|
395
|
+
var _b, _c, _d, _e, _f;
|
|
395
396
|
const { retryPost, retry3xx, retry4xx, retry5xx } = res.req;
|
|
396
397
|
const { method } = res.req.init;
|
|
397
398
|
if (method === 'POST' && !retryPost)
|
|
398
399
|
return false;
|
|
399
400
|
const { statusFamily } = res;
|
|
400
|
-
const statusCode = ((
|
|
401
|
+
const statusCode = ((_b = res.fetchResponse) === null || _b === void 0 ? void 0 : _b.status) || 0;
|
|
401
402
|
if (statusFamily === 5 && !retry5xx)
|
|
402
403
|
return false;
|
|
403
404
|
if ([408, 429].includes(statusCode)) {
|
|
@@ -409,14 +410,14 @@ export class Fetcher {
|
|
|
409
410
|
if (statusFamily === 3 && !retry3xx)
|
|
410
411
|
return false;
|
|
411
412
|
// should not retry on `unexpected redirect` in error.cause.cause
|
|
412
|
-
if ((
|
|
413
|
+
if ((_f = (_e = (_d = (_c = res.err) === null || _c === void 0 ? void 0 : _c.cause) === null || _d === void 0 ? void 0 : _d.cause) === null || _e === void 0 ? void 0 : _e.message) === null || _f === void 0 ? void 0 : _f.includes('unexpected redirect')) {
|
|
413
414
|
return false;
|
|
414
415
|
}
|
|
415
416
|
return true; // default is true
|
|
416
417
|
}
|
|
417
418
|
getStatusFamily(res) {
|
|
418
|
-
var
|
|
419
|
-
const status = (
|
|
419
|
+
var _b;
|
|
420
|
+
const status = (_b = res.fetchResponse) === null || _b === void 0 ? void 0 : _b.status;
|
|
420
421
|
if (!status)
|
|
421
422
|
return;
|
|
422
423
|
if (status >= 500)
|
|
@@ -449,8 +450,8 @@ export class Fetcher {
|
|
|
449
450
|
return shortUrl;
|
|
450
451
|
}
|
|
451
452
|
normalizeCfg(cfg) {
|
|
452
|
-
var
|
|
453
|
-
if ((
|
|
453
|
+
var _b;
|
|
454
|
+
if ((_b = cfg.baseUrl) === null || _b === void 0 ? void 0 : _b.endsWith('/')) {
|
|
454
455
|
console.warn(`Fetcher: baseUrl should not end with slash: ${cfg.baseUrl}`);
|
|
455
456
|
cfg.baseUrl = cfg.baseUrl.slice(0, cfg.baseUrl.length - 1);
|
|
456
457
|
}
|
|
@@ -477,7 +478,7 @@ export class Fetcher {
|
|
|
477
478
|
retry: Object.assign({}, defRetryOptions),
|
|
478
479
|
init: {
|
|
479
480
|
method: cfg.method || 'GET',
|
|
480
|
-
headers: Object.assign({ 'user-agent':
|
|
481
|
+
headers: _filterNullishValues(Object.assign({ 'user-agent': Fetcher.userAgent }, cfg.headers)),
|
|
481
482
|
credentials: cfg.credentials,
|
|
482
483
|
redirect: cfg.redirect,
|
|
483
484
|
},
|
|
@@ -487,7 +488,7 @@ export class Fetcher {
|
|
|
487
488
|
return norm;
|
|
488
489
|
}
|
|
489
490
|
normalizeOptions(opt) {
|
|
490
|
-
var
|
|
491
|
+
var _b;
|
|
491
492
|
const req = Object.assign(Object.assign(Object.assign(Object.assign({}, _pick(this.cfg, [
|
|
492
493
|
'timeoutSeconds',
|
|
493
494
|
'retryPost',
|
|
@@ -503,6 +504,8 @@ export class Fetcher {
|
|
|
503
504
|
])), { started: Date.now() }), _omit(opt, ['method', 'headers', 'credentials'])), { inputUrl: opt.url || '', fullUrl: opt.url || '', retry: Object.assign(Object.assign({}, this.cfg.retry), _filterUndefinedValues(opt.retry || {})), init: _merge(Object.assign(Object.assign({}, this.cfg.init), { headers: Object.assign({}, this.cfg.init.headers), method: opt.method || this.cfg.init.method, credentials: opt.credentials || this.cfg.init.credentials, redirect: opt.redirect || this.cfg.init.redirect || 'follow' }), {
|
|
504
505
|
headers: _mapKeys(opt.headers || {}, k => k.toLowerCase()),
|
|
505
506
|
}) });
|
|
507
|
+
// Because all header values are stringified, so `a: undefined` becomes `undefined` as a string
|
|
508
|
+
_filterNullishValues(req.init.headers, true);
|
|
506
509
|
// setup url
|
|
507
510
|
const baseUrl = opt.baseUrl || this.cfg.baseUrl;
|
|
508
511
|
if (baseUrl) {
|
|
@@ -540,10 +543,19 @@ export class Fetcher {
|
|
|
540
543
|
req.init.body = opt.body;
|
|
541
544
|
}
|
|
542
545
|
// Unless `accept` header was already set - set it based on responseType
|
|
543
|
-
(
|
|
546
|
+
(_b = req.init.headers)['accept'] || (_b['accept'] = acceptByResponseType[req.responseType]);
|
|
544
547
|
return req;
|
|
545
548
|
}
|
|
546
549
|
}
|
|
550
|
+
_a = Fetcher;
|
|
551
|
+
/**
|
|
552
|
+
* Included in UserAgent when run in Node.
|
|
553
|
+
* In the browser it's not included, as we want "browser own" UserAgent to be included instead.
|
|
554
|
+
*
|
|
555
|
+
* Version is to be incremented every time a difference in behaviour (or a bugfix) is done.
|
|
556
|
+
*/
|
|
557
|
+
Fetcher.VERSION = 1;
|
|
558
|
+
Fetcher.userAgent = isServerSide() ? `fetcher${_a.VERSION}` : undefined;
|
|
547
559
|
export function getFetcher(cfg = {}) {
|
|
548
560
|
return Fetcher.create(cfg);
|
|
549
561
|
}
|
package/dist-esm/index.js
CHANGED
|
@@ -86,5 +86,4 @@ export * from './web';
|
|
|
86
86
|
export * from './zod/zod.util';
|
|
87
87
|
export * from './zod/zod.shared.schemas';
|
|
88
88
|
import { z, ZodSchema, ZodError } from 'zod';
|
|
89
|
-
|
|
90
|
-
export { is, z, ZodSchema, ZodError };
|
|
89
|
+
export { z, ZodSchema, ZodError };
|
|
@@ -52,8 +52,7 @@ export async function pRetry(fn, opt = {}) {
|
|
|
52
52
|
}
|
|
53
53
|
if (attempt >= maxAttempts || (predicate && !predicate(err, attempt, maxAttempts))) {
|
|
54
54
|
// Give up
|
|
55
|
-
_errorDataAppend(err, opt.errorData);
|
|
56
|
-
throw err;
|
|
55
|
+
throw _errorDataAppend(err, opt.errorData);
|
|
57
56
|
}
|
|
58
57
|
// Retry after delay
|
|
59
58
|
delay *= delayMultiplier;
|
|
@@ -41,8 +41,7 @@ export async function pTimeout(fn, opt) {
|
|
|
41
41
|
catch (err) {
|
|
42
42
|
// keep original stack
|
|
43
43
|
err.stack = fakeError.stack.replace('Error: TimeoutError', err.name + ': ' + err.message);
|
|
44
|
-
_errorDataAppend(err, opt.errorData);
|
|
45
|
-
reject(err);
|
|
44
|
+
reject(_errorDataAppend(err, opt.errorData));
|
|
46
45
|
}
|
|
47
46
|
return;
|
|
48
47
|
}
|
package/package.json
CHANGED
package/src/error/error.util.ts
CHANGED
|
@@ -185,21 +185,24 @@ export function _isErrorLike(o: any): o is ErrorLike {
|
|
|
185
185
|
|
|
186
186
|
/**
|
|
187
187
|
* Convenience function to safely add properties to Error's `data` object
|
|
188
|
-
* (even if it wasn't previously existing)
|
|
188
|
+
* (even if it wasn't previously existing).
|
|
189
|
+
* Mutates err.
|
|
190
|
+
* Returns err for convenience, so you can re-throw it directly.
|
|
189
191
|
*
|
|
190
192
|
* @example
|
|
191
193
|
*
|
|
192
194
|
* try {} catch (err) {
|
|
193
|
-
* _errorDataAppend(err, {
|
|
195
|
+
* throw _errorDataAppend(err, {
|
|
194
196
|
* httpStatusCode: 401,
|
|
195
197
|
* })
|
|
196
198
|
* }
|
|
197
199
|
*/
|
|
198
|
-
export function _errorDataAppend(err:
|
|
199
|
-
if (!data) return
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
...err.data,
|
|
200
|
+
export function _errorDataAppend<ERR>(err: ERR, data?: ErrorData): ERR {
|
|
201
|
+
if (!data) return err
|
|
202
|
+
;(err as any).data = {
|
|
203
|
+
...(err as any).data,
|
|
203
204
|
...data,
|
|
204
205
|
}
|
|
206
|
+
|
|
207
|
+
return err
|
|
205
208
|
}
|
package/src/http/fetcher.ts
CHANGED
|
@@ -58,6 +58,15 @@ const defRetryOptions: FetcherRetryOptions = {
|
|
|
58
58
|
* Works in both Browser and Node, using `globalThis.fetch`.
|
|
59
59
|
*/
|
|
60
60
|
export class Fetcher {
|
|
61
|
+
/**
|
|
62
|
+
* Included in UserAgent when run in Node.
|
|
63
|
+
* In the browser it's not included, as we want "browser own" UserAgent to be included instead.
|
|
64
|
+
*
|
|
65
|
+
* Version is to be incremented every time a difference in behaviour (or a bugfix) is done.
|
|
66
|
+
*/
|
|
67
|
+
static readonly VERSION = 1
|
|
68
|
+
static readonly userAgent = isServerSide() ? `fetcher${this.VERSION}` : undefined
|
|
69
|
+
|
|
61
70
|
private constructor(cfg: FetcherCfg & FetcherOptions = {}) {
|
|
62
71
|
if (typeof globalThis.fetch !== 'function') {
|
|
63
72
|
throw new TypeError(`globalThis.fetch is not available`)
|
|
@@ -594,10 +603,10 @@ export class Fetcher {
|
|
|
594
603
|
retry: { ...defRetryOptions },
|
|
595
604
|
init: {
|
|
596
605
|
method: cfg.method || 'GET',
|
|
597
|
-
headers: {
|
|
598
|
-
'user-agent':
|
|
606
|
+
headers: _filterNullishValues({
|
|
607
|
+
'user-agent': Fetcher.userAgent,
|
|
599
608
|
...cfg.headers,
|
|
600
|
-
},
|
|
609
|
+
}),
|
|
601
610
|
credentials: cfg.credentials,
|
|
602
611
|
redirect: cfg.redirect,
|
|
603
612
|
},
|
|
@@ -647,6 +656,10 @@ export class Fetcher {
|
|
|
647
656
|
} satisfies RequestInit,
|
|
648
657
|
),
|
|
649
658
|
}
|
|
659
|
+
|
|
660
|
+
// Because all header values are stringified, so `a: undefined` becomes `undefined` as a string
|
|
661
|
+
_filterNullishValues(req.init.headers, true)
|
|
662
|
+
|
|
650
663
|
// setup url
|
|
651
664
|
const baseUrl = opt.baseUrl || this.cfg.baseUrl
|
|
652
665
|
if (baseUrl) {
|
package/src/index.ts
CHANGED
|
@@ -86,7 +86,6 @@ export * from './web'
|
|
|
86
86
|
export * from './zod/zod.util'
|
|
87
87
|
export * from './zod/zod.shared.schemas'
|
|
88
88
|
import { z, ZodSchema, ZodError, ZodIssue } from 'zod'
|
|
89
|
-
import { is } from './vendor/is'
|
|
90
89
|
|
|
91
|
-
export {
|
|
90
|
+
export { z, ZodSchema, ZodError }
|
|
92
91
|
export type { ZodIssue }
|
package/src/promise/pRetry.ts
CHANGED
|
@@ -163,8 +163,7 @@ export async function pRetry<T>(
|
|
|
163
163
|
|
|
164
164
|
if (attempt >= maxAttempts || (predicate && !predicate(err as Error, attempt, maxAttempts))) {
|
|
165
165
|
// Give up
|
|
166
|
-
_errorDataAppend(err, opt.errorData)
|
|
167
|
-
throw err
|
|
166
|
+
throw _errorDataAppend(err, opt.errorData)
|
|
168
167
|
}
|
|
169
168
|
|
|
170
169
|
// Retry after delay
|
package/src/promise/pTimeout.ts
CHANGED
|
@@ -82,8 +82,7 @@ export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions)
|
|
|
82
82
|
} catch (err: any) {
|
|
83
83
|
// keep original stack
|
|
84
84
|
err.stack = fakeError.stack!.replace('Error: TimeoutError', err.name + ': ' + err.message)
|
|
85
|
-
_errorDataAppend(err, opt.errorData)
|
|
86
|
-
reject(err)
|
|
85
|
+
reject(_errorDataAppend(err, opt.errorData))
|
|
87
86
|
}
|
|
88
87
|
return
|
|
89
88
|
}
|