fexios-browser 1.1.0 → 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 CHANGED
@@ -1,3 +1,42 @@
1
- # Fexios
1
+ # Fexios-Browser
2
2
 
3
- 对fetch的封装,使其能够表现的像axios
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] = null;
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 !== null) {
50
+ if (handler) {
51
51
  fn(handler);
52
52
  }
53
53
  });
@@ -1,10 +1,14 @@
1
- declare class InterceptorManager {
2
- private handlers;
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: Function, rejected: Function): number;
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: Function): void;
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
- declare class InterceptorManager {
2
- private handlers;
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: Function, rejected: Function): number;
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: Function): void;
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,6 +1,6 @@
1
1
  import {
2
2
  InterceptorManager_default
3
- } from "./chunk-UDNTQS3I.js";
3
+ } from "./chunk-HIR6OZ3W.js";
4
4
  export {
5
5
  InterceptorManager_default as default
6
6
  };
@@ -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] = null;
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 !== null) {
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
+ }
@@ -0,0 +1,6 @@
1
+ import { FRequestConfig, FResponse } from './index.cjs';
2
+ import './InterceptorManager.cjs';
3
+
4
+ declare function dispatchRequest<T>(config: FRequestConfig<T>): Promise<FResponse<T>>;
5
+
6
+ export { dispatchRequest as default };
@@ -0,0 +1,6 @@
1
+ import { FRequestConfig, FResponse } from './index.js';
2
+ import './InterceptorManager.js';
3
+
4
+ declare function dispatchRequest<T>(config: FRequestConfig<T>): Promise<FResponse<T>>;
5
+
6
+ export { dispatchRequest as default };
@@ -0,0 +1,6 @@
1
+ import {
2
+ dispatchRequest
3
+ } from "./chunk-ZTE6UN5I.js";
4
+ export {
5
+ dispatchRequest as default
6
+ };
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] = null;
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 !== null) {
80
+ if (handler) {
53
81
  fn(handler);
54
82
  }
55
83
  });
@@ -58,7 +86,17 @@ var InterceptorManager = class {
58
86
  var InterceptorManager_default = InterceptorManager;
59
87
 
60
88
  // src/index.ts
61
- var Fexios = class _Fexios {
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,94 +105,39 @@ var Fexios = class _Fexios {
67
105
  request: new InterceptorManager_default(),
68
106
  response: new InterceptorManager_default()
69
107
  };
70
- }
71
- create(initConfig) {
72
- const config = {
73
- ...this.defaults,
74
- ...initConfig
75
- };
76
- return new _Fexios(config);
77
- }
78
- async request(request) {
79
- try {
80
- const mergedRequest = {
81
- ...request,
82
- headers: { ...this.defaults.headers, ...request.headers },
83
- baseURL: request.baseURL || this.defaults.baseURL
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);
84
117
  };
85
- let chain = Promise.resolve(mergedRequest);
86
- this.interceptors.request.forEach(({ fulfilled, rejected }) => {
87
- chain = chain.then(fulfilled, rejected);
88
- });
89
- let processedRequest = await chain;
90
- const rowResponse = await fetch(new URL(processedRequest.url, processedRequest.baseURL), {
91
- method: processedRequest.method,
92
- headers: processedRequest.headers,
93
- body: processedRequest.data ? JSON.stringify(processedRequest.data) : void 0
94
- });
95
- const contentType = rowResponse.headers.get("Content-Type") || "";
96
- let responseData;
97
- if (contentType.includes("application/json")) {
98
- responseData = await rowResponse.json();
99
- } else {
100
- responseData = rowResponse;
101
- }
102
- const headers = {};
103
- rowResponse.headers.forEach((value, key) => {
104
- headers[key] = value;
105
- });
106
- let response = {
107
- status: rowResponse.status,
108
- statusText: rowResponse.statusText,
109
- data: responseData,
110
- headers,
111
- request
112
- };
113
- let responseChain;
114
- if (rowResponse.ok) {
115
- responseChain = Promise.resolve(response);
116
- } else {
117
- responseChain = Promise.reject(response);
118
- }
119
- this.interceptors.response.forEach(({ fulfilled, rejected }) => {
120
- responseChain = responseChain.then(fulfilled, rejected);
121
- });
122
- return await responseChain;
123
- } catch (error) {
124
- throw error;
125
- }
126
- }
127
- async get(url, request) {
128
- const getRequest = {
129
- ...request,
130
- url,
131
- method: "GET"
132
- };
133
- return this.request(getRequest);
134
- }
135
- async post(url, request) {
136
- const postRequest = {
137
- ...request,
138
- url,
139
- method: "POST"
140
- };
141
- return this.request(postRequest);
142
- }
143
- async delete(url, request) {
144
- const deleteRequest = {
145
- ...request,
146
- url,
147
- method: "DELETE"
148
- };
149
- return this.request(deleteRequest);
118
+ });
150
119
  }
