fexios-browser 2.1.0 → 2.1.2
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/dist/InterceptorManager.cjs +56 -0
- package/dist/InterceptorManager.d.cts +14 -0
- package/dist/InterceptorManager.d.ts +14 -0
- package/dist/InterceptorManager.js +6 -0
- package/dist/chunk-4RAYGLHZ.js +34 -0
- package/dist/chunk-HIR6OZ3W.js +36 -0
- package/dist/chunk-NNXGOWMX.js +32 -0
- package/dist/dispatchRequest.cjs +52 -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 +192 -0
- package/dist/index.d.cts +102 -0
- package/dist/index.d.ts +102 -0
- package/dist/index.js +86 -0
- package/dist/utils.cjs +54 -0
- package/dist/utils.d.cts +6 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.js +6 -0
- package/package.json +1 -1
@@ -0,0 +1,56 @@
|
|
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;
|
@@ -0,0 +1,14 @@
|
|
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 };
|
@@ -0,0 +1,14 @@
|
|
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 };
|
@@ -0,0 +1,34 @@
|
|
1
|
+
// src/utils.ts
|
2
|
+
function useUtil() {
|
3
|
+
function normalizeHeaders(headers = {}) {
|
4
|
+
if (headers instanceof Headers) {
|
5
|
+
const normalized = {};
|
6
|
+
headers.forEach((value, key) => {
|
7
|
+
normalized[key] = value;
|
8
|
+
});
|
9
|
+
return normalized;
|
10
|
+
}
|
11
|
+
return headers;
|
12
|
+
}
|
13
|
+
function deepMerge(target, source) {
|
14
|
+
const result = { ...target };
|
15
|
+
for (const key in source) {
|
16
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
17
|
+
if (typeof target[key] === "object" && typeof source[key] === "object" && target[key] !== null && source[key] !== null) {
|
18
|
+
result[key] = deepMerge(target[key], source[key]);
|
19
|
+
} else {
|
20
|
+
result[key] = source[key];
|
21
|
+
}
|
22
|
+
}
|
23
|
+
}
|
24
|
+
return result;
|
25
|
+
}
|
26
|
+
return {
|
27
|
+
normalizeHeaders,
|
28
|
+
deepMerge
|
29
|
+
};
|
30
|
+
}
|
31
|
+
|
32
|
+
export {
|
33
|
+
useUtil
|
34
|
+
};
|
@@ -0,0 +1,36 @@
|
|
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
|
+
};
|
@@ -0,0 +1,32 @@
|
|
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
|
+
type: contentType.includes("application/json") ? "json" : "other",
|
23
|
+
data: responseData,
|
24
|
+
headers,
|
25
|
+
config
|
26
|
+
};
|
27
|
+
return response;
|
28
|
+
}
|
29
|
+
|
30
|
+
export {
|
31
|
+
dispatchRequest
|
32
|
+
};
|
@@ -0,0 +1,52 @@
|
|
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
|
+
type: contentType.includes("application/json") ? "json" : "other",
|
47
|
+
data: responseData,
|
48
|
+
headers,
|
49
|
+
config
|
50
|
+
};
|
51
|
+
return response;
|
52
|
+
}
|
package/dist/index.cjs
ADDED
@@ -0,0 +1,192 @@
|
|
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
|
+
type: contentType.includes("application/json") ? "json" : "other",
|
49
|
+
data: responseData,
|
50
|
+
headers,
|
51
|
+
config
|
52
|
+
};
|
53
|
+
return response;
|
54
|
+
}
|
55
|
+
|
56
|
+
// src/InterceptorManager.ts
|
57
|
+
var InterceptorManager = class {
|
58
|
+
handlers;
|
59
|
+
constructor() {
|
60
|
+
this.handlers = [];
|
61
|
+
}
|
62
|
+
use(fulfilled, rejected) {
|
63
|
+
this.handlers.push({
|
64
|
+
onFulfilled: fulfilled,
|
65
|
+
onRejected: rejected
|
66
|
+
});
|
67
|
+
return this.handlers.length - 1;
|
68
|
+
}
|
69
|
+
eject(id) {
|
70
|
+
if (this.handlers[id]) {
|
71
|
+
this.handlers[id] = void 0;
|
72
|
+
}
|
73
|
+
}
|
74
|
+
clear() {
|
75
|
+
if (this.handlers) {
|
76
|
+
this.handlers = [];
|
77
|
+
}
|
78
|
+
}
|
79
|
+
forEach(fn) {
|
80
|
+
this.handlers.forEach((handler) => {
|
81
|
+
if (handler) {
|
82
|
+
fn(handler);
|
83
|
+
}
|
84
|
+
});
|
85
|
+
}
|
86
|
+
};
|
87
|
+
var InterceptorManager_default = InterceptorManager;
|
88
|
+
|
89
|
+
// src/utils.ts
|
90
|
+
function useUtil() {
|
91
|
+
function normalizeHeaders(headers = {}) {
|
92
|
+
if (headers instanceof Headers) {
|
93
|
+
const normalized = {};
|
94
|
+
headers.forEach((value, key) => {
|
95
|
+
normalized[key] = value;
|
96
|
+
});
|
97
|
+
return normalized;
|
98
|
+
}
|
99
|
+
return headers;
|
100
|
+
}
|
101
|
+
function deepMerge(target, source) {
|
102
|
+
const result = { ...target };
|
103
|
+
for (const key in source) {
|
104
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
105
|
+
if (typeof target[key] === "object" && typeof source[key] === "object" && target[key] !== null && source[key] !== null) {
|
106
|
+
result[key] = deepMerge(target[key], source[key]);
|
107
|
+
} else {
|
108
|
+
result[key] = source[key];
|
109
|
+
}
|
110
|
+
}
|
111
|
+
}
|
112
|
+
return result;
|
113
|
+
}
|
114
|
+
return {
|
115
|
+
normalizeHeaders,
|
116
|
+
deepMerge
|
117
|
+
};
|
118
|
+
}
|
119
|
+
|
120
|
+
// src/index.ts
|
121
|
+
var Fexios = class {
|
122
|
+
// 没有请求数据
|
123
|
+
get;
|
124
|
+
delete;
|
125
|
+
head;
|
126
|
+
options;
|
127
|
+
// 有请求数据
|
128
|
+
post;
|
129
|
+
put;
|
130
|
+
patch;
|
131
|
+
defaults;
|
132
|
+
interceptors;
|
133
|
+
constructor(initConfig) {
|
134
|
+
this.defaults = initConfig;
|
135
|
+
this.interceptors = {
|
136
|
+
request: new InterceptorManager_default(),
|
137
|
+
response: new InterceptorManager_default()
|
138
|
+
};
|
139
|
+
const withDataMethods = ["post", "put", "patch"];
|
140
|
+
withDataMethods.forEach((method) => {
|
141
|
+
this[method] = async (url, data, request) => {
|
142
|
+
const requestConfig = {
|
143
|
+
...request,
|
144
|
+
url,
|
145
|
+
method: method.toUpperCase(),
|
146
|
+
data
|
147
|
+
};
|
148
|
+
return this.request(requestConfig);
|
149
|
+
};
|
150
|
+
});
|
151
|
+
const withoutDataMethods = ["get", "delete", "head", "options"];
|
152
|
+
withoutDataMethods.forEach((method) => {
|
153
|
+
this[method] = async (url, request) => {
|
154
|
+
const requestConfig = {
|
155
|
+
...request,
|
156
|
+
url,
|
157
|
+
method: method.toUpperCase()
|
158
|
+
};
|
159
|
+
return this.request(requestConfig);
|
160
|
+
};
|
161
|
+
});
|
162
|
+
}
|
163
|
+
async request(config) {
|
164
|
+
const { deepMerge, normalizeHeaders } = useUtil();
|
165
|
+
config.headers = config.headers || {};
|
166
|
+
const mergedRequest = {
|
167
|
+
...this.defaults,
|
168
|
+
...config,
|
169
|
+
headers: deepMerge(
|
170
|
+
normalizeHeaders(this.defaults.headers),
|
171
|
+
normalizeHeaders(config.headers)
|
172
|
+
)
|
173
|
+
};
|
174
|
+
console.log(mergedRequest);
|
175
|
+
let chain = [dispatchRequest, void 0];
|
176
|
+
this.interceptors.request.forEach((interceptor) => {
|
177
|
+
interceptor.onFulfilled;
|
178
|
+
chain.unshift(interceptor.onFulfilled, interceptor.onRejected);
|
179
|
+
});
|
180
|
+
this.interceptors.request.forEach((interceptor) => {
|
181
|
+
chain.push(interceptor.onFulfilled, interceptor.onRejected);
|
182
|
+
});
|
183
|
+
let i = 0;
|
184
|
+
let len = chain.length;
|
185
|
+
let promise = Promise.resolve(mergedRequest);
|
186
|
+
while (i < len) {
|
187
|
+
promise = promise.then(chain[i++], chain[i++]);
|
188
|
+
}
|
189
|
+
return promise;
|
190
|
+
}
|
191
|
+
};
|
192
|
+
var index_default = Fexios;
|
package/dist/index.d.cts
ADDED
@@ -0,0 +1,102 @@
|
|
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';
|
7
|
+
/**
|
8
|
+
* 请求配置类型
|
9
|
+
*/
|
10
|
+
type FRequestConfig<T> = {
|
11
|
+
/**
|
12
|
+
* 基于baseURL的请求地址
|
13
|
+
*/
|
14
|
+
url: string | URL;
|
15
|
+
/**
|
16
|
+
* 请求头
|
17
|
+
*/
|
18
|
+
headers?: Record<string, string> | Headers;
|
19
|
+
/**
|
20
|
+
* 请求方法
|
21
|
+
*/
|
22
|
+
method: Method;
|
23
|
+
/**
|
24
|
+
* baseURL
|
25
|
+
*/
|
26
|
+
baseURL?: string | URL;
|
27
|
+
/**
|
28
|
+
* 传输数据
|
29
|
+
*/
|
30
|
+
data?: T;
|
31
|
+
/**
|
32
|
+
* 跨域是否携带cookie
|
33
|
+
*/
|
34
|
+
withCredentials?: boolean;
|
35
|
+
/**
|
36
|
+
* 超时时间
|
37
|
+
*/
|
38
|
+
timeout?: number;
|
39
|
+
};
|
40
|
+
/**
|
41
|
+
* 响应类型
|
42
|
+
*/
|
43
|
+
type FResponse<R, T> = {
|
44
|
+
/**
|
45
|
+
* HTTP响应码
|
46
|
+
*/
|
47
|
+
status: number;
|
48
|
+
/**
|
49
|
+
* HTTP响应信息
|
50
|
+
*/
|
51
|
+
statusText: string;
|
52
|
+
/**
|
53
|
+
* 响应头
|
54
|
+
*/
|
55
|
+
headers: Record<string, string>;
|
56
|
+
/**
|
57
|
+
* 请求的配置
|
58
|
+
*/
|
59
|
+
config: FRequestConfig<T>;
|
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
|
+
});
|
75
|
+
/**
|
76
|
+
* 初始化配置类型
|
77
|
+
*/
|
78
|
+
interface InitConfig extends Omit<FRequestConfig<any>, "url" | "method" | "data"> {
|
79
|
+
}
|
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>>;
|
93
|
+
defaults: InitConfig;
|
94
|
+
interceptors: {
|
95
|
+
request: InterceptorManager<FRequestConfig<T>>;
|
96
|
+
response: InterceptorManager<FResponse<R, T>>;
|
97
|
+
};
|
98
|
+
constructor(initConfig: InitConfig);
|
99
|
+
request(config: FRequestConfig<T>): Promise<FResponse<R, T>>;
|
100
|
+
}
|
101
|
+
|
102
|
+
export { type FRequestConfig, type FResponse, type InitConfig, type Method, Fexios as default };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,102 @@
|
|
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';
|
7
|
+
/**
|
8
|
+
* 请求配置类型
|
9
|
+
*/
|
10
|
+
type FRequestConfig<T> = {
|
11
|
+
/**
|
12
|
+
* 基于baseURL的请求地址
|
13
|
+
*/
|
14
|
+
url: string | URL;
|
15
|
+
/**
|
16
|
+
* 请求头
|
17
|
+
*/
|
18
|
+
headers?: Record<string, string> | Headers;
|
19
|
+
/**
|
20
|
+
* 请求方法
|
21
|
+
*/
|
22
|
+
method: Method;
|
23
|
+
/**
|
24
|
+
* baseURL
|
25
|
+
*/
|
26
|
+
baseURL?: string | URL;
|
27
|
+
/**
|
28
|
+
* 传输数据
|
29
|
+
*/
|
30
|
+
data?: T;
|
31
|
+
/**
|
32
|
+
* 跨域是否携带cookie
|
33
|
+
*/
|
34
|
+
withCredentials?: boolean;
|
35
|
+
/**
|
36
|
+
* 超时时间
|
37
|
+
*/
|
38
|
+
timeout?: number;
|
39
|
+
};
|
40
|
+
/**
|
41
|
+
* 响应类型
|
42
|
+
*/
|
43
|
+
type FResponse<R, T> = {
|
44
|
+
/**
|
45
|
+
* HTTP响应码
|
46
|
+
*/
|
47
|
+
status: number;
|
48
|
+
/**
|
49
|
+
* HTTP响应信息
|
50
|
+
*/
|
51
|
+
statusText: string;
|
52
|
+
/**
|
53
|
+
* 响应头
|
54
|
+
*/
|
55
|
+
headers: Record<string, string>;
|
56
|
+
/**
|
57
|
+
* 请求的配置
|
58
|
+
*/
|
59
|
+
config: FRequestConfig<T>;
|
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
|
+
});
|
75
|
+
/**
|
76
|
+
* 初始化配置类型
|
77
|
+
*/
|
78
|
+
interface InitConfig extends Omit<FRequestConfig<any>, "url" | "method" | "data"> {
|
79
|
+
}
|
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>>;
|
93
|
+
defaults: InitConfig;
|
94
|
+
interceptors: {
|
95
|
+
request: InterceptorManager<FRequestConfig<T>>;
|
96
|
+
response: InterceptorManager<FResponse<R, T>>;
|
97
|
+
};
|
98
|
+
constructor(initConfig: InitConfig);
|
99
|
+
request(config: FRequestConfig<T>): Promise<FResponse<R, T>>;
|
100
|
+
}
|
101
|
+
|
102
|
+
export { type FRequestConfig, type FResponse, type InitConfig, type Method, Fexios as default };
|
package/dist/index.js
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
import {
|
2
|
+
dispatchRequest
|
3
|
+
} from "./chunk-NNXGOWMX.js";
|
4
|
+
import {
|
5
|
+
InterceptorManager_default
|
6
|
+
} from "./chunk-HIR6OZ3W.js";
|
7
|
+
import {
|
8
|
+
useUtil
|
9
|
+
} from "./chunk-4RAYGLHZ.js";
|
10
|
+
|
11
|
+
// src/index.ts
|
12
|
+
var Fexios = class {
|
13
|
+
// 没有请求数据
|
14
|
+
get;
|
15
|
+
delete;
|
16
|
+
head;
|
17
|
+
options;
|
18
|
+
// 有请求数据
|
19
|
+
post;
|
20
|
+
put;
|
21
|
+
patch;
|
22
|
+
defaults;
|
23
|
+
interceptors;
|
24
|
+
constructor(initConfig) {
|
25
|
+
this.defaults = initConfig;
|
26
|
+
this.interceptors = {
|
27
|
+
request: new InterceptorManager_default(),
|
28
|
+
response: new InterceptorManager_default()
|
29
|
+
};
|
30
|
+
const withDataMethods = ["post", "put", "patch"];
|
31
|
+
withDataMethods.forEach((method) => {
|
32
|
+
this[method] = async (url, data, request) => {
|
33
|
+
const requestConfig = {
|
34
|
+
...request,
|
35
|
+
url,
|
36
|
+
method: method.toUpperCase(),
|
37
|
+
data
|
38
|
+
};
|
39
|
+
return this.request(requestConfig);
|
40
|
+
};
|
41
|
+
});
|
42
|
+
const withoutDataMethods = ["get", "delete", "head", "options"];
|
43
|
+
withoutDataMethods.forEach((method) => {
|
44
|
+
this[method] = async (url, request) => {
|
45
|
+
const requestConfig = {
|
46
|
+
...request,
|
47
|
+
url,
|
48
|
+
method: method.toUpperCase()
|
49
|
+
};
|
50
|
+
return this.request(requestConfig);
|
51
|
+
};
|
52
|
+
});
|
53
|
+
}
|
54
|
+
async request(config) {
|
55
|
+
const { deepMerge, normalizeHeaders } = useUtil();
|
56
|
+
config.headers = config.headers || {};
|
57
|
+
const mergedRequest = {
|
58
|
+
...this.defaults,
|
59
|
+
...config,
|
60
|
+
headers: deepMerge(
|
61
|
+
normalizeHeaders(this.defaults.headers),
|
62
|
+
normalizeHeaders(config.headers)
|
63
|
+
)
|
64
|
+
};
|
65
|
+
console.log(mergedRequest);
|
66
|
+
let chain = [dispatchRequest, void 0];
|
67
|
+
this.interceptors.request.forEach((interceptor) => {
|
68
|
+
interceptor.onFulfilled;
|
69
|
+
chain.unshift(interceptor.onFulfilled, interceptor.onRejected);
|
70
|
+
});
|
71
|
+
this.interceptors.request.forEach((interceptor) => {
|
72
|
+
chain.push(interceptor.onFulfilled, interceptor.onRejected);
|
73
|
+
});
|
74
|
+
let i = 0;
|
75
|
+
let len = chain.length;
|
76
|
+
let promise = Promise.resolve(mergedRequest);
|
77
|
+
while (i < len) {
|
78
|
+
promise = promise.then(chain[i++], chain[i++]);
|
79
|
+
}
|
80
|
+
return promise;
|
81
|
+
}
|
82
|
+
};
|
83
|
+
var index_default = Fexios;
|
84
|
+
export {
|
85
|
+
index_default as default
|
86
|
+
};
|
package/dist/utils.cjs
ADDED
@@ -0,0 +1,54 @@
|
|
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/utils.ts
|
21
|
+
var utils_exports = {};
|
22
|
+
__export(utils_exports, {
|
23
|
+
default: () => useUtil
|
24
|
+
});
|
25
|
+
module.exports = __toCommonJS(utils_exports);
|
26
|
+
function useUtil() {
|
27
|
+
function normalizeHeaders(headers = {}) {
|
28
|
+
if (headers instanceof Headers) {
|
29
|
+
const normalized = {};
|
30
|
+
headers.forEach((value, key) => {
|
31
|
+
normalized[key] = value;
|
32
|
+
});
|
33
|
+
return normalized;
|
34
|
+
}
|
35
|
+
return headers;
|
36
|
+
}
|
37
|
+
function deepMerge(target, source) {
|
38
|
+
const result = { ...target };
|
39
|
+
for (const key in source) {
|
40
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
41
|
+
if (typeof target[key] === "object" && typeof source[key] === "object" && target[key] !== null && source[key] !== null) {
|
42
|
+
result[key] = deepMerge(target[key], source[key]);
|
43
|
+
} else {
|
44
|
+
result[key] = source[key];
|
45
|
+
}
|
46
|
+
}
|
47
|
+
}
|
48
|
+
return result;
|
49
|
+
}
|
50
|
+
return {
|
51
|
+
normalizeHeaders,
|
52
|
+
deepMerge
|
53
|
+
};
|
54
|
+
}
|
package/dist/utils.d.cts
ADDED
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED