beer-network 1.1.1-alpha.2 → 1.1.1-alpha.20

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,25 +16,144 @@ 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
+ /**
28
+ * Next 缓存.
29
+ */
30
+ next?: {
31
+ revalidate: number;
32
+ };
33
+ };
19
34
  export default class Fetch {
20
35
  /**
21
36
  * 请求地址.
22
37
  */
23
38
  private readonly pathPrefix;
24
39
  constructor(pathPrefix: string);
40
+ private sendBody;
41
+ private sendForm;
42
+ private sendFormData;
43
+ private send;
44
+ /**
45
+ * 发送POST 请求.
46
+ * @param path 路径.
47
+ * @param params 参数.
48
+ * @param header 头部.
49
+ * @param option 选项.
50
+ */
51
+ post<T>(path: string, params?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
25
52
  /**
26
53
  * 发送POST/JSON 请求.
27
54
  * @param path 路径.
28
55
  * @param params 参数.
29
56
  * @param body 内容体.
57
+ * @param header 头部.
58
+ * @param option 选项.
59
+ */
60
+ postBody<T>(path: string, params?: any, body?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
61
+ /**
62
+ * 发送POST/Form 请求.
63
+ * @param path 路径.
64
+ * @param params 参数.
65
+ * @param form Form参数.
66
+ * @param header 头部.
67
+ * @param option 选项.
68
+ */
69
+ postForm<T>(path: string, params?: any, form?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
70
+ /**
71
+ * 发送POST/FormData 请求.
72
+ * @param path 路径.
73
+ * @param params 参数.
74
+ * @param formData Form参数.
75
+ * @param header 头部.
76
+ * @param option 选项.
77
+ */
78
+ postFormData<T>(path: string, params?: any, formData?: FormData, header?: any, option?: Option): Promise<JsonResponse<T>>;
79
+ /**
80
+ * 发送PUT 请求.
81
+ * @param path 路径.
82
+ * @param params 参数.
83
+ * @param header 头部.
84
+ * @param option 选项.
85
+ */
86
+ put<T>(path: string, params?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
87
+ /**
88
+ * 发送PUT/JSON 请求.
89
+ * @param path 路径.
90
+ * @param params 参数.
91
+ * @param body 内容体.
92
+ * @param header 头部.
93
+ * @param option 选项.
94
+ */
95
+ putBody<T>(path: string, params?: any, body?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
96
+ /**
97
+ * 发送PUT/Form 请求.
98
+ * @param path 路径.
99
+ * @param params 参数.
100
+ * @param form Form参数.
101
+ * @param header 头部.
102
+ * @param option 选项.
103
+ */
104
+ putForm<T>(path: string, params?: any, form?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
105
+ /**
106
+ * 发送PUT/FormData 请求.
107
+ * @param path 路径.
108
+ * @param params 参数.
109
+ * @param formData Form参数.
110
+ * @param header 头部.
111
+ * @param option 选项.
112
+ */
113
+ putFormData<T>(path: string, params?: any, formData?: FormData, header?: any, option?: Option): Promise<JsonResponse<T>>;
114
+ /**
115
+ * 发送DELETE 请求.
116
+ * @param path 路径.
117
+ * @param params 参数.
118
+ * @param header 头部.
119
+ * @param option 选项.
120
+ */
121
+ delete<T>(path: string, params?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
122
+ /**
123
+ * 发送DELETE/JSON 请求.
124
+ * @param path 路径.
125
+ * @param params 参数.
126
+ * @param body 内容体.
127
+ * @param header 头部.
128
+ * @param option 选项.
129
+ */
130
+ deleteBody<T>(path: string, params?: any, body?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
131
+ /**
132
+ * 发送DELETE/Form 请求.
133
+ * @param path 路径.
134
+ * @param params 参数.
135
+ * @param form Form参数.
136
+ * @param header 头部.
137
+ * @param option 选项.
138
+ */
139
+ deleteForm<T>(path: string, params?: any, form?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
140
+ /**
141
+ * 发送DELETE/FormData 请求.
142
+ * @param path 路径.
143
+ * @param params 参数.
144
+ * @param formData Form参数.
145
+ * @param header 头部.
146
+ * @param option 选项.
30
147
  */
31
- post<T>(path: string, params?: any, body?: any): Promise<JsonResponse<T>>;
148
+ deleteFormData<T>(path: string, params?: any, formData?: FormData, header?: any, option?: Option): Promise<JsonResponse<T>>;
32
149
  /**
33
150
  * 发送Get 请求.
34
151
  * @param path 路径.
35
152
  * @param params 参数.
153
+ * @param header 头部.
154
+ * @param option 选项.
36
155
  */
37
- get<T>(path: string, params?: any): Promise<JsonResponse<T>>;
156
+ get<T>(path: string, params?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
38
157
  /**
39
158
  * 缓存.
40
159
  * @param key 缓存Key.
package/api.js CHANGED
@@ -1,51 +1,239 @@
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
  }
8
- /**
9
- * 发送POST/JSON 请求.
10
- * @param path 路径.
11
- * @param params 参数.
12
- * @param body 内容体.
13
- */
14
- async post(path, params, body) {
6
+ async sendBody(method, path, params, body, header, option) {
15
7
  const paramQuery = new URLSearchParams();
16
8
  Object.keys(params || {})
17
9
  .forEach(key => {
18
10
  paramQuery.append(key, params[key]);
19
11
  });
20
12
  const response = await fetch(this.pathPrefix + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
21
- method: 'POST',
13
+ method,
22
14
  headers: {
23
15
  ...this.authorization(),
16
+ ...(header || {}),
24
17
  'Content-Type': 'application/json'
25
18
  },
26
- body: JSON.stringify(body || {})
19
+ body: JSON.stringify(body || {}),
20
+ signal: option?.signal || null,
21
+ next: option?.next
27
22
  });
28
23
  return this.responseJson(response);
29
24
  }
30
- /**
31
- * 发送Get 请求.
32
- * @param path 路径.
33
- * @param params 参数.
34
- */
35
- async get(path, params) {
25
+ async sendForm(method, path, params, form, header, option) {
26
+ const paramQuery = new URLSearchParams();
27
+ Object.keys(params || {})
28
+ .forEach(key => {
29
+ paramQuery.append(key, params[key]);
30
+ });
31
+ const formData = new FormData();
32
+ const appendFormData = (data, parentKey = '') => {
33
+ for (const key of data) {
34
+ const currentKey = parentKey ? `${parentKey}[${key}]` : key;
35
+ if (typeof data[key] === 'object' && data[key] !== null) {
36
+ appendFormData(data[key], currentKey);
37
+ }
38
+ else if (Array.isArray(data[key])) {
39
+ data[key].forEach((item, index) => {
40
+ const arrayKey = `${currentKey}[${index}]`;
41
+ appendFormData({ [arrayKey]: item });
42
+ });
43
+ }
44
+ else {
45
+ formData.append(currentKey, data[key]);
46
+ }
47
+ }
48
+ };
49
+ appendFormData(form);
50
+ const response = await fetch(this.pathPrefix + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
51
+ method,
52
+ headers: {
53
+ ...this.authorization(),
54
+ ...(header || {}),
55
+ 'Content-Type': 'application/x-www-form-urlencoded'
56
+ },
57
+ body: formData,
58
+ signal: option?.signal || null,
59
+ next: option?.next
60
+ });
61
+ return this.responseJson(response);
62
+ }
63
+ async sendFormData(method, path, params, formData, header, option) {
64
+ const paramQuery = new URLSearchParams();
65
+ Object.keys(params || {})
66
+ .forEach(key => {
67
+ paramQuery.append(key, params[key]);
68
+ });
69
+ const response = await fetch(this.pathPrefix + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
70
+ method,
71
+ headers: {
72
+ ...this.authorization(),
73
+ ...(header || {})
74
+ },
75
+ body: formData,
76
+ signal: option?.signal || null,
77
+ next: option?.next
78
+ });
79
+ return this.responseJson(response);
80
+ }
81
+ async send(method, path, params, header, option) {
36
82
  const paramQuery = new URLSearchParams();
37
83
  Object.keys(params || {})
38
84
  .forEach(key => {
39
85
  paramQuery.append(key, params[key]);
40
86
  });
41
87
  const response = await fetch(this.pathPrefix + path + '?' + paramQuery.toString(), {
42
- method: 'GET',
88
+ method,
43
89
  headers: {
44
- ...this.authorization()
45
- }
90
+ ...this.authorization(),
91
+ ...(header || {})
92
+ },
93
+ signal: option?.signal || null,
94
+ next: option?.next
46
95
  });
47
96
  return this.responseJson(response);
48
97
  }
98
+ /**
99
+ * 发送POST 请求.
100
+ * @param path 路径.
101
+ * @param params 参数.
102
+ * @param header 头部.
103
+ * @param option 选项.
104
+ */
105
+ async post(path, params, header, option) {
106
+ return this.send('POST', path, params, header, option);
107
+ }
108
+ /**
109
+ * 发送POST/JSON 请求.
110
+ * @param path 路径.
111
+ * @param params 参数.
112
+ * @param body 内容体.
113
+ * @param header 头部.
114
+ * @param option 选项.
115
+ */
116
+ async postBody(path, params, body, header, option) {
117
+ return this.sendBody('POST', path, params, body, header, option);
118
+ }
119
+ /**
120
+ * 发送POST/Form 请求.
121
+ * @param path 路径.
122
+ * @param params 参数.
123
+ * @param form Form参数.
124
+ * @param header 头部.
125
+ * @param option 选项.
126
+ */
127
+ async postForm(path, params, form, header, option) {
128
+ return this.sendForm('POST', path, params, form, header, option);
129
+ }
130
+ /**
131
+ * 发送POST/FormData 请求.
132
+ * @param path 路径.
133
+ * @param params 参数.
134
+ * @param formData Form参数.
135
+ * @param header 头部.
136
+ * @param option 选项.
137
+ */
138
+ async postFormData(path, params, formData, header, option) {
139
+ return this.sendFormData('POST', path, params, formData, header, option);
140
+ }
141
+ /**
142
+ * 发送PUT 请求.
143
+ * @param path 路径.
144
+ * @param params 参数.
145
+ * @param header 头部.
146
+ * @param option 选项.
147
+ */
148
+ async put(path, params, header, option) {
149
+ return this.send('PUT', path, params, header, option);
150
+ }
151
+ /**
152
+ * 发送PUT/JSON 请求.
153
+ * @param path 路径.
154
+ * @param params 参数.
155
+ * @param body 内容体.
156
+ * @param header 头部.
157
+ * @param option 选项.
158
+ */
159
+ async putBody(path, params, body, header, option) {
160
+ return this.sendBody('PUT', path, params, body, header, option);
161
+ }
162
+ /**
163
+ * 发送PUT/Form 请求.
164
+ * @param path 路径.
165
+ * @param params 参数.
166
+ * @param form Form参数.
167
+ * @param header 头部.
168
+ * @param option 选项.
169
+ */
170
+ async putForm(path, params, form, header, option) {
171
+ return this.sendForm('PUT', path, params, form, header, option);
172
+ }
173
+ /**
174
+ * 发送PUT/FormData 请求.
175
+ * @param path 路径.
176
+ * @param params 参数.
177
+ * @param formData Form参数.
178
+ * @param header 头部.
179
+ * @param option 选项.
180
+ */
181
+ async putFormData(path, params, formData, header, option) {
182
+ return this.sendFormData('PUT', path, params, formData, header, option);
183
+ }
184
+ /**
185
+ * 发送DELETE 请求.
186
+ * @param path 路径.
187
+ * @param params 参数.
188
+ * @param header 头部.
189
+ * @param option 选项.
190
+ */
191
+ async delete(path, params, header, option) {
192
+ return this.send('DELETE', path, params, header, option);
193
+ }
194
+ /**
195
+ * 发送DELETE/JSON 请求.
196
+ * @param path 路径.
197
+ * @param params 参数.
198
+ * @param body 内容体.
199
+ * @param header 头部.
200
+ * @param option 选项.
201
+ */
202
+ async deleteBody(path, params, body, header, option) {
203
+ return this.sendBody('DELETE', path, params, body, header, option);
204
+ }
205
+ /**
206
+ * 发送DELETE/Form 请求.
207
+ * @param path 路径.
208
+ * @param params 参数.
209
+ * @param form Form参数.
210
+ * @param header 头部.
211
+ * @param option 选项.
212
+ */
213
+ async deleteForm(path, params, form, header, option) {
214
+ return this.sendForm('DELETE', path, params, form, header, option);
215
+ }
216
+ /**
217
+ * 发送DELETE/FormData 请求.
218
+ * @param path 路径.
219
+ * @param params 参数.
220
+ * @param formData Form参数.
221
+ * @param header 头部.
222
+ * @param option 选项.
223
+ */
224
+ async deleteFormData(path, params, formData, header, option) {
225
+ return this.sendFormData('DELETE', path, params, formData, header, option);
226
+ }
227
+ /**
228
+ * 发送Get 请求.
229
+ * @param path 路径.
230
+ * @param params 参数.
231
+ * @param header 头部.
232
+ * @param option 选项.
233
+ */
234
+ async get(path, params, header, option) {
235
+ return this.send('GET', path, params, header, option);
236
+ }
49
237
  /**
50
238
  * 缓存.
51
239
  * @param key 缓存Key.
@@ -76,7 +264,7 @@ class Fetch {
76
264
  * 默认授权方式.
77
265
  */
78
266
  authorization() {
79
- const bearer = session_1.Session.getBearer();
267
+ const bearer = Session.getBearer();
80
268
  if (bearer === undefined || bearer === '') {
81
269
  return {};
82
270
  }
@@ -88,8 +276,13 @@ class Fetch {
88
276
  */
89
277
  async responseJson(response) {
90
278
  const result = await response.json();
279
+ if (typeof window === 'undefined') {
280
+ return result;
281
+ }
282
+ const redirect = encodeURIComponent(window.location.href);
91
283
  if (result?.code === '401') {
92
- setTimeout(() => window.location.replace('/auth/login'), 500);
284
+ const link = (window?.authLogin || '/auth/login') + '?redirect=' + redirect;
285
+ setTimeout(() => window.location.replace(link), 500);
93
286
  return result;
94
287
  }
95
288
  return result;
@@ -117,4 +310,3 @@ class Fetch {
117
310
  });
118
311
  }
119
312
  }
120
- exports.default = Fetch;
package/elementUtils.d.ts CHANGED
@@ -1,12 +1,13 @@
1
- export declare class ElementUtils {
1
+ export default class ElementUtils {
2
2
  /**
3
3
  * 选择文件本地文件.
4
+ * @param multiple 是否多选.
4
5
  */
5
- static selectFile(): Promise<FileList | null>;
6
+ static selectFile(multiple?: boolean | undefined): Promise<FileList | null>;
6
7
  /**
7
8
  * 下载文件.
8
9
  * @param url 文件下载地址.
9
10
  * @param fileName 文件名称.
10
11
  */
11
- static download(url: string, fileName: string): void;
12
+ static download(url: string, fileName?: string): void;
12
13
  }
package/elementUtils.js CHANGED
@@ -1,14 +1,13 @@
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
  * 选择文件本地文件.
4
+ * @param multiple 是否多选.
7
5
  */
8
- static selectFile() {
6
+ static selectFile(multiple) {
9
7
  return new Promise((resolve) => {
10
8
  const fileInput = document.createElement('input');
11
9
  fileInput.type = 'file';
10
+ fileInput.multiple = multiple || false;
12
11
  fileInput.style.display = 'none';
13
12
  document.body.appendChild(fileInput);
14
13
  fileInput.addEventListener('change', () => {
@@ -26,10 +25,9 @@ class ElementUtils {
26
25
  static download(url, fileName) {
27
26
  const link = document.createElement('a');
28
27
  link.href = url;
29
- link.download = fileName;
28
+ link.download = fileName || '';
30
29
  document.body.appendChild(link);
31
30
  link.click();
32
31
  document.body.removeChild(link);
33
32
  }
34
33
  }
35
- exports.ElementUtils = ElementUtils;
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "beer-network",
3
3
  "private": false,
4
- "version": "1.1.1-alpha.2",
5
- "type": "module",
4
+ "version": "1.1.1-alpha.20",
6
5
  "scripts": {
7
- "pub-w": "copy package.json .\\dist\\package.json && npm publish ./dist"
6
+ "pub-w": "tsc && copy package.json .\\dist\\package.json && npm publish ./dist",
7
+ "pub-m": "tsc && cp package.json ./dist/package.json && npm publish ./dist"
8
8
  },
9
9
  "dependencies": {
10
10
  },
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,11 +6,14 @@ class Session {
9
6
  * <li>window.accessToken 设置身份令牌, 直接返回.</li>
10
7
  */
11
8
  static getBearer() {
9
+ if (typeof window === 'undefined') {
10
+ return undefined;
11
+ }
12
12
  if (window.accessToken !== undefined && window.accessToken !== '') {
13
13
  return window.accessToken;
14
14
  }
15
15
  const key = window.sessionKey || 'login_user';
16
- const loginUser = JSON.parse(localStorage.getItem(key) || '{}');
16
+ const loginUser = JSON.parse(localStorage.getItem(key) || sessionStorage.getItem(key) || '{}');
17
17
  if (window.accessTokenKey !== undefined && window.accessTokenKey !== '') {
18
18
  return loginUser[window.accessTokenKey];
19
19
  }
@@ -23,6 +23,9 @@ class Session {
23
23
  * 检查令牌是否存在.
24
24
  */
25
25
  static checkAccessTokenExist() {
26
+ if (typeof window === 'undefined') {
27
+ return false;
28
+ }
26
29
  const bearer = Session.getBearer();
27
30
  return !(bearer === undefined || bearer === '');
28
31
  }
@@ -31,10 +34,13 @@ class Session {
31
34
  * @param url 退出之后的地址.
32
35
  */
33
36
  static logout(url) {
34
- localStorage.clear();
37
+ if (typeof window === 'undefined') {
38
+ return;
39
+ }
40
+ localStorage?.clear();
41
+ sessionStorage?.clear();
35
42
  if (url !== undefined && url !== '') {
36
43
  window.location.href = url;
37
44
  }
38
45
  }
39
46
  }
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
+ }