@readyfor/api-client-base 1.155.0-pr1529.49ab6aa → 1.155.0-pr1530.162100f
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.d.ts +8 -0
- package/dist/fetcher.js +11 -5
- package/dist/index.mjs +11 -5
- package/package.json +1 -1
|
@@ -12,6 +12,14 @@ type LoggingConfig = {
|
|
|
12
12
|
export type RequestConfig = {
|
|
13
13
|
hostName?: string | ((path: string) => string);
|
|
14
14
|
defaultRequestInit?: RequestInit;
|
|
15
|
+
/**
|
|
16
|
+
* 設定時ではなく API 呼び出しごとに評価される。転送する値は設定時ではなくこの関数の中で読むこと。
|
|
17
|
+
* すべての宛先への fetch で呼ばれるため、転送してよい宛先かは必要に応じて `input` で判断すること。
|
|
18
|
+
* 受け取ったリクエストヘッダーを丸ごと返さず、必要なもの(Cookie や User-Agent など)だけを返すこと。host や connection などが混ざると fetch が失敗する。
|
|
19
|
+
* 同じフィールドや同名のヘッダーを `defaultRequestInit` や API 呼び出し時の RequestInit で指定した場合は、そちらが優先される。
|
|
20
|
+
* サーバー側での利用を想定している。ブラウザでは Cookie などは forbidden header name のため送られない。
|
|
21
|
+
*/
|
|
22
|
+
forwardedRequestInit?: (input: RequestInfo) => RequestInit | Promise<RequestInit>;
|
|
15
23
|
onError?: (error: unknown) => void;
|
|
16
24
|
};
|
|
17
25
|
export type Config = RequestConfig & DeserializeResponseConfig & LoggingConfig;
|
package/dist/fetcher.js
CHANGED
|
@@ -4,7 +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
|
|
7
|
+
const fetchWithForwardedRequestInit = (input, customRequestInit, requestInit) => {
|
|
8
|
+
const mergedRequestInit = require_utils_requestInit.mergeRequestInit(defaultRequestInit, require_utils_requestInit.mergeRequestInit(customRequestInit, requestInit));
|
|
9
|
+
const { forwardedRequestInit } = require_apiClientConfigStore.store.getState();
|
|
10
|
+
if (forwardedRequestInit === void 0) return fetch(input, mergedRequestInit);
|
|
11
|
+
return Promise.resolve().then(() => forwardedRequestInit(input)).then((forwarded) => fetch(input, require_utils_requestInit.mergeRequestInit(forwarded, mergedRequestInit)));
|
|
12
|
+
};
|
|
13
|
+
const createJsonFetcher = (schema, customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithForwardedRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
8
14
|
options.onResponse?.(res);
|
|
9
15
|
const text = await res.text();
|
|
10
16
|
const body = text ? JSON.parse(text) : {};
|
|
@@ -25,7 +31,7 @@ const createJsonFetcher = (schema, customRequestInit = {}, options = {}) => (inp
|
|
|
25
31
|
}
|
|
26
32
|
throw result.error;
|
|
27
33
|
});
|
|
28
|
-
const createTextFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
34
|
+
const createTextFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithForwardedRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
29
35
|
options.onResponse?.(res);
|
|
30
36
|
const body = await res.text();
|
|
31
37
|
if (res.ok) return body;
|
|
@@ -35,7 +41,7 @@ const createTextFetcher = (customRequestInit = {}, options = {}) => (input, requ
|
|
|
35
41
|
require_apiClientConfigStore.store.getState().onError?.(error);
|
|
36
42
|
throw error;
|
|
37
43
|
});
|
|
38
|
-
const createBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
44
|
+
const createBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithForwardedRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
39
45
|
options.onResponse?.(res);
|
|
40
46
|
const body = await res.blob();
|
|
41
47
|
if (res.ok) return {
|
|
@@ -48,7 +54,7 @@ const createBlobFetcher = (customRequestInit = {}, options = {}) => (input, requ
|
|
|
48
54
|
require_apiClientConfigStore.store.getState().onError?.(error);
|
|
49
55
|
throw error;
|
|
50
56
|
});
|
|
51
|
-
const createFileOrBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
57
|
+
const createFileOrBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithForwardedRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
52
58
|
options.onResponse?.(res);
|
|
53
59
|
const body = await res.blob();
|
|
54
60
|
if (res.ok) return {
|
|
@@ -61,7 +67,7 @@ const createFileOrBlobFetcher = (customRequestInit = {}, options = {}) => (input
|
|
|
61
67
|
require_apiClientConfigStore.store.getState().onError?.(error);
|
|
62
68
|
throw error;
|
|
63
69
|
});
|
|
64
|
-
const createVoidFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
70
|
+
const createVoidFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithForwardedRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
65
71
|
options.onResponse?.(res);
|
|
66
72
|
if (res.ok) return;
|
|
67
73
|
const code = Object.values(require_apiError.errorStatusCode).includes(res.status) ? res.status : 999;
|
package/dist/index.mjs
CHANGED
|
@@ -79,7 +79,13 @@ const getErrorStatus = (response) => {
|
|
|
79
79
|
//#endregion
|
|
80
80
|
//#region src/fetcher.ts
|
|
81
81
|
const defaultRequestInit = { credentials: "include" };
|
|
82
|
-
const
|
|
82
|
+
const fetchWithForwardedRequestInit = (input, customRequestInit, requestInit) => {
|
|
83
|
+
const mergedRequestInit = mergeRequestInit(defaultRequestInit, mergeRequestInit(customRequestInit, requestInit));
|
|
84
|
+
const { forwardedRequestInit } = store.getState();
|
|
85
|
+
if (forwardedRequestInit === void 0) return fetch(input, mergedRequestInit);
|
|
86
|
+
return Promise.resolve().then(() => forwardedRequestInit(input)).then((forwarded) => fetch(input, mergeRequestInit(forwarded, mergedRequestInit)));
|
|
87
|
+
};
|
|
88
|
+
const createJsonFetcher = (schema, customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithForwardedRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
83
89
|
options.onResponse?.(res);
|
|
84
90
|
const text = await res.text();
|
|
85
91
|
const body = text ? JSON.parse(text) : {};
|
|
@@ -99,7 +105,7 @@ const createJsonFetcher = (schema, customRequestInit = {}, options = {}) => (inp
|
|
|
99
105
|
}
|
|
100
106
|
throw result.error;
|
|
101
107
|
});
|
|
102
|
-
const createTextFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
108
|
+
const createTextFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithForwardedRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
103
109
|
options.onResponse?.(res);
|
|
104
110
|
const body = await res.text();
|
|
105
111
|
if (res.ok) return body;
|
|
@@ -108,7 +114,7 @@ const createTextFetcher = (customRequestInit = {}, options = {}) => (input, requ
|
|
|
108
114
|
store.getState().onError?.(error);
|
|
109
115
|
throw error;
|
|
110
116
|
});
|
|
111
|
-
const createBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
117
|
+
const createBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithForwardedRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
112
118
|
options.onResponse?.(res);
|
|
113
119
|
const body = await res.blob();
|
|
114
120
|
if (res.ok) return {
|
|
@@ -120,7 +126,7 @@ const createBlobFetcher = (customRequestInit = {}, options = {}) => (input, requ
|
|
|
120
126
|
store.getState().onError?.(error);
|
|
121
127
|
throw error;
|
|
122
128
|
});
|
|
123
|
-
const createFileOrBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
129
|
+
const createFileOrBlobFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithForwardedRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
124
130
|
options.onResponse?.(res);
|
|
125
131
|
const body = await res.blob();
|
|
126
132
|
if (res.ok) return {
|
|
@@ -132,7 +138,7 @@ const createFileOrBlobFetcher = (customRequestInit = {}, options = {}) => (input
|
|
|
132
138
|
store.getState().onError?.(error);
|
|
133
139
|
throw error;
|
|
134
140
|
});
|
|
135
|
-
const createVoidFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) =>
|
|
141
|
+
const createVoidFetcher = (customRequestInit = {}, options = {}) => (input, requestInit = {}) => fetchWithForwardedRequestInit(input, customRequestInit, requestInit).then(async (res) => {
|
|
136
142
|
options.onResponse?.(res);
|
|
137
143
|
if (res.ok) return;
|
|
138
144
|
throw new HTTPError(Object.values(errorStatusCode).includes(res.status) ? res.status : 999, await res.text());
|