jax-hono 1.0.19 → 1.0.21

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/utils/http.ts ADDED
@@ -0,0 +1,106 @@
1
+ import axios, {
2
+ AxiosError,
3
+ type AxiosInstance,
4
+ type AxiosRequestConfig,
5
+ type AxiosResponse,
6
+ type InternalAxiosRequestConfig,
7
+ } from "axios";
8
+
9
+ export type HttpErrorInfo = {
10
+ message: string;
11
+ data?: unknown;
12
+ status?: number;
13
+ };
14
+
15
+ export type HttpRequestConfig = InternalAxiosRequestConfig & {
16
+ debug?: boolean;
17
+ onSuccess: (data: unknown) => unknown;
18
+ onFail: (error: unknown, res?: unknown) => Promise<never>;
19
+ onResponse: (response: AxiosResponse) => unknown | Promise<unknown>;
20
+ };
21
+
22
+ export type HttpOptions = AxiosRequestConfig & {
23
+ debug?: boolean;
24
+ onRequest?: (config: HttpRequestConfig) => void | Promise<void>;
25
+ onResponse?: (response: AxiosResponse) => unknown | Promise<unknown>;
26
+ onError?: (error: AxiosError, errorInfo: HttpErrorInfo) => void;
27
+ };
28
+
29
+ export function createHttp(options: HttpOptions): AxiosInstance {
30
+ const instance = axios.create(options);
31
+
32
+ instance.interceptors.request.use(
33
+ async (config) => {
34
+ const httpConfig = config as HttpRequestConfig;
35
+
36
+ httpConfig.onSuccess = (data) => data;
37
+ httpConfig.onFail = (error, res) => {
38
+ console.error("[http error]", error, res);
39
+ return Promise.reject(error);
40
+ };
41
+ httpConfig.onResponse =
42
+ options.onResponse ??
43
+ ((response) => {
44
+ return response.data;
45
+ });
46
+
47
+ if (httpConfig.debug || options.debug) {
48
+ console.log(
49
+ "请求地址:",
50
+ `[${httpConfig.method?.toUpperCase()}]`,
51
+ (httpConfig.baseURL ?? "") + (httpConfig.url ?? ""),
52
+ );
53
+ console.log("URL参数:", httpConfig.params);
54
+ console.log("提交数据:", httpConfig.data);
55
+ }
56
+
57
+ await options.onRequest?.(httpConfig);
58
+
59
+ return httpConfig;
60
+ },
61
+ (error) => {
62
+ return Promise.reject(error);
63
+ },
64
+ );
65
+
66
+ const handleResponse = async (response: AxiosResponse) => {
67
+ const config = response.config as HttpRequestConfig;
68
+ const { headers, data } = response;
69
+ const contentType = headers["content-type"];
70
+
71
+ if (response.status === 200) {
72
+ // if (contentType === "image/jpeg") return data;
73
+ return config.onResponse(response);
74
+ }
75
+
76
+ return config.onFail(`请求失败,状态码${response.status}`, response.data);
77
+ };
78
+
79
+ const handleResponseError = (error: unknown) => {
80
+ const axiosError = error as AxiosError;
81
+ const config = axiosError.config as HttpRequestConfig | undefined;
82
+ const errorInfo: HttpErrorInfo = {
83
+ message: axiosError.message || "服务器异常",
84
+ };
85
+
86
+ if (axiosError.response) {
87
+ errorInfo.data = axiosError.response.data;
88
+ errorInfo.status = axiosError.response.status;
89
+ }
90
+
91
+ options.onError?.(axiosError, errorInfo);
92
+
93
+ if (!config) {
94
+ return Promise.reject(errorInfo.message);
95
+ }
96
+
97
+ return config.onFail(errorInfo.message, errorInfo);
98
+ };
99
+
100
+ instance.interceptors.response.use(
101
+ handleResponse as any,
102
+ handleResponseError,
103
+ );
104
+
105
+ return instance;
106
+ }
package/utils/index.ts CHANGED
@@ -1,6 +1,7 @@
1
- export * from "./array";
2
- export * from "./async";
1
+ export * from "./array";
2
+ export * from "./async";
3
3
  export * from "./crypto";
4
+ export * from "./http";
4
5
  export * from "./order";
5
6
  export * from "./regexp";
6
7
  export * from "./transform";
package/utils/regexp.ts CHANGED
@@ -1,17 +1,17 @@
1
- /**
2
- * 转义正则表达式中的特殊字符,将字符串安全地用于动态构建正则表达式。
3
- *
4
- * 会将 `. * + ? ^ $ { } ( ) | [ ] \` 等特殊字符前加上反斜杠进行转义,
5
- * 确保它们被当作字面字符匹配,而非正则语法符号。
6
- *
7
- * @param value - 需要转义的值,可以是任意类型,内部会通过 `String()` 转换为字符串
8
- * @returns 转义后的字符串,可直接嵌入 `new RegExp()` 中安全使用
9
- *
10
- * @example
11
- * escapeRegex('hello.world') // => 'hello\\.world'
12
- * escapeRegex('price$') // => 'price\\$'
13
- * escapeRegex(123) // => '123'
14
- */
1
+ /**
2
+ * 转义正则表达式中的特殊字符,将字符串安全地用于动态构建正则表达式。
3
+ *
4
+ * 会将 `. * + ? ^ $ { } ( ) | [ ] \` 等特殊字符前加上反斜杠进行转义,
5
+ * 确保它们被当作字面字符匹配,而非正则语法符号。
6
+ *
7
+ * @param value - 需要转义的值,可以是任意类型,内部会通过 `String()` 转换为字符串
8
+ * @returns 转义后的字符串,可直接嵌入 `new RegExp()` 中安全使用
9
+ *
10
+ * @example
11
+ * escapeRegex('hello.world') // => 'hello\\.world'
12
+ * escapeRegex('price$') // => 'price\\$'
13
+ * escapeRegex(123) // => '123'
14
+ */
15
15
  export function escapeRegex(value: unknown) {
16
16
  return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
17
17
  }
@@ -1,3 +1,3 @@
1
- import { pick, omit } from 'lodash-es';
2
-
3
- export { pick, omit };
1
+ import { pick, omit } from 'lodash-es';
2
+
3
+ export { pick, omit };