etherreq 1.0.1-0.1 → 1.0.1-0.10

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.
@@ -0,0 +1,33 @@
1
+ # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
+ # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3
+
4
+ name: Node.js Package
5
+
6
+ on:
7
+ release:
8
+ types: [created]
9
+
10
+ jobs:
11
+ build:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+ - uses: actions/setup-node@v4
16
+ with:
17
+ node-version: 20
18
+ - run: npm ci
19
+ - run: npm test
20
+
21
+ publish-npm:
22
+ needs: build
23
+ runs-on: ubuntu-latest
24
+ steps:
25
+ - uses: actions/checkout@v4
26
+ - uses: actions/setup-node@v4
27
+ with:
28
+ node-version: 20
29
+ registry-url: https://registry.npmjs.org/
30
+ - run: npm ci
31
+ - run: npm publish
32
+ env:
33
+ NODE_AUTH_TOKEN: ${{secrets.npm_token}}
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ 一个轻量级、无感知的 HTTP 请求库,基于 Axios 核心理念封装,支持自动 Token 注入、拦截器、TypeScript 类型定义等功能。
2
+
3
+
4
+
5
+ 安装
6
+
7
+ ```
8
+ npm install etherreq
9
+ # 或者
10
+ yarn add etherreq
11
+ ```
12
+
package/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "etherreq",
3
- "version": "1.0.10.1",
3
+ "version": "1.0.10.10",
4
4
  "description": "A lightweight custom HTTP request library.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/types/etherreq.d.ts",
7
7
  "scripts": {
8
8
  "test": "echo \"Error: no test specified\" && exit 1"
9
9
  },
10
- "keywords": ["http", "request"],
10
+ "keywords": [
11
+ "http",
12
+ "request"
13
+ ],
11
14
  "author": "guochang2635@icloud.com",
12
15
  "license": "MIT"
13
- }
16
+ }
package/src/index.js CHANGED
@@ -1,16 +1,61 @@
1
1
  // src/index.js
2
- import { request } from './request';
2
+ import { request, baseURL, setBaseURL } from './request';
3
3
 
4
- const createMethod = (method) => (url, options) =>
5
- request(url, { ...options, method });
4
+ // 设置全局 base URL
5
+ setBaseURL('http://localhost:8081/api');
6
+
7
+ // 示例封装方法
8
+ const createMethod = (method) => (url, data, callback) => {
9
+ let options;
10
+
11
+ if (
12
+ data &&
13
+ typeof data === 'object' &&
14
+ !Array.isArray(data) &&
15
+ !(data instanceof FormData) &&
16
+ !data.method &&
17
+ !data.headers &&
18
+ !data.params &&
19
+ !data.baseURL
20
+ ) {
21
+ options = { body: data, method };
22
+ } else {
23
+ options = { ...data, method };
24
+ }
25
+
26
+ const promise = request(url, options);
27
+
28
+ if (typeof callback === 'function') {
29
+ promise.then(data => callback(null, data)).catch(err => callback(err, null));
30
+ } else {
31
+ return promise;
32
+ }
33
+ };
6
34
 
7
35
  export const etherreq = Object.assign(
8
- (url, options) => createMethod('GET')(url, options),
36
+ (url, options, callback) => createMethod('GET')(url, options, callback),
9
37
  {
10
38
  get: createMethod('GET'),
11
39
  post: createMethod('POST'),
12
40
  put: createMethod('PUT'),
13
41
  delete: createMethod('DELETE'),
14
42
  del: createMethod('DELETE'),
43
+ login: (url, data, callback) => {
44
+ const loginPromise = createMethod('POST')(url, data);
45
+
46
+ loginPromise.then(response => {
47
+ const token = response.data?.token;
48
+ if (token) {
49
+ localStorage.setItem('token', token); // 保存 token 到 localStorage
50
+ }
51
+ return response;
52
+ });
53
+
54
+ if (typeof callback === 'function') {
55
+ loginPromise.then(data => callback(null, data)).catch(err => callback(err, null));
56
+ } else {
57
+ return loginPromise;
58
+ }
59
+ },
15
60
  }
16
61
  );
