etherreq 1.0.1-0.8 → 1.0.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.
@@ -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.8",
3
+ "version": "1.0.2",
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,7 +1,29 @@
1
- import { request } from './request';
1
+ // src/index.js
2
+ import { request, baseURL, setBaseURL } from './request';
2
3
 
3
- const createMethod = (method) => (url, options, callback) => {
4
- const promise = 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);
5
27
 
6
28
  if (typeof callback === 'function') {
7
29
  promise.then(data => callback(null, data)).catch(err => callback(err, null));
@@ -18,5 +40,24 @@ export const etherreq = Object.assign(
18
40
  put: createMethod('PUT'),
19
41
  delete: createMethod('DELETE'),
20
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
+ },
21
60
  }
22
- );
61
+ );
62
+
63
+ export { setBaseURL, _baseURL as baseURL };
package/src/request.js CHANGED
@@ -38,17 +38,27 @@ instance.interceptors.response.use(
38
38
  }
39
39
  );
40
40
 
41
+ let _baseURL = 'https://api.example.com'; // 内部变量用于保存 base URL
42
+
41
43
  /**
42
44
  * 封装 request 函数,支持 baseURl 拼接等逻辑
43
45
  * @param {string} url - 请求路径
44
46
  * @param {Object} options - 请求配置
45
47
  */
46
48
  export const request = (url, options = {}) => {
47
- const baseURL = options.baseURL || 'https://api.example.com';
49
+ const baseURL = options.baseURL || _baseURL;
48
50
  const finalURL = new URL(url, baseURL).toString();
49
51
 
50
52
  return instance({
51
53
  ...options,
52
54
  url: finalURL,
53
55
  });
56
+ };
57
+
58
+ // 导出可读写的 baseURL 变量
59
+ export { _baseURL as baseURL };
60
+
61
+ // 允许外部设置 baseURL
62
+ export const setBaseURL = (newBaseURL) => {
63
+ _baseURL = newBaseURL;
54
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 };