beer-network 1.2.3 → 1.2.5
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 +12 -1
- package/api.js +59 -9
- package/package.json +12 -3
package/api.d.ts
CHANGED
|
@@ -36,6 +36,9 @@ export default class Fetch {
|
|
|
36
36
|
* 请求地址.
|
|
37
37
|
*/
|
|
38
38
|
private readonly pathPrefix;
|
|
39
|
+
private readonly isNode;
|
|
40
|
+
private isNextJS;
|
|
41
|
+
private nodeHeaders;
|
|
39
42
|
constructor(pathPrefix?: string);
|
|
40
43
|
private sendBody;
|
|
41
44
|
private sendForm;
|
|
@@ -165,7 +168,15 @@ export default class Fetch {
|
|
|
165
168
|
/**
|
|
166
169
|
* 默认授权方式.
|
|
167
170
|
*/
|
|
168
|
-
authorization(): {}
|
|
171
|
+
authorization(): Promise<{}>;
|
|
172
|
+
/**
|
|
173
|
+
* 尝试获取头.
|
|
174
|
+
*/
|
|
175
|
+
private tryAuthorization;
|
|
176
|
+
/**
|
|
177
|
+
* 默认请求头.
|
|
178
|
+
*/
|
|
179
|
+
headers(): Promise<{}>;
|
|
169
180
|
/**
|
|
170
181
|
* 默认响应数据解析方式.
|
|
171
182
|
* @param response 响应数据.
|
package/api.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { Session } from './session';
|
|
2
2
|
export default class Fetch {
|
|
3
3
|
constructor(pathPrefix) {
|
|
4
|
+
this.isNextJS = undefined;
|
|
5
|
+
this.nodeHeaders = undefined;
|
|
4
6
|
this.pathPrefix = pathPrefix || '';
|
|
7
|
+
this.isNode = typeof process !== 'undefined' && !!process.versions?.node;
|
|
5
8
|
}
|
|
6
9
|
async sendBody(method, path, params, body, header, option) {
|
|
7
10
|
const paramQuery = new URLSearchParams();
|
|
@@ -12,7 +15,8 @@ export default class Fetch {
|
|
|
12
15
|
const response = await fetch(this.getPathPrefix() + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
|
|
13
16
|
method,
|
|
14
17
|
headers: {
|
|
15
|
-
...this.authorization(),
|
|
18
|
+
...(await this.authorization()),
|
|
19
|
+
...(await this.headers()),
|
|
16
20
|
...(header || {}),
|
|
17
21
|
'Content-Type': 'application/json'
|
|
18
22
|
},
|
|
@@ -50,7 +54,8 @@ export default class Fetch {
|
|
|
50
54
|
const response = await fetch(this.getPathPrefix() + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
|
|
51
55
|
method,
|
|
52
56
|
headers: {
|
|
53
|
-
...this.authorization(),
|
|
57
|
+
...(await this.authorization()),
|
|
58
|
+
...(await this.headers()),
|
|
54
59
|
...(header || {}),
|
|
55
60
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
56
61
|
},
|
|
@@ -69,7 +74,8 @@ export default class Fetch {
|
|
|
69
74
|
const response = await fetch(this.getPathPrefix() + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
|
|
70
75
|
method,
|
|
71
76
|
headers: {
|
|
72
|
-
...this.authorization(),
|
|
77
|
+
...(await this.authorization()),
|
|
78
|
+
...(await this.headers()),
|
|
73
79
|
...(header || {})
|
|
74
80
|
},
|
|
75
81
|
body: formData,
|
|
@@ -87,7 +93,8 @@ export default class Fetch {
|
|
|
87
93
|
const response = await fetch(this.getPathPrefix() + path + '?' + paramQuery.toString(), {
|
|
88
94
|
method,
|
|
89
95
|
headers: {
|
|
90
|
-
...this.authorization(),
|
|
96
|
+
...(await this.authorization()),
|
|
97
|
+
...(await this.headers()),
|
|
91
98
|
...(header || {})
|
|
92
99
|
},
|
|
93
100
|
signal: option?.signal || null,
|
|
@@ -263,13 +270,48 @@ export default class Fetch {
|
|
|
263
270
|
/**
|
|
264
271
|
* 默认授权方式.
|
|
265
272
|
*/
|
|
266
|
-
authorization() {
|
|
273
|
+
async authorization() {
|
|
274
|
+
if (this.isNode) {
|
|
275
|
+
// 非浏览器
|
|
276
|
+
const bearer = await this.tryAuthorization();
|
|
277
|
+
if (bearer === undefined || bearer === '') {
|
|
278
|
+
return {};
|
|
279
|
+
}
|
|
280
|
+
return { Authorization: 'Bearer ' + bearer };
|
|
281
|
+
}
|
|
282
|
+
// 浏览器
|
|
267
283
|
const bearer = Session.getBearer();
|
|
268
284
|
if (bearer === undefined || bearer === '') {
|
|
269
285
|
return {};
|
|
270
286
|
}
|
|
271
287
|
return { Authorization: 'Bearer ' + bearer };
|
|
272
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* 尝试获取头.
|
|
291
|
+
*/
|
|
292
|
+
async tryAuthorization(index = 0) {
|
|
293
|
+
if (index > 2) {
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
if (this.isNextJS && this.nodeHeaders !== undefined) {
|
|
297
|
+
return this.nodeHeaders.get('AccessToken') ?? this.nodeHeaders.get('Authorization') ?? '';
|
|
298
|
+
}
|
|
299
|
+
try {
|
|
300
|
+
const nextModule = await import('next/headers');
|
|
301
|
+
this.isNextJS = true;
|
|
302
|
+
this.nodeHeaders = await nextModule.headers();
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
this.isNextJS = false;
|
|
306
|
+
}
|
|
307
|
+
return this.tryAuthorization(index + 1);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* 默认请求头.
|
|
311
|
+
*/
|
|
312
|
+
async headers() {
|
|
313
|
+
return {};
|
|
314
|
+
}
|
|
273
315
|
/**
|
|
274
316
|
* 默认响应数据解析方式.
|
|
275
317
|
* @param response 响应数据.
|
|
@@ -301,11 +343,19 @@ export default class Fetch {
|
|
|
301
343
|
};
|
|
302
344
|
}
|
|
303
345
|
async pageTable(response) {
|
|
346
|
+
if (response.success) {
|
|
347
|
+
return {
|
|
348
|
+
data: response.data.records || response.data,
|
|
349
|
+
page: response.data.currentPage || 1,
|
|
350
|
+
total: response.data.totalSize || response.data.length,
|
|
351
|
+
success: true
|
|
352
|
+
};
|
|
353
|
+
}
|
|
304
354
|
return {
|
|
305
|
-
data:
|
|
306
|
-
page:
|
|
307
|
-
total:
|
|
308
|
-
success:
|
|
355
|
+
data: [],
|
|
356
|
+
page: 1,
|
|
357
|
+
total: 0,
|
|
358
|
+
success: true
|
|
309
359
|
};
|
|
310
360
|
}
|
|
311
361
|
async sleep(value) {
|
package/package.json
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "beer-network",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.5",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"pub-w": "tsc && copy package.json .\\dist\\package.json && npm publish ./dist",
|
|
7
7
|
"pub-m": "tsc && cp package.json ./dist/package.json && npm publish ./dist"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
},
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"next": "*"
|
|
13
|
+
},
|
|
14
|
+
"peerDependenciesMeta": {
|
|
15
|
+
"next": {
|
|
16
|
+
"optional": true
|
|
17
|
+
}
|
|
18
|
+
},
|
|
11
19
|
"devDependencies": {
|
|
12
|
-
"
|
|
20
|
+
"next": "^16.1.3",
|
|
13
21
|
"@babel/eslint-parser": "^7.22.15",
|
|
14
22
|
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
15
23
|
"@typescript-eslint/parser": "^6.0.0",
|
|
@@ -19,6 +27,7 @@
|
|
|
19
27
|
"eslint-plugin-import": "^2.28.1",
|
|
20
28
|
"eslint-plugin-react": "^7.33.2",
|
|
21
29
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
22
|
-
"eslint-plugin-react-refresh": "^0.4.3"
|
|
30
|
+
"eslint-plugin-react-refresh": "^0.4.3",
|
|
31
|
+
"typescript": "^5.0.2"
|
|
23
32
|
}
|
|
24
33
|
}
|