fexios-browser 2.0.0 → 2.1.1
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/README.md +49 -9
- package/dist/{chunk-ZTE6UN5I.js → chunk-NNXGOWMX.js} +1 -0
- package/dist/dispatchRequest.cjs +1 -0
- package/dist/dispatchRequest.d.cts +1 -1
- package/dist/dispatchRequest.d.ts +1 -1
- package/dist/dispatchRequest.js +1 -1
- package/dist/index.cjs +25 -9
- package/dist/index.d.cts +44 -26
- package/dist/index.d.ts +44 -26
- package/dist/index.js +25 -10
- package/package.json +1 -1
package/README.md
CHANGED
@@ -20,23 +20,63 @@ npm i fexios-browser
|
|
20
20
|
pnpm add fexios-browser
|
21
21
|
```
|
22
22
|
|
23
|
-
##
|
23
|
+
## 基本使用
|
24
24
|
|
25
25
|
```ts
|
26
26
|
type UserRequest = {
|
27
|
-
|
28
|
-
|
29
|
-
};
|
27
|
+
account: string,
|
28
|
+
password: string,
|
29
|
+
};
|
30
30
|
|
31
|
-
|
31
|
+
type UserResponse = UserRequest & {
|
32
|
+
token: string,
|
33
|
+
};
|
34
|
+
// 请求类型和响应类型
|
35
|
+
const api = new Fexios<UserRequest, UserResponse>({
|
32
36
|
baseURL: "http://xxx",
|
33
37
|
headers: { "Content-Type": "application/json" }
|
34
38
|
});
|
35
39
|
|
36
40
|
const response = await api.post('/api/v1/login', {
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
+
account: "18259008878",
|
42
|
+
password: "123456"
|
43
|
+
});
|
44
|
+
|
45
|
+
if (response.type === 'json') { // 如果能够自动json处理
|
46
|
+
console.log(response.data satisfies UserResponse);
|
47
|
+
} else { // 否则返回Response
|
48
|
+
console.log(response.data satisfies Response);
|
49
|
+
}
|
50
|
+
```
|
51
|
+
|
52
|
+
## 拦截器
|
53
|
+
|
54
|
+
```ts
|
55
|
+
// 请求拦截器
|
56
|
+
api.interceptors.request.use((config) => {
|
57
|
+
console.log({
|
58
|
+
message: 'Request in interceptor',
|
59
|
+
data: config,
|
60
|
+
});
|
61
|
+
return config;
|
62
|
+
}, (error) => {
|
63
|
+
console.error({
|
64
|
+
message: 'An error occurred during the request',
|
65
|
+
reason: error,
|
66
|
+
});
|
67
|
+
return Promise.reject(error);
|
68
|
+
});
|
69
|
+
// 响应拦截器
|
70
|
+
api.interceptors.response.use((response) => {
|
71
|
+
console.log({
|
72
|
+
message: 'Response in interceptor',
|
73
|
+
data: response,
|
74
|
+
});
|
75
|
+
return response;
|
76
|
+
}, (error) => {
|
77
|
+
console.error({
|
78
|
+
message: 'An error occurred during the response',
|
79
|
+
reason: error,
|
80
|
+
});
|
41
81
|
});
|
42
82
|
```
|
package/dist/dispatchRequest.cjs
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { FRequestConfig, FResponse } from './index.cjs';
|
2
2
|
import './InterceptorManager.cjs';
|
3
3
|
|
4
|
-
declare function dispatchRequest<T>(config: FRequestConfig<T>): Promise<FResponse<T>>;
|
4
|
+
declare function dispatchRequest<R, T>(config: FRequestConfig<T>): Promise<FResponse<R, T>>;
|
5
5
|
|
6
6
|
export { dispatchRequest as default };
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { FRequestConfig, FResponse } from './index.js';
|
2
2
|
import './InterceptorManager.js';
|
3
3
|
|
4
|
-
declare function dispatchRequest<T>(config: FRequestConfig<T>): Promise<FResponse<T>>;
|
4
|
+
declare function dispatchRequest<R, T>(config: FRequestConfig<T>): Promise<FResponse<R, T>>;
|
5
5
|
|
6
6
|
export { dispatchRequest as default };
|
package/dist/dispatchRequest.js
CHANGED
package/dist/index.cjs
CHANGED
@@ -45,6 +45,7 @@ async function dispatchRequest(config) {
|
|
45
45
|
const response = {
|
46
46
|
status: rowResponse.status,
|
47
47
|
statusText: rowResponse.statusText,
|
48
|
+
type: contentType.includes("application/json") ? "json" : "other",
|
48
49
|
data: responseData,
|
49
50
|
headers,
|
50
51
|
config
|
@@ -87,16 +88,15 @@ var InterceptorManager_default = InterceptorManager;
|
|
87
88
|
|
88
89
|
// src/index.ts
|
89
90
|
var Fexios = class {
|
91
|
+
// 没有请求数据
|
90
92
|
get;
|
91
|
-
post;
|
92
93
|
delete;
|
93
|
-
put;
|
94
94
|
head;
|
95
95
|
options;
|
96
|
+
// 有请求数据
|
97
|
+
post;
|
98
|
+
put;
|
96
99
|
patch;
|
97
|
-
purge;
|
98
|
-
link;
|
99
|
-
unlink;
|
100
100
|
defaults;
|
101
101
|
interceptors;
|
102
102
|
constructor(initConfig) {
|
@@ -105,8 +105,20 @@ var Fexios = class {
|
|
105
105
|
request: new InterceptorManager_default(),
|
106
106
|
response: new InterceptorManager_default()
|
107
107
|
};
|
108
|
-
const
|
109
|
-
|
108
|
+
const withDataMethods = ["post", "put", "patch"];
|
109
|
+
withDataMethods.forEach((method) => {
|
110
|
+
this[method] = async (url, data, request) => {
|
111
|
+
const requestConfig = {
|
112
|
+
...request,
|
113
|
+
url,
|
114
|
+
method: method.toUpperCase(),
|
115
|
+
data
|
116
|
+
};
|
117
|
+
return this.request(requestConfig);
|
118
|
+
};
|
119
|
+
});
|
120
|
+
const withoutDataMethods = ["get", "delete", "head", "options"];
|
121
|
+
withoutDataMethods.forEach((method) => {
|
110
122
|
this[method] = async (url, request) => {
|
111
123
|
const requestConfig = {
|
112
124
|
...request,
|
@@ -118,10 +130,14 @@ var Fexios = class {
|
|
118
130
|
});
|
119
131
|
}
|
120
132
|
async request(config) {
|
133
|
+
config.headers = config.headers || {};
|
121
134
|
const mergedRequest = {
|
135
|
+
...this.defaults,
|
122
136
|
...config,
|
123
|
-
headers: {
|
124
|
-
|
137
|
+
headers: {
|
138
|
+
...this.defaults.headers,
|
139
|
+
...config.headers
|
140
|
+
}
|
125
141
|
};
|
126
142
|
let chain = [dispatchRequest, void 0];
|
127
143
|
this.interceptors.request.forEach((interceptor) => {
|
package/dist/index.d.cts
CHANGED
@@ -3,11 +3,11 @@ import InterceptorManager from './InterceptorManager.cjs';
|
|
3
3
|
/**
|
4
4
|
* 请求方法类型
|
5
5
|
*/
|
6
|
-
type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'head' | 'HEAD' | 'options' | 'OPTIONS' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH'
|
6
|
+
type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'head' | 'HEAD' | 'options' | 'OPTIONS' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH';
|
7
7
|
/**
|
8
8
|
* 请求配置类型
|
9
9
|
*/
|
10
|
-
|
10
|
+
type FRequestConfig<T> = {
|
11
11
|
/**
|
12
12
|
* 基于baseURL的请求地址
|
13
13
|
*/
|
@@ -15,7 +15,7 @@ interface FRequestConfig<T> {
|
|
15
15
|
/**
|
16
16
|
* 请求头
|
17
17
|
*/
|
18
|
-
headers?: Record<string, string
|
18
|
+
headers?: Record<string, string> | Headers;
|
19
19
|
/**
|
20
20
|
* 请求方法
|
21
21
|
*/
|
@@ -28,11 +28,19 @@ interface FRequestConfig<T> {
|
|
28
28
|
* 传输数据
|
29
29
|
*/
|
30
30
|
data?: T;
|
31
|
-
|
31
|
+
/**
|
32
|
+
* 跨域是否携带cookie
|
33
|
+
*/
|
34
|
+
withCredentials?: boolean;
|
35
|
+
/**
|
36
|
+
* 超时时间
|
37
|
+
*/
|
38
|
+
timeout?: number;
|
39
|
+
};
|
32
40
|
/**
|
33
41
|
* 响应类型
|
34
42
|
*/
|
35
|
-
|
43
|
+
type FResponse<R, T> = {
|
36
44
|
/**
|
37
45
|
* HTTP响应码
|
38
46
|
*/
|
@@ -41,10 +49,6 @@ interface FResponse<T> {
|
|
41
49
|
* HTTP响应信息
|
42
50
|
*/
|
43
51
|
statusText: string;
|
44
|
-
/**
|
45
|
-
* 响应数据
|
46
|
-
*/
|
47
|
-
data: any;
|
48
52
|
/**
|
49
53
|
* 响应头
|
50
54
|
*/
|
@@ -53,32 +57,46 @@ interface FResponse<T> {
|
|
53
57
|
* 请求的配置
|
54
58
|
*/
|
55
59
|
config: FRequestConfig<T>;
|
56
|
-
}
|
60
|
+
} & ({
|
61
|
+
/**
|
62
|
+
* 响应数据
|
63
|
+
* json自动解析
|
64
|
+
*/
|
65
|
+
data: R;
|
66
|
+
type: "json";
|
67
|
+
} | {
|
68
|
+
/**
|
69
|
+
* 响应数据
|
70
|
+
* 其他类型,保留原始Response
|
71
|
+
*/
|
72
|
+
data: Response;
|
73
|
+
type: "other";
|
74
|
+
});
|
57
75
|
/**
|
58
76
|
* 初始化配置类型
|
59
77
|
*/
|
60
|
-
interface InitConfig {
|
61
|
-
baseURL: string | URL;
|
62
|
-
headers: Record<string, string>;
|
78
|
+
interface InitConfig extends Omit<FRequestConfig<any>, "url" | "method" | "data"> {
|
63
79
|
}
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
80
|
+
/**
|
81
|
+
* Fexios类
|
82
|
+
* T: 传输数据类型
|
83
|
+
* R: 响应数据类型
|
84
|
+
*/
|
85
|
+
declare class Fexios<T = any, R = any> {
|
86
|
+
get: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
87
|
+
delete: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
88
|
+
head: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
89
|
+
options: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
90
|
+
post: (url: string | URL, data?: T, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
91
|
+
put: (url: string | URL, data?: T, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
92
|
+
patch: (url: string | URL, data?: T, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
75
93
|
defaults: InitConfig;
|
76
94
|
interceptors: {
|
77
95
|
request: InterceptorManager<FRequestConfig<T>>;
|
78
|
-
response: InterceptorManager<FResponse<T>>;
|
96
|
+
response: InterceptorManager<FResponse<R, T>>;
|
79
97
|
};
|
80
98
|
constructor(initConfig: InitConfig);
|
81
|
-
request(config: FRequestConfig<T>): Promise<FResponse<T>>;
|
99
|
+
request(config: FRequestConfig<T>): Promise<FResponse<R, T>>;
|
82
100
|
}
|
83
101
|
|
84
102
|
export { type FRequestConfig, type FResponse, type InitConfig, type Method, Fexios as default };
|
package/dist/index.d.ts
CHANGED
@@ -3,11 +3,11 @@ import InterceptorManager from './InterceptorManager.js';
|
|
3
3
|
/**
|
4
4
|
* 请求方法类型
|
5
5
|
*/
|
6
|
-
type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'head' | 'HEAD' | 'options' | 'OPTIONS' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH'
|
6
|
+
type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'head' | 'HEAD' | 'options' | 'OPTIONS' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH';
|
7
7
|
/**
|
8
8
|
* 请求配置类型
|
9
9
|
*/
|
10
|
-
|
10
|
+
type FRequestConfig<T> = {
|
11
11
|
/**
|
12
12
|
* 基于baseURL的请求地址
|
13
13
|
*/
|
@@ -15,7 +15,7 @@ interface FRequestConfig<T> {
|
|
15
15
|
/**
|
16
16
|
* 请求头
|
17
17
|
*/
|
18
|
-
headers?: Record<string, string
|
18
|
+
headers?: Record<string, string> | Headers;
|
19
19
|
/**
|
20
20
|
* 请求方法
|
21
21
|
*/
|
@@ -28,11 +28,19 @@ interface FRequestConfig<T> {
|
|
28
28
|
* 传输数据
|
29
29
|
*/
|
30
30
|
data?: T;
|
31
|
-
|
31
|
+
/**
|
32
|
+
* 跨域是否携带cookie
|
33
|
+
*/
|
34
|
+
withCredentials?: boolean;
|
35
|
+
/**
|
36
|
+
* 超时时间
|
37
|
+
*/
|
38
|
+
timeout?: number;
|
39
|
+
};
|
32
40
|
/**
|
33
41
|
* 响应类型
|
34
42
|
*/
|
35
|
-
|
43
|
+
type FResponse<R, T> = {
|
36
44
|
/**
|
37
45
|
* HTTP响应码
|
38
46
|
*/
|
@@ -41,10 +49,6 @@ interface FResponse<T> {
|
|
41
49
|
* HTTP响应信息
|
42
50
|
*/
|
43
51
|
statusText: string;
|
44
|
-
/**
|
45
|
-
* 响应数据
|
46
|
-
*/
|
47
|
-
data: any;
|
48
52
|
/**
|
49
53
|
* 响应头
|
50
54
|
*/
|
@@ -53,32 +57,46 @@ interface FResponse<T> {
|
|
53
57
|
* 请求的配置
|
54
58
|
*/
|
55
59
|
config: FRequestConfig<T>;
|
56
|
-
}
|
60
|
+
} & ({
|
61
|
+
/**
|
62
|
+
* 响应数据
|
63
|
+
* json自动解析
|
64
|
+
*/
|
65
|
+
data: R;
|
66
|
+
type: "json";
|
67
|
+
} | {
|
68
|
+
/**
|
69
|
+
* 响应数据
|
70
|
+
* 其他类型,保留原始Response
|
71
|
+
*/
|
72
|
+
data: Response;
|
73
|
+
type: "other";
|
74
|
+
});
|
57
75
|
/**
|
58
76
|
* 初始化配置类型
|
59
77
|
*/
|
60
|
-
interface InitConfig {
|
61
|
-
baseURL: string | URL;
|
62
|
-
headers: Record<string, string>;
|
78
|
+
interface InitConfig extends Omit<FRequestConfig<any>, "url" | "method" | "data"> {
|
63
79
|
}
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
80
|
+
/**
|
81
|
+
* Fexios类
|
82
|
+
* T: 传输数据类型
|
83
|
+
* R: 响应数据类型
|
84
|
+
*/
|
85
|
+
declare class Fexios<T = any, R = any> {
|
86
|
+
get: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
87
|
+
delete: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
88
|
+
head: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
89
|
+
options: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
90
|
+
post: (url: string | URL, data?: T, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
91
|
+
put: (url: string | URL, data?: T, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
92
|
+
patch: (url: string | URL, data?: T, request?: Omit<FRequestConfig<T>, "url" | "method" | "data">) => Promise<FResponse<R, T>>;
|
75
93
|
defaults: InitConfig;
|
76
94
|
interceptors: {
|
77
95
|
request: InterceptorManager<FRequestConfig<T>>;
|
78
|
-
response: InterceptorManager<FResponse<T>>;
|
96
|
+
response: InterceptorManager<FResponse<R, T>>;
|
79
97
|
};
|
80
98
|
constructor(initConfig: InitConfig);
|
81
|
-
request(config: FRequestConfig<T>): Promise<FResponse<T>>;
|
99
|
+
request(config: FRequestConfig<T>): Promise<FResponse<R, T>>;
|
82
100
|
}
|
83
101
|
|
84
102
|
export { type FRequestConfig, type FResponse, type InitConfig, type Method, Fexios as default };
|
package/dist/index.js
CHANGED
@@ -1,22 +1,21 @@
|
|
1
1
|
import {
|
2
2
|
dispatchRequest
|
3
|
-
} from "./chunk-
|
3
|
+
} from "./chunk-NNXGOWMX.js";
|
4
4
|
import {
|
5
5
|
InterceptorManager_default
|
6
6
|
} from "./chunk-HIR6OZ3W.js";
|
7
7
|
|
8
8
|
// src/index.ts
|
9
9
|
var Fexios = class {
|
10
|
+
// 没有请求数据
|
10
11
|
get;
|
11
|
-
post;
|
12
12
|
delete;
|
13
|
-
put;
|
14
13
|
head;
|
15
14
|
options;
|
15
|
+
// 有请求数据
|
16
|
+
post;
|
17
|
+
put;
|
16
18
|
patch;
|
17
|
-
purge;
|
18
|
-
link;
|
19
|
-
unlink;
|
20
19
|
defaults;
|
21
20
|
interceptors;
|
22
21
|
constructor(initConfig) {
|
@@ -25,8 +24,20 @@ var Fexios = class {
|
|
25
24
|
request: new InterceptorManager_default(),
|
26
25
|
response: new InterceptorManager_default()
|
27
26
|
};
|
28
|
-
const
|
29
|
-
|
27
|
+
const withDataMethods = ["post", "put", "patch"];
|
28
|
+
withDataMethods.forEach((method) => {
|
29
|
+
this[method] = async (url, data, request) => {
|
30
|
+
const requestConfig = {
|
31
|
+
...request,
|
32
|
+
url,
|
33
|
+
method: method.toUpperCase(),
|
34
|
+
data
|
35
|
+
};
|
36
|
+
return this.request(requestConfig);
|
37
|
+
};
|
38
|
+
});
|
39
|
+
const withoutDataMethods = ["get", "delete", "head", "options"];
|
40
|
+
withoutDataMethods.forEach((method) => {
|
30
41
|
this[method] = async (url, request) => {
|
31
42
|
const requestConfig = {
|
32
43
|
...request,
|
@@ -38,10 +49,14 @@ var Fexios = class {
|
|
38
49
|
});
|
39
50
|
}
|
40
51
|
async request(config) {
|
52
|
+
config.headers = config.headers || {};
|
41
53
|
const mergedRequest = {
|
54
|
+
...this.defaults,
|
42
55
|
...config,
|
43
|
-
headers: {
|
44
|
-
|
56
|
+
headers: {
|
57
|
+
...this.defaults.headers,
|
58
|
+
...config.headers
|
59
|
+
}
|
45
60
|
};
|
46
61
|
let chain = [dispatchRequest, void 0];
|
47
62
|
this.interceptors.request.forEach((interceptor) => {
|