151
- async put(url, request) {
152
- const putRequest = {
153
- ...request,
154
- url,
155
- method: "PUT"
120
+ async request(config) {
121
+ const mergedRequest = {
122
+ ...config,
123
+ headers: { ...this.defaults.headers, ...config.headers },
124
+ baseURL: config.baseURL || this.defaults.baseURL
156
125
  };
157
- return this.request(putRequest);
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;
158
141
  }
159
142
  };
160
143
  var index_default = Fexios;
package/dist/index.d.cts CHANGED
@@ -1,36 +1,84 @@
1
1
  import InterceptorManager from './InterceptorManager.cjs';
2
2
 
3
- interface FRequest {
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
- method: "GET" | "POST" | "DELETE" | "PUT";
19
+ /**
20
+ * 请求方法
21
+ */
22
+ method: Method;
23
+ /**
24
+ * baseURL
25
+ */
7
26
  baseURL?: string | URL;
8
- data?: any;
27
+ /**
28
+ * 传输数据
29
+ */
30
+ data?: T;
9
31
  }
10
- interface FResponse {
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
- request: FRequest;
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
- create(initConfig: Partial<InitConfig>): Fexios;
29
- request(request: FRequest): Promise<FResponse>;
30
- get(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
31
- post(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
32
- delete(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
33
- put(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
81
+ request(config: FRequestConfig<T>): Promise<FResponse<T>>;
34
82
  }
35
83
 
36
- export { type FRequest, type FResponse, type InitConfig, Fexios as default };
84
+ export { type FRequestConfig, type FResponse, type InitConfig, type Method, Fexios as default };
package/dist/index.d.ts CHANGED
@@ -1,36 +1,84 @@
1
1
  import InterceptorManager from './InterceptorManager.js';
2
2
 
3
- interface FRequest {
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
- method: "GET" | "POST" | "DELETE" | "PUT";
19
+ /**
20
+ * 请求方法
21
+ */
22
+ method: Method;
23
+ /**
24
+ * baseURL
25
+ */
7
26
  baseURL?: string | URL;
8
- data?: any;
27
+ /**
28
+ * 传输数据
29
+ */
30
+ data?: T;
9
31
  }
10
- interface FResponse {
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
- request: FRequest;
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
- create(initConfig: Partial<InitConfig>): Fexios;
29
- request(request: FRequest): Promise<FResponse>;
30
- get(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
31
- post(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
32
- delete(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
33
- put(url: string | URL, request: Omit<FRequest, "url" | "method">): Promise<FResponse>;
81
+ request(config: FRequestConfig<T>): Promise<FResponse<T>>;
34
82
  }
35
83
 
36
- export { type FRequest, type FResponse, type InitConfig, Fexios as default };
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-UDNTQS3I.js";
6
+ } from "./chunk-HIR6OZ3W.js";
4
7
 
5
8
  // src/index.ts
6
- var Fexios = class _Fexios {
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,94 +25,39 @@ var Fexios = class _Fexios {
12
25
  request: new InterceptorManager_default(),
13
26
  response: new InterceptorManager_default()
14
27
  };
15
- }
16
- create(initConfig) {
17
- const config = {
18
- ...this.defaults,
19
- ...initConfig
20
- };
21
- return new _Fexios(config);
22
- }
23
- async request(request) {
24
- try {
25
- const mergedRequest = {
26
- ...request,
27
- headers: { ...this.defaults.headers, ...request.headers },
28
- baseURL: request.baseURL || this.defaults.baseURL
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);
29
37
  };
30
- let chain = Promise.resolve(mergedRequest);
31
- this.interceptors.request.forEach(({ fulfilled, rejected }) => {
32
- chain = chain.then(fulfilled, rejected);
33
- });
34
- let processedRequest = await chain;
35
- const rowResponse = await fetch(new URL(processedRequest.url, processedRequest.baseURL), {
36
- method: processedRequest.method,
37
- headers: processedRequest.headers,
38
- body: processedRequest.data ? JSON.stringify(processedRequest.data) : void 0
39
- });
40
- const contentType = rowResponse.headers.get("Content-Type") || "";
41
- let responseData;
42
- if (contentType.includes("application/json")) {
43
- responseData = await rowResponse.json();
44
- } else {
45
- responseData = rowResponse;
46
- }
47
- const headers = {};
48
- rowResponse.headers.forEach((value, key) => {
49
- headers[key] = value;
50
- });
51
- let response = {
52
- status: rowResponse.status,
53
- statusText: rowResponse.statusText,
54
- data: responseData,
55
- headers,
56
- request
57
- };
58
- let responseChain;
59
- if (rowResponse.ok) {
60
- responseChain = Promise.resolve(response);
61
- } else {
62
- responseChain = Promise.reject(response);
63
- }
64
- this.interceptors.response.forEach(({ fulfilled, rejected }) => {
65
- responseChain = responseChain.then(fulfilled, rejected);
66
- });
67
- return await responseChain;
68
- } catch (error) {
69
- throw error;
70
- }
38
+ });
71
39
  }
72
- async get(url, request) {
73
- const getRequest = {
74
- ...request,
75
- url,
76
- method: "GET"
40
+ async request(config) {
41
+ const mergedRequest = {
42
+ ...config,
43
+ headers: { ...this.defaults.headers, ...config.headers },
44
+ baseURL: config.baseURL || this.defaults.baseURL
77
45
  };
78
- return this.request(getRequest);
79
- }
80
- async post(url, request) {
81
- const postRequest = {
82
- ...request,
83
- url,
84
- method: "POST"
85
- };
86
- return this.request(postRequest);
87
- }
88
- async delete(url, request) {
89
- const deleteRequest = {
90
- ...request,
91
- url,
92
- method: "DELETE"
93
- };
94
- return this.request(deleteRequest);
95
- }
96
- async put(url, request) {
97
- const putRequest = {
98
- ...request,
99
- url,
100
- method: "PUT"
101
- };
102
- 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;
103
61
  }
104
62
  };
105
63
  var index_default = Fexios;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fexios-browser",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "description": "对fetch的封装, 使其能够表现的像axios",
5
5
  "main": "dist/index.cjs",
6
6
  "types": "dist/index.d.ts",