@readyfor/api-client-base 1.155.0-pr1530.36d885b → 1.155.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/dist/{apiClientConfigStore-DDleIkQN.mjs → apiClientConfigStore-Buly03AC.mjs} +3 -2
- package/dist/apiClientConfigStore.d.ts +3 -5
- package/dist/apiClientConfigStore.js +3 -2
- package/dist/fetcher.js +10 -10
- package/dist/index.mjs +11 -11
- package/dist/react/index.mjs +3 -3
- package/dist/react/useApiClientConfig.js +2 -2
- package/package.json +1 -1
|
@@ -79,8 +79,9 @@ const mergeRequestInit = ({ headers: baseHeaders, ...base }, { headers: override
|
|
|
79
79
|
const store = globalThis[Symbol.for("@readyfor/api-client-base/config")] ??= createStore({});
|
|
80
80
|
const setApiClientConfig = (config) => store.setState(() => config);
|
|
81
81
|
const buildRequestInitWithDefaultConfig = (customRequestInit = {}) => {
|
|
82
|
-
const { defaultRequestInit
|
|
83
|
-
|
|
82
|
+
const { defaultRequestInit } = store.getState();
|
|
83
|
+
if (typeof defaultRequestInit === "function") return customRequestInit;
|
|
84
|
+
return mergeRequestInit(defaultRequestInit ?? {}, customRequestInit);
|
|
84
85
|
};
|
|
85
86
|
//#endregion
|
|
86
87
|
export { mergeHeadersInit as a, mergeRequestInit as i, setApiClientConfig as n, toHeadersInit as o, store as r, createStore as s, buildRequestInitWithDefaultConfig as t };
|
|
@@ -11,15 +11,13 @@ type LoggingConfig = {
|
|
|
11
11
|
};
|
|
12
12
|
export type RequestConfig = {
|
|
13
13
|
hostName?: string | ((path: string) => string);
|
|
14
|
-
defaultRequestInit?: RequestInit;
|
|
15
14
|
/**
|
|
16
|
-
*
|
|
15
|
+
* 関数を渡すと、設定時ではなく API 呼び出しごとに fetch の直前で評価される。リクエストごとに変わる値は設定時ではなくこの関数の中で読むこと。
|
|
17
16
|
* すべての宛先への fetch で呼ばれるため、転送してよい宛先かは必要に応じて `input` で判断すること。
|
|
18
17
|
* 受け取ったリクエストヘッダーを丸ごと返さず、必要なもの(Cookie や User-Agent など)だけを返すこと。host や connection などが混ざると fetch が失敗する。
|
|
19
|
-
*
|
|
20
|
-
* サーバー側での利用を想定している。ブラウザでは Cookie などは forbidden header name のため送られない。
|
|
18
|
+
* 同じフィールドや同名のヘッダーを API 呼び出し時の RequestInit で指定した場合は、そちらが優先される。
|
|
21
19
|
*/
|
|
22
|
-
|
|
20
|
+
defaultRequestInit?: RequestInit | ((input: RequestInfo) => RequestInit | Promise<RequestInit>);
|
|
23
21
|
onError?: (error: unknown) => void;
|
|
24
22
|
};
|
|
25
23
|
export type Config = RequestConfig & DeserializeResponseConfig & LoggingConfig;
|
|
@@ -4,8 +4,9 @@ const require_utils_requestInit = require("./utils/requestInit.js");
|
|
|
4
4
|
const store = globalThis[Symbol.for("@readyfor/api-client-base/config")] ??= require_store.createStore({});
|
|
5
5
|
const setApiClientConfig = (config) => store.setState(() => config);
|
|
6
6
|
const buildRequestInitWithDefaultConfig = (customRequestInit = {}) => {
|
|
7
|
-
const { defaultRequestInit
|
|
8
|
-
|
|
7
|
+
const { defaultRequestInit } = store.getState();
|
|
8
|
+
if (typeof defaultRequestInit === "function") return customRequestInit;
|
|
9
|
+
return require_utils_requestInit.mergeRequestInit(defaultRequestInit ?? {}, customRequestInit);
|
|
9
10
|
};
|
|
10
11
|
//#endregion
|
|
11
12
|
exports.buildRequestInitWithDefaultConfig = buildRequestInitWithDefaultConfig;
|
package/dist/fetcher.js
CHANGED
|
@@ -4,13 +4,13 @@ const require_apiClientConfigStore = require("./apiClientConfigStore.js");
|
|
|
4
4
|
const require_apiError = require("./apiError.js");
|
|
5
5
|
//#region src/fetcher.ts
|
|
6
6
|
const defaultRequestInit = { credentials: "include" };
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const {
|
|
10
|
-
if (
|
|
11
|
-
return Promise.resolve().then(() =>
|
|
7
|
+
const fetchWithDefaultRequestInit = (input, customRequestInit, requestInit) => {
|
|
8
|
+
const callerRequestInit = require_utils_requestInit.mergeRequestInit(customRequestInit, requestInit);
|
|
9
|
+
const { defaultRequestInit: configDefaultRequestInit } = require_apiClientConfigStore.store.getState();
|
|
10
|
+
if (typeof configDefaultRequestInit !== "function") return fetch(input, require_utils_requestInit.mergeRequestInit(defaultRequestInit, callerRequestInit));
|
|
11
|
+
return Promise.resolve().then(() => configDefaultRequestInit(input)).then((resolved) => fetch(input, require_utils_requestInit.mergeRequestInit(defaultRequestInit, require_utils_requestInit.mergeRequestInit(resolved, callerRequestInit))));
|
|
12
12
|
};
|
|
13
|
-
const createJsonFetcher = (schema, customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
13
|
+
const createJsonFetcher = (schema, customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithDefaultRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
14
14
|
options.onResponse?.(res);
|
|
15
15
|
const text = await res.text();
|
|
16
16
|
const body = text ? JSON.parse(text) : {};
|
|
@@ -31,7 +31,7 @@ const createJsonFetcher = (schema, customRequestInit = {}, options = {}) => (inp
|
|
|
31
31
|
}
|
|
32
32
|
throw result.error;
|
|
33
33
|
});
|
|
34
|
-
const createTextFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
34
|
+
const createTextFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithDefaultRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
35
35
|
options.onResponse?.(res);
|
|
36
36
|
const body = await res.text();
|
|
37
37
|
if (res.ok) return body;
|
|
@@ -41,7 +41,7 @@ const createTextFetcher = (customRequestInit = {}, options = {}) => (input, requ
|
|
|
41
41
|
require_apiClientConfigStore.store.getState().onError?.(error);
|
|
42
42
|
throw error;
|
|
43
43
|
});
|
|
44
|
-
const createBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
44
|
+
const createBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithDefaultRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
45
45
|
options.onResponse?.(res);
|
|
46
46
|
const body = await res.blob();
|
|
47
47
|
if (res.ok) return {
|
|
@@ -54,7 +54,7 @@ const createBlobFetcher = (customRequestInit = {}, options = {}) => (input, requ
|
|
|
54
54
|
require_apiClientConfigStore.store.getState().onError?.(error);
|
|
55
55
|
throw error;
|
|
56
56
|
});
|
|
57
|
-
const createFileOrBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
57
|
+
const createFileOrBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithDefaultRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
58
58
|
options.onResponse?.(res);
|
|
59
59
|
const body = await res.blob();
|
|
60
60
|
if (res.ok) return {
|
|
@@ -67,7 +67,7 @@ const createFileOrBlobFetcher = (customRequestInit = {}, options = {}) => (input
|
|
|
67
67
|
require_apiClientConfigStore.store.getState().onError?.(error);
|
|
68
68
|
throw error;
|
|
69
69
|
});
|
|
70
|
-
const createVoidFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
70
|
+
const createVoidFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithDefaultRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
71
71
|
options.onResponse?.(res);
|
|
72
72
|
if (res.ok) return;
|
|
73
73
|
const code = Object.values(require_apiError.errorStatusCode).includes(res.status) ? res.status : 999;
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as mergeHeadersInit, i as mergeRequestInit, n as setApiClientConfig, o as toHeadersInit, r as store, s as createStore, t as buildRequestInitWithDefaultConfig } from "./apiClientConfigStore-
|
|
1
|
+
import { a as mergeHeadersInit, i as mergeRequestInit, n as setApiClientConfig, o as toHeadersInit, r as store, s as createStore, t as buildRequestInitWithDefaultConfig } from "./apiClientConfigStore-Buly03AC.mjs";
|
|
2
2
|
import qs from "qs";
|
|
3
3
|
//#region src/zod.ts
|
|
4
4
|
const schemaForType = () => (arg) => {
|
|
@@ -79,13 +79,13 @@ const getErrorStatus = (response) => {
|
|
|
79
79
|
//#endregion
|
|
80
80
|
//#region src/fetcher.ts
|
|
81
81
|
const defaultRequestInit = { credentials: "include" };
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
const {
|
|
85
|
-
if (
|
|
86
|
-
return Promise.resolve().then(() =>
|
|
82
|
+
const fetchWithDefaultRequestInit = (input, customRequestInit, requestInit) => {
|
|
83
|
+
const callerRequestInit = mergeRequestInit(customRequestInit, requestInit);
|
|
84
|
+
const { defaultRequestInit: configDefaultRequestInit } = store.getState();
|
|
85
|
+
if (typeof configDefaultRequestInit !== "function") return fetch(input, mergeRequestInit(defaultRequestInit, callerRequestInit));
|
|
86
|
+
return Promise.resolve().then(() => configDefaultRequestInit(input)).then((resolved) => fetch(input, mergeRequestInit(defaultRequestInit, mergeRequestInit(resolved, callerRequestInit))));
|
|
87
87
|
};
|
|
88
|
-
const createJsonFetcher = (schema, customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
88
|
+
const createJsonFetcher = (schema, customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithDefaultRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
89
89
|
options.onResponse?.(res);
|
|
90
90
|
const text = await res.text();
|
|
91
91
|
const body = text ? JSON.parse(text) : {};
|
|
@@ -105,7 +105,7 @@ const createJsonFetcher = (schema, customRequestInit = {}, options = {}) => (inp
|
|
|
105
105
|
}
|
|
106
106
|
throw result.error;
|
|
107
107
|
});
|
|
108
|
-
const createTextFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
108
|
+
const createTextFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithDefaultRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
109
109
|
options.onResponse?.(res);
|
|
110
110
|
const body = await res.text();
|
|
111
111
|
if (res.ok) return body;
|
|
@@ -114,7 +114,7 @@ const createTextFetcher = (customRequestInit = {}, options = {}) => (input, requ
|
|
|
114
114
|
store.getState().onError?.(error);
|
|
115
115
|
throw error;
|
|
116
116
|
});
|
|
117
|
-
const createBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
117
|
+
const createBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithDefaultRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
118
118
|
options.onResponse?.(res);
|
|
119
119
|
const body = await res.blob();
|
|
120
120
|
if (res.ok) return {
|
|
@@ -126,7 +126,7 @@ const createBlobFetcher = (customRequestInit = {}, options = {}) => (input, requ
|
|
|
126
126
|
store.getState().onError?.(error);
|
|
127
127
|
throw error;
|
|
128
128
|
});
|
|
129
|
-
const createFileOrBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
129
|
+
const createFileOrBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithDefaultRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
130
130
|
options.onResponse?.(res);
|
|
131
131
|
const body = await res.blob();
|
|
132
132
|
if (res.ok) return {
|
|
@@ -138,7 +138,7 @@ const createFileOrBlobFetcher = (customRequestInit = {}, options = {}) => (input
|
|
|
138
138
|
store.getState().onError?.(error);
|
|
139
139
|
throw error;
|
|
140
140
|
});
|
|
141
|
-
const createVoidFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
141
|
+
const createVoidFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithDefaultRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
142
142
|
options.onResponse?.(res);
|
|
143
143
|
if (res.ok) return;
|
|
144
144
|
throw new HTTPError(Object.values(errorStatusCode).includes(res.status) ? res.status : 999, await res.text());
|
package/dist/react/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as mergeRequestInit, r as store } from "../apiClientConfigStore-
|
|
1
|
+
import { i as mergeRequestInit, r as store } from "../apiClientConfigStore-Buly03AC.mjs";
|
|
2
2
|
import { createContext, useContext, useEffect, useMemo, useRef } from "react";
|
|
3
3
|
import { SWRConfig } from "swr";
|
|
4
4
|
import { jsx } from "react/jsx-runtime";
|
|
@@ -55,8 +55,8 @@ const useApiClientConfigWithSelector = (selector) => {
|
|
|
55
55
|
return useSyncExternalStoreWithSelector(store.subscribe, store.getState, store.getState, selector);
|
|
56
56
|
};
|
|
57
57
|
const useRequestInit = (customRequestInit = {}) => {
|
|
58
|
-
const { defaultRequestInit
|
|
59
|
-
return useMemo(() => mergeRequestInit(defaultRequestInit, customRequestInit), [defaultRequestInit, customRequestInit]);
|
|
58
|
+
const { defaultRequestInit } = useApiClientConfig();
|
|
59
|
+
return useMemo(() => typeof defaultRequestInit === "function" ? customRequestInit : mergeRequestInit(defaultRequestInit ?? {}, customRequestInit), [defaultRequestInit, customRequestInit]);
|
|
60
60
|
};
|
|
61
61
|
//#endregion
|
|
62
62
|
export { ApiClientConfigProvider, createSwrConfig, useApiClientConfig, useApiClientConfigContext, useApiClientConfigWithSelector, useRequestInit };
|
|
@@ -14,8 +14,8 @@ const useApiClientConfigWithSelector = (selector) => {
|
|
|
14
14
|
return (0, use_sync_external_store_shim_with_selector_js.useSyncExternalStoreWithSelector)(store.subscribe, store.getState, store.getState, selector);
|
|
15
15
|
};
|
|
16
16
|
const useRequestInit = (customRequestInit = {}) => {
|
|
17
|
-
const { defaultRequestInit
|
|
18
|
-
return (0, react.useMemo)(() => require_utils_requestInit.mergeRequestInit(defaultRequestInit, customRequestInit), [defaultRequestInit, customRequestInit]);
|
|
17
|
+
const { defaultRequestInit } = useApiClientConfig();
|
|
18
|
+
return (0, react.useMemo)(() => typeof defaultRequestInit === "function" ? customRequestInit : require_utils_requestInit.mergeRequestInit(defaultRequestInit ?? {}, customRequestInit), [defaultRequestInit, customRequestInit]);
|
|
19
19
|
};
|
|
20
20
|
//#endregion
|
|
21
21
|
exports.useApiClientConfig = useApiClientConfig;
|