@fuman/fetch 0.0.12 → 0.1.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/addons/tough-cookie.d.cts +7 -3
- package/addons/tough-cookie.d.ts +7 -3
- package/ffetch.cjs +5 -5
- package/ffetch.js +5 -5
- package/package.json +2 -2
- package/tough.cjs +12 -5
- package/tough.js +12 -5
|
@@ -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
|
@@ -25,8 +25,8 @@ class FfetchResultImpl {
|
|
|
25
25
|
_options;
|
|
26
26
|
_headers;
|
|
27
27
|
#stack;
|
|
28
|
-
constructor(
|
|
29
|
-
this.#fetch =
|
|
28
|
+
constructor(fetch, url, init, headers, options, stack) {
|
|
29
|
+
this.#fetch = fetch;
|
|
30
30
|
this._init = init;
|
|
31
31
|
this._url = url;
|
|
32
32
|
this._options = options;
|
|
@@ -125,7 +125,7 @@ function _wrapMethod(method, fn) {
|
|
|
125
125
|
}
|
|
126
126
|
function createFfetch(baseOptions = {}) {
|
|
127
127
|
const captureStackTrace = baseOptions.captureStackTrace ?? true;
|
|
128
|
-
const baseFetch = baseOptions.fetch ?? fetch;
|
|
128
|
+
const baseFetch = baseOptions.fetch ?? globalThis.fetch?.bind(globalThis);
|
|
129
129
|
const wrappedFetch = baseOptions.middlewares !== void 0 && baseOptions.middlewares.length > 0 ? utils.composeMiddlewares(baseOptions.middlewares, baseFetch) : baseFetch;
|
|
130
130
|
const addons = baseOptions.addons ?? [];
|
|
131
131
|
let FfetchResultInner;
|
|
@@ -219,8 +219,8 @@ function createFfetch(baseOptions = {}) {
|
|
|
219
219
|
...otherOptions.middlewares ?? []
|
|
220
220
|
],
|
|
221
221
|
headers: {
|
|
222
|
-
...baseOptions.headers,
|
|
223
|
-
...otherOptions.headers
|
|
222
|
+
...headersToObject(baseOptions.headers),
|
|
223
|
+
...headersToObject(otherOptions.headers)
|
|
224
224
|
}
|
|
225
225
|
});
|
|
226
226
|
};
|
package/ffetch.js
CHANGED
|
@@ -23,8 +23,8 @@ class FfetchResultImpl {
|
|
|
23
23
|
_options;
|
|
24
24
|
_headers;
|
|
25
25
|
#stack;
|
|
26
|
-
constructor(
|
|
27
|
-
this.#fetch =
|
|
26
|
+
constructor(fetch, url, init, headers, options, stack) {
|
|
27
|
+
this.#fetch = fetch;
|
|
28
28
|
this._init = init;
|
|
29
29
|
this._url = url;
|
|
30
30
|
this._options = options;
|
|
@@ -123,7 +123,7 @@ function _wrapMethod(method, fn) {
|
|
|
123
123
|
}
|
|
124
124
|
function createFfetch(baseOptions = {}) {
|
|
125
125
|
const captureStackTrace = baseOptions.captureStackTrace ?? true;
|
|
126
|
-
const baseFetch = baseOptions.fetch ?? fetch;
|
|
126
|
+
const baseFetch = baseOptions.fetch ?? globalThis.fetch?.bind(globalThis);
|
|
127
127
|
const wrappedFetch = baseOptions.middlewares !== void 0 && baseOptions.middlewares.length > 0 ? composeMiddlewares(baseOptions.middlewares, baseFetch) : baseFetch;
|
|
128
128
|
const addons = baseOptions.addons ?? [];
|
|
129
129
|
let FfetchResultInner;
|
|
@@ -217,8 +217,8 @@ function createFfetch(baseOptions = {}) {
|
|
|
217
217
|
...otherOptions.middlewares ?? []
|
|
218
218
|
],
|
|
219
219
|
headers: {
|
|
220
|
-
...baseOptions.headers,
|
|
221
|
-
...otherOptions.headers
|
|
220
|
+
...headersToObject(baseOptions.headers),
|
|
221
|
+
...headersToObject(otherOptions.headers)
|
|
222
222
|
}
|
|
223
223
|
});
|
|
224
224
|
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fuman/fetch",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"description": "tiny wrapper over fetch",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@fuman/utils": "^0.0.
|
|
8
|
+
"@fuman/utils": "^0.0.14"
|
|
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
|
};
|