package/src/request.js CHANGED
@@ -1,26 +1,36 @@
1
1
  // src/request.js
2
2
  import { create } from './myaxios';
3
3
 
4
+ // 创建一个带有默认配置的请求实例
4
5
  const instance = create({
5
- baseURL: 'https://api.example.com',
6
+ baseURL: 'https://api.example.com', // 默认基础 URL
6
7
  });
7
8
 
8
- // 请求拦截器 - 自动注入 token
9
+ // 请求拦截器:自动注入 token Content-Type
9
10
  instance.interceptors.request.use((config) => {
10
11
  const token = localStorage.getItem('token');
12
+
13
+ // 构建最终 headers
14
+ const headers = {
15
+ ...(config.headers || {}), // 用户自定义 headers
16
+ Authorization: token ? `Bearer ${token}` : undefined,
17
+ };
18
+
19
+ // 如果是 POST/PUT 等非 GET 请求,并且没有指定 Content-Type,则默认为 application/json
20
+ if (config.method !== 'GET' && !headers['Content-Type']) {
21
+ headers['Content-Type'] = 'application/json';
22
+ }
23
+
11
24
  return {
12
25
  ...config,
13
- headers: {
14
- ...(config.headers || {}),
15
- Authorization: token ? `Bearer ${token}` : undefined,
16
- },
26
+ headers,
17
27
  };
18
28
  });
19
29
 
20
- // 响应拦截器 - 返回 data
30
+ // 响应拦截器:自动提取 response.data
21
31
  instance.interceptors.response.use(
22
32
  (response) => {
23
- return response.data; // 自动提取 data
33
+ return response.data; // 返回 data 字段作为结果
24
34
  },
25
35
  (error) => {
26
36
  console.error('请求异常:', error);
@@ -28,9 +38,27 @@ instance.interceptors.response.use(
28
38
  }
29
39
  );
30
40
 
31
- export const request = (url, options) => {
41
+ let _baseURL = 'https://api.example.com'; // 内部变量用于保存 base URL
42
+
43
+ /**
44
+ * 封装 request 函数,支持 baseURl 拼接等逻辑
45
+ * @param {string} url - 请求路径
46
+ * @param {Object} options - 请求配置
47
+ */
48
+ export const request = (url, options = {}) => {
49
+ const baseURL = options.baseURL || _baseURL;
50
+ const finalURL = new URL(url, baseURL).toString();
51
+
32
52
  return instance({
33
53
  ...options,
34
- url: options.baseURL + url,
54
+ url: finalURL,
35
55
  });
56
+ };
57
+
58
+ // 导出可读写的 baseURL 变量
59
+ export { _baseURL as baseURL };
60
+
61
+ // 允许外部设置 baseURL
62
+ export const setBaseURL = (newBaseURL) => {
63
+ _baseURL = newBaseURL;
36
64
  };
@@ -1,5 +1,3 @@
1
- // src/types/etherreq.d.ts
2
-
3
1
  interface EtherRequestOptions {
4
2
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
5
3
  headers?: Record<string, string | number | boolean>;
@@ -28,9 +26,16 @@ interface EtherreqStatic extends EtherRequestMethod {
28
26
  put: EtherRequestMethod;
29
27
  delete: EtherRequestMethod;
30
28
  del: EtherRequestMethod;
29
+ login: EtherRequestMethod; // 新增 login 类型
31
30
  }
32
31
 
33
32
  // 导出对象
34
33
  declare const etherreq: EtherreqStatic;
35
34
 
35
+ declare module 'etherreq' {
36
+ export const etherreq: EtherreqStatic;
37
+ export let baseURL: string;
38
+ export function setBaseURL(url: string): void;
39
+ }
40
+
36
41
  export { etherreq };