@andbridge/glowpod 1.0.0
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/glowpod.ts +3 -0
- package/package.json +20 -0
- package/src/config/config.ts +2 -0
- package/src/core/core.ts +45 -0
- package/src/interceptors/interceptors.tsx +105 -0
- package/src/model/model.ts +31 -0
- package/tsconfig.json +9 -0
package/glowpod.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@andbridge/glowpod",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "glowpod.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"packageManager": "pnpm@10.15.0",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"typescript": "~5.9.3"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"axios": "^1.13.2",
|
|
18
|
+
"element-plus": "^2.11.7"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/core/core.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Options, Response } from "../model/model";
|
|
2
|
+
import axios, { AxiosInstance, CreateAxiosDefaults } from "axios";
|
|
3
|
+
export default class Core {
|
|
4
|
+
protected instance: AxiosInstance;
|
|
5
|
+
|
|
6
|
+
constructor(options: CreateAxiosDefaults) {
|
|
7
|
+
this.instance = axios.create(options);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public getInstance = () => {
|
|
11
|
+
return this.instance;
|
|
12
|
+
};
|
|
13
|
+
public Get = <T>(options: Options): T =>
|
|
14
|
+
this.instance({
|
|
15
|
+
method: "GET",
|
|
16
|
+
url: options.url,
|
|
17
|
+
params: options.params,
|
|
18
|
+
headers: { ...options.headers },
|
|
19
|
+
...options.others,
|
|
20
|
+
}) as unknown as T;
|
|
21
|
+
public Put = <T>(options: Options): T =>
|
|
22
|
+
this.instance({
|
|
23
|
+
method: "PUT",
|
|
24
|
+
url: options.url,
|
|
25
|
+
params: options.params,
|
|
26
|
+
headers: { ...options.headers },
|
|
27
|
+
...options.others,
|
|
28
|
+
}) as unknown as T;
|
|
29
|
+
public Delete = <T>(options: Options): T =>
|
|
30
|
+
this.instance({
|
|
31
|
+
method: "DELETE",
|
|
32
|
+
url: options.url,
|
|
33
|
+
params: options.params,
|
|
34
|
+
headers: { ...options.headers },
|
|
35
|
+
...options.others,
|
|
36
|
+
}) as unknown as T;
|
|
37
|
+
public Post = <T>(options: Options): T =>
|
|
38
|
+
this.instance({
|
|
39
|
+
method: "POST",
|
|
40
|
+
url: options.url,
|
|
41
|
+
data: options.params,
|
|
42
|
+
headers: { ...options.headers },
|
|
43
|
+
...options.others,
|
|
44
|
+
}) as unknown as T;
|
|
45
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AxiosInstance,
|
|
3
|
+
AxiosResponse,
|
|
4
|
+
AxiosError,
|
|
5
|
+
InternalAxiosRequestConfig,
|
|
6
|
+
} from "axios";
|
|
7
|
+
import { InterceptorsOptions, Response } from "../model/model";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 设置拦截器
|
|
11
|
+
* @param instance axios实例
|
|
12
|
+
* @param @InterceptorsOptions
|
|
13
|
+
* @returns void
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const _authFailedCaller = () => {};
|
|
17
|
+
|
|
18
|
+
const _networkFailedCaller = () => {};
|
|
19
|
+
|
|
20
|
+
const _serverKillCaller = () => {};
|
|
21
|
+
|
|
22
|
+
enum NetworkStatus {
|
|
23
|
+
success = 200,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const setupInterceptors = ({
|
|
27
|
+
instance,
|
|
28
|
+
authTokenStorageName = "token",
|
|
29
|
+
authFailedCaller = _authFailedCaller,
|
|
30
|
+
networkSuccessCode = 200,
|
|
31
|
+
authFailedCode = 401,
|
|
32
|
+
networkFailedCaller = _networkFailedCaller,
|
|
33
|
+
serverKillCaller = _serverKillCaller,
|
|
34
|
+
responseStruct = {
|
|
35
|
+
codeName: "code",
|
|
36
|
+
messageName: "message",
|
|
37
|
+
dataName: "data",
|
|
38
|
+
},
|
|
39
|
+
}: InterceptorsOptions & {
|
|
40
|
+
instance: AxiosInstance;
|
|
41
|
+
}) => {
|
|
42
|
+
let timer: NodeJS.Timeout | null = null;
|
|
43
|
+
let isDefaultOpenErrorHandle = true;
|
|
44
|
+
const defaultErrorHandler = (response: any) => {
|
|
45
|
+
const hiddenDefaultErrorHandler = () => (isDefaultOpenErrorHandle = false);
|
|
46
|
+
if (timer) clearTimeout(timer);
|
|
47
|
+
timer = setTimeout(() => {
|
|
48
|
+
if (!isDefaultOpenErrorHandle) return;
|
|
49
|
+
if (response.status != NetworkStatus.success) {
|
|
50
|
+
serverKillCaller();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (response.data?.[responseStruct.codeName] == authFailedCode) {
|
|
54
|
+
authFailedCaller();
|
|
55
|
+
}
|
|
56
|
+
if (response.data?.[responseStruct.codeName] != networkSuccessCode) {
|
|
57
|
+
networkFailedCaller();
|
|
58
|
+
}
|
|
59
|
+
}, 0);
|
|
60
|
+
return hiddenDefaultErrorHandler;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// 请求拦截器
|
|
64
|
+
instance.interceptors.request.use(
|
|
65
|
+
async (config) => {
|
|
66
|
+
// 从存储中获取token
|
|
67
|
+
const token = localStorage.getItem(authTokenStorageName as string);
|
|
68
|
+
if (token && config.headers) {
|
|
69
|
+
config.headers.set(authTokenStorageName, token);
|
|
70
|
+
}
|
|
71
|
+
return config;
|
|
72
|
+
},
|
|
73
|
+
(error: AxiosError) => {
|
|
74
|
+
console.log("请求错误拦截器:", error);
|
|
75
|
+
defaultErrorHandler(error);
|
|
76
|
+
return Promise.reject(error);
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
// 响应拦截器
|
|
81
|
+
|
|
82
|
+
instance.interceptors.response.use(
|
|
83
|
+
(response) => {
|
|
84
|
+
if (response.config.responseType === "blob") {
|
|
85
|
+
return response;
|
|
86
|
+
} else {
|
|
87
|
+
switch (response.data?.[responseStruct.codeName]) {
|
|
88
|
+
case networkSuccessCode:
|
|
89
|
+
return response.data?.[responseStruct.dataName];
|
|
90
|
+
default:
|
|
91
|
+
const hiddenDefaultErrorHandler = defaultErrorHandler(response);
|
|
92
|
+
throw {
|
|
93
|
+
...response,
|
|
94
|
+
hiddenDefaultErrorHandler,
|
|
95
|
+
uri: instance.getUri(),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
(error: AxiosError) => {
|
|
101
|
+
const hiddenDefaultErrorHandler = defaultErrorHandler(error);
|
|
102
|
+
throw { ...error, hiddenDefaultErrorHandler, uri: instance.getUri() };
|
|
103
|
+
}
|
|
104
|
+
);
|
|
105
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AxiosError, AxiosResponse } from "axios";
|
|
2
|
+
|
|
3
|
+
export interface Options {
|
|
4
|
+
url: string;
|
|
5
|
+
headers?: {};
|
|
6
|
+
params?: {} | null;
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface Response<T> {
|
|
11
|
+
code: number;
|
|
12
|
+
message: string;
|
|
13
|
+
data: T;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ResponseError<T = unknown, D = any> extends AxiosError<T, D> {
|
|
17
|
+
hiddenDefaultErrorHandler(): void;
|
|
18
|
+
}
|
|
19
|
+
export type InterceptorsOptions = {
|
|
20
|
+
authTokenStorageName?: string;
|
|
21
|
+
networkSuccessCode?: number;
|
|
22
|
+
authFailedCode?: number;
|
|
23
|
+
authFailedCaller?: Function;
|
|
24
|
+
networkFailedCaller?: Function;
|
|
25
|
+
serverKillCaller?: Function;
|
|
26
|
+
responseStruct?: {
|
|
27
|
+
codeName?: string;
|
|
28
|
+
messageName?: string;
|
|
29
|
+
dataName?: string;
|
|
30
|
+
};
|
|
31
|
+
};
|