fexios-browser 1.1.1 → 2.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/README.md +41 -2
- package/dist/InterceptorManager.cjs +4 -4
- package/dist/InterceptorManager.d.cts +9 -5
- package/dist/InterceptorManager.d.ts +9 -5
- package/dist/InterceptorManager.js +1 -1
- package/dist/{chunk-UDNTQS3I.js → chunk-HIR6OZ3W.js} +4 -4
- package/dist/chunk-ZTE6UN5I.js +31 -0
- package/dist/dispatchRequest.cjs +51 -0
- package/dist/dispatchRequest.d.cts +6 -0
- package/dist/dispatchRequest.d.ts +6 -0
- package/dist/dispatchRequest.js +6 -0
- package/dist/index.cjs +72 -82
- package/dist/index.d.cts +63 -14
- package/dist/index.d.ts +63 -14
- package/dist/index.js +44 -79
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1,3 +1,42 @@
|
|
1
|
-
# Fexios
|
1
|
+
# Fexios-Browser
|
2
2
|
|
3
|
-
|
3
|
+
## 简介
|
4
|
+
|
5
|
+
对fetch的封装
|
6
|
+
拥有以下功能
|
7
|
+
|
8
|
+
- 自动`await json`
|
9
|
+
- 有拦截器
|
10
|
+
- 服务器响应放在data里面
|
11
|
+
- 请求配置拥有泛型
|
12
|
+
|
13
|
+
## 安装
|
14
|
+
|
15
|
+
```bash
|
16
|
+
npm i fexios-browser
|
17
|
+
```
|
18
|
+
|
19
|
+
```bash
|
20
|
+
pnpm add fexios-browser
|
21
|
+
```
|
22
|
+
|
23
|
+
## 使用
|
24
|
+
|
25
|
+
```ts
|
26
|
+
type UserRequest = {
|
27
|
+
account: string,
|
28
|
+
password: string,
|
29
|
+
};
|
30
|
+
|
31
|
+
const api = new Fexios<UserRequest>({
|
32
|
+
baseURL: "http://xxx",
|
33
|
+
headers: { "Content-Type": "application/json" }
|
34
|
+
});
|
35
|
+
|
36
|
+
const response = await api.post('/api/v1/login', {
|
37
|
+
data: {
|
38
|
+
account: "admin",
|
39
|
+
password: "admin"
|
40
|
+
}
|
41
|
+
});
|
42
|
+
```
|
@@ -30,14 +30,14 @@ var InterceptorManager = class {
|
|
30
30
|
}
|
31
31
|
use(fulfilled, rejected) {
|
32
32
|
this.handlers.push({
|
33
|
-
fulfilled,
|
34
|
-
rejected
|
33
|
+
onFulfilled: fulfilled,
|
34
|
+
onRejected: rejected
|
35
35
|
});
|
36
36
|
return this.handlers.length - 1;
|
37
37
|
}
|
38
38
|
eject(id) {
|
39
39
|
if (this.handlers[id]) {
|
40
|
-
this.handlers[id] =
|
40
|
+
this.handlers[id] = void 0;
|
41
41
|
}
|
42
42
|
}
|
43
43
|
clear() {
|
@@ -47,7 +47,7 @@ var InterceptorManager = class {
|
|
47
47
|
}
|
48
48
|
forEach(fn) {
|
49
49
|
this.handlers.forEach((handler) => {
|
50
|
-
if (handler
|
50
|
+
if (handler) {
|
51
51
|
fn(handler);
|
52
52
|
}
|
53
53
|
});
|
@@ -1,10 +1,14 @@
|
|
1
|
-
|
2
|
-
|
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)[];
|
3
7
|
constructor();
|
4
|
-
use(fulfilled:
|
8
|
+
use(fulfilled?: ((value: T) => T | Promise<T>), rejected?: ((error: any) => any)): number;
|
5
9
|
eject(id: number): void;
|
6
10
|
clear(): void;
|
7
|
-
forEach(fn:
|
11
|
+
forEach(fn: (value: Handler<T>) => any): void;
|
8
12
|
}
|
9
13
|
|
10
|
-
export { InterceptorManager as default };
|
14
|
+
export { type Handler, InterceptorManager as default };
|
@@ -1,10 +1,14 @@
|
|
1
|
-
|
2
|
-
|
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)[];
|
3
7
|
constructor();
|
4
|
-
use(fulfilled:
|
8
|
+
use(fulfilled?: ((value: T) => T | Promise<T>), rejected?: ((error: any) => any)): number;
|
5
9
|
eject(id: number): void;
|
6
10
|
clear(): void;
|
7
|
-
forEach(fn:
|
11
|
+
forEach(fn: (value: Handler<T>) => any): void;
|
8
12
|
}
|
9
13
|
|
10
|
-
export { InterceptorManager as default };
|
14
|
+
export { type Handler, InterceptorManager as default };
|
@@ -6,14 +6,14 @@ var InterceptorManager = class {
|
|
6
6
|
}
|
7
7
|
use(fulfilled, rejected) {
|
8
8
|
this.handlers.push({
|
9
|
-
fulfilled,
|
10
|
-
rejected
|
9
|
+
onFulfilled: fulfilled,
|
10
|
+
onRejected: rejected
|
11
11
|
});
|
12
12
|
return this.handlers.length - 1;
|
13
13
|
}
|
14
14
|
eject(id) {
|
15
15
|
if (this.handlers[id]) {
|
16
|
-
this.handlers[id] =
|
16
|
+
this.handlers[id] = void 0;
|
17
17
|
}
|
18
18
|
}
|
19
19
|
clear() {
|
@@ -23,7 +23,7 @@ var InterceptorManager = class {
|
|
23
23
|
}
|
24
24
|
forEach(fn) {
|
25
25
|
this.handlers.forEach((handler) => {
|
26
|
-
if (handler
|
26
|
+
if (handler) {
|
27
27
|
fn(handler);
|
28
28
|
}
|
29
29
|
});
|
@@ -0,0 +1,31 @@
|
|
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
|
+
};
|
@@ -0,0 +1,51 @@
|
|
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/index.cjs
CHANGED
@@ -24,6 +24,34 @@ __export(index_exports, {
|
|
24
24
|
});
|
25
25
|
module.exports = __toCommonJS(index_exports);
|
26
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
|
+
|
27
55
|
// src/InterceptorManager.ts
|
28
56
|
var InterceptorManager = class {
|
29
57
|
handlers;
|
@@ -32,14 +60,14 @@ var InterceptorManager = class {
|
|
32
60
|
}
|
33
61
|
use(fulfilled, rejected) {
|
34
62
|
this.handlers.push({
|
35
|
-
fulfilled,
|
36
|
-
rejected
|
63
|
+
onFulfilled: fulfilled,
|
64
|
+
onRejected: rejected
|
37
65
|
});
|
38
66
|
return this.handlers.length - 1;
|
39
67
|
}
|
40
68
|
eject(id) {
|
41
69
|
if (this.handlers[id]) {
|
42
|
-
this.handlers[id] =
|
70
|
+
this.handlers[id] = void 0;
|
43
71
|
}
|
44
72
|
}
|
45
73
|
clear() {
|
@@ -49,7 +77,7 @@ var InterceptorManager = class {
|
|
49
77
|
}
|
50
78
|
forEach(fn) {
|
51
79
|
this.handlers.forEach((handler) => {
|
52
|
-
if (handler
|
80
|
+
if (handler) {
|
53
81
|
fn(handler);
|
54
82
|
}
|
55
83
|
});
|
@@ -59,6 +87,16 @@ var InterceptorManager_default = InterceptorManager;
|
|
59
87
|
|
60
88
|
// src/index.ts
|
61
89
|
var Fexios = class {
|
90
|
+
get;
|
91
|
+
post;
|
92
|
+
delete;
|
93
|
+
put;
|
94
|
+
head;
|
95
|
+
options;
|
96
|
+
patch;
|
97
|
+
purge;
|
98
|
+
link;
|
99
|
+
unlink;
|
62
100
|
defaults;
|
63
101
|
interceptors;
|
64
102
|
constructor(initConfig) {
|
@@ -67,87 +105,39 @@ var Fexios = class {
|
|
67
105
|
request: new InterceptorManager_default(),
|
68
106
|
response: new InterceptorManager_default()
|
69
107
|
};
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
this.interceptors.request.forEach(({ fulfilled, rejected }) => {
|
80
|
-
chain = chain.then(fulfilled, rejected);
|
81
|
-
});
|
82
|
-
let processedRequest = await chain;
|
83
|
-
const rowResponse = await fetch(new URL(processedRequest.url, processedRequest.baseURL), {
|
84
|
-
method: processedRequest.method,
|
85
|
-
headers: processedRequest.headers,
|
86
|
-
body: processedRequest.data ? JSON.stringify(processedRequest.data) : void 0
|
87
|
-
});
|
88
|
-
const contentType = rowResponse.headers.get("Content-Type") || "";
|
89
|
-
let responseData;
|
90
|
-
if (contentType.includes("application/json")) {
|
91
|
-
responseData = await rowResponse.json();
|
92
|
-
} else {
|
93
|
-
responseData = rowResponse;
|
94
|
-
}
|
95
|
-
const headers = {};
|
96
|
-
rowResponse.headers.forEach((value, key) => {
|
97
|
-
headers[key] = value;
|
98
|
-
});
|
99
|
-
let response = {
|
100
|
-
status: rowResponse.status,
|
101
|
-
statusText: rowResponse.statusText,
|
102
|
-
data: responseData,
|
103
|
-
headers,
|
104
|
-
request
|
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);
|
105
117
|
};
|
106
|
-
|
107
|
-
if (rowResponse.ok) {
|
108
|
-
responseChain = Promise.resolve(response);
|
109
|
-
} else {
|
110
|
-
responseChain = Promise.reject(response);
|
111
|
-
}
|
112
|
-
this.interceptors.response.forEach(({ fulfilled, rejected }) => {
|
113
|
-
responseChain = responseChain.then(fulfilled, rejected);
|
114
|
-
});
|
115
|
-
return await responseChain;
|
116
|
-
} catch (error) {
|
117
|
-
throw error;
|
118
|
-
}
|
119
|
-
}
|
120
|
-
async get(url, request) {
|
121
|
-
const getRequest = {
|
122
|
-
...request,
|
123
|
-
url,
|
124
|
-
method: "GET"
|
125
|
-
};
|
126
|
-
return this.request(getRequest);
|
127
|
-
}
|
128
|
-
async post(url, request) {
|
129
|
-
const postRequest = {
|
130
|
-
...request,
|
131
|
-
url,
|
132
|
-
method: "POST"
|
133
|
-
};
|
134
|
-
return this.request(postRequest);
|
135
|
-
}
|
136
|
-
async delete(url, request) {
|
137
|
-
const deleteRequest = {
|
138
|
-
...request,
|
139
|
-
url,
|
140
|
-
method: "DELETE"
|
141
|
-
};
|
142
|
-
return this.request(deleteRequest);
|
118
|
+
});
|
143
119
|
}
|
144
|
-
async
|
145
|
-
const
|
146
|
-
...
|
147
|
-
|
148
|
-
|
120
|
+
async request(config) {
|
121
|
+
const mergedRequest = {
|
122
|
+
...config,
|
123
|
+
headers: { ...this.defaults.headers, ...config.headers },
|
124
|
+
baseURL: config.baseURL || this.defaults.baseURL
|
149
125
|
};
|
150
|
-
|
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;
|
151
141
|
}
|
152
142
|
};
|
153
143
|
var index_default = Fexios;
|
package/dist/index.d.cts
CHANGED
@@ -1,35 +1,84 @@
|
|
1
1
|
import InterceptorManager from './InterceptorManager.cjs';
|
2
2
|
|
3
|
-
|
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
|
+
*/
|
4
14
|
url: string | URL;
|
15
|
+
/**
|
16
|
+
* 请求头
|
17
|
+
*/
|
5
18
|
headers?: Record<string, string>;
|
6
|
-
|
19
|
+
/**
|
20
|
+
* 请求方法
|
21
|
+
*/
|
22
|
+
method: Method;
|
23
|
+
/**
|
24
|
+
* baseURL
|
25
|
+
*/
|
7
26
|
baseURL?: string | URL;
|
8
|
-
|
27
|
+
/**
|
28
|
+
* 传输数据
|
29
|
+
*/
|
30
|
+
data?: T;
|
9
31
|
}
|
10
|
-
|
32
|
+
/**
|
33
|
+
* 响应类型
|
34
|
+
*/
|
35
|
+
interface FResponse<T> {
|
36
|
+
/**
|
37
|
+
* HTTP响应码
|
38
|
+
*/
|
11
39
|
status: number;
|
40
|
+
/**
|
41
|
+
* HTTP响应信息
|
42
|
+
*/
|
12
43
|
statusText: string;
|
44
|
+
/**
|
45
|
+
* 响应数据
|
46
|
+
*/
|
13
47
|
data: any;
|
48
|
+
/**
|
49
|
+
* 响应头
|
50
|
+
*/
|
14
51
|
headers: Record<string, string>;
|
15
|
-
|
52
|
+
/**
|
53
|
+
* 请求的配置
|
54
|
+
*/
|
55
|
+
config: FRequestConfig<T>;
|
16
56
|
}
|
57
|
+
/**
|
58
|
+
* 初始化配置类型
|
59
|
+
*/
|
17
60
|
interface InitConfig {
|
18
61
|
baseURL: string | URL;
|
19
62
|
headers: Record<string, string>;
|
20
63
|
}
|
21
|
-
declare class Fexios {
|
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>>;
|
22
75
|
defaults: InitConfig;
|
23
76
|
interceptors: {
|
24
|
-
request: InterceptorManager
|
25
|
-
response: InterceptorManager
|
77
|
+
request: InterceptorManager<FRequestConfig<T>>;
|
78
|
+
response: InterceptorManager<FResponse<T>>;
|
26
79
|
};
|
27
80
|
constructor(initConfig: InitConfig);
|
28
|
-
request(
|
29
|
-
get(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
|
30
|
-
post(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
|
31
|
-
delete(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
|
32
|
-
put(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
|
81
|
+
request(config: FRequestConfig<T>): Promise<FResponse<T>>;
|
33
82
|
}
|
34
83
|
|
35
|
-
export { type
|
84
|
+
export { type FRequestConfig, type FResponse, type InitConfig, type Method, Fexios as default };
|
package/dist/index.d.ts
CHANGED
@@ -1,35 +1,84 @@
|
|
1
1
|
import InterceptorManager from './InterceptorManager.js';
|
2
2
|
|
3
|
-
|
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
|
+
*/
|
4
14
|
url: string | URL;
|
15
|
+
/**
|
16
|
+
* 请求头
|
17
|
+
*/
|
5
18
|
headers?: Record<string, string>;
|
6
|
-
|
19
|
+
/**
|
20
|
+
* 请求方法
|
21
|
+
*/
|
22
|
+
method: Method;
|
23
|
+
/**
|
24
|
+
* baseURL
|
25
|
+
*/
|
7
26
|
baseURL?: string | URL;
|
8
|
-
|
27
|
+
/**
|
28
|
+
* 传输数据
|
29
|
+
*/
|
30
|
+
data?: T;
|
9
31
|
}
|
10
|
-
|
32
|
+
/**
|
33
|
+
* 响应类型
|
34
|
+
*/
|
35
|
+
interface FResponse<T> {
|
36
|
+
/**
|
37
|
+
* HTTP响应码
|
38
|
+
*/
|
11
39
|
status: number;
|
40
|
+
/**
|
41
|
+
* HTTP响应信息
|
42
|
+
*/
|
12
43
|
statusText: string;
|
44
|
+
/**
|
45
|
+
* 响应数据
|
46
|
+
*/
|
13
47
|
data: any;
|
48
|
+
/**
|
49
|
+
* 响应头
|
50
|
+
*/
|
14
51
|
headers: Record<string, string>;
|
15
|
-
|
52
|
+
/**
|
53
|
+
* 请求的配置
|
54
|
+
*/
|
55
|
+
config: FRequestConfig<T>;
|
16
56
|
}
|
57
|
+
/**
|
58
|
+
* 初始化配置类型
|
59
|
+
*/
|
17
60
|
interface InitConfig {
|
18
61
|
baseURL: string | URL;
|
19
62
|
headers: Record<string, string>;
|
20
63
|
}
|
21
|
-
declare class Fexios {
|
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>>;
|
22
75
|
defaults: InitConfig;
|
23
76
|
interceptors: {
|
24
|
-
request: InterceptorManager
|
25
|
-
response: InterceptorManager
|
77
|
+
request: InterceptorManager<FRequestConfig<T>>;
|
78
|
+
response: InterceptorManager<FResponse<T>>;
|
26
79
|
};
|
27
80
|
constructor(initConfig: InitConfig);
|
28
|
-
request(
|
29
|
-
get(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
|
30
|
-
post(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
|
31
|
-
delete(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
|
32
|
-
put(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
|
81
|
+
request(config: FRequestConfig<T>): Promise<FResponse<T>>;
|
33
82
|
}
|
34
83
|
|
35
|
-
export { type
|
84
|
+
export { type FRequestConfig, type FResponse, type InitConfig, type Method, Fexios as default };
|
package/dist/index.js
CHANGED
@@ -1,9 +1,22 @@
|
|
1
|
+
import {
|
2
|
+
dispatchRequest
|
3
|
+
} from "./chunk-ZTE6UN5I.js";
|
1
4
|
import {
|
2
5
|
InterceptorManager_default
|
3
|
-
} from "./chunk-
|
6
|
+
} from "./chunk-HIR6OZ3W.js";
|
4
7
|
|
5
8
|
// src/index.ts
|
6
9
|
var Fexios = class {
|
10
|
+
get;
|
11
|
+
post;
|
12
|
+
delete;
|
13
|
+
put;
|
14
|
+
head;
|
15
|
+
options;
|
16
|
+
patch;
|
17
|
+
purge;
|
18
|
+
link;
|
19
|
+
unlink;
|
7
20
|
defaults;
|
8
21
|
interceptors;
|
9
22
|
constructor(initConfig) {
|
@@ -12,87 +25,39 @@ var Fexios = class {
|
|
12
25
|
request: new InterceptorManager_default(),
|
13
26
|
response: new InterceptorManager_default()
|
14
27
|
};
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
this.interceptors.request.forEach(({ fulfilled, rejected }) => {
|
25
|
-
chain = chain.then(fulfilled, rejected);
|
26
|
-
});
|
27
|
-
let processedRequest = await chain;
|
28
|
-
const rowResponse = await fetch(new URL(processedRequest.url, processedRequest.baseURL), {
|
29
|
-
method: processedRequest.method,
|
30
|
-
headers: processedRequest.headers,
|
31
|
-
body: processedRequest.data ? JSON.stringify(processedRequest.data) : void 0
|
32
|
-
});
|
33
|
-
const contentType = rowResponse.headers.get("Content-Type") || "";
|
34
|
-
let responseData;
|
35
|
-
if (contentType.includes("application/json")) {
|
36
|
-
responseData = await rowResponse.json();
|
37
|
-
} else {
|
38
|
-
responseData = rowResponse;
|
39
|
-
}
|
40
|
-
const headers = {};
|
41
|
-
rowResponse.headers.forEach((value, key) => {
|
42
|
-
headers[key] = value;
|
43
|
-
});
|
44
|
-
let response = {
|
45
|
-
status: rowResponse.status,
|
46
|
-
statusText: rowResponse.statusText,
|
47
|
-
data: responseData,
|
48
|
-
headers,
|
49
|
-
request
|
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);
|
50
37
|
};
|
51
|
-
|
52
|
-
if (rowResponse.ok) {
|
53
|
-
responseChain = Promise.resolve(response);
|
54
|
-
} else {
|
55
|
-
responseChain = Promise.reject(response);
|
56
|
-
}
|
57
|
-
this.interceptors.response.forEach(({ fulfilled, rejected }) => {
|
58
|
-
responseChain = responseChain.then(fulfilled, rejected);
|
59
|
-
});
|
60
|
-
return await responseChain;
|
61
|
-
} catch (error) {
|
62
|
-
throw error;
|
63
|
-
}
|
64
|
-
}
|
65
|
-
async get(url, request) {
|
66
|
-
const getRequest = {
|
67
|
-
...request,
|
68
|
-
url,
|
69
|
-
method: "GET"
|
70
|
-
};
|
71
|
-
return this.request(getRequest);
|
38
|
+
});
|
72
39
|
}
|
73
|
-
async
|
74
|
-
const
|
75
|
-
...
|
76
|
-
|
77
|
-
|
40
|
+
async request(config) {
|
41
|
+
const mergedRequest = {
|
42
|
+
...config,
|
43
|
+
headers: { ...this.defaults.headers, ...config.headers },
|
44
|
+
baseURL: config.baseURL || this.defaults.baseURL
|
78
45
|
};
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
};
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
};
|
95
|
-
return this.request(putRequest);
|
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;
|
96
61
|
}
|
97
62
|
};
|
98
63
|
var index_default = Fexios;
|