beer-network 1.1.1-alpha.0 → 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.
@@ -16,32 +16,64 @@ 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
  * 请求地址.
22
31
  */
23
32
  private readonly pathPrefix;
24
- private constructor();
33
+ constructor(pathPrefix: string);
25
34
  /**
26
35
  * 发送POST/JSON 请求.
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 ADDED
@@ -0,0 +1,196 @@
1
+ import { Session } from './session';
2
+ export default class Fetch {
3
+ constructor(pathPrefix) {
4
+ this.pathPrefix = pathPrefix;
5
+ }
6
+ /**
7
+ * 发送POST/JSON 请求.
8
+ * @param path 路径.
9
+ * @param params 参数.
10
+ * @param body 内容体.
11
+ * @param header 头部.
12
+ * @param option 选项.
13
+ */
14
+ async post(path, params, body, header, option) {
15
+ const paramQuery = new URLSearchParams();
16
+ Object.keys(params || {})
17
+ .forEach(key => {
18
+ paramQuery.append(key, params[key]);
19
+ });
20
+ const response = await fetch(this.pathPrefix + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
21
+ method: 'POST',
22
+ headers: {
23
+ ...this.authorization(),
24
+ ...(header || {}),
25
+ 'Content-Type': 'application/json'
26
+ },
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
100
+ });
101
+ return this.responseJson(response);
102
+ }
103
+ /**
104
+ * 发送Get 请求.
105
+ * @param path 路径.
106
+ * @param params 参数.
107
+ * @param header 头部.
108
+ * @param option 选项.
109
+ */
110
+ async get(path, params, header, option) {
111
+ const paramQuery = new URLSearchParams();
112
+ Object.keys(params || {})
113
+ .forEach(key => {
114
+ paramQuery.append(key, params[key]);
115
+ });
116
+ const response = await fetch(this.pathPrefix + path + '?' + paramQuery.toString(), {
117
+ method: 'GET',
118
+ headers: {
119
+ ...this.authorization(),
120
+ ...(header || {})
121
+ },
122
+ signal: option?.signal || null
123
+ });
124
+ return this.responseJson(response);
125
+ }
126
+ /**
127
+ * 缓存.
128
+ * @param key 缓存Key.
129
+ * @param callback 数据回执函数.
130
+ * @param minute 缓存时间 (分钟).
131
+ * @param isCache 是否开启缓存.
132
+ */
133
+ async cache(key, callback, minute, isCache) {
134
+ const cacheKey = 'CACHE_' + key;
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
+ }
143
+ }
144
+ }
145
+ const response = await callback();
146
+ if (response.code === '0') {
147
+ response.expireTime = new Date().getTime() + 60000 * minute;
148
+ localStorage.setItem(cacheKey, JSON.stringify(response));
149
+ }
150
+ return response;
151
+ }
152
+ /**
153
+ * 默认授权方式.
154
+ */
155
+ authorization() {
156
+ const bearer = Session.getBearer();
157
+ if (bearer === undefined || bearer === '') {
158
+ return {};
159
+ }
160
+ return { Authorization: 'Bearer ' + bearer };
161
+ }
162
+ /**
163
+ * 默认响应数据解析方式.
164
+ * @param response 响应数据.
165
+ */
166
+ async responseJson(response) {
167
+ const result = await response.json();
168
+ if (result?.code === '401') {
169
+ setTimeout(() => window.location.replace('/auth/login'), 500);
170
+ return result;
171
+ }
172
+ return result;
173
+ }
174
+ pageParams(params) {
175
+ return {
176
+ ...params,
177
+ currentPage: params.current,
178
+ current: undefined
179
+ };
180
+ }
181
+ async pageTable(response) {
182
+ return {
183
+ data: response.data.records || response.data,
184
+ page: response.data.currentPage || 1,
185
+ total: response.data.totalSize || response.data.length,
186
+ success: response.success
187
+ };
188
+ }
189
+ async sleep(value) {
190
+ return new Promise((resolve) => {
191
+ setTimeout(() => {
192
+ resolve(undefined);
193
+ }, value);
194
+ });
195
+ }
196
+ }
@@ -1,4 +1,4 @@
1
- export declare class ElementUtils {
1
+ export default class ElementUtils {
2
2
  /**
3
3
  * 选择文件本地文件.
4
4
  */
@@ -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,27 +1,24 @@
1
- {
2
- "name": "beer-network",
3
- "private": false,
4
- "version": "1.1.1-alpha.0",
5
- "type": "module",
6
- "files": [
7
- "dist"
8
- ],
9
- "scripts": {
10
- "publish": "npm publish"
11
- },
12
- "dependencies": {
13
- },
14
- "devDependencies": {
15
- "typescript": "^5.0.2",
16
- "@babel/eslint-parser": "^7.22.15",
17
- "@typescript-eslint/eslint-plugin": "^6.0.0",
18
- "@typescript-eslint/parser": "^6.0.0",
19
- "eslint": "^8.45.0",
20
- "eslint-config-airbnb": "^19.0.4",
21
- "eslint-config-ali": "^14.0.2",
22
- "eslint-plugin-import": "^2.28.1",
23
- "eslint-plugin-react": "^7.33.2",
24
- "eslint-plugin-react-hooks": "^4.6.0",
25
- "eslint-plugin-react-refresh": "^0.4.3"
26
- }
27
- }
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
+ }
@@ -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
+ }
package/dist/api.js DELETED
@@ -1,117 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const session_1 = require("./session");
4
- class Fetch {
5
- constructor(pathPrefix) {
6
- this.pathPrefix = pathPrefix;
7
- }
8
- /**
9
- * 发送POST/JSON 请求.
10
- * @param path 路径.
11
- * @param params 参数.
12
- * @param body 内容体.
13
- */
14
- async post(path, params, body) {
15
- const paramQuery = new URLSearchParams();
16
- Object.keys(params || {})
17
- .forEach(key => {
18
- paramQuery.append(key, params[key]);
19
- });
20
- const response = await fetch(this.pathPrefix + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
21
- method: 'POST',
22
- headers: {
23
- ...this.authorization(),
24
- 'Content-Type': 'application/json'
25
- },
26
- body: JSON.stringify(body || {})
27
- });
28
- return this.responseJson(response);
29
- }
30
- /**
31
- * 发送Get 请求.
32
- * @param path 路径.
33
- * @param params 参数.
34
- */
35
- async get(path, params) {
36
- const paramQuery = new URLSearchParams();
37
- Object.keys(params || {})
38
- .forEach(key => {
39
- paramQuery.append(key, params[key]);
40
- });
41
- const response = await fetch(this.pathPrefix + path + '?' + paramQuery.toString(), {
42
- method: 'GET',
43
- headers: {
44
- ...this.authorization()
45
- }
46
- });
47
- return this.responseJson(response);
48
- }
49
- /**
50
- * 缓存.
51
- * @param key 缓存Key.
52
- * @param callback 数据回执函数.
53
- * @param minute 缓存时间 (分钟).
54
- */
55
- async cache(key, callback, minute) {
56
- 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;
63
- }
64
- }
65
- const response = await callback();
66
- if (response.code === '0') {
67
- response.expireTime = new Date().getTime() + 60000 * minute;
68
- localStorage.setItem(cacheKey, JSON.stringify(response));
69
- }
70
- return response;
71
- }
72
- /**
73
- * 默认授权方式.
74
- */
75
- authorization() {
76
- const bearer = session_1.Session.getBearer();
77
- if (bearer === undefined || bearer === '') {
78
- return {};
79
- }
80
- return { Authorization: 'Bearer ' + bearer };
81
- }
82
- /**
83
- * 默认响应数据解析方式.
84
- * @param response 响应数据.
85
- */
86
- async responseJson(response) {
87
- const result = await response.json();
88
- if (result?.code === '401') {
89
- setTimeout(() => window.location.replace('/auth/login'), 500);
90
- return result;
91
- }
92
- return result;
93
- }
94
- pageParams(params) {
95
- return {
96
- ...params,
97
- currentPage: params.current,
98
- current: undefined
99
- };
100
- }
101
- async pageTable(response) {
102
- return {
103
- data: response.data.records || response.data,
104
- page: response.data.currentPage || 1,
105
- total: response.data.totalSize || response.data.length,
106
- success: response.success
107
- };
108
- }
109
- async sleep(value) {
110
- return new Promise((resolve) => {
111
- setTimeout(() => {
112
- resolve(undefined);
113
- }, value);
114
- });
115
- }
116
- }
117
- exports.default = Fetch;
File without changes