@fuman/fetch 0.0.13 → 0.1.1
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/addons/rate-limit.d.cts +1 -1
- package/addons/rate-limit.d.ts +1 -1
- package/addons/tough-cookie.d.cts +7 -3
- package/addons/tough-cookie.d.ts +7 -3
- package/ffetch.cjs +5 -0
- package/ffetch.d.cts +24 -0
- package/ffetch.d.ts +24 -0
- package/ffetch.js +5 -0
- package/package.json +2 -2
- package/tough.cjs +12 -5
- package/tough.js +12 -5
package/addons/rate-limit.d.cts
CHANGED
package/addons/rate-limit.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { CookieJar } from 'tough-cookie';
|
|
1
|
+
import { CookieJar, GetCookiesOptions, SetCookieOptions } from 'tough-cookie';
|
|
2
2
|
import { FfetchAddon } from './types.js';
|
|
3
3
|
export interface FfetchToughCookieAddon {
|
|
4
|
-
/** cookie jar to use */
|
|
5
|
-
cookies?: CookieJar
|
|
4
|
+
/** cookie jar to use, or extended config */
|
|
5
|
+
cookies?: CookieJar | {
|
|
6
|
+
jar: CookieJar;
|
|
7
|
+
getCookiesOptions?: GetCookiesOptions;
|
|
8
|
+
setCookieOptions?: SetCookieOptions;
|
|
9
|
+
};
|
|
6
10
|
}
|
|
7
11
|
export declare function toughCookieAddon(): FfetchAddon<FfetchToughCookieAddon, object>;
|
package/addons/tough-cookie.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { CookieJar } from 'tough-cookie';
|
|
1
|
+
import { CookieJar, GetCookiesOptions, SetCookieOptions } from 'tough-cookie';
|
|
2
2
|
import { FfetchAddon } from './types.js';
|
|
3
3
|
export interface FfetchToughCookieAddon {
|
|
4
|
-
/** cookie jar to use */
|
|
5
|
-
cookies?: CookieJar
|
|
4
|
+
/** cookie jar to use, or extended config */
|
|
5
|
+
cookies?: CookieJar | {
|
|
6
|
+
jar: CookieJar;
|
|
7
|
+
getCookiesOptions?: GetCookiesOptions;
|
|
8
|
+
setCookieOptions?: SetCookieOptions;
|
|
9
|
+
};
|
|
6
10
|
}
|
|
7
11
|
export declare function toughCookieAddon(): FfetchAddon<FfetchToughCookieAddon, object>;
|
package/ffetch.cjs
CHANGED
|
@@ -66,6 +66,9 @@ class FfetchResultImpl {
|
|
|
66
66
|
} catch {
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
|
+
if (this._options.mapError != null) {
|
|
70
|
+
throw this._options.mapError(err);
|
|
71
|
+
}
|
|
69
72
|
throw err;
|
|
70
73
|
}
|
|
71
74
|
return res;
|
|
@@ -195,6 +198,8 @@ function createFfetch(baseOptions = {}) {
|
|
|
195
198
|
}
|
|
196
199
|
init.headers = headers;
|
|
197
200
|
options.validateResponse ??= baseOptions.validateResponse;
|
|
201
|
+
options.readBodyOnError ??= baseOptions.readBodyOnError;
|
|
202
|
+
options.mapError ??= baseOptions.mapError;
|
|
198
203
|
return new FfetchResultInner(fetcher, url, init, headers, options, stack);
|
|
199
204
|
};
|
|
200
205
|
const fn = fn_;
|
package/ffetch.d.cts
CHANGED
|
@@ -23,6 +23,30 @@ export interface FfetchOptions {
|
|
|
23
23
|
* @default true
|
|
24
24
|
*/
|
|
25
25
|
readBodyOnError?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* optional function to map the error
|
|
28
|
+
*
|
|
29
|
+
* useful when your API has an app-level error format, and you want to throw a custom error
|
|
30
|
+
*
|
|
31
|
+
* @param err original {@link HttpError} that would have been thrown
|
|
32
|
+
* @returns the error to be thrown instead
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const api = createFfetch({
|
|
36
|
+
* ...,
|
|
37
|
+
* mapError: (err) => {
|
|
38
|
+
* if (err.response.headers.get('Content-Type') !== 'application/json') return err
|
|
39
|
+
* try {
|
|
40
|
+
* // note: .bodyText is only available if `readBodyOnError` is true
|
|
41
|
+
* const json = JSON.parse(err.bodyText)
|
|
42
|
+
* throw new ApiError(json.errorCode, json.message)
|
|
43
|
+
* } catch {
|
|
44
|
+
* return err
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
* })
|
|
48
|
+
*/
|
|
49
|
+
mapError?: (err: HttpError) => Error;
|
|
26
50
|
/**
|
|
27
51
|
* base url to be prepended to the url
|
|
28
52
|
*
|
package/ffetch.d.ts
CHANGED
|
@@ -23,6 +23,30 @@ export interface FfetchOptions {
|
|
|
23
23
|
* @default true
|
|
24
24
|
*/
|
|
25
25
|
readBodyOnError?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* optional function to map the error
|
|
28
|
+
*
|
|
29
|
+
* useful when your API has an app-level error format, and you want to throw a custom error
|
|
30
|
+
*
|
|
31
|
+
* @param err original {@link HttpError} that would have been thrown
|
|
32
|
+
* @returns the error to be thrown instead
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const api = createFfetch({
|
|
36
|
+
* ...,
|
|
37
|
+
* mapError: (err) => {
|
|
38
|
+
* if (err.response.headers.get('Content-Type') !== 'application/json') return err
|
|
39
|
+
* try {
|
|
40
|
+
* // note: .bodyText is only available if `readBodyOnError` is true
|
|
41
|
+
* const json = JSON.parse(err.bodyText)
|
|
42
|
+
* throw new ApiError(json.errorCode, json.message)
|
|
43
|
+
* } catch {
|
|
44
|
+
* return err
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
* })
|
|
48
|
+
*/
|
|
49
|
+
mapError?: (err: HttpError) => Error;
|
|
26
50
|
/**
|
|
27
51
|
* base url to be prepended to the url
|
|
28
52
|
*
|
package/ffetch.js
CHANGED
|
@@ -64,6 +64,9 @@ class FfetchResultImpl {
|
|
|
64
64
|
} catch {
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
|
+
if (this._options.mapError != null) {
|
|
68
|
+
throw this._options.mapError(err);
|
|
69
|
+
}
|
|
67
70
|
throw err;
|
|
68
71
|
}
|
|
69
72
|
return res;
|
|
@@ -193,6 +196,8 @@ function createFfetch(baseOptions = {}) {
|
|
|
193
196
|
}
|
|
194
197
|
init.headers = headers;
|
|
195
198
|
options.validateResponse ??= baseOptions.validateResponse;
|
|
199
|
+
options.readBodyOnError ??= baseOptions.readBodyOnError;
|
|
200
|
+
options.mapError ??= baseOptions.mapError;
|
|
196
201
|
return new FfetchResultInner(fetcher, url, init, headers, options, stack);
|
|
197
202
|
};
|
|
198
203
|
const fn = fn_;
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fuman/fetch",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.1.1",
|
|
5
5
|
"description": "tiny wrapper over fetch",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@fuman/utils": "^0.0.
|
|
8
|
+
"@fuman/utils": "^0.0.15"
|
|
9
9
|
},
|
|
10
10
|
"peerDependencies": {
|
|
11
11
|
"@badrap/valita": ">=0.4.0",
|
package/tough.cjs
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
function cookieJarMiddleware(
|
|
3
|
+
function cookieJarMiddleware({
|
|
4
|
+
jar,
|
|
5
|
+
getCookiesOptions,
|
|
6
|
+
setCookieOptions = { ignoreError: true }
|
|
7
|
+
}) {
|
|
4
8
|
return async (ctx, next) => {
|
|
5
|
-
ctx.headers.append("Cookie", await jar.getCookieString(ctx.url));
|
|
9
|
+
ctx.headers.append("Cookie", await jar.getCookieString(ctx.url, getCookiesOptions));
|
|
6
10
|
const res = await next(ctx);
|
|
7
11
|
for (const header of res.headers.getSetCookie()) {
|
|
8
|
-
await jar.setCookie(header, res.url);
|
|
12
|
+
await jar.setCookie(header, res.url, setCookieOptions);
|
|
9
13
|
}
|
|
10
14
|
return res;
|
|
11
15
|
};
|
|
@@ -14,9 +18,12 @@ function toughCookieAddon() {
|
|
|
14
18
|
return {
|
|
15
19
|
beforeRequest(ctx) {
|
|
16
20
|
if (ctx.options.cookies != null || ctx.baseOptions.cookies != null) {
|
|
17
|
-
|
|
21
|
+
let cfg = ctx.options.cookies ?? ctx.baseOptions.cookies;
|
|
22
|
+
if (!("jar" in cfg)) {
|
|
23
|
+
cfg = { jar: cfg };
|
|
24
|
+
}
|
|
18
25
|
ctx.options.middlewares ??= [];
|
|
19
|
-
ctx.options.middlewares.push(cookieJarMiddleware(
|
|
26
|
+
ctx.options.middlewares.push(cookieJarMiddleware(cfg));
|
|
20
27
|
}
|
|
21
28
|
}
|
|
22
29
|
};
|
package/tough.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
function cookieJarMiddleware(
|
|
1
|
+
function cookieJarMiddleware({
|
|
2
|
+
jar,
|
|
3
|
+
getCookiesOptions,
|
|
4
|
+
setCookieOptions = { ignoreError: true }
|
|
5
|
+
}) {
|
|
2
6
|
return async (ctx, next) => {
|
|
3
|
-
ctx.headers.append("Cookie", await jar.getCookieString(ctx.url));
|
|
7
|
+
ctx.headers.append("Cookie", await jar.getCookieString(ctx.url, getCookiesOptions));
|
|
4
8
|
const res = await next(ctx);
|
|
5
9
|
for (const header of res.headers.getSetCookie()) {
|
|
6
|
-
await jar.setCookie(header, res.url);
|
|
10
|
+
await jar.setCookie(header, res.url, setCookieOptions);
|
|
7
11
|
}
|
|
8
12
|
return res;
|
|
9
13
|
};
|
|
@@ -12,9 +16,12 @@ function toughCookieAddon() {
|
|
|
12
16
|
return {
|
|
13
17
|
beforeRequest(ctx) {
|
|
14
18
|
if (ctx.options.cookies != null || ctx.baseOptions.cookies != null) {
|
|
15
|
-
|
|
19
|
+
let cfg = ctx.options.cookies ?? ctx.baseOptions.cookies;
|
|
20
|
+
if (!("jar" in cfg)) {
|
|
21
|
+
cfg = { jar: cfg };
|
|
22
|
+
}
|
|
16
23
|
ctx.options.middlewares ??= [];
|
|
17
|
-
ctx.options.middlewares.push(cookieJarMiddleware(
|
|
24
|
+
ctx.options.middlewares.push(cookieJarMiddleware(cfg));
|
|
18
25
|
}
|
|
19
26
|
}
|
|
20
27
|
};
|