@readyfor/api-client-base 0.0.1-pr889.422eb87
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/LICENSE.md +1 -0
- package/dist/apiClientConfigStore.d.mts +17 -0
- package/dist/apiClientConfigStore.d.ts +17 -0
- package/dist/apiClientConfigStore.js +56 -0
- package/dist/apiClientConfigStore.mjs +9 -0
- package/dist/apiError.d.mts +30 -0
- package/dist/apiError.d.ts +30 -0
- package/dist/apiError.js +94 -0
- package/dist/apiError.mjs +13 -0
- package/dist/chunk-3SEO7S3Q.mjs +23 -0
- package/dist/chunk-472P3XVV.mjs +13 -0
- package/dist/chunk-C5P5R2CL.mjs +79 -0
- package/dist/chunk-DHYJM4DQ.mjs +19 -0
- package/dist/chunk-H3C7VSOJ.mjs +25 -0
- package/dist/chunk-JCZWXJBU.mjs +10 -0
- package/dist/chunk-PY35XWDS.mjs +66 -0
- package/dist/chunk-RGDL3AEX.mjs +22 -0
- package/dist/chunk-VVRQ7D57.mjs +25 -0
- package/dist/chunk-WBQAMGXK.mjs +0 -0
- package/dist/fetcher.d.mts +12 -0
- package/dist/fetcher.d.ts +12 -0
- package/dist/fetcher.js +170 -0
- package/dist/fetcher.mjs +16 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +232 -0
- package/dist/index.mjs +43 -0
- package/dist/react/ApiClientConfigProvider.d.mts +17 -0
- package/dist/react/ApiClientConfigProvider.d.ts +17 -0
- package/dist/react/ApiClientConfigProvider.js +87 -0
- package/dist/react/ApiClientConfigProvider.mjs +11 -0
- package/dist/react/index.d.mts +8 -0
- package/dist/react/index.d.ts +8 -0
- package/dist/react/index.js +112 -0
- package/dist/react/index.mjs +20 -0
- package/dist/react/swr.d.mts +13 -0
- package/dist/react/swr.d.ts +13 -0
- package/dist/react/swr.js +43 -0
- package/dist/react/swr.mjs +6 -0
- package/dist/react/useApiClientConfig.d.mts +8 -0
- package/dist/react/useApiClientConfig.d.ts +8 -0
- package/dist/react/useApiClientConfig.js +86 -0
- package/dist/react/useApiClientConfig.mjs +12 -0
- package/dist/requestUrl.d.mts +6 -0
- package/dist/requestUrl.d.ts +6 -0
- package/dist/requestUrl.js +78 -0
- package/dist/requestUrl.mjs +8 -0
- package/dist/requestUrl.test.d.mts +2 -0
- package/dist/requestUrl.test.d.ts +2 -0
- package/dist/requestUrl.test.js +171 -0
- package/dist/requestUrl.test.mjs +114 -0
- package/dist/store.d.mts +13 -0
- package/dist/store.d.ts +13 -0
- package/dist/store.js +47 -0
- package/dist/store.mjs +6 -0
- package/dist/types.d.mts +12 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.js +18 -0
- package/dist/types.mjs +1 -0
- package/dist/zod.d.mts +6 -0
- package/dist/zod.d.ts +6 -0
- package/dist/zod.js +35 -0
- package/dist/zod.mjs +8 -0
- package/package.json +66 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Copyright (C) 2011 READYFOR Inc.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Store } from './store.mjs';
|
|
2
|
+
import { UrlConfig } from './requestUrl.mjs';
|
|
3
|
+
|
|
4
|
+
type DeserializeResponseConfig = {
|
|
5
|
+
forceDeserializeResponse?: false;
|
|
6
|
+
} | {
|
|
7
|
+
forceDeserializeResponse: true;
|
|
8
|
+
onFailedDeserializeResponse: (input: RequestInfo, error: Error) => void;
|
|
9
|
+
};
|
|
10
|
+
type LoggingConfig = {
|
|
11
|
+
showsSchemaParseError?: boolean;
|
|
12
|
+
};
|
|
13
|
+
type Config = UrlConfig & DeserializeResponseConfig & LoggingConfig;
|
|
14
|
+
declare const store: Store<Config>;
|
|
15
|
+
declare const setApiClientConfig: (config: Config) => void;
|
|
16
|
+
|
|
17
|
+
export { type Config, setApiClientConfig, store };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Store } from './store.js';
|
|
2
|
+
import { UrlConfig } from './requestUrl.js';
|
|
3
|
+
|
|
4
|
+
type DeserializeResponseConfig = {
|
|
5
|
+
forceDeserializeResponse?: false;
|
|
6
|
+
} | {
|
|
7
|
+
forceDeserializeResponse: true;
|
|
8
|
+
onFailedDeserializeResponse: (input: RequestInfo, error: Error) => void;
|
|
9
|
+
};
|
|
10
|
+
type LoggingConfig = {
|
|
11
|
+
showsSchemaParseError?: boolean;
|
|
12
|
+
};
|
|
13
|
+
type Config = UrlConfig & DeserializeResponseConfig & LoggingConfig;
|
|
14
|
+
declare const store: Store<Config>;
|
|
15
|
+
declare const setApiClientConfig: (config: Config) => void;
|
|
16
|
+
|
|
17
|
+
export { type Config, setApiClientConfig, store };
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
|
|
20
|
+
// src/apiClientConfigStore.ts
|
|
21
|
+
var apiClientConfigStore_exports = {};
|
|
22
|
+
__export(apiClientConfigStore_exports, {
|
|
23
|
+
setApiClientConfig: () => setApiClientConfig,
|
|
24
|
+
store: () => store
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(apiClientConfigStore_exports);
|
|
27
|
+
|
|
28
|
+
// src/store.ts
|
|
29
|
+
var createStore = (initialState) => {
|
|
30
|
+
let state = initialState;
|
|
31
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
32
|
+
const getState = () => state;
|
|
33
|
+
const setState = (fn) => {
|
|
34
|
+
state = fn(state);
|
|
35
|
+
listeners.forEach((listener) => listener());
|
|
36
|
+
};
|
|
37
|
+
const subscribe = (listener) => {
|
|
38
|
+
listeners.add(listener);
|
|
39
|
+
return () => listeners.delete(listener);
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
getState,
|
|
43
|
+
setState,
|
|
44
|
+
subscribe
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/apiClientConfigStore.ts
|
|
49
|
+
var defaultConfig = {};
|
|
50
|
+
var store = createStore(defaultConfig);
|
|
51
|
+
var setApiClientConfig = (config) => store.setState(() => config);
|
|
52
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
53
|
+
0 && (module.exports = {
|
|
54
|
+
setApiClientConfig,
|
|
55
|
+
store
|
|
56
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type ErrorStatusCode = 301 | 302 | 400 | 401 | 403 | 404 | 413 | 418 | 422 | 500 | 502 | 503 | 504 | 901 | 999;
|
|
2
|
+
declare const errorStatusCode: {
|
|
3
|
+
readonly MOVED_PERMANENTLY: 301;
|
|
4
|
+
readonly FOUND: 302;
|
|
5
|
+
readonly BAD_REQUEST: 400;
|
|
6
|
+
readonly UNAUTHORIZED: 401;
|
|
7
|
+
readonly FORBIDDEN: 403;
|
|
8
|
+
readonly NOT_FOUND: 404;
|
|
9
|
+
readonly PAYLOAD_TOO_LARGE: 413;
|
|
10
|
+
readonly NETWORK_ERROR: 418;
|
|
11
|
+
readonly UNPROCESSABLE_ENTITY: 422;
|
|
12
|
+
readonly INTERNAL_SERVER_ERROR: 500;
|
|
13
|
+
readonly BAD_GATEWAY: 502;
|
|
14
|
+
readonly SERVER_UNAVAILABLE: 503;
|
|
15
|
+
readonly GATEWAY_TIMEOUT: 504;
|
|
16
|
+
readonly ZOD_ERROR: 901;
|
|
17
|
+
readonly UNHANDLED_ERROR: 999;
|
|
18
|
+
};
|
|
19
|
+
declare const errorTitle: {
|
|
20
|
+
[key in ErrorStatusCode]: string;
|
|
21
|
+
};
|
|
22
|
+
declare class HTTPError extends Error {
|
|
23
|
+
status: ErrorStatusCode;
|
|
24
|
+
body?: unknown;
|
|
25
|
+
constructor(status: ErrorStatusCode);
|
|
26
|
+
constructor(status: ErrorStatusCode, body: unknown);
|
|
27
|
+
}
|
|
28
|
+
declare const getErrorStatus: (response: unknown) => ErrorStatusCode | undefined;
|
|
29
|
+
|
|
30
|
+
export { type ErrorStatusCode, HTTPError, errorStatusCode, errorTitle, getErrorStatus };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type ErrorStatusCode = 301 | 302 | 400 | 401 | 403 | 404 | 413 | 418 | 422 | 500 | 502 | 503 | 504 | 901 | 999;
|
|
2
|
+
declare const errorStatusCode: {
|
|
3
|
+
readonly MOVED_PERMANENTLY: 301;
|
|
4
|
+
readonly FOUND: 302;
|
|
5
|
+
readonly BAD_REQUEST: 400;
|
|
6
|
+
readonly UNAUTHORIZED: 401;
|
|
7
|
+
readonly FORBIDDEN: 403;
|
|
8
|
+
readonly NOT_FOUND: 404;
|
|
9
|
+
readonly PAYLOAD_TOO_LARGE: 413;
|
|
10
|
+
readonly NETWORK_ERROR: 418;
|
|
11
|
+
readonly UNPROCESSABLE_ENTITY: 422;
|
|
12
|
+
readonly INTERNAL_SERVER_ERROR: 500;
|
|
13
|
+
readonly BAD_GATEWAY: 502;
|
|
14
|
+
readonly SERVER_UNAVAILABLE: 503;
|
|
15
|
+
readonly GATEWAY_TIMEOUT: 504;
|
|
16
|
+
readonly ZOD_ERROR: 901;
|
|
17
|
+
readonly UNHANDLED_ERROR: 999;
|
|
18
|
+
};
|
|
19
|
+
declare const errorTitle: {
|
|
20
|
+
[key in ErrorStatusCode]: string;
|
|
21
|
+
};
|
|
22
|
+
declare class HTTPError extends Error {
|
|
23
|
+
status: ErrorStatusCode;
|
|
24
|
+
body?: unknown;
|
|
25
|
+
constructor(status: ErrorStatusCode);
|
|
26
|
+
constructor(status: ErrorStatusCode, body: unknown);
|
|
27
|
+
}
|
|
28
|
+
declare const getErrorStatus: (response: unknown) => ErrorStatusCode | undefined;
|
|
29
|
+
|
|
30
|
+
export { type ErrorStatusCode, HTTPError, errorStatusCode, errorTitle, getErrorStatus };
|
package/dist/apiError.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
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
|
+
|
|
20
|
+
// src/apiError.ts
|
|
21
|
+
var apiError_exports = {};
|
|
22
|
+
__export(apiError_exports, {
|
|
23
|
+
HTTPError: () => HTTPError,
|
|
24
|
+
errorStatusCode: () => errorStatusCode,
|
|
25
|
+
errorTitle: () => errorTitle,
|
|
26
|
+
getErrorStatus: () => getErrorStatus
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(apiError_exports);
|
|
29
|
+
|
|
30
|
+
// src/zod.ts
|
|
31
|
+
var isZodError = (obj) => obj !== null && typeof obj === "object" && obj.constructor.name === "ZodError";
|
|
32
|
+
|
|
33
|
+
// src/apiError.ts
|
|
34
|
+
var errorStatusCode = {
|
|
35
|
+
MOVED_PERMANENTLY: 301,
|
|
36
|
+
FOUND: 302,
|
|
37
|
+
BAD_REQUEST: 400,
|
|
38
|
+
UNAUTHORIZED: 401,
|
|
39
|
+
FORBIDDEN: 403,
|
|
40
|
+
NOT_FOUND: 404,
|
|
41
|
+
PAYLOAD_TOO_LARGE: 413,
|
|
42
|
+
NETWORK_ERROR: 418,
|
|
43
|
+
UNPROCESSABLE_ENTITY: 422,
|
|
44
|
+
INTERNAL_SERVER_ERROR: 500,
|
|
45
|
+
BAD_GATEWAY: 502,
|
|
46
|
+
SERVER_UNAVAILABLE: 503,
|
|
47
|
+
GATEWAY_TIMEOUT: 504,
|
|
48
|
+
ZOD_ERROR: 901,
|
|
49
|
+
UNHANDLED_ERROR: 999
|
|
50
|
+
};
|
|
51
|
+
var errorTitle = {
|
|
52
|
+
301: "\u30DA\u30FC\u30B8\u304C\u79FB\u52D5\u3055\u308C\u3066\u3044\u307E\u3059",
|
|
53
|
+
302: "\u30DA\u30FC\u30B8\u304C\u4E00\u6642\u7684\u306B\u79FB\u52D5\u3055\u308C\u3066\u3044\u307E\u3059",
|
|
54
|
+
400: "\u30EA\u30AF\u30A8\u30B9\u30C8\u304C\u4E0D\u6B63\u3067\u3059",
|
|
55
|
+
401: "\u30ED\u30B0\u30A4\u30F3\u3057\u3066\u304F\u3060\u3055\u3044",
|
|
56
|
+
403: "\u4E0D\u6B63\u306A\u30C8\u30FC\u30AF\u30F3\u3067\u3059",
|
|
57
|
+
404: "\u30DA\u30FC\u30B8\u304C\u898B\u3064\u304B\u308A\u307E\u305B\u3093\u3067\u3057\u305F",
|
|
58
|
+
413: "\u901A\u4FE1\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
59
|
+
418: "\u901A\u4FE1\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
60
|
+
422: "\u30C7\u30FC\u30BF\u304C\u4E0D\u6B63\u3067\u3059",
|
|
61
|
+
500: "\u30B5\u30FC\u30D0\u30FC\u3067\u30A8\u30E9\u30FC\u304C\u8D77\u304D\u307E\u3057\u305F",
|
|
62
|
+
502: "\u30B5\u30FC\u30D0\u30FC\u3067\u30A8\u30E9\u30FC\u304C\u8D77\u304D\u307E\u3057\u305F",
|
|
63
|
+
503: "\u30E1\u30F3\u30C6\u30CA\u30F3\u30B9\u4E2D\u3067\u3059",
|
|
64
|
+
504: "\u901A\u4FE1\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
65
|
+
901: "\u554F\u984C\u304C\u767A\u751F\u3057\u307E\u3057\u305F",
|
|
66
|
+
999: "\u30B5\u30FC\u30D0\u30FC\u3067\u30A8\u30E9\u30FC\u304C\u8D77\u304D\u307E\u3057\u305F"
|
|
67
|
+
};
|
|
68
|
+
var HTTPError = class extends Error {
|
|
69
|
+
status;
|
|
70
|
+
body;
|
|
71
|
+
constructor(status, body) {
|
|
72
|
+
super(errorTitle[status]);
|
|
73
|
+
this.name = "HTTPError";
|
|
74
|
+
this.status = status;
|
|
75
|
+
this.body = body;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
var getErrorStatus = (response) => {
|
|
79
|
+
if (response instanceof Object && "status" in response && typeof response["status"] === "number") {
|
|
80
|
+
const status = Number(response["status"]);
|
|
81
|
+
return Object.values(errorStatusCode).includes(status) ? status : 999;
|
|
82
|
+
}
|
|
83
|
+
if (isZodError(response)) {
|
|
84
|
+
return errorStatusCode.ZOD_ERROR;
|
|
85
|
+
}
|
|
86
|
+
return void 0;
|
|
87
|
+
};
|
|
88
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
89
|
+
0 && (module.exports = {
|
|
90
|
+
HTTPError,
|
|
91
|
+
errorStatusCode,
|
|
92
|
+
errorTitle,
|
|
93
|
+
getErrorStatus
|
|
94
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// src/store.ts
|
|
2
|
+
var createStore = (initialState) => {
|
|
3
|
+
let state = initialState;
|
|
4
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
5
|
+
const getState = () => state;
|
|
6
|
+
const setState = (fn) => {
|
|
7
|
+
state = fn(state);
|
|
8
|
+
listeners.forEach((listener) => listener());
|
|
9
|
+
};
|
|
10
|
+
const subscribe = (listener) => {
|
|
11
|
+
listeners.add(listener);
|
|
12
|
+
return () => listeners.delete(listener);
|
|
13
|
+
};
|
|
14
|
+
return {
|
|
15
|
+
getState,
|
|
16
|
+
setState,
|
|
17
|
+
subscribe
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
createStore
|
|
23
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createStore
|
|
3
|
+
} from "./chunk-3SEO7S3Q.mjs";
|
|
4
|
+
|
|
5
|
+
// src/apiClientConfigStore.ts
|
|
6
|
+
var defaultConfig = {};
|
|
7
|
+
var store = createStore(defaultConfig);
|
|
8
|
+
var setApiClientConfig = (config) => store.setState(() => config);
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
store,
|
|
12
|
+
setApiClientConfig
|
|
13
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HTTPError,
|
|
3
|
+
errorStatusCode
|
|
4
|
+
} from "./chunk-PY35XWDS.mjs";
|
|
5
|
+
import {
|
|
6
|
+
store
|
|
7
|
+
} from "./chunk-472P3XVV.mjs";
|
|
8
|
+
|
|
9
|
+
// src/fetcher.ts
|
|
10
|
+
var defaultRequestInit = {
|
|
11
|
+
credentials: "include"
|
|
12
|
+
};
|
|
13
|
+
var createJsonFetcher = (schema, customRequestInit = {}) => (input, requestInit = {}) => createTextFetcher(customRequestInit)(input, requestInit).then((text) => {
|
|
14
|
+
const parsedResponse = JSON.parse(text);
|
|
15
|
+
const result = schema.safeParse(parsedResponse);
|
|
16
|
+
const config = store.getState();
|
|
17
|
+
if (result.success) {
|
|
18
|
+
return result.data;
|
|
19
|
+
}
|
|
20
|
+
if (config.showsSchemaParseError) {
|
|
21
|
+
console.error(result.error);
|
|
22
|
+
}
|
|
23
|
+
if (config.forceDeserializeResponse) {
|
|
24
|
+
config.onFailedDeserializeResponse(input, result.error);
|
|
25
|
+
return parsedResponse;
|
|
26
|
+
}
|
|
27
|
+
throw result.error;
|
|
28
|
+
});
|
|
29
|
+
var createTextFetcher = (customRequestInit = {}) => (input, requestInit = {}) => fetch(input, {
|
|
30
|
+
...defaultRequestInit,
|
|
31
|
+
...customRequestInit,
|
|
32
|
+
...requestInit,
|
|
33
|
+
headers: {
|
|
34
|
+
...defaultRequestInit.headers,
|
|
35
|
+
...customRequestInit.headers,
|
|
36
|
+
...requestInit.headers
|
|
37
|
+
}
|
|
38
|
+
}).then(async (res) => {
|
|
39
|
+
const body = await res.text();
|
|
40
|
+
if (res.ok) return body;
|
|
41
|
+
const code = Object.values(errorStatusCode).includes(res.status) ? res.status : 999;
|
|
42
|
+
throw new HTTPError(code, body);
|
|
43
|
+
});
|
|
44
|
+
var createBlobFetcher = (customRequestInit = {}) => (input, requestInit = {}) => fetch(input, {
|
|
45
|
+
...defaultRequestInit,
|
|
46
|
+
...customRequestInit,
|
|
47
|
+
...requestInit,
|
|
48
|
+
headers: {
|
|
49
|
+
...defaultRequestInit.headers,
|
|
50
|
+
...customRequestInit.headers,
|
|
51
|
+
...requestInit.headers
|
|
52
|
+
}
|
|
53
|
+
}).then(async (res) => {
|
|
54
|
+
const body = await res.blob();
|
|
55
|
+
if (res.ok) return { body, headers: res.headers };
|
|
56
|
+
const code = Object.values(errorStatusCode).includes(res.status) ? res.status : 999;
|
|
57
|
+
throw new HTTPError(code, body);
|
|
58
|
+
});
|
|
59
|
+
var createVoidFetcher = (customRequestInit = {}) => (input, requestInit = {}) => fetch(input, {
|
|
60
|
+
...defaultRequestInit,
|
|
61
|
+
...customRequestInit,
|
|
62
|
+
...requestInit,
|
|
63
|
+
headers: {
|
|
64
|
+
...defaultRequestInit.headers,
|
|
65
|
+
...customRequestInit.headers,
|
|
66
|
+
...requestInit.headers
|
|
67
|
+
}
|
|
68
|
+
}).then(async (res) => {
|
|
69
|
+
if (res.ok) return;
|
|
70
|
+
const code = Object.values(errorStatusCode).includes(res.status) ? res.status : 999;
|
|
71
|
+
throw new HTTPError(code);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export {
|
|
75
|
+
createJsonFetcher,
|
|
76
|
+
createTextFetcher,
|
|
77
|
+
createBlobFetcher,
|
|
78
|
+
createVoidFetcher
|
|
79
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/react/swr.ts
|
|
2
|
+
import { useRef } from "react";
|
|
3
|
+
var abortMiddleware = (useSWRNext) => (key, fetcher, config) => {
|
|
4
|
+
const ctrlRef = useRef();
|
|
5
|
+
const extendedFetcher = fetcher ? (key2) => {
|
|
6
|
+
ctrlRef.current?.abort();
|
|
7
|
+
ctrlRef.current = new AbortController();
|
|
8
|
+
return fetcher(key2, { signal: ctrlRef.current.signal });
|
|
9
|
+
} : fetcher;
|
|
10
|
+
return useSWRNext(key, extendedFetcher, config);
|
|
11
|
+
};
|
|
12
|
+
var createSwrConfig = (config) => ({
|
|
13
|
+
...config,
|
|
14
|
+
use: [...config?.use ?? [], abortMiddleware]
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
createSwrConfig
|
|
19
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useApiClientConfigContext
|
|
3
|
+
} from "./chunk-VVRQ7D57.mjs";
|
|
4
|
+
|
|
5
|
+
// src/react/useApiClientConfig.ts
|
|
6
|
+
import { useSyncExternalStore } from "use-sync-external-store/shim";
|
|
7
|
+
import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/with-selector";
|
|
8
|
+
var useApiClientConfig = () => {
|
|
9
|
+
const store = useApiClientConfigContext();
|
|
10
|
+
return useSyncExternalStore(store.subscribe, store.getState);
|
|
11
|
+
};
|
|
12
|
+
var useApiClientConfigWithSelector = (selector) => {
|
|
13
|
+
const store = useApiClientConfigContext();
|
|
14
|
+
return useSyncExternalStoreWithSelector(
|
|
15
|
+
store.subscribe,
|
|
16
|
+
store.getState,
|
|
17
|
+
null,
|
|
18
|
+
selector
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
useApiClientConfig,
|
|
24
|
+
useApiClientConfigWithSelector
|
|
25
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isZodError
|
|
3
|
+
} from "./chunk-JCZWXJBU.mjs";
|
|
4
|
+
|
|
5
|
+
// src/apiError.ts
|
|
6
|
+
var errorStatusCode = {
|
|
7
|
+
MOVED_PERMANENTLY: 301,
|
|
8
|
+
FOUND: 302,
|
|
9
|
+
BAD_REQUEST: 400,
|
|
10
|
+
UNAUTHORIZED: 401,
|
|
11
|
+
FORBIDDEN: 403,
|
|
12
|
+
NOT_FOUND: 404,
|
|
13
|
+
PAYLOAD_TOO_LARGE: 413,
|
|
14
|
+
NETWORK_ERROR: 418,
|
|
15
|
+
UNPROCESSABLE_ENTITY: 422,
|
|
16
|
+
INTERNAL_SERVER_ERROR: 500,
|
|
17
|
+
BAD_GATEWAY: 502,
|
|
18
|
+
SERVER_UNAVAILABLE: 503,
|
|
19
|
+
GATEWAY_TIMEOUT: 504,
|
|
20
|
+
ZOD_ERROR: 901,
|
|
21
|
+
UNHANDLED_ERROR: 999
|
|
22
|
+
};
|
|
23
|
+
var errorTitle = {
|
|
24
|
+
301: "\u30DA\u30FC\u30B8\u304C\u79FB\u52D5\u3055\u308C\u3066\u3044\u307E\u3059",
|
|
25
|
+
302: "\u30DA\u30FC\u30B8\u304C\u4E00\u6642\u7684\u306B\u79FB\u52D5\u3055\u308C\u3066\u3044\u307E\u3059",
|
|
26
|
+
400: "\u30EA\u30AF\u30A8\u30B9\u30C8\u304C\u4E0D\u6B63\u3067\u3059",
|
|
27
|
+
401: "\u30ED\u30B0\u30A4\u30F3\u3057\u3066\u304F\u3060\u3055\u3044",
|
|
28
|
+
403: "\u4E0D\u6B63\u306A\u30C8\u30FC\u30AF\u30F3\u3067\u3059",
|
|
29
|
+
404: "\u30DA\u30FC\u30B8\u304C\u898B\u3064\u304B\u308A\u307E\u305B\u3093\u3067\u3057\u305F",
|
|
30
|
+
413: "\u901A\u4FE1\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
31
|
+
418: "\u901A\u4FE1\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
32
|
+
422: "\u30C7\u30FC\u30BF\u304C\u4E0D\u6B63\u3067\u3059",
|
|
33
|
+
500: "\u30B5\u30FC\u30D0\u30FC\u3067\u30A8\u30E9\u30FC\u304C\u8D77\u304D\u307E\u3057\u305F",
|
|
34
|
+
502: "\u30B5\u30FC\u30D0\u30FC\u3067\u30A8\u30E9\u30FC\u304C\u8D77\u304D\u307E\u3057\u305F",
|
|
35
|
+
503: "\u30E1\u30F3\u30C6\u30CA\u30F3\u30B9\u4E2D\u3067\u3059",
|
|
36
|
+
504: "\u901A\u4FE1\u306B\u5931\u6557\u3057\u307E\u3057\u305F",
|
|
37
|
+
901: "\u554F\u984C\u304C\u767A\u751F\u3057\u307E\u3057\u305F",
|
|
38
|
+
999: "\u30B5\u30FC\u30D0\u30FC\u3067\u30A8\u30E9\u30FC\u304C\u8D77\u304D\u307E\u3057\u305F"
|
|
39
|
+
};
|
|
40
|
+
var HTTPError = class extends Error {
|
|
41
|
+
status;
|
|
42
|
+
body;
|
|
43
|
+
constructor(status, body) {
|
|
44
|
+
super(errorTitle[status]);
|
|
45
|
+
this.name = "HTTPError";
|
|
46
|
+
this.status = status;
|
|
47
|
+
this.body = body;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var getErrorStatus = (response) => {
|
|
51
|
+
if (response instanceof Object && "status" in response && typeof response["status"] === "number") {
|
|
52
|
+
const status = Number(response["status"]);
|
|
53
|
+
return Object.values(errorStatusCode).includes(status) ? status : 999;
|
|
54
|
+
}
|
|
55
|
+
if (isZodError(response)) {
|
|
56
|
+
return errorStatusCode.ZOD_ERROR;
|
|
57
|
+
}
|
|
58
|
+
return void 0;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export {
|
|
62
|
+
errorStatusCode,
|
|
63
|
+
errorTitle,
|
|
64
|
+
HTTPError,
|
|
65
|
+
getErrorStatus
|
|
66
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
store
|
|
3
|
+
} from "./chunk-472P3XVV.mjs";
|
|
4
|
+
|
|
5
|
+
// src/requestUrl.ts
|
|
6
|
+
import qs from "qs";
|
|
7
|
+
function __internal__requestUrl(...args) {
|
|
8
|
+
const config = store.getState();
|
|
9
|
+
const rawPath = args[0];
|
|
10
|
+
const params = args[1] || {};
|
|
11
|
+
const query = args[2];
|
|
12
|
+
const hostName = config.hostName ? typeof config.hostName === "string" ? config.hostName : config.hostName(rawPath) : "";
|
|
13
|
+
const path = hostName + Object.entries(params).reduce(
|
|
14
|
+
(path2, [key, value]) => path2.replace(`{${key}}`, String(value)),
|
|
15
|
+
rawPath
|
|
16
|
+
);
|
|
17
|
+
return query ? `${path}?${qs.stringify(query)}` : path;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export {
|
|
21
|
+
__internal__requestUrl
|
|
22
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSwrConfig
|
|
3
|
+
} from "./chunk-DHYJM4DQ.mjs";
|
|
4
|
+
import {
|
|
5
|
+
store
|
|
6
|
+
} from "./chunk-472P3XVV.mjs";
|
|
7
|
+
|
|
8
|
+
// src/react/ApiClientConfigProvider.tsx
|
|
9
|
+
import { createContext, useContext, useEffect } from "react";
|
|
10
|
+
import { SWRConfig } from "swr";
|
|
11
|
+
import { jsx } from "react/jsx-runtime";
|
|
12
|
+
var ApiClientConfigContext = createContext(store);
|
|
13
|
+
var ApiClientConfigProvider = (props) => {
|
|
14
|
+
const { swr, ...config } = props.config;
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
store.setState(() => config);
|
|
17
|
+
}, [config]);
|
|
18
|
+
return /* @__PURE__ */ jsx(ApiClientConfigContext.Provider, { value: store, children: /* @__PURE__ */ jsx(SWRConfig, { value: createSwrConfig(swr), children: props.children }) });
|
|
19
|
+
};
|
|
20
|
+
var useApiClientConfigContext = () => useContext(ApiClientConfigContext) || store;
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
ApiClientConfigProvider,
|
|
24
|
+
useApiClientConfigContext
|
|
25
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
declare const createJsonFetcher: <T = never>(schema: z.ZodType<T, any, any>, customRequestInit?: RequestInit) => (input: RequestInfo, requestInit?: RequestInit) => Promise<T>;
|
|
4
|
+
declare const createTextFetcher: (customRequestInit?: RequestInit) => (input: RequestInfo, requestInit?: RequestInit) => Promise<string>;
|
|
5
|
+
type BlobFetcherResponse = {
|
|
6
|
+
body: Blob;
|
|
7
|
+
headers: Headers;
|
|
8
|
+
};
|
|
9
|
+
declare const createBlobFetcher: (customRequestInit?: RequestInit) => (input: RequestInfo, requestInit?: RequestInit) => Promise<BlobFetcherResponse>;
|
|
10
|
+
declare const createVoidFetcher: (customRequestInit?: RequestInit) => (input: RequestInfo, requestInit?: RequestInit) => Promise<void>;
|
|
11
|
+
|
|
12
|
+
export { type BlobFetcherResponse, createBlobFetcher, createJsonFetcher, createTextFetcher, createVoidFetcher };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
declare const createJsonFetcher: <T = never>(schema: z.ZodType<T, any, any>, customRequestInit?: RequestInit) => (input: RequestInfo, requestInit?: RequestInit) => Promise<T>;
|
|
4
|
+
declare const createTextFetcher: (customRequestInit?: RequestInit) => (input: RequestInfo, requestInit?: RequestInit) => Promise<string>;
|
|
5
|
+
type BlobFetcherResponse = {
|
|
6
|
+
body: Blob;
|
|
7
|
+
headers: Headers;
|
|
8
|
+
};
|
|
9
|
+
declare const createBlobFetcher: (customRequestInit?: RequestInit) => (input: RequestInfo, requestInit?: RequestInit) => Promise<BlobFetcherResponse>;
|
|
10
|
+
declare const createVoidFetcher: (customRequestInit?: RequestInit) => (input: RequestInfo, requestInit?: RequestInit) => Promise<void>;
|
|
11
|
+
|
|
12
|
+
export { type BlobFetcherResponse, createBlobFetcher, createJsonFetcher, createTextFetcher, createVoidFetcher };
|