beer-network 1.1.1-alpha.1 → 1.1.1-alpha.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.
package/api.d.ts CHANGED
@@ -16,6 +16,15 @@ export type RequestData<T> = {
16
16
  total: number;
17
17
  success: boolean;
18
18
  };
19
+ /**
20
+ * 其它菜单.
21
+ */
22
+ export type Option = {
23
+ /**
24
+ * 中止信号量.
25
+ */
26
+ signal?: AbortSignal;
27
+ };
19
28
  export default class Fetch {
20
29
  /**
21
30
  * 请求地址.
@@ -27,21 +36,44 @@ export default class Fetch {
27
36
  * @param path 路径.
28
37
  * @param params 参数.
29
38
  * @param body 内容体.
39
+ * @param header 头部.
40
+ * @param option 选项.
41
+ */
42
+ post<T>(path: string, params?: any, body?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
43
+ /**
44
+ * 发送POST/Form 请求.
45
+ * @param path 路径.
46
+ * @param params 参数.
47
+ * @param form Form参数.
48
+ * @param header 头部.
49
+ * @param option 选项.
50
+ */
51
+ postForm<T>(path: string, params?: any, form?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
52
+ /**
53
+ * 发送POST/FormData 请求.
54
+ * @param path 路径.
55
+ * @param params 参数.
56
+ * @param formData Form参数.
57
+ * @param header 头部.
58
+ * @param option 选项.
30
59
  */
31
- post<T>(path: string, params?: any, body?: any): Promise<JsonResponse<T>>;
60
+ postFormData<T>(path: string, params?: any, formData?: FormData, header?: any, option?: Option): Promise<JsonResponse<T>>;
32
61
  /**
33
62
  * 发送Get 请求.
34
63
  * @param path 路径.
35
64
  * @param params 参数.
65
+ * @param header 头部.
66
+ * @param option 选项.
36
67
  */
37
- get<T>(path: string, params?: any): Promise<JsonResponse<T>>;
68
+ get<T>(path: string, params?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
38
69
  /**
39
70
  * 缓存.
40
71
  * @param key 缓存Key.
41
72
  * @param callback 数据回执函数.
42
73
  * @param minute 缓存时间 (分钟).
74
+ * @param isCache 是否开启缓存.
43
75
  */
44
- cache<T>(key: string, callback: () => Promise<T>, minute: number): Promise<T>;
76
+ cache<T>(key: string, callback: () => Promise<T>, minute: number, isCache: boolean): Promise<T>;
45
77
  /**
46
78
  * 默认授权方式.
47
79
  */
package/api.js CHANGED
@@ -1,7 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const session_1 = require("./session");
4
- class Fetch {
1
+ import { Session } from './session';
2
+ export default class Fetch {
5
3
  constructor(pathPrefix) {
6
4
  this.pathPrefix = pathPrefix;
7
5
  }
@@ -10,8 +8,10 @@ class Fetch {
10
8
  * @param path 路径.
11
9
  * @param params 参数.
12
10
  * @param body 内容体.
11
+ * @param header 头部.
12
+ * @param option 选项.
13
13
  */
14
- async post(path, params, body) {
14
+ async post(path, params, body, header, option) {
15
15
  const paramQuery = new URLSearchParams();
16
16
  Object.keys(params || {})
17
17
  .forEach(key => {
@@ -21,9 +21,82 @@ class Fetch {
21
21
  method: 'POST',
22
22
  headers: {
23
23
  ...this.authorization(),
24
+ ...(header || {}),
24
25
  'Content-Type': 'application/json'
25
26
  },
26
- body: JSON.stringify(body || {})
27
+ body: JSON.stringify(body || {}),
28
+ signal: option?.signal || null
29
+ });
30
+ return this.responseJson(response);
31
+ }
32
+ /**
33
+ * 发送POST/Form 请求.
34
+ * @param path 路径.
35
+ * @param params 参数.
36
+ * @param form Form参数.
37
+ * @param header 头部.
38
+ * @param option 选项.
39
+ */
40
+ async postForm(path, params, form, header, option) {
41
+ const paramQuery = new URLSearchParams();
42
+ Object.keys(params || {})
43
+ .forEach(key => {
44
+ paramQuery.append(key, params[key]);
45
+ });
46
+ const formData = new FormData();
47
+ const appendFormData = (data, parentKey = '') => {
48
+ for (const key of data) {
49
+ const currentKey = parentKey ? `${parentKey}[${key}]` : key;
50
+ if (typeof data[key] === 'object' && data[key] !== null) {
51
+ appendFormData(data[key], currentKey);
52
+ }
53
+ else if (Array.isArray(data[key])) {
54
+ data[key].forEach((item, index) => {
55
+ const arrayKey = `${currentKey}[${index}]`;
56
+ appendFormData({ [arrayKey]: item });
57
+ });
58
+ }
59
+ else {
60
+ formData.append(currentKey, data[key]);
61
+ }
62
+ }
63
+ };
64
+ appendFormData(form);
65
+ const response = await fetch(this.pathPrefix + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
66
+ method: 'POST',
67
+ headers: {
68
+ ...this.authorization(),
69
+ ...(header || {}),
70
+ 'Content-Type': 'application/x-www-form-urlencoded'
71
+ },
72
+ body: formData,
73
+ signal: option?.signal || null
74
+ });
75
+ return this.responseJson(response);
76
+ }
77
+ /**
78
+ * 发送POST/FormData 请求.
79
+ * @param path 路径.
80
+ * @param params 参数.
81
+ * @param formData Form参数.
82
+ * @param header 头部.
83
+ * @param option 选项.
84
+ */
85
+ async postFormData(path, params, formData, header, option) {
86
+ const paramQuery = new URLSearchParams();
87
+ Object.keys(params || {})
88
+ .forEach(key => {
89
+ paramQuery.append(key, params[key]);
90
+ });
91
+ const response = await fetch(this.pathPrefix + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
92
+ method: 'POST',
93
+ headers: {
94
+ ...this.authorization(),
95
+ ...(header || {}),
96
+ 'Content-Type': 'multipart/form-data'
97
+ },
98
+ body: formData,
99
+ signal: option?.signal || null
27
100
  });
28
101
  return this.responseJson(response);
29
102
  }
@@ -31,8 +104,10 @@ class Fetch {
31
104
  * 发送Get 请求.
32
105
  * @param path 路径.
33
106
  * @param params 参数.
107
+ * @param header 头部.
108
+ * @param option 选项.
34
109
  */
35
- async get(path, params) {
110
+ async get(path, params, header, option) {
36
111
  const paramQuery = new URLSearchParams();
37
112
  Object.keys(params || {})
38
113
  .forEach(key => {
@@ -41,8 +116,10 @@ class Fetch {
41
116
  const response = await fetch(this.pathPrefix + path + '?' + paramQuery.toString(), {
42
117
  method: 'GET',
43
118
  headers: {
44
- ...this.authorization()
45
- }
119
+ ...this.authorization(),
120
+ ...(header || {})
121
+ },
122
+ signal: option?.signal || null
46
123
  });
47
124
  return this.responseJson(response);
48
125
  }
@@ -51,15 +128,18 @@ class Fetch {
51
128
  * @param key 缓存Key.
52
129
  * @param callback 数据回执函数.
53
130
  * @param minute 缓存时间 (分钟).
131
+ * @param isCache 是否开启缓存.
54
132
  */
55
- async cache(key, callback, minute) {
133
+ async cache(key, callback, minute, isCache) {
56
134
  const cacheKey = 'CACHE_' + key;
57
- const cacheValue = localStorage.getItem(cacheKey);
58
- if (cacheValue !== null) {
59
- const result = JSON.parse(cacheValue);
60
- const { expireTime } = result;
61
- if (expireTime !== undefined && expireTime >= new Date().getTime()) {
62
- return result;
135
+ if (isCache) {
136
+ const cacheValue = localStorage.getItem(cacheKey);
137
+ if (cacheValue !== null) {
138
+ const result = JSON.parse(cacheValue);
139
+ const { expireTime } = result;
140
+ if (expireTime !== undefined && expireTime >= new Date().getTime()) {
141
+ return result;
142
+ }
63
143
  }
64
144
  }
65
145
  const response = await callback();
@@ -73,7 +153,7 @@ class Fetch {
73
153
  * 默认授权方式.
74
154
  */
75
155
  authorization() {
76
- const bearer = session_1.Session.getBearer();
156
+ const bearer = Session.getBearer();
77
157
  if (bearer === undefined || bearer === '') {
78
158
  return {};
79
159
  }
@@ -114,4 +194,3 @@ class Fetch {
114
194
  });
115
195
  }
116
196
  }
117
- exports.default = Fetch;
package/elementUtils.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare class ElementUtils {
1
+ export default class ElementUtils {
2
2
  /**
3
3
  * 选择文件本地文件.
4
4
  */
package/elementUtils.js CHANGED
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ElementUtils = void 0;
4
- class ElementUtils {
1
+ export default class ElementUtils {
5
2
  /**
6
3
  * 选择文件本地文件.
7
4
  */
@@ -32,4 +29,3 @@ class ElementUtils {
32
29
  document.body.removeChild(link);
33
30
  }
34
31
  }
35
- exports.ElementUtils = ElementUtils;
package/package.json CHANGED
@@ -1,24 +1,24 @@
1
- {
2
- "name": "beer-network",
3
- "private": false,
4
- "version": "1.1.1-alpha.1",
5
- "type": "module",
6
- "scripts": {
7
- "publish": "cd dist && npm publish && cd -"
8
- },
9
- "dependencies": {
10
- },
11
- "devDependencies": {
12
- "typescript": "^5.0.2",
13
- "@babel/eslint-parser": "^7.22.15",
14
- "@typescript-eslint/eslint-plugin": "^6.0.0",
15
- "@typescript-eslint/parser": "^6.0.0",
16
- "eslint": "^8.45.0",
17
- "eslint-config-airbnb": "^19.0.4",
18
- "eslint-config-ali": "^14.0.2",
19
- "eslint-plugin-import": "^2.28.1",
20
- "eslint-plugin-react": "^7.33.2",
21
- "eslint-plugin-react-hooks": "^4.6.0",
22
- "eslint-plugin-react-refresh": "^0.4.3"
23
- }
24
- }
1
+ {
2
+ "name": "beer-network",
3
+ "private": false,
4
+ "version": "1.1.1-alpha.10",
5
+ "type": "module",
6
+ "scripts": {
7
+ "pub-w": "tsc && copy package.json .\\dist\\package.json && npm publish ./dist"
8
+ },
9
+ "dependencies": {
10
+ },
11
+ "devDependencies": {
12
+ "typescript": "^5.0.2",
13
+ "@babel/eslint-parser": "^7.22.15",
14
+ "@typescript-eslint/eslint-plugin": "^6.0.0",
15
+ "@typescript-eslint/parser": "^6.0.0",
16
+ "eslint": "^8.45.0",
17
+ "eslint-config-airbnb": "^19.0.4",
18
+ "eslint-config-ali": "^14.0.2",
19
+ "eslint-plugin-import": "^2.28.1",
20
+ "eslint-plugin-react": "^7.33.2",
21
+ "eslint-plugin-react-hooks": "^4.6.0",
22
+ "eslint-plugin-react-refresh": "^0.4.3"
23
+ }
24
+ }
package/session.js CHANGED
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Session = void 0;
4
- class Session {
1
+ export class Session {
5
2
  /**
6
3
  * 获取令牌.
7
4
  * <li>window.sessionKey 设置读取缓存用户信息的Key, 默认 login_user.</li>
@@ -9,12 +6,12 @@ class Session {
9
6
  * <li>window.accessToken 设置身份令牌, 直接返回.</li>
10
7
  */
11
8
  static getBearer() {
12
- if (window.accessToken != undefined && window.accessToken !== '') {
9
+ if (window.accessToken !== undefined && window.accessToken !== '') {
13
10
  return window.accessToken;
14
11
  }
15
12
  const key = window.sessionKey || 'login_user';
16
- const loginUser = JSON.parse(localStorage.getItem(key) || '{}');
17
- if (window.accessTokenKey != undefined && window.accessTokenKey !== '') {
13
+ const loginUser = JSON.parse(localStorage.getItem(key) || sessionStorage.getItem(key) || '{}');
14
+ if (window.accessTokenKey !== undefined && window.accessTokenKey !== '') {
18
15
  return loginUser[window.accessTokenKey];
19
16
  }
20
17
  return loginUser.accessToken || loginUser.token || undefined;
@@ -37,4 +34,3 @@ class Session {
37
34
  }
38
35
  }
39
36
  }
40
- exports.Session = Session;
package/utils.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ export default class Utils {
2
+ /**
3
+ * 获取URI 地址参数.
4
+ * @param key 参数名称.
5
+ */
6
+ static getParam(key: string): string | undefined;
7
+ /**
8
+ * 复制到剪切板.
9
+ * @param value 内容.
10
+ */
11
+ static copy(value: string): Promise<void>;
12
+ }
package/utils.js ADDED
@@ -0,0 +1,36 @@
1
+ export default class Utils {
2
+ /**
3
+ * 获取URI 地址参数.
4
+ * @param key 参数名称.
5
+ */
6
+ static getParam(key) {
7
+ const values = window.location.search.split('?');
8
+ if (values.length <= 1) {
9
+ return undefined;
10
+ }
11
+ const value = values[1];
12
+ if (value === undefined) {
13
+ return undefined;
14
+ }
15
+ for (const it of decodeURIComponent(value)
16
+ .split('&')) {
17
+ const args = it.trim()
18
+ .split('=');
19
+ if (args.length !== 2) {
20
+ continue;
21
+ }
22
+ if (args[0].toString()
23
+ .toLocaleLowerCase() === key.toLocaleLowerCase()) {
24
+ return args[1];
25
+ }
26
+ }
27
+ return undefined;
28
+ }
29
+ /**
30
+ * 复制到剪切板.
31
+ * @param value 内容.
32
+ */
33
+ static async copy(value) {
34
+ return navigator.clipboard.writeText(value);
35
+ }
36
+ }