etherreq 1.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/.github/workfiows/publish-npm.yml +30 -0
- package/package.json +12 -0
- package/src/index.js +16 -0
- package/src/myaxios.js +75 -0
- package/src/request.js +36 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
name: Publish to NPM
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main # 当推送到 main 分支时触发
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
publish:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- name: Checkout code
|
|
13
|
+
uses: actions/checkout@v4
|
|
14
|
+
|
|
15
|
+
- name: Setup Node.js
|
|
16
|
+
uses: actions/setup-node@v4
|
|
17
|
+
with:
|
|
18
|
+
node-version: '20.x'
|
|
19
|
+
registry-url: https://registry.npmjs.org/
|
|
20
|
+
|
|
21
|
+
- name: Install dependencies
|
|
22
|
+
run: npm install
|
|
23
|
+
|
|
24
|
+
- name: Build package (if needed)
|
|
25
|
+
run: echo "No build step required for this package."
|
|
26
|
+
|
|
27
|
+
- name: Publish to NPM
|
|
28
|
+
run: npm publish
|
|
29
|
+
env:
|
|
30
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/package.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "etherreq",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A lightweight custom HTTP request library.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": ["http", "request", "axios"],
|
|
10
|
+
"author": "Your Name <your.email@example.com>",
|
|
11
|
+
"license": "MIT"
|
|
12
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/index.js
|
|
2
|
+
import { request } from './request';
|
|
3
|
+
|
|
4
|
+
const createMethod = (method) => (url, options) =>
|
|
5
|
+
request(url, { ...options, method });
|
|
6
|
+
|
|
7
|
+
export const etherreq = Object.assign(
|
|
8
|
+
(url, options) => createMethod('GET')(url, options),
|
|
9
|
+
{
|
|
10
|
+
get: createMethod('GET'),
|
|
11
|
+
post: createMethod('POST'),
|
|
12
|
+
put: createMethod('PUT'),
|
|
13
|
+
delete: createMethod('DELETE'),
|
|
14
|
+
del: createMethod('DELETE'),
|
|
15
|
+
}
|
|
16
|
+
);
|
package/src/myaxios.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// src/myaxios.js
|
|
2
|
+
|
|
3
|
+
export const create = (defaultConfig = {}) => {
|
|
4
|
+
const interceptors = {
|
|
5
|
+
request: [],
|
|
6
|
+
response: [],
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const use = (fulfilled, rejected, type = 'request') => {
|
|
10
|
+
interceptors[type].push({ fulfilled, rejected });
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const dispatchRequest = async (config) => {
|
|
14
|
+
// 请求拦截器执行
|
|
15
|
+
for (const interceptor of interceptors.request) {
|
|
16
|
+
config = await interceptor.fulfilled(config);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// 执行请求
|
|
20
|
+
let response;
|
|
21
|
+
try {
|
|
22
|
+
const { url, method = 'GET', headers = {}, body } = config;
|
|
23
|
+
|
|
24
|
+
const options = {
|
|
25
|
+
method,
|
|
26
|
+
headers,
|
|
27
|
+
body: method !== 'GET' ? JSON.stringify(body) : undefined,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const res = await fetch(url, options);
|
|
31
|
+
const data = await res.json();
|
|
32
|
+
|
|
33
|
+
response = {
|
|
34
|
+
data,
|
|
35
|
+
status: res.status,
|
|
36
|
+
statusText: res.statusText,
|
|
37
|
+
headers: res.headers,
|
|
38
|
+
};
|
|
39
|
+
} catch (error) {
|
|
40
|
+
// 响应拦截器 - 错误处理
|
|
41
|
+
for (const interceptor of interceptors.response) {
|
|
42
|
+
if (interceptor.rejected) {
|
|
43
|
+
return interceptor.rejected(error);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 响应拦截器执行
|
|
50
|
+
for (const interceptor of interceptors.response) {
|
|
51
|
+
response = await interceptor.fulfilled(response);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return response;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const instance = (config) => {
|
|
58
|
+
return dispatchRequest({
|
|
59
|
+
...defaultConfig,
|
|
60
|
+
...config,
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
instance.use = (fulfilled, rejected) => use(fulfilled, rejected, 'request');
|
|
65
|
+
instance.interceptors = {
|
|
66
|
+
request: {
|
|
67
|
+
use: (fulfilled, rejected) => use(fulfilled, rejected, 'request'),
|
|
68
|
+
},
|
|
69
|
+
response: {
|
|
70
|
+
use: (fulfilled, rejected) => use(fulfilled, rejected, 'response'),
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
return instance;
|
|
75
|
+
};
|
package/src/request.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/request.js
|
|
2
|
+
import { create } from './myaxios';
|
|
3
|
+
|
|
4
|
+
const instance = create({
|
|
5
|
+
baseURL: 'https://api.example.com',
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
// 请求拦截器 - 自动注入 token
|
|
9
|
+
instance.interceptors.request.use((config) => {
|
|
10
|
+
const token = localStorage.getItem('token');
|
|
11
|
+
return {
|
|
12
|
+
...config,
|
|
13
|
+
headers: {
|
|
14
|
+
...(config.headers || {}),
|
|
15
|
+
Authorization: token ? `Bearer ${token}` : undefined,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// 响应拦截器 - 返回 data
|
|
21
|
+
instance.interceptors.response.use(
|
|
22
|
+
(response) => {
|
|
23
|
+
return response.data; // 自动提取 data
|
|
24
|
+
},
|
|
25
|
+
(error) => {
|
|
26
|
+
console.error('请求异常:', error);
|
|
27
|
+
return Promise.reject(error);
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
export const request = (url, options) => {
|
|
32
|
+
return instance({
|
|
33
|
+
...options,
|
|
34
|
+
url: options.baseURL + url,
|
|
35
|
+
});
|
|
36
|
+
};
|