@readyfor/api-client-base 1.22.0 → 1.22.1-pr1285.b385439

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.
Files changed (57) hide show
  1. package/dist/_virtual/_rolldown/runtime.js +23 -0
  2. package/dist/apiClientConfigStore-CNRWwVM3.mjs +86 -0
  3. package/dist/apiClientConfigStore.d.ts +11 -10
  4. package/dist/apiClientConfigStore.js +10 -37
  5. package/dist/apiError.d.ts +24 -25
  6. package/dist/apiError.js +68 -95
  7. package/dist/fetcher.d.ts +8 -7
  8. package/dist/fetcher.js +57 -125
  9. package/dist/index.d.ts +10 -10
  10. package/dist/index.js +31 -38
  11. package/dist/index.mjs +146 -62
  12. package/dist/react/ApiClientConfigProvider.d.ts +10 -9
  13. package/dist/react/ApiClientConfigProvider.js +24 -41
  14. package/dist/react/index.d.ts +4 -7
  15. package/dist/react/index.js +10 -26
  16. package/dist/react/index.mjs +61 -23
  17. package/dist/react/swr.d.ts +10 -9
  18. package/dist/react/swr.js +24 -37
  19. package/dist/react/useApiClientConfig.d.ts +5 -5
  20. package/dist/react/useApiClientConfig.js +18 -50
  21. package/dist/requestUrl.d.ts +3 -2
  22. package/dist/requestUrl.js +14 -49
  23. package/dist/store.d.ts +6 -5
  24. package/dist/store.js +20 -43
  25. package/dist/types.d.ts +5 -11
  26. package/dist/types.js +0 -16
  27. package/dist/utils/headersInit.d.ts +3 -2
  28. package/dist/utils/headersInit.js +45 -49
  29. package/dist/utils/requestInit.d.ts +10 -3
  30. package/dist/utils/requestInit.js +14 -31
  31. package/dist/zod.d.ts +4 -3
  32. package/dist/zod.js +6 -30
  33. package/package.json +2 -19
  34. package/dist/apiClientConfigStore.mjs +0 -13
  35. package/dist/apiError.mjs +0 -19
  36. package/dist/chunk-3SEO7S3Q.mjs +0 -23
  37. package/dist/chunk-5JS6U5IM.mjs +0 -37
  38. package/dist/chunk-DH33ZDBG.mjs +0 -19
  39. package/dist/chunk-FNLUAQWC.mjs +0 -80
  40. package/dist/chunk-GD4RTKUU.mjs +0 -22
  41. package/dist/chunk-JCZWXJBU.mjs +0 -10
  42. package/dist/chunk-NOC6G3HZ.mjs +0 -21
  43. package/dist/chunk-NYICHGY2.mjs +0 -30
  44. package/dist/chunk-OVR3ZT2S.mjs +0 -118
  45. package/dist/chunk-QXFPBYPL.mjs +0 -25
  46. package/dist/chunk-TZUKIYFN.mjs +0 -14
  47. package/dist/chunk-WBQAMGXK.mjs +0 -0
  48. package/dist/fetcher.mjs +0 -20
  49. package/dist/react/ApiClientConfigProvider.mjs +0 -13
  50. package/dist/react/swr.mjs +0 -6
  51. package/dist/react/useApiClientConfig.mjs +0 -16
  52. package/dist/requestUrl.mjs +0 -10
  53. package/dist/store.mjs +0 -6
  54. package/dist/types.mjs +0 -1
  55. package/dist/utils/headersInit.mjs +0 -8
  56. package/dist/utils/requestInit.mjs +0 -7
  57. package/dist/zod.mjs +0 -8
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- export { Config, RequestConfig, buildRequestInitWithDefaultConfig, setApiClientConfig, store } from './apiClientConfigStore.js';
2
- export { ErrorStatusCode, HTTPError, errorStatusCode, errorTitle, getErrorStatus, getHttpErrorBody, getHttpErrorBodyReason, isHttpErrorWithStatus } from './apiError.js';
3
- export { BlobFetcherResponse, FileOrBlobFetcherResponse, createBlobFetcher, createFileOrBlobFetcher, createJsonFetcher, createTextFetcher, createVoidFetcher } from './fetcher.js';
4
- export { __internal__requestUrl } from './requestUrl.js';
5
- export { Store, createStore } from './store.js';
6
- export { ForceDig, QueryPropsFor } from './types.js';
7
- export { isZodError, schemaForType } from './zod.js';
8
- export { mergeRequestInit } from './utils/requestInit.js';
9
- export { mergeHeadersInit, toHeadersInit } from './utils/headersInit.js';
10
- import 'zod';
1
+ import { Store, createStore } from "./store.js";
2
+ import { Config, RequestConfig, buildRequestInitWithDefaultConfig, setApiClientConfig, store } from "./apiClientConfigStore.js";
3
+ import { ErrorStatusCode, HTTPError, errorStatusCode, errorTitle, getErrorStatus, getHttpErrorBody, getHttpErrorBodyReason, isHttpErrorWithStatus } from "./apiError.js";
4
+ import { BlobFetcherResponse, FileOrBlobFetcherResponse, createBlobFetcher, createFileOrBlobFetcher, createJsonFetcher, createTextFetcher, createVoidFetcher } from "./fetcher.js";
5
+ import { __internal__requestUrl } from "./requestUrl.js";
6
+ import { ForceDig, QueryPropsFor } from "./types.js";
7
+ import { isZodError, schemaForType } from "./zod.js";
8
+ import { mergeRequestInit } from "./utils/requestInit.js";
9
+ import { mergeHeadersInit, toHeadersInit } from "./utils/headersInit.js";
10
+ export { BlobFetcherResponse, Config, ErrorStatusCode, FileOrBlobFetcherResponse, ForceDig, HTTPError, QueryPropsFor, RequestConfig, Store, __internal__requestUrl, buildRequestInitWithDefaultConfig, createBlobFetcher, createFileOrBlobFetcher, createJsonFetcher, createStore, createTextFetcher, createVoidFetcher, errorStatusCode, errorTitle, getErrorStatus, getHttpErrorBody, getHttpErrorBodyReason, isHttpErrorWithStatus, isZodError, mergeHeadersInit, mergeRequestInit, schemaForType, setApiClientConfig, store, toHeadersInit };
package/dist/index.js CHANGED
@@ -1,38 +1,31 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
- var index_exports = {};
17
- module.exports = __toCommonJS(index_exports);
18
- __reExport(index_exports, require("./apiClientConfigStore"), module.exports);
19
- __reExport(index_exports, require("./apiError"), module.exports);
20
- __reExport(index_exports, require("./fetcher"), module.exports);
21
- __reExport(index_exports, require("./requestUrl"), module.exports);
22
- __reExport(index_exports, require("./store"), module.exports);
23
- __reExport(index_exports, require("./types"), module.exports);
24
- __reExport(index_exports, require("./zod"), module.exports);
25
- __reExport(index_exports, require("./utils/requestInit"), module.exports);
26
- __reExport(index_exports, require("./utils/headersInit"), module.exports);
27
- // Annotate the CommonJS export names for ESM import in node:
28
- 0 && (module.exports = {
29
- ...require("./apiClientConfigStore"),
30
- ...require("./apiError"),
31
- ...require("./fetcher"),
32
- ...require("./requestUrl"),
33
- ...require("./store"),
34
- ...require("./types"),
35
- ...require("./zod"),
36
- ...require("./utils/requestInit"),
37
- ...require("./utils/headersInit")
38
- });
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_store = require("./store.js");
3
+ const require_utils_headersInit = require("./utils/headersInit.js");
4
+ const require_utils_requestInit = require("./utils/requestInit.js");
5
+ const require_apiClientConfigStore = require("./apiClientConfigStore.js");
6
+ const require_zod = require("./zod.js");
7
+ const require_apiError = require("./apiError.js");
8
+ const require_fetcher = require("./fetcher.js");
9
+ const require_requestUrl = require("./requestUrl.js");
10
+ exports.HTTPError = require_apiError.HTTPError;
11
+ exports.__internal__requestUrl = require_requestUrl.__internal__requestUrl;
12
+ exports.buildRequestInitWithDefaultConfig = require_apiClientConfigStore.buildRequestInitWithDefaultConfig;
13
+ exports.createBlobFetcher = require_fetcher.createBlobFetcher;
14
+ exports.createFileOrBlobFetcher = require_fetcher.createFileOrBlobFetcher;
15
+ exports.createJsonFetcher = require_fetcher.createJsonFetcher;
16
+ exports.createStore = require_store.createStore;
17
+ exports.createTextFetcher = require_fetcher.createTextFetcher;
18
+ exports.createVoidFetcher = require_fetcher.createVoidFetcher;
19
+ exports.errorStatusCode = require_apiError.errorStatusCode;
20
+ exports.errorTitle = require_apiError.errorTitle;
21
+ exports.getErrorStatus = require_apiError.getErrorStatus;
22
+ exports.getHttpErrorBody = require_apiError.getHttpErrorBody;
23
+ exports.getHttpErrorBodyReason = require_apiError.getHttpErrorBodyReason;
24
+ exports.isHttpErrorWithStatus = require_apiError.isHttpErrorWithStatus;
25
+ exports.isZodError = require_zod.isZodError;
26
+ exports.mergeHeadersInit = require_utils_headersInit.mergeHeadersInit;
27
+ exports.mergeRequestInit = require_utils_requestInit.mergeRequestInit;
28
+ exports.schemaForType = require_zod.schemaForType;
29
+ exports.setApiClientConfig = require_apiClientConfigStore.setApiClientConfig;
30
+ exports.store = require_apiClientConfigStore.store;
31
+ exports.toHeadersInit = require_utils_headersInit.toHeadersInit;
package/dist/index.mjs CHANGED
@@ -1,63 +1,147 @@
1
- import {
2
- createBlobFetcher,
3
- createFileOrBlobFetcher,
4
- createJsonFetcher,
5
- createTextFetcher,
6
- createVoidFetcher
7
- } from "./chunk-OVR3ZT2S.mjs";
8
- import {
9
- HTTPError,
10
- errorStatusCode,
11
- errorTitle,
12
- getErrorStatus,
13
- getHttpErrorBody,
14
- getHttpErrorBodyReason,
15
- isHttpErrorWithStatus
16
- } from "./chunk-FNLUAQWC.mjs";
17
- import {
18
- __internal__requestUrl
19
- } from "./chunk-GD4RTKUU.mjs";
20
- import {
21
- buildRequestInitWithDefaultConfig,
22
- setApiClientConfig,
23
- store
24
- } from "./chunk-NOC6G3HZ.mjs";
25
- import {
26
- mergeRequestInit
27
- } from "./chunk-TZUKIYFN.mjs";
28
- import {
29
- mergeHeadersInit,
30
- toHeadersInit
31
- } from "./chunk-NYICHGY2.mjs";
32
- import {
33
- createStore
34
- } from "./chunk-3SEO7S3Q.mjs";
35
- import "./chunk-WBQAMGXK.mjs";
36
- import {
37
- isZodError,
38
- schemaForType
39
- } from "./chunk-JCZWXJBU.mjs";
40
- export {
41
- HTTPError,
42
- __internal__requestUrl,
43
- buildRequestInitWithDefaultConfig,
44
- createBlobFetcher,
45
- createFileOrBlobFetcher,
46
- createJsonFetcher,
47
- createStore,
48
- createTextFetcher,
49
- createVoidFetcher,
50
- errorStatusCode,
51
- errorTitle,
52
- getErrorStatus,
53
- getHttpErrorBody,
54
- getHttpErrorBodyReason,
55
- isHttpErrorWithStatus,
56
- isZodError,
57
- mergeHeadersInit,
58
- mergeRequestInit,
59
- schemaForType,
60
- setApiClientConfig,
61
- store,
62
- toHeadersInit
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-CNRWwVM3.mjs";
2
+ import qs from "qs";
3
+ //#region src/zod.ts
4
+ const schemaForType = () => (arg) => {
5
+ return arg;
63
6
  };
7
+ const isZodError = (obj) => obj !== null && typeof obj === "object" && obj.constructor.name === "ZodError";
8
+ //#endregion
9
+ //#region src/apiError.ts
10
+ const errorStatusCode = {
11
+ MOVED_PERMANENTLY: 301,
12
+ FOUND: 302,
13
+ BAD_REQUEST: 400,
14
+ UNAUTHORIZED: 401,
15
+ FORBIDDEN: 403,
16
+ NOT_FOUND: 404,
17
+ CONFLICT: 409,
18
+ PAYLOAD_TOO_LARGE: 413,
19
+ NETWORK_ERROR: 418,
20
+ UNPROCESSABLE_ENTITY: 422,
21
+ INTERNAL_SERVER_ERROR: 500,
22
+ BAD_GATEWAY: 502,
23
+ SERVER_UNAVAILABLE: 503,
24
+ GATEWAY_TIMEOUT: 504,
25
+ ZOD_ERROR: 901,
26
+ UNHANDLED_ERROR: 999
27
+ };
28
+ const errorTitle = {
29
+ 301: "ページが移動されています",
30
+ 302: "ページが一時的に移動されています",
31
+ 400: "リクエストが不正です",
32
+ 401: "ログインしてください",
33
+ 403: "不正なトークンです",
34
+ 404: "ページが見つかりませんでした",
35
+ 409: "編集内容が競合しています",
36
+ 413: "通信に失敗しました",
37
+ 418: "通信に失敗しました",
38
+ 422: "データが不正です",
39
+ 500: "サーバーでエラーが起きました",
40
+ 502: "サーバーでエラーが起きました",
41
+ 503: "メンテナンス中です",
42
+ 504: "通信に失敗しました",
43
+ 901: "問題が発生しました",
44
+ 999: "サーバーでエラーが起きました"
45
+ };
46
+ var HTTPError = class extends Error {
47
+ status;
48
+ body;
49
+ constructor(status, body) {
50
+ super(errorTitle[status]);
51
+ this.name = "HTTPError";
52
+ this.status = status;
53
+ this.body = body;
54
+ }
55
+ };
56
+ /**
57
+ * duck typing で HTTPError の status を判定する。
58
+ * pnpm の strict isolation により instanceof HTTPError が
59
+ * 別バージョンのクラスと一致しないケースがあるため、
60
+ * プロパティベースで判定する。
61
+ */
62
+ const isHttpErrorWithStatus = (error, status) => error instanceof Error && "status" in error && error.status === status;
63
+ const getHttpErrorBody = (error) => error instanceof Error && "body" in error ? error.body : void 0;
64
+ const getHttpErrorBodyReason = (body) => {
65
+ if (body !== null && typeof body === "object" && "reason" in body) {
66
+ const reason = body.reason;
67
+ return typeof reason === "string" ? reason : void 0;
68
+ }
69
+ };
70
+ const getErrorStatus = (response) => {
71
+ if (response instanceof Object && "status" in response && typeof response["status"] === "number") {
72
+ const status = Number(response["status"]);
73
+ return Object.values(errorStatusCode).includes(status) ? status : 999;
74
+ }
75
+ if (isZodError(response)) return errorStatusCode.ZOD_ERROR;
76
+ };
77
+ //#endregion
78
+ //#region src/fetcher.ts
79
+ const defaultRequestInit = { credentials: "include" };
80
+ const createJsonFetcher = (schema, customRequestInit = {}) => (input, requestInit = {}) => fetch(input, mergeRequestInit(defaultRequestInit, mergeRequestInit(customRequestInit, requestInit))).then(async (res) => {
81
+ const text = await res.text();
82
+ const body = text ? JSON.parse(text) : {};
83
+ if (res.ok) return body;
84
+ throw new HTTPError(Object.values(errorStatusCode).includes(res.status) ? res.status : 999, body);
85
+ }).catch((error) => {
86
+ store.getState().onError?.(error);
87
+ throw error;
88
+ }).then((parsedResponse) => {
89
+ const result = schema.safeParse(parsedResponse);
90
+ const config = store.getState();
91
+ if (result.success) return result.data;
92
+ if (config.showsSchemaParseError) console.error(result.error);
93
+ if (config.forceDeserializeResponse) {
94
+ config.onFailedDeserializeResponse(input, result.error);
95
+ return parsedResponse;
96
+ }
97
+ throw result.error;
98
+ });
99
+ const createTextFetcher = (customRequestInit = {}) => (input, requestInit = {}) => fetch(input, mergeRequestInit(defaultRequestInit, mergeRequestInit(customRequestInit, requestInit))).then(async (res) => {
100
+ const body = await res.text();
101
+ if (res.ok) return body;
102
+ throw new HTTPError(Object.values(errorStatusCode).includes(res.status) ? res.status : 999, body);
103
+ }).catch((error) => {
104
+ store.getState().onError?.(error);
105
+ throw error;
106
+ });
107
+ const createBlobFetcher = (customRequestInit = {}) => (input, requestInit = {}) => fetch(input, mergeRequestInit(defaultRequestInit, mergeRequestInit(customRequestInit, requestInit))).then(async (res) => {
108
+ const body = await res.blob();
109
+ if (res.ok) return {
110
+ body,
111
+ headers: res.headers
112
+ };
113
+ throw new HTTPError(Object.values(errorStatusCode).includes(res.status) ? res.status : 999, body);
114
+ }).catch((error) => {
115
+ store.getState().onError?.(error);
116
+ throw error;
117
+ });
118
+ const createFileOrBlobFetcher = (customRequestInit = {}) => (input, requestInit = {}) => fetch(input, mergeRequestInit(defaultRequestInit, mergeRequestInit(customRequestInit, requestInit))).then(async (res) => {
119
+ const body = await res.blob();
120
+ if (res.ok) return {
121
+ body,
122
+ headers: res.headers
123
+ };
124
+ throw new HTTPError(Object.values(errorStatusCode).includes(res.status) ? res.status : 999, body);
125
+ }).catch((error) => {
126
+ store.getState().onError?.(error);
127
+ throw error;
128
+ });
129
+ const createVoidFetcher = (customRequestInit = {}) => (input, requestInit = {}) => fetch(input, mergeRequestInit(defaultRequestInit, mergeRequestInit(customRequestInit, requestInit))).then(async (res) => {
130
+ if (res.ok) return;
131
+ throw new HTTPError(Object.values(errorStatusCode).includes(res.status) ? res.status : 999, await res.text());
132
+ }).catch((error) => {
133
+ store.getState().onError?.(error);
134
+ throw error;
135
+ });
136
+ //#endregion
137
+ //#region src/requestUrl.ts
138
+ function __internal__requestUrl(...args) {
139
+ const config = store.getState();
140
+ const rawPath = args[0];
141
+ const params = args[1] || {};
142
+ const query = args[2];
143
+ const path = (config.hostName ? typeof config.hostName === "string" ? config.hostName : config.hostName(rawPath) : "") + Object.entries(params).reduce((path, [key, value]) => path.replace(`{${key}}`, String(value)), rawPath);
144
+ return query ? `${path}?${qs.stringify(query)}` : path;
145
+ }
146
+ //#endregion
147
+ export { HTTPError, __internal__requestUrl, buildRequestInitWithDefaultConfig, createBlobFetcher, createFileOrBlobFetcher, createJsonFetcher, createStore, createTextFetcher, createVoidFetcher, errorStatusCode, errorTitle, getErrorStatus, getHttpErrorBody, getHttpErrorBodyReason, isHttpErrorWithStatus, isZodError, mergeHeadersInit, mergeRequestInit, schemaForType, setApiClientConfig, store, toHeadersInit };
@@ -1,16 +1,17 @@
1
- import { FC, ReactNode } from 'react';
2
- import { Config } from '../apiClientConfigStore.js';
3
- import { Store } from '../store.js';
4
- import { SWRConfiguration } from 'swr';
1
+ import { Store } from "../store.js";
2
+ import { Config } from "../apiClientConfigStore.js";
3
+ import { FC, ReactNode } from "react";
4
+ import { SWRConfiguration } from "swr";
5
5
 
6
+ //#region src/react/ApiClientConfigProvider.d.ts
6
7
  type Configuration = Config & {
7
- swr?: SWRConfiguration;
8
+ swr?: SWRConfiguration;
8
9
  };
9
10
  type Props = {
10
- config: Configuration;
11
- children: ReactNode;
11
+ config: Configuration;
12
+ children: ReactNode;
12
13
  };
13
14
  declare const ApiClientConfigProvider: FC<Props>;
14
15
  declare const useApiClientConfigContext: () => Store<Config>;
15
-
16
- export { ApiClientConfigProvider, useApiClientConfigContext };
16
+ //#endregion
17
+ export { ApiClientConfigProvider, useApiClientConfigContext };
@@ -1,43 +1,26 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var ApiClientConfigProvider_exports = {};
20
- __export(ApiClientConfigProvider_exports, {
21
- ApiClientConfigProvider: () => ApiClientConfigProvider,
22
- useApiClientConfigContext: () => useApiClientConfigContext
23
- });
24
- module.exports = __toCommonJS(ApiClientConfigProvider_exports);
25
- var import_jsx_runtime = require("react/jsx-runtime");
26
- var import_react = require("react");
27
- var import_apiClientConfigStore = require("../apiClientConfigStore");
28
- var import_swr = require("swr");
29
- var import_swr2 = require("./swr");
30
- const ApiClientConfigContext = (0, import_react.createContext)(import_apiClientConfigStore.store);
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ require("../_virtual/_rolldown/runtime.js");
3
+ const require_apiClientConfigStore = require("../apiClientConfigStore.js");
4
+ const require_react_swr = require("./swr.js");
5
+ let react = require("react");
6
+ let swr = require("swr");
7
+ let react_jsx_runtime = require("react/jsx-runtime");
8
+ //#region src/react/ApiClientConfigProvider.tsx
9
+ const ApiClientConfigContext = (0, react.createContext)(require_apiClientConfigStore.store);
31
10
  const ApiClientConfigProvider = (props) => {
32
- const { swr, ...config } = props.config;
33
- (0, import_react.useEffect)(() => {
34
- import_apiClientConfigStore.store.setState(() => config);
35
- }, [config]);
36
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ApiClientConfigContext.Provider, { value: import_apiClientConfigStore.store, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_swr.SWRConfig, { value: (0, import_swr2.createSwrConfig)(swr), children: props.children }) });
11
+ const { swr: swr$1, ...config } = props.config;
12
+ (0, react.useEffect)(() => {
13
+ require_apiClientConfigStore.store.setState(() => config);
14
+ }, [config]);
15
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ApiClientConfigContext.Provider, {
16
+ value: require_apiClientConfigStore.store,
17
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(swr.SWRConfig, {
18
+ value: require_react_swr.createSwrConfig(swr$1),
19
+ children: props.children
20
+ })
21
+ });
37
22
  };
