@aura-react-comp/request 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +31 -2
- package/esm/index.d.ts +55 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +151 -0
- package/package.json +47 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026-present Aura Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @aura-react-comp/request
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
基于原生 `fetch` 的轻量 HTTP 请求封装,提供拦截器、超时与统一错误处理。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @aura-react-comp/request
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createRequest } from '@aura-react-comp/request';
|
|
15
|
+
|
|
16
|
+
const request = createRequest({
|
|
17
|
+
baseURL: '/api',
|
|
18
|
+
timeout: 10000,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const users = await request.get('/users', { page: 1 });
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 说明
|
|
25
|
+
|
|
26
|
+
- 无第三方 HTTP 库依赖,直接基于原生 `fetch`。
|
|
27
|
+
- 可在浏览器与 Node 18+ 环境使用。
|
|
28
|
+
- 可被 tree-shaking(`sideEffects: false`)。
|
|
29
|
+
|
|
30
|
+
## 许可证
|
|
31
|
+
|
|
32
|
+
MIT
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** 请求方法类型 */
|
|
2
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD';
|
|
3
|
+
/** 请求配置 */
|
|
4
|
+
export interface RequestConfig<T = unknown> {
|
|
5
|
+
/** 请求地址 */
|
|
6
|
+
url: string;
|
|
7
|
+
/** 请求方法 */
|
|
8
|
+
method?: HttpMethod;
|
|
9
|
+
/** 请求头 */
|
|
10
|
+
headers?: Record<string, string>;
|
|
11
|
+
/** 查询参数 */
|
|
12
|
+
params?: Record<string, string | number | boolean>;
|
|
13
|
+
/** 请求体 */
|
|
14
|
+
data?: unknown;
|
|
15
|
+
/** 基础 URL 前缀 */
|
|
16
|
+
baseURL?: string;
|
|
17
|
+
/** 请求超时时间(毫秒) */
|
|
18
|
+
timeout?: number;
|
|
19
|
+
/** 携带凭证 */
|
|
20
|
+
credentials?: RequestCredentials;
|
|
21
|
+
/** 响应类型 */
|
|
22
|
+
responseType?: 'json' | 'text' | 'blob' | 'arrayBuffer';
|
|
23
|
+
/** 请求前拦截器 */
|
|
24
|
+
beforeRequest?: (config: RequestConfig<T>) => RequestConfig<T>;
|
|
25
|
+
/** 响应后拦截器 */
|
|
26
|
+
afterResponse?: (response: unknown) => unknown;
|
|
27
|
+
}
|
|
28
|
+
/** 统一响应结构 */
|
|
29
|
+
export interface ApiResponse<T = unknown> {
|
|
30
|
+
code: number;
|
|
31
|
+
data: T;
|
|
32
|
+
message: string;
|
|
33
|
+
}
|
|
34
|
+
/** 请求错误类 */
|
|
35
|
+
export declare class RequestError extends Error {
|
|
36
|
+
code: number;
|
|
37
|
+
constructor(message: string, code: number);
|
|
38
|
+
}
|
|
39
|
+
/** 设置全局默认配置 */
|
|
40
|
+
export declare const setRequestDefaults: (config: Partial<RequestConfig>) => void;
|
|
41
|
+
/** 核心请求函数 */
|
|
42
|
+
declare function request<T = unknown>(config: RequestConfig<T>): Promise<T>;
|
|
43
|
+
/** GET 请求 */
|
|
44
|
+
export declare const get: <T = unknown>(url: string, params?: Record<string, string | number | boolean>, config?: Partial<RequestConfig<T>>) => Promise<T>;
|
|
45
|
+
/** POST 请求 */
|
|
46
|
+
export declare const post: <T = unknown>(url: string, data?: unknown, config?: Partial<RequestConfig<T>>) => Promise<T>;
|
|
47
|
+
/** PUT 请求 */
|
|
48
|
+
export declare const put: <T = unknown>(url: string, data?: unknown, config?: Partial<RequestConfig<T>>) => Promise<T>;
|
|
49
|
+
/** DELETE 请求 */
|
|
50
|
+
export declare const del: <T = unknown>(url: string, config?: Partial<RequestConfig<T>>) => Promise<T>;
|
|
51
|
+
/** PATCH 请求 */
|
|
52
|
+
export declare const patch: <T = unknown>(url: string, data?: unknown, config?: Partial<RequestConfig<T>>) => Promise<T>;
|
|
53
|
+
export { request };
|
|
54
|
+
export default request;
|
|
55
|
+
//# sourceMappingURL=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MAAM,CAAC;gBACD,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;CAK1C;AAUD,eAAe;AACf,eAAO,MAAM,kBAAkB,WAAY,QAAQ,aAAa,CAAC,SAEhE,CAAC;AAwBF,aAAa;AACb,iBAAe,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAqDxE;AAED,aAAa;AACb,eAAO,MAAM,GAAG,qBAAsB,MAAM,WAAW,OAAO,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,WAAW,QAAQ,cAAc,CAAC,CAAC,CAAC,eAC7E,CAAC;AAExD,cAAc;AACd,eAAO,MAAM,IAAI,qBAAsB,MAAM,SAAS,OAAO,WAAW,QAAQ,cAAc,CAAC,CAAC,CAAC,eAC3C,CAAC;AAEvD,aAAa;AACb,eAAO,MAAM,GAAG,qBAAsB,MAAM,SAAS,OAAO,WAAW,QAAQ,cAAc,CAAC,CAAC,CAAC,eAC3C,CAAC;AAEtD,gBAAgB;AAChB,eAAO,MAAM,GAAG,qBAAsB,MAAM,WAAW,QAAQ,cAAc,CAAC,CAAC,CAAC,eAC9B,CAAC;AAEnD,eAAe;AACf,eAAO,MAAM,KAAK,qBAAsB,MAAM,SAAS,OAAO,WAAW,QAAQ,cAAc,CAAC,CAAC,CAAC,eAC3C,CAAC;AAExD,OAAO,EAAE,OAAO,EAAE,CAAC;AACnB,eAAe,OAAO,CAAC"}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/** 请求方法类型 */
|
|
2
|
+
|
|
3
|
+
/** 请求配置 */
|
|
4
|
+
|
|
5
|
+
/** 统一响应结构 */
|
|
6
|
+
|
|
7
|
+
/** 请求错误类 */
|
|
8
|
+
export class RequestError extends Error {
|
|
9
|
+
code;
|
|
10
|
+
constructor(message, code) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'RequestError';
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** 全局默认配置 */
|
|
18
|
+
const globalConfig = {
|
|
19
|
+
baseURL: '',
|
|
20
|
+
timeout: 10000,
|
|
21
|
+
credentials: 'same-origin',
|
|
22
|
+
responseType: 'json'
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** 设置全局默认配置 */
|
|
26
|
+
export const setRequestDefaults = config => {
|
|
27
|
+
Object.assign(globalConfig, config);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** 拼接查询参数 */
|
|
31
|
+
const buildQueryString = params => {
|
|
32
|
+
if (!params) return '';
|
|
33
|
+
const search = new URLSearchParams(Object.entries(params).reduce((acc, [k, v]) => {
|
|
34
|
+
acc[k] = String(v);
|
|
35
|
+
return acc;
|
|
36
|
+
}, {})).toString();
|
|
37
|
+
return search ? `?${search}` : '';
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** 解析响应体 */
|
|
41
|
+
const parseResponse = async (response, responseType) => {
|
|
42
|
+
switch (responseType) {
|
|
43
|
+
case 'text':
|
|
44
|
+
return response.text();
|
|
45
|
+
case 'blob':
|
|
46
|
+
return response.blob();
|
|
47
|
+
case 'arrayBuffer':
|
|
48
|
+
return response.arrayBuffer();
|
|
49
|
+
default:
|
|
50
|
+
return response.json();
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** 核心请求函数 */
|
|
55
|
+
async function request(config) {
|
|
56
|
+
const merged = {
|
|
57
|
+
...globalConfig,
|
|
58
|
+
...config
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// 请求前拦截
|
|
62
|
+
const finalConfig = merged.beforeRequest ? merged.beforeRequest(merged) : merged;
|
|
63
|
+
const {
|
|
64
|
+
url,
|
|
65
|
+
method = 'GET',
|
|
66
|
+
headers = {},
|
|
67
|
+
params,
|
|
68
|
+
data,
|
|
69
|
+
baseURL,
|
|
70
|
+
timeout,
|
|
71
|
+
credentials,
|
|
72
|
+
responseType,
|
|
73
|
+
afterResponse
|
|
74
|
+
} = finalConfig;
|
|
75
|
+
const fullURL = `${baseURL || ''}${url}${buildQueryString(params)}`;
|
|
76
|
+
const controller = new AbortController();
|
|
77
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout || 10000);
|
|
78
|
+
const fetchOptions = {
|
|
79
|
+
method,
|
|
80
|
+
headers: {
|
|
81
|
+
'Content-Type': 'application/json',
|
|
82
|
+
...headers
|
|
83
|
+
},
|
|
84
|
+
credentials,
|
|
85
|
+
signal: controller.signal
|
|
86
|
+
};
|
|
87
|
+
if (method !== 'GET' && method !== 'HEAD' && data !== undefined) {
|
|
88
|
+
fetchOptions.body = JSON.stringify(data);
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const response = await fetch(fullURL, fetchOptions);
|
|
92
|
+
clearTimeout(timeoutId);
|
|
93
|
+
if (!response.ok) {
|
|
94
|
+
throw new RequestError(`请求失败: ${response.status} ${response.statusText}`, response.status);
|
|
95
|
+
}
|
|
96
|
+
let result = await parseResponse(response, responseType || 'json');
|
|
97
|
+
|
|
98
|
+
// 响应后拦截
|
|
99
|
+
if (afterResponse) {
|
|
100
|
+
result = afterResponse(result);
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
clearTimeout(timeoutId);
|
|
105
|
+
if (error instanceof DOMException && error.name === 'AbortError') {
|
|
106
|
+
throw new RequestError('请求超时', 408);
|
|
107
|
+
}
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** GET 请求 */
|
|
113
|
+
export const get = (url, params, config) => request({
|
|
114
|
+
...config,
|
|
115
|
+
url,
|
|
116
|
+
method: 'GET',
|
|
117
|
+
params
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
/** POST 请求 */
|
|
121
|
+
export const post = (url, data, config) => request({
|
|
122
|
+
...config,
|
|
123
|
+
url,
|
|
124
|
+
method: 'POST',
|
|
125
|
+
data
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
/** PUT 请求 */
|
|
129
|
+
export const put = (url, data, config) => request({
|
|
130
|
+
...config,
|
|
131
|
+
url,
|
|
132
|
+
method: 'PUT',
|
|
133
|
+
data
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
/** DELETE 请求 */
|
|
137
|
+
export const del = (url, config) => request({
|
|
138
|
+
...config,
|
|
139
|
+
url,
|
|
140
|
+
method: 'DELETE'
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
/** PATCH 请求 */
|
|
144
|
+
export const patch = (url, data, config) => request({
|
|
145
|
+
...config,
|
|
146
|
+
url,
|
|
147
|
+
method: 'PATCH',
|
|
148
|
+
data
|
|
149
|
+
});
|
|
150
|
+
export { request };
|
|
151
|
+
export default request;
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aura-react-comp/request",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "基于原生 Fetch 的 HTTP 请求封装",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"aura",
|
|
7
|
+
"http",
|
|
8
|
+
"fetch",
|
|
9
|
+
"request",
|
|
10
|
+
"interceptor"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://asbeforelandy.github.io/aura/",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/AsBeforeLandy/aura/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/AsBeforeLandy/aura.git",
|
|
19
|
+
"directory": "packages/request"
|
|
20
|
+
},
|
|
21
|
+
"author": "Aura Team",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "esm/index.js",
|
|
25
|
+
"module": "esm/index.js",
|
|
26
|
+
"types": "esm/index.d.ts",
|
|
27
|
+
"files": [
|
|
28
|
+
"esm"
|
|
29
|
+
],
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./esm/index.d.ts",
|
|
34
|
+
"import": "./esm/index.js",
|
|
35
|
+
"default": "./esm/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@aura-react-comp/shared": "0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"registry": "https://registry.npmjs.org/"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "father build",
|
|
48
|
+
"dev": "father dev"
|
|
49
|
+
}
|
|
6
50
|
}
|