fexios-browser 2.0.0 → 2.1.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/README.md +49 -9
- package/package.json +1 -1
- package/dist/InterceptorManager.cjs +0 -56
- package/dist/InterceptorManager.d.cts +0 -14
- package/dist/InterceptorManager.d.ts +0 -14
- package/dist/InterceptorManager.js +0 -6
- package/dist/chunk-HIR6OZ3W.js +0 -36
- package/dist/chunk-ZTE6UN5I.js +0 -31
- package/dist/dispatchRequest.cjs +0 -51
- package/dist/dispatchRequest.d.cts +0 -6
- package/dist/dispatchRequest.d.ts +0 -6
- package/dist/dispatchRequest.js +0 -6
- package/dist/index.cjs +0 -143
- package/dist/index.d.cts +0 -84
- package/dist/index.d.ts +0 -84
- package/dist/index.js +0 -66
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/package.json
CHANGED
@@ -1,56 +0,0 @@
|
|
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/InterceptorManager.ts
|
21
|
-
var InterceptorManager_exports = {};
|
22
|
-
__export(InterceptorManager_exports, {
|
23
|
-
default: () => InterceptorManager_default
|
24
|
-
});
|
25
|
-
module.exports = __toCommonJS(InterceptorManager_exports);
|
26
|
-
var InterceptorManager = class {
|
27
|
-
handlers;
|
28
|
-
constructor() {
|
29
|
-
this.handlers = [];
|
30
|
-
}
|
31
|
-
use(fulfilled, rejected) {
|
32
|
-
this.handlers.push({
|
33
|
-
onFulfilled: fulfilled,
|
34
|
-
onRejected: rejected
|
35
|
-
});
|
36
|
-
return this.handlers.length - 1;
|
37
|
-
}
|
38
|
-
eject(id) {
|
39
|
-
if (this.handlers[id]) {
|
40
|
-
this.handlers[id] = void 0;
|
41
|
-
}
|
42
|
-
}
|
43
|
-
clear() {
|
44
|
-
if (this.handlers) {
|
45
|
-
this.handlers = [];
|
46
|
-
}
|
47
|
-
}
|
48
|
-
forEach(fn) {
|
49
|
-
this.handlers.forEach((handler) => {
|
50
|
-
if (handler) {
|
51
|
-
fn(handler);
|
52
|
-
}
|
53
|
-
});
|
54
|
-
}
|
55
|
-
};
|
56
|
-
var InterceptorManager_default = InterceptorManager;
|
@@ -1,14 +0,0 @@
|
|
1
|
-
interface Handler<T> {
|
2
|
-
onFulfilled?: ((value: T) => T | Promise<T>);
|
3
|
-
onRejected?: ((error: any) => any);
|
4
|
-
}
|
5
|
-
declare class InterceptorManager<T> {
|
6
|
-
handlers: (Handler<T> | undefined)[];
|
7
|
-
constructor();
|
8
|
-
use(fulfilled?: ((value: T) => T | Promise<T>), rejected?: ((error: any) => any)): number;
|
9
|
-
eject(id: number): void;
|
10
|
-
clear(): void;
|
11
|
-
forEach(fn: (value: Handler<T>) => any): void;
|
12
|
-
}
|
13
|
-
|
14
|
-
export { type Handler, InterceptorManager as default };
|
@@ -1,14 +0,0 @@
|
|
1
|
-
interface Handler<T> {
|
2
|
-
onFulfilled?: ((value: T) => T | Promise<T>);
|
3
|
-
onRejected?: ((error: any) => any);
|
4
|
-
}
|
5
|
-
declare class InterceptorManager<T> {
|
6
|
-
handlers: (Handler<T> | undefined)[];
|
7
|
-
constructor();
|
8
|
-
use(fulfilled?: ((value: T) => T | Promise<T>), rejected?: ((error: any) => any)): number;
|
9
|
-
eject(id: number): void;
|
10
|
-
clear(): void;
|
11
|
-
forEach(fn: (value: Handler<T>) => any): void;
|
12
|
-
}
|
13
|
-
|
14
|
-
export { type Handler, InterceptorManager as default };
|
package/dist/chunk-HIR6OZ3W.js
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
// src/InterceptorManager.ts
|
2
|
-
var InterceptorManager = class {
|
3
|
-
handlers;
|
4
|
-
constructor() {
|
5
|
-
this.handlers = [];
|
6
|
-
}
|
7
|
-
use(fulfilled, rejected) {
|
8
|
-
this.handlers.push({
|
9
|
-
onFulfilled: fulfilled,
|
10
|
-
onRejected: rejected
|
11
|
-
});
|
12
|
-
return this.handlers.length - 1;
|
13
|
-
}
|
14
|
-
eject(id) {
|
15
|
-
if (this.handlers[id]) {
|
16
|
-
this.handlers[id] = void 0;
|
17
|
-
}
|
18
|
-
}
|
19
|
-
clear() {
|
20
|
-
if (this.handlers) {
|
21
|
-
this.handlers = [];
|
22
|
-
}
|
23
|
-
}
|
24
|
-
forEach(fn) {
|
25
|
-
this.handlers.forEach((handler) => {
|
26
|
-
if (handler) {
|
27
|
-
fn(handler);
|
28
|
-
}
|
29
|
-
});
|
30
|
-
}
|
31
|
-
};
|
32
|
-
var InterceptorManager_default = InterceptorManager;
|
33
|
-
|
34
|
-
export {
|
35
|
-
InterceptorManager_default
|
36
|
-
};
|
package/dist/chunk-ZTE6UN5I.js
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
// src/dispatchRequest.ts
|
2
|
-
async function dispatchRequest(config) {
|
3
|
-
const rowResponse = await fetch(new URL(config.url, config.baseURL), {
|
4
|
-
method: config.method,
|
5
|
-
headers: config.headers,
|
6
|
-
body: config.data ? JSON.stringify(config.data) : void 0
|
7
|
-
});
|
8
|
-
const contentType = rowResponse.headers.get("Content-Type") || "";
|
9
|
-
let responseData;
|
10
|
-
if (contentType.includes("application/json")) {
|
11
|
-
responseData = await rowResponse.json();
|
12
|
-
} else {
|
13
|
-
responseData = rowResponse;
|
14
|
-
}
|
15
|
-
const headers = {};
|
16
|
-
rowResponse.headers.forEach((value, key) => {
|
17
|
-
headers[key] = value;
|
18
|
-
});
|
19
|
-
const response = {
|
20
|
-
status: rowResponse.status,
|
21
|
-
statusText: rowResponse.statusText,
|
22
|
-
data: responseData,
|
23
|
-
headers,
|
24
|
-
config
|
25
|
-
};
|
26
|
-
return response;
|
27
|
-
}
|
28
|
-
|
29
|
-
export {
|
30
|
-
dispatchRequest
|
31
|
-
};
|
package/dist/dispatchRequest.cjs
DELETED
@@ -1,51 +0,0 @@
|
|
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/dispatchRequest.ts
|
21
|
-
var dispatchRequest_exports = {};
|
22
|
-
__export(dispatchRequest_exports, {
|
23
|
-
default: () => dispatchRequest
|
24
|
-
});
|
25
|
-
module.exports = __toCommonJS(dispatchRequest_exports);
|
26
|
-
async function dispatchRequest(config) {
|
27
|
-
const rowResponse = await fetch(new URL(config.url, config.baseURL), {
|
28
|
-
method: config.method,
|
29
|
-
headers: config.headers,
|
30
|
-
body: config.data ? JSON.stringify(config.data) : void 0
|
31
|
-
});
|
32
|
-
const contentType = rowResponse.headers.get("Content-Type") || "";
|
33
|
-
let responseData;
|
34
|
-
if (contentType.includes("application/json")) {
|
35
|
-
responseData = await rowResponse.json();
|
36
|
-
} else {
|
37
|
-
responseData = rowResponse;
|
38
|
-
}
|
39
|
-
const headers = {};
|
40
|
-
rowResponse.headers.forEach((value, key) => {
|
41
|
-
headers[key] = value;
|
42
|
-
});
|
43
|
-
const response = {
|
44
|
-
status: rowResponse.status,
|
45
|
-
statusText: rowResponse.statusText,
|
46
|
-
data: responseData,
|
47
|
-
headers,
|
48
|
-
config
|
49
|
-
};
|
50
|
-
return response;
|
51
|
-
}
|
package/dist/dispatchRequest.js
DELETED
package/dist/index.cjs
DELETED
@@ -1,143 +0,0 @@
|
|
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/index.ts
|
21
|
-
var index_exports = {};
|
22
|
-
__export(index_exports, {
|
23
|
-
default: () => index_default
|
24
|
-
});
|
25
|
-
module.exports = __toCommonJS(index_exports);
|
26
|
-
|
27
|
-
// src/dispatchRequest.ts
|
28
|
-
async function dispatchRequest(config) {
|
29
|
-
const rowResponse = await fetch(new URL(config.url, config.baseURL), {
|
30
|
-
method: config.method,
|
31
|
-
headers: config.headers,
|
32
|
-
body: config.data ? JSON.stringify(config.data) : void 0
|
33
|
-
});
|
34
|
-
const contentType = rowResponse.headers.get("Content-Type") || "";
|
35
|
-
let responseData;
|
36
|
-
if (contentType.includes("application/json")) {
|
37
|
-
responseData = await rowResponse.json();
|
38
|
-
} else {
|
39
|
-
responseData = rowResponse;
|
40
|
-
}
|
41
|
-
const headers = {};
|
42
|
-
rowResponse.headers.forEach((value, key) => {
|
43
|
-
headers[key] = value;
|
44
|
-
});
|
45
|
-
const response = {
|
46
|
-
status: rowResponse.status,
|
47
|
-
statusText: rowResponse.statusText,
|
48
|
-
data: responseData,
|
49
|
-
headers,
|
50
|
-
config
|
51
|
-
};
|
52
|
-
return response;
|
53
|
-
}
|
54
|
-
|
55
|
-
// src/InterceptorManager.ts
|
56
|
-
var InterceptorManager = class {
|
57
|
-
handlers;
|
58
|
-
constructor() {
|
59
|
-
this.handlers = [];
|
60
|
-
}
|
61
|
-
use(fulfilled, rejected) {
|
62
|
-
this.handlers.push({
|
63
|
-
onFulfilled: fulfilled,
|
64
|
-
onRejected: rejected
|
65
|
-
});
|
66
|
-
return this.handlers.length - 1;
|
67
|
-
}
|
68
|
-
eject(id) {
|
69
|
-
if (this.handlers[id]) {
|
70
|
-
this.handlers[id] = void 0;
|
71
|
-
}
|
72
|
-
}
|
73
|
-
clear() {
|
74
|
-
if (this.handlers) {
|
75
|
-
this.handlers = [];
|
76
|
-
}
|
77
|
-
}
|
78
|
-
forEach(fn) {
|
79
|
-
this.handlers.forEach((handler) => {
|
80
|
-
if (handler) {
|
81
|
-
fn(handler);
|
82
|
-
}
|
83
|
-
});
|
84
|
-
}
|
85
|
-
};
|
86
|
-
var InterceptorManager_default = InterceptorManager;
|
87
|
-
|
88
|
-
// src/index.ts
|
89
|
-
var Fexios = class {
|
90
|
-
get;
|
91
|
-
post;
|
92
|
-
delete;
|
93
|
-
put;
|
94
|
-
head;
|
95
|
-
options;
|
96
|
-
patch;
|
97
|
-
purge;
|
98
|
-
link;
|
99
|
-
unlink;
|
100
|
-
defaults;
|
101
|
-
interceptors;
|
102
|
-
constructor(initConfig) {
|
103
|
-
this.defaults = initConfig;
|
104
|
-
this.interceptors = {
|
105
|
-
request: new InterceptorManager_default(),
|
106
|
-
response: new InterceptorManager_default()
|
107
|
-
};
|
108
|
-
const methods = ["get", "post", "delete", "put", "head", "options", "patch", "purge", "link", "unlink"];
|
109
|
-
methods.forEach((method) => {
|
110
|
-
this[method] = async (url, request) => {
|
111
|
-
const requestConfig = {
|
112
|
-
...request,
|
113
|
-
url,
|
114
|
-
method: method.toUpperCase()
|
115
|
-
};
|
116
|
-
return this.request(requestConfig);
|
117
|
-
};
|
118
|
-
});
|
119
|
-
}
|
120
|
-
async request(config) {
|
121
|
-
const mergedRequest = {
|
122
|
-
...config,
|
123
|
-
headers: { ...this.defaults.headers, ...config.headers },
|
124
|
-
baseURL: config.baseURL || this.defaults.baseURL
|
125
|
-
};
|
126
|
-
let chain = [dispatchRequest, void 0];
|
127
|
-
this.interceptors.request.forEach((interceptor) => {
|
128
|
-
interceptor.onFulfilled;
|
129
|
-
chain.unshift(interceptor.onFulfilled, interceptor.onRejected);
|
130
|
-
});
|
131
|
-
this.interceptors.request.forEach((interceptor) => {
|
132
|
-
chain.push(interceptor.onFulfilled, interceptor.onRejected);
|
133
|
-
});
|
134
|
-
let i = 0;
|
135
|
-
let len = chain.length;
|
136
|
-
let promise = Promise.resolve(mergedRequest);
|
137
|
-
while (i < len) {
|
138
|
-
promise = promise.then(chain[i++], chain[i++]);
|
139
|
-
}
|
140
|
-
return promise;
|
141
|
-
}
|
142
|
-
};
|
143
|
-
var index_default = Fexios;
|
package/dist/index.d.cts
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
import InterceptorManager from './InterceptorManager.cjs';
|
2
|
-
|
3
|
-
/**
|
4
|
-
* 请求方法类型
|
5
|
-
*/
|
6
|
-
type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'head' | 'HEAD' | 'options' | 'OPTIONS' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH' | 'purge' | 'PURGE' | 'link' | 'LINK' | 'unlink' | 'UNLINK';
|
7
|
-
/**
|
8
|
-
* 请求配置类型
|
9
|
-
*/
|
10
|
-
interface FRequestConfig<T> {
|
11
|
-
/**
|
12
|
-
* 基于baseURL的请求地址
|
13
|
-
*/
|
14
|
-
url: string | URL;
|
15
|
-
/**
|
16
|
-
* 请求头
|
17
|
-
*/
|
18
|
-
headers?: Record<string, string>;
|
19
|
-
/**
|
20
|
-
* 请求方法
|
21
|
-
*/
|
22
|
-
method: Method;
|
23
|
-
/**
|
24
|
-
* baseURL
|
25
|
-
*/
|
26
|
-
baseURL?: string | URL;
|
27
|
-
/**
|
28
|
-
* 传输数据
|
29
|
-
*/
|
30
|
-
data?: T;
|
31
|
-
}
|
32
|
-
/**
|
33
|
-
* 响应类型
|
34
|
-
*/
|
35
|
-
interface FResponse<T> {
|
36
|
-
/**
|
37
|
-
* HTTP响应码
|
38
|
-
*/
|
39
|
-
status: number;
|
40
|
-
/**
|
41
|
-
* HTTP响应信息
|
42
|
-
*/
|
43
|
-
statusText: string;
|
44
|
-
/**
|
45
|
-
* 响应数据
|
46
|
-
*/
|
47
|
-
data: any;
|
48
|
-
/**
|
49
|
-
* 响应头
|
50
|
-
*/
|
51
|
-
headers: Record<string, string>;
|
52
|
-
/**
|
53
|
-
* 请求的配置
|
54
|
-
*/
|
55
|
-
config: FRequestConfig<T>;
|
56
|
-
}
|
57
|
-
/**
|
58
|
-
* 初始化配置类型
|
59
|
-
*/
|
60
|
-
interface InitConfig {
|
61
|
-
baseURL: string | URL;
|
62
|
-
headers: Record<string, string>;
|
63
|
-
}
|
64
|
-
declare class Fexios<T = any> {
|
65
|
-
get: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
66
|
-
post: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
67
|
-
delete: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
68
|
-
put: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
69
|
-
head: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
70
|
-
options: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
71
|
-
patch: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
72
|
-
purge: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
73
|
-
link: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
74
|
-
unlink: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
75
|
-
defaults: InitConfig;
|
76
|
-
interceptors: {
|
77
|
-
request: InterceptorManager<FRequestConfig<T>>;
|
78
|
-
response: InterceptorManager<FResponse<T>>;
|
79
|
-
};
|
80
|
-
constructor(initConfig: InitConfig);
|
81
|
-
request(config: FRequestConfig<T>): Promise<FResponse<T>>;
|
82
|
-
}
|
83
|
-
|
84
|
-
export { type FRequestConfig, type FResponse, type InitConfig, type Method, Fexios as default };
|
package/dist/index.d.ts
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
import InterceptorManager from './InterceptorManager.js';
|
2
|
-
|
3
|
-
/**
|
4
|
-
* 请求方法类型
|
5
|
-
*/
|
6
|
-
type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'head' | 'HEAD' | 'options' | 'OPTIONS' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH' | 'purge' | 'PURGE' | 'link' | 'LINK' | 'unlink' | 'UNLINK';
|
7
|
-
/**
|
8
|
-
* 请求配置类型
|
9
|
-
*/
|
10
|
-
interface FRequestConfig<T> {
|
11
|
-
/**
|
12
|
-
* 基于baseURL的请求地址
|
13
|
-
*/
|
14
|
-
url: string | URL;
|
15
|
-
/**
|
16
|
-
* 请求头
|
17
|
-
*/
|
18
|
-
headers?: Record<string, string>;
|
19
|
-
/**
|
20
|
-
* 请求方法
|
21
|
-
*/
|
22
|
-
method: Method;
|
23
|
-
/**
|
24
|
-
* baseURL
|
25
|
-
*/
|
26
|
-
baseURL?: string | URL;
|
27
|
-
/**
|
28
|
-
* 传输数据
|
29
|
-
*/
|
30
|
-
data?: T;
|
31
|
-
}
|
32
|
-
/**
|
33
|
-
* 响应类型
|
34
|
-
*/
|
35
|
-
interface FResponse<T> {
|
36
|
-
/**
|
37
|
-
* HTTP响应码
|
38
|
-
*/
|
39
|
-
status: number;
|
40
|
-
/**
|
41
|
-
* HTTP响应信息
|
42
|
-
*/
|
43
|
-
statusText: string;
|
44
|
-
/**
|
45
|
-
* 响应数据
|
46
|
-
*/
|
47
|
-
data: any;
|
48
|
-
/**
|
49
|
-
* 响应头
|
50
|
-
*/
|
51
|
-
headers: Record<string, string>;
|
52
|
-
/**
|
53
|
-
* 请求的配置
|
54
|
-
*/
|
55
|
-
config: FRequestConfig<T>;
|
56
|
-
}
|
57
|
-
/**
|
58
|
-
* 初始化配置类型
|
59
|
-
*/
|
60
|
-
interface InitConfig {
|
61
|
-
baseURL: string | URL;
|
62
|
-
headers: Record<string, string>;
|
63
|
-
}
|
64
|
-
declare class Fexios<T = any> {
|
65
|
-
get: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
66
|
-
post: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
67
|
-
delete: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
68
|
-
put: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
69
|
-
head: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
70
|
-
options: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
71
|
-
patch: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
72
|
-
purge: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
73
|
-
link: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
74
|
-
unlink: (url: string | URL, request?: Omit<FRequestConfig<T>, "url" | "method">) => Promise<FResponse<T>>;
|
75
|
-
defaults: InitConfig;
|
76
|
-
interceptors: {
|
77
|
-
request: InterceptorManager<FRequestConfig<T>>;
|
78
|
-
response: InterceptorManager<FResponse<T>>;
|
79
|
-
};
|
80
|
-
constructor(initConfig: InitConfig);
|
81
|
-
request(config: FRequestConfig<T>): Promise<FResponse<T>>;
|
82
|
-
}
|
83
|
-
|
84
|
-
export { type FRequestConfig, type FResponse, type InitConfig, type Method, Fexios as default };
|
package/dist/index.js
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
dispatchRequest
|
3
|
-
} from "./chunk-ZTE6UN5I.js";
|
4
|
-
import {
|
5
|
-
InterceptorManager_default
|
6
|
-
} from "./chunk-HIR6OZ3W.js";
|
7
|
-
|
8
|
-
// src/index.ts
|
9
|
-
var Fexios = class {
|
10
|
-
get;
|
11
|
-
post;
|
12
|
-
delete;
|
13
|
-
put;
|
14
|
-
head;
|
15
|
-
options;
|
16
|
-
patch;
|
17
|
-
purge;
|
18
|
-
link;
|
19
|
-
unlink;
|
20
|
-
defaults;
|
21
|
-
interceptors;
|
22
|
-
constructor(initConfig) {
|
23
|
-
this.defaults = initConfig;
|
24
|
-
this.interceptors = {
|
25
|
-
request: new InterceptorManager_default(),
|
26
|
-
response: new InterceptorManager_default()
|
27
|
-
};
|
28
|
-
const methods = ["get", "post", "delete", "put", "head", "options", "patch", "purge", "link", "unlink"];
|
29
|
-
methods.forEach((method) => {
|
30
|
-
this[method] = async (url, request) => {
|
31
|
-
const requestConfig = {
|
32
|
-
...request,
|
33
|
-
url,
|
34
|
-
method: method.toUpperCase()
|
35
|
-
};
|
36
|
-
return this.request(requestConfig);
|
37
|
-
};
|
38
|
-
});
|
39
|
-
}
|
40
|
-
async request(config) {
|
41
|
-
const mergedRequest = {
|
42
|
-
...config,
|
43
|
-
headers: { ...this.defaults.headers, ...config.headers },
|
44
|
-
baseURL: config.baseURL || this.defaults.baseURL
|
45
|
-
};
|
46
|
-
let chain = [dispatchRequest, void 0];
|
47
|
-
this.interceptors.request.forEach((interceptor) => {
|
48
|
-
interceptor.onFulfilled;
|
49
|
-
chain.unshift(interceptor.onFulfilled, interceptor.onRejected);
|
50
|
-
});
|
51
|
-
this.interceptors.request.forEach((interceptor) => {
|
52
|
-
chain.push(interceptor.onFulfilled, interceptor.onRejected);
|
53
|
-
});
|
54
|
-
let i = 0;
|
55
|
-
let len = chain.length;
|
56
|
-
let promise = Promise.resolve(mergedRequest);
|
57
|
-
while (i < len) {
|
58
|
-
promise = promise.then(chain[i++], chain[i++]);
|
59
|
-
}
|
60
|
-
return promise;
|
61
|
-
}
|
62
|
-
};
|
63
|
-
var index_default = Fexios;
|
64
|
-
export {
|
65
|
-
index_default as default
|
66
|
-
};
|