38
- const useApiClientConfigContext = () => (0, import_react.useContext)(ApiClientConfigContext) || import_apiClientConfigStore.store;
39
- // Annotate the CommonJS export names for ESM import in node:
40
- 0 && (module.exports = {
41
- ApiClientConfigProvider,
42
- useApiClientConfigContext
43
- });
23
+ const useApiClientConfigContext = () => (0, react.useContext)(ApiClientConfigContext) || require_apiClientConfigStore.store;
24
+ //#endregion
25
+ exports.ApiClientConfigProvider = ApiClientConfigProvider;
26
+ exports.useApiClientConfigContext = useApiClientConfigContext;
@@ -1,7 +1,4 @@
1
- export { ApiClientConfigProvider, useApiClientConfigContext } from './ApiClientConfigProvider.js';
2
- export { Configuration, createSwrConfig } from './swr.js';
3
- export { useApiClientConfig, useApiClientConfigWithSelector, useRequestInit } from './useApiClientConfig.js';
4
- import 'react';
5
- import '../apiClientConfigStore.js';
6
- import '../store.js';
7
- import 'swr';
1
+ import { ApiClientConfigProvider, useApiClientConfigContext } from "./ApiClientConfigProvider.js";
2
+ import { Configuration, createSwrConfig } from "./swr.js";
3
+ import { useApiClientConfig, useApiClientConfigWithSelector, useRequestInit } from "./useApiClientConfig.js";
4
+ export { ApiClientConfigProvider, Configuration, createSwrConfig, useApiClientConfig, useApiClientConfigContext, useApiClientConfigWithSelector, useRequestInit };
@@ -1,26 +1,10 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
- var react_exports = {};
17
- module.exports = __toCommonJS(react_exports);
18
- __reExport(react_exports, require("./ApiClientConfigProvider"), module.exports);
19
- __reExport(react_exports, require("./swr"), module.exports);
20
- __reExport(react_exports, require("./useApiClientConfig"), module.exports);
21
- // Annotate the CommonJS export names for ESM import in node:
22
- 0 && (module.exports = {
23
- ...require("./ApiClientConfigProvider"),
24
- ...require("./swr"),
25
- ...require("./useApiClientConfig")
26
- });
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_react_swr = require("./swr.js");
3
+ const require_react_ApiClientConfigProvider = require("./ApiClientConfigProvider.js");
4
+ const require_react_useApiClientConfig = require("./useApiClientConfig.js");
5
+ exports.ApiClientConfigProvider = require_react_ApiClientConfigProvider.ApiClientConfigProvider;
6
+ exports.createSwrConfig = require_react_swr.createSwrConfig;
7
+ exports.useApiClientConfig = require_react_useApiClientConfig.useApiClientConfig;
8
+ exports.useApiClientConfigContext = require_react_ApiClientConfigProvider.useApiClientConfigContext;
9
+ exports.useApiClientConfigWithSelector = require_react_useApiClientConfig.useApiClientConfigWithSelector;
10
+ exports.useRequestInit = require_react_useApiClientConfig.useRequestInit;
@@ -1,24 +1,62 @@
1
- import {
2
- useApiClientConfig,
3
- useApiClientConfigWithSelector,
4
- useRequestInit
5
- } from "../chunk-5JS6U5IM.mjs";
6
- import {
7
- ApiClientConfigProvider,
8
- useApiClientConfigContext
9
- } from "../chunk-QXFPBYPL.mjs";
10
- import {
11
- createSwrConfig
12
- } from "../chunk-DH33ZDBG.mjs";
13
- import "../chunk-NOC6G3HZ.mjs";
14
- import "../chunk-TZUKIYFN.mjs";
15
- import "../chunk-NYICHGY2.mjs";
16
- import "../chunk-3SEO7S3Q.mjs";
17
- export {
18
- ApiClientConfigProvider,
19
- createSwrConfig,
20
- useApiClientConfig,
21
- useApiClientConfigContext,
22
- useApiClientConfigWithSelector,
23
- useRequestInit
1
+ import { i as mergeRequestInit, r as store } from "../apiClientConfigStore-CNRWwVM3.mjs";
2
+ import { createContext, useContext, useEffect, useMemo, useRef } from "react";
3
+ import { SWRConfig } from "swr";
4
+ import { jsx } from "react/jsx-runtime";
5
+ import { useSyncExternalStore } from "use-sync-external-store/shim/index.js";
6
+ import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/with-selector.js";
7
+ //#region src/react/swr.ts
8
+ /**
9
+ * requestのfetchを中断させるためのmiddleware
10
+ *
11
+ * 同一hookにおいて、keyが更新されるなどにより、一つ前のrequestが返る前に再度fetchが行われる場合がある
12
+ * その場合、一つ前のfetchが戻ってきた値がそのまま捨てられるため、responseに時間がかかるAPIの場合、純粋な負荷増加に繋がってしまう
13
+ * そのため、最新のrequestのみを返すように、一つ前のrequestをabortする
14
+ */
15
+ const abortMiddleware = (useSWRNext) => (key, fetcher, config) => {
16
+ const ctrlRef = useRef(void 0);
17
+ return useSWRNext(key, fetcher ? (key, params) => {
18
+ ctrlRef.current?.abort();
19
+ ctrlRef.current = new AbortController();
20
+ return fetcher(key, {
21
+ ...params,
22
+ signal: ctrlRef.current.signal
23
+ });
24
+ } : fetcher, config);
24
25
  };
26
+ const createSwrConfig = (config) => ({
27
+ ...config,
28
+ use: [...config?.use ?? [], abortMiddleware]
29
+ });
30
+ //#endregion
31
+ //#region src/react/ApiClientConfigProvider.tsx
32
+ const ApiClientConfigContext = createContext(store);
33
+ const ApiClientConfigProvider = (props) => {
34
+ const { swr, ...config } = props.config;
35
+ useEffect(() => {
36
+ store.setState(() => config);
37
+ }, [config]);
38
+ return /* @__PURE__ */ jsx(ApiClientConfigContext.Provider, {
39
+ value: store,
40
+ children: /* @__PURE__ */ jsx(SWRConfig, {
41
+ value: createSwrConfig(swr),
42
+ children: props.children
43
+ })
44
+ });
45
+ };
46
+ const useApiClientConfigContext = () => useContext(ApiClientConfigContext) || store;
47
+ //#endregion
48
+ //#region src/react/useApiClientConfig.ts
49
+ const useApiClientConfig = () => {
50
+ const store = useApiClientConfigContext();
51
+ return useSyncExternalStore(store.subscribe, store.getState, store.getState);
52
+ };
53
+ const useApiClientConfigWithSelector = (selector) => {
54
+ const store = useApiClientConfigContext();
55
+ return useSyncExternalStoreWithSelector(store.subscribe, store.getState, store.getState, selector);
56
+ };
57
+ const useRequestInit = (customRequestInit = {}) => {
58
+ const { defaultRequestInit = {} } = useApiClientConfig();
59
+ return useMemo(() => mergeRequestInit(defaultRequestInit, customRequestInit), [defaultRequestInit, customRequestInit]);
60
+ };
61
+ //#endregion
62
+ export { ApiClientConfigProvider, createSwrConfig, useApiClientConfig, useApiClientConfigContext, useApiClientConfigWithSelector, useRequestInit };
@@ -1,13 +1,14 @@
1
- import { SWRConfiguration } from 'swr';
1
+ import { SWRConfiguration } from "swr";
2
2
 
3
+ //#region src/react/swr.d.ts
3
4
  type Configuration<T> = SWRConfiguration<T> & {
4
- /**
5
- * fetchを止めるかどうかのオプション
6
- * trueの場合: useSWRの第一引数にnullが渡されます
7
- * false,または未指定の場合: useSWRの第一引数にURLが渡されます
8
- */
9
- shouldNotFetch?: boolean;
5
+ /**
6
+ * fetchを止めるかどうかのオプション
7
+ * trueの場合: useSWRの第一引数にnullが渡されます
8
+ * false,または未指定の場合: useSWRの第一引数にURLが渡されます
9
+ */
10
+ shouldNotFetch?: boolean;
10
11
  };
11
12
  declare const createSwrConfig: (config?: SWRConfiguration) => SWRConfiguration;
12
-
13
- export { type Configuration, createSwrConfig };
13
+ //#endregion
14
+ export { Configuration, createSwrConfig };
package/dist/react/swr.js CHANGED
@@ -1,41 +1,28 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var swr_exports = {};
20
- __export(swr_exports, {
21
- createSwrConfig: () => createSwrConfig
22
- });
23
- module.exports = __toCommonJS(swr_exports);
24
- var import_react = require("react");
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ require("../_virtual/_rolldown/runtime.js");
3
+ let react = require("react");
4
+ //#region src/react/swr.ts
5
+ /**
6
+ * requestのfetchを中断させるためのmiddleware
7
+ *
8
+ * 同一hookにおいて、keyが更新されるなどにより、一つ前のrequestが返る前に再度fetchが行われる場合がある
9
+ * その場合、一つ前のfetchが戻ってきた値がそのまま捨てられるため、responseに時間がかかるAPIの場合、純粋な負荷増加に繋がってしまう
10
+ * そのため、最新のrequestのみを返すように、一つ前のrequestをabortする
11
+ */
25
12
  const abortMiddleware = (useSWRNext) => (key, fetcher, config) => {
26
- const ctrlRef = (0, import_react.useRef)(void 0);
27
- const extendedFetcher = fetcher ? (key2, params) => {
28
- ctrlRef.current?.abort();
29
- ctrlRef.current = new AbortController();
30
- return fetcher(key2, { ...params, signal: ctrlRef.current.signal });
31
- } : fetcher;
32
- return useSWRNext(key, extendedFetcher, config);
13
+ const ctrlRef = (0, react.useRef)(void 0);
14
+ return useSWRNext(key, fetcher ? (key, params) => {
15
+ ctrlRef.current?.abort();
16
+ ctrlRef.current = new AbortController();
17
+ return fetcher(key, {
18
+ ...params,
19
+ signal: ctrlRef.current.signal
20
+ });
21
+ } : fetcher, config);
33
22
  };
34
23
  const createSwrConfig = (config) => ({
35
- ...config,
36
- use: [...config?.use ?? [], abortMiddleware]
37
- });
38
- // Annotate the CommonJS export names for ESM import in node:
39
- 0 && (module.exports = {
40
- createSwrConfig
24
+ ...config,
25
+ use: [...config?.use ?? [], abortMiddleware]
41
26
  });
27
+ //#endregion
28
+ exports.createSwrConfig = createSwrConfig;
@@ -1,8 +1,8 @@
1
- import { Config } from '../apiClientConfigStore.js';
2
- import '../store.js';
1
+ import { Config } from "../apiClientConfigStore.js";
3
2
 
4
- declare const useApiClientConfig: () => Config;
3
+ //#region src/react/useApiClientConfig.d.ts
4
+ declare const useApiClientConfig: () => any;
5
5
  declare const useApiClientConfigWithSelector: <T>(selector: (state: Config) => T) => T;
6
6
  declare const useRequestInit: (customRequestInit?: RequestInit) => RequestInit;
7
-
8
- export { useApiClientConfig, useApiClientConfigWithSelector, useRequestInit };
7
+ //#endregion
8
+ export { useApiClientConfig, useApiClientConfigWithSelector, useRequestInit };