beer-network 1.3.3 → 1.4.1

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
@@ -1,3 +1,4 @@
1
+ import type { CacheManager } from './cache-manager';
1
2
  /**
2
3
  * Json 响应通用实体.
3
4
  */
@@ -24,12 +25,13 @@ export type Option = {
24
25
  * 中止信号量.
25
26
  */
26
27
  signal?: AbortSignal;
27
- /**
28
- * NextJS 缓存.
29
- */
30
- next?: {
31
- revalidate: number;
32
- };
28
+ };
29
+ type RequestHeader = Record<string, string>;
30
+ type RequestParams = Record<string, string>;
31
+ type PageTableData<T> = T[] | {
32
+ records?: T[];
33
+ currentPage?: number;
34
+ totalSize?: number;
33
35
  };
34
36
  export default class Fetch {
35
37
  /**
@@ -37,14 +39,12 @@ export default class Fetch {
37
39
  */
38
40
  private readonly pathPrefix;
39
41
  /**
40
- * 是否是NodeJS 环境.
42
+ * 缓存管理器.
41
43
  */
42
- private readonly isNode;
43
- /**
44
- * 是否支持 NextJs 框架.
45
- */
46
- private isNextJS;
44
+ private readonly cacheManager;
47
45
  constructor(pathPrefix?: string);
46
+ private appendUrlSearchParams;
47
+ private preprocessing;
48
48
  private sendBody;
49
49
  private sendForm;
50
50
  private sendFormData;
@@ -56,7 +56,7 @@ export default class Fetch {
56
56
  * @param header 头部.
57
57
  * @param option 选项.
58
58
  */
59
- post<T>(path: string, params?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
59
+ post<T>(path: string, params?: RequestParams, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
60
60
  /**
61
61
  * 发送POST/JSON 请求.
62
62
  * @param path 路径.
@@ -65,7 +65,7 @@ export default class Fetch {
65
65
  * @param header 头部.
66
66
  * @param option 选项.
67
67
  */
68
- postBody<T>(path: string, params?: any, body?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
68
+ postBody<T>(path: string, params?: RequestParams, body?: unknown, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
69
69
  /**
70
70
  * 发送POST/Form 请求.
71
71
  * @param path 路径.
@@ -74,7 +74,7 @@ export default class Fetch {
74
74
  * @param header 头部.
75
75
  * @param option 选项.
76
76
  */
77
- postForm<T>(path: string, params?: any, form?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
77
+ postForm<T>(path: string, params?: RequestParams, form?: RequestParams, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
78
78
  /**
79
79
  * 发送POST/FormData 请求.
80
80
  * @param path 路径.
@@ -83,7 +83,7 @@ export default class Fetch {
83
83
  * @param header 头部.
84
84
  * @param option 选项.
85
85
  */
86
- postFormData<T>(path: string, params?: any, formData?: FormData, header?: any, option?: Option): Promise<JsonResponse<T>>;
86
+ postFormData<T>(path: string, params?: RequestParams, formData?: FormData, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
87
87
  /**
88
88
  * 发送PUT 请求.
89
89
  * @param path 路径.
@@ -91,7 +91,7 @@ export default class Fetch {
91
91
  * @param header 头部.
92
92
  * @param option 选项.
93
93
  */
94
- put<T>(path: string, params?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
94
+ put<T>(path: string, params?: RequestParams, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
95
95
  /**
96
96
  * 发送PUT/JSON 请求.
97
97
  * @param path 路径.
@@ -100,7 +100,7 @@ export default class Fetch {
100
100
  * @param header 头部.
101
101
  * @param option 选项.
102
102
  */
103
- putBody<T>(path: string, params?: any, body?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
103
+ putBody<T>(path: string, params?: RequestParams, body?: unknown, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
104
104
  /**
105
105
  * 发送PUT/Form 请求.
106
106
  * @param path 路径.
@@ -109,7 +109,7 @@ export default class Fetch {
109
109
  * @param header 头部.
110
110
  * @param option 选项.
111
111
  */
112
- putForm<T>(path: string, params?: any, form?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
112
+ putForm<T>(path: string, params?: RequestParams, form?: RequestParams, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
113
113
  /**
114
114
  * 发送PUT/FormData 请求.
115
115
  * @param path 路径.
@@ -118,7 +118,7 @@ export default class Fetch {
118
118
  * @param header 头部.
119
119
  * @param option 选项.
120
120
  */
121
- putFormData<T>(path: string, params?: any, formData?: FormData, header?: any, option?: Option): Promise<JsonResponse<T>>;
121
+ putFormData<T>(path: string, params?: RequestParams, formData?: FormData, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
122
122
  /**
123
123
  * 发送DELETE 请求.
124
124
  * @param path 路径.
@@ -126,7 +126,7 @@ export default class Fetch {
126
126
  * @param header 头部.
127
127
  * @param option 选项.
128
128
  */
129
- delete<T>(path: string, params?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
129
+ delete<T>(path: string, params?: RequestParams, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
130
130
  /**
131
131
  * 发送DELETE/JSON 请求.
132
132
  * @param path 路径.
@@ -135,7 +135,7 @@ export default class Fetch {
135
135
  * @param header 头部.
136
136
  * @param option 选项.
137
137
  */
138
- deleteBody<T>(path: string, params?: any, body?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
138
+ deleteBody<T>(path: string, params?: RequestParams, body?: unknown, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
139
139
  /**
140
140
  * 发送DELETE/Form 请求.
141
141
  * @param path 路径.
@@ -144,7 +144,7 @@ export default class Fetch {
144
144
  * @param header 头部.
145
145
  * @param option 选项.
146
146
  */
147
- deleteForm<T>(path: string, params?: any, form?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
147
+ deleteForm<T>(path: string, params?: RequestParams, form?: RequestParams, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
148
148
  /**
149
149
  * 发送DELETE/FormData 请求.
150
150
  * @param path 路径.
@@ -153,7 +153,7 @@ export default class Fetch {
153
153
  * @param header 头部.
154
154
  * @param option 选项.
155
155
  */
156
- deleteFormData<T>(path: string, params?: any, formData?: FormData, header?: any, option?: Option): Promise<JsonResponse<T>>;
156
+ deleteFormData<T>(path: string, params?: RequestParams, formData?: FormData, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
157
157
  /**
158
158
  * 发送Get 请求.
159
159
  * @param path 路径.
@@ -161,7 +161,7 @@ export default class Fetch {
161
161
  * @param header 头部.
162
162
  * @param option 选项.
163
163
  */
164
- get<T>(path: string, params?: any, header?: any, option?: Option): Promise<JsonResponse<T>>;
164
+ get<T>(path: string, params?: RequestParams, header?: RequestHeader, option?: Option): Promise<JsonResponse<T>>;
165
165
  /**
166
166
  * 缓存.
167
167
  * @param key 缓存Key.
@@ -173,15 +173,15 @@ export default class Fetch {
173
173
  /**
174
174
  * 默认授权方式.
175
175
  */
176
- authorization(): Promise<{}>;
176
+ authorization(): Promise<RequestHeader>;
177
177
  /**
178
- * 尝试获取头.
178
+ * 获取缓存管理器.
179
179
  */
180
- private tryAuthorization;
180
+ createCacheManager(): CacheManager | undefined;
181
181
  /**
182
182
  * 默认请求头.
183
183
  */
184
- headers(): Promise<{}>;
184
+ headers(): Promise<Record<string, string>>;
185
185
  /**
186
186
  * 默认响应数据解析方式.
187
187
  * @param response 响应数据.
@@ -191,7 +191,25 @@ export default class Fetch {
191
191
  * 获取路径前缀.
192
192
  */
193
193
  getPathPrefix(): string;
194
- pageParams(params: any): any;
195
- pageTable<T>(response: JsonResponse<any>): Promise<Partial<RequestData<T>>>;
194
+ /**
195
+ * 分页参数.
196
+ * @param params 入参.
197
+ */
198
+ pageParams<T extends {
199
+ current?: unknown;
200
+ }>(params: T): T & {
201
+ currentPage: T['current'];
202
+ current: undefined;
203
+ };
204
+ /**
205
+ * 分页参数.
206
+ * @param response 响应.
207
+ */
208
+ pageTable<T>(response: JsonResponse<PageTableData<T>>): Promise<Partial<RequestData<T>>>;
209
+ /**
210
+ * 休眠.
211
+ * @param value 时间戳.
212
+ */
196
213
  sleep(value: number): Promise<unknown>;
197
214
  }
215
+ export {};
package/api.js CHANGED
@@ -1,133 +1,134 @@
1
1
  import Session from './session';
2
- import { getNodeHeaders } from './next-headers';
3
2
  export default class Fetch {
4
3
  /**
5
4
  * 请求地址.
6
5
  */
7
6
  pathPrefix;
8
7
  /**
9
- * 是否是NodeJS 环境.
8
+ * 缓存管理器.
10
9
  */
11
- isNode;
12
- /**
13
- * 是否支持 NextJs 框架.
14
- */
15
- isNextJS = undefined;
10
+ cacheManager;
16
11
  constructor(pathPrefix) {
17
12
  this.pathPrefix = pathPrefix || '';
18
- this.isNode = typeof process !== 'undefined' && !!process.versions?.node;
13
+ this.cacheManager = this.createCacheManager();
19
14
  }
20
- async sendBody(method, path, params, body, header, option) {
21
- const paramQuery = new URLSearchParams();
22
- Object.keys(params || {})
23
- .forEach(key => {
24
- const value = params[key];
25
- if (value === undefined || value === null || value === '') {
15
+ appendUrlSearchParams(searchParams, params) {
16
+ Object.keys(params ?? {}).forEach(key => {
17
+ const value = params?.[key];
18
+ if (value === '') {
26
19
  return;
27
20
  }
28
- paramQuery.append(key, value);
21
+ searchParams.append(key, value ?? '');
29
22
  });
30
- const response = await fetch(this.getPathPrefix() + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
23
+ }
24
+ async preprocessing(method, path, params, header, body) {
25
+ const requestHeader = header ?? {};
26
+ const paramQuery = new URLSearchParams();
27
+ this.appendUrlSearchParams(paramQuery, params);
28
+ const paramQueryString = paramQuery.toString();
29
+ const { cacheManager } = this;
30
+ if (cacheManager === undefined) {
31
+ return {
32
+ paramQueryString,
33
+ requestHeader,
34
+ cacheManager,
35
+ cacheResponse: undefined
36
+ };
37
+ }
38
+ const cacheResponse = (await cacheManager.get(method, path, paramQueryString, requestHeader, body)) ?? undefined;
39
+ return {
40
+ paramQueryString,
41
+ requestHeader,
42
+ cacheManager,
43
+ cacheResponse
44
+ };
45
+ }
46
+ async sendBody(method, path, params, body, header, option) {
47
+ const { paramQueryString, requestHeader, cacheManager, cacheResponse } = await this.preprocessing(method, path, params, header, body);
48
+ if (cacheResponse !== undefined) {
49
+ return cacheResponse;
50
+ }
51
+ const response = await fetch(this.getPathPrefix() + path + (paramQueryString === '' ? '' : '?') + paramQueryString, {
31
52
  method,
32
53
  headers: {
33
54
  ...(await this.authorization()),
34
55
  ...(await this.headers()),
35
- ...(header || {}),
56
+ ...requestHeader,
36
57
  'Content-Type': 'application/json'
37
58
  },
38
- body: JSON.stringify(body || {}),
39
- signal: option?.signal || null,
40
- next: option?.next
59
+ body: JSON.stringify(body ?? {}),
60
+ signal: option?.signal ?? null
41
61
  });
42
- return this.responseJson(response);
62
+ const result = await this.responseJson(response);
63
+ if (cacheManager !== undefined) {
64
+ await cacheManager.put(method, path, paramQueryString, requestHeader, body, result);
65
+ }
66
+ return result;
43
67
  }
44
68
  async sendForm(method, path, params, form, header, option) {
45
- const paramQuery = new URLSearchParams();
46
- Object.keys(params || {})
47
- .forEach(key => {
48
- const value = params[key];
49
- if (value === undefined || value === null || value === '') {
50
- return;
51
- }
52
- paramQuery.append(key, value);
53
- });
54
- const formData = new FormData();
55
- const appendFormData = (data, parentKey = '') => {
56
- for (const key of data) {
57
- const currentKey = parentKey ? `${parentKey}[${key}]` : key;
58
- if (typeof data[key] === 'object' && data[key] !== null) {
59
- appendFormData(data[key], currentKey);
60
- }
61
- else if (Array.isArray(data[key])) {
62
- data[key].forEach((item, index) => {
63
- const arrayKey = `${currentKey}[${index}]`;
64
- appendFormData({ [arrayKey]: item });
65
- });
66
- }
67
- else {
68
- formData.append(currentKey, data[key]);
69
- }
70
- }
71
- };
72
- appendFormData(form);
73
- const response = await fetch(this.getPathPrefix() + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
69
+ const { paramQueryString, requestHeader, cacheManager, cacheResponse } = await this.preprocessing(method, path, params, header);
70
+ if (cacheResponse !== undefined) {
71
+ return cacheResponse;
72
+ }
73
+ const formParams = new URLSearchParams();
74
+ this.appendUrlSearchParams(formParams, form);
75
+ const response = await fetch(this.getPathPrefix() + path + (paramQueryString === '' ? '' : '?') + paramQueryString, {
74
76
  method,
75
77
  headers: {
76
78
  ...(await this.authorization()),
77
79
  ...(await this.headers()),
78
- ...(header || {}),
80
+ ...requestHeader,
79
81
  'Content-Type': 'application/x-www-form-urlencoded'
80
82
  },
81
- body: formData,
82
- signal: option?.signal || null,
83
- next: option?.next
83
+ body: formParams,
84
+ signal: option?.signal ?? null
84
85
  });
85
- return this.responseJson(response);
86
+ const result = await this.responseJson(response);
87
+ if (cacheManager !== undefined) {
88
+ await cacheManager.put(method, path, paramQueryString, requestHeader, undefined, result);
89
+ }
90
+ return result;
86
91
  }
87
92
  async sendFormData(method, path, params, formData, header, option) {
88
- const paramQuery = new URLSearchParams();
89
- Object.keys(params || {})
90
- .forEach(key => {
91
- const value = params[key];
92
- if (value === undefined || value === null || value === '') {
93
- return;
94
- }
95
- paramQuery.append(key, value);
96
- });
97
- const response = await fetch(this.getPathPrefix() + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
93
+ const { paramQueryString, requestHeader, cacheManager, cacheResponse } = await this.preprocessing(method, path, params, header);
94
+ if (cacheResponse !== undefined) {
95
+ return cacheResponse;
96
+ }
97
+ const response = await fetch(this.getPathPrefix() + path + (paramQueryString === '' ? '' : '?') + paramQueryString, {
98
98
  method,
99
99
  headers: {
100
100
  ...(await this.authorization()),
101
101
  ...(await this.headers()),
102
- ...(header || {})
102
+ ...requestHeader
103
103
  },
104
104
  body: formData,
105
- signal: option?.signal || null,
106
- next: option?.next
105
+ signal: option?.signal ?? null
107
106
  });
108
- return this.responseJson(response);
107
+ const result = await this.responseJson(response);
108
+ if (cacheManager !== undefined) {
109
+ await cacheManager.put(method, path, paramQueryString, requestHeader, undefined, result);
110
+ }
111
+ return result;
109
112
  }
110
113
  async send(method, path, params, header, option) {
111
- const paramQuery = new URLSearchParams();
112
- Object.keys(params || {})
113
- .forEach(key => {
114
- const value = params[key];
115
- if (value === undefined || value === null || value === '') {
116
- return;
117
- }
118
- paramQuery.append(key, value);
119
- });
120
- const response = await fetch(this.getPathPrefix() + path + '?' + paramQuery.toString(), {
114
+ const { paramQueryString, requestHeader, cacheManager, cacheResponse } = await this.preprocessing(method, path, params, header);
115
+ if (cacheResponse !== undefined) {
116
+ return cacheResponse;
117
+ }
118
+ const response = await fetch(this.getPathPrefix() + path + '?' + paramQueryString, {
121
119
  method,
122
120
  headers: {
123
121
  ...(await this.authorization()),
124
122
  ...(await this.headers()),
125
- ...(header || {})
123
+ ...requestHeader
126
124
  },
127
- signal: option?.signal || null,
128
- next: option?.next
125
+ signal: option?.signal ?? null
129
126
  });
130
- return this.responseJson(response);
127
+ const result = await this.responseJson(response);
128
+ if (cacheManager !== undefined) {
129
+ await cacheManager.put(method, path, paramQueryString, requestHeader, undefined, result);
130
+ }
131
+ return result;
131
132
  }
132
133
  /**
133
134
  * 发送POST 请求.
@@ -288,9 +289,10 @@ export default class Fetch {
288
289
  }
289
290
  }
290
291
  const response = await callback();
291
- if (response.code === '0') {
292
- response.expireTime = new Date().getTime() + 60000 * minute;
293
- localStorage.setItem(cacheKey, JSON.stringify(response));
292
+ const cacheResponse = response;
293
+ if (cacheResponse.code === '0') {
294
+ cacheResponse.expireTime = new Date().getTime() + 60000 * minute;
295
+ localStorage.setItem(cacheKey, JSON.stringify(cacheResponse));
294
296
  }
295
297
  return response;
296
298
  }
@@ -298,14 +300,6 @@ export default class Fetch {
298
300
  * 默认授权方式.
299
301
  */
300
302
  async authorization() {
301
- if (this.isNode) {
302
- // 非浏览器
303
- const bearer = await this.tryAuthorization();
304
- if (bearer === undefined || bearer === '') {
305
- return {};
306
- }
307
- return { Authorization: 'Bearer ' + bearer };
308
- }
309
303
  // 浏览器
310
304
  const bearer = Session.getBearer();
311
305
  if (bearer === undefined || bearer === '') {
@@ -314,31 +308,10 @@ export default class Fetch {
314
308
  return { Authorization: 'Bearer ' + bearer };
315
309
  }
316
310
  /**
317
- * 尝试获取头.
311
+ * 获取缓存管理器.
318
312
  */
319
- async tryAuthorization(index = 0) {
320
- if (index > 2) {
321
- return undefined;
322
- }
323
- if (this.isNextJS === true) {
324
- const nodeHeaders = await getNodeHeaders();
325
- return nodeHeaders.get('x-access-token')
326
- ?? nodeHeaders.get('access-token')
327
- ?? nodeHeaders.get('authorization')
328
- ?? nodeHeaders.get('AccessToken')
329
- ?? nodeHeaders.get('Authorization')
330
- ?? '';
331
- }
332
- if (this.isNextJS === undefined) {
333
- try {
334
- await getNodeHeaders();
335
- this.isNextJS = true;
336
- }
337
- catch {
338
- this.isNextJS = false;
339
- }
340
- }
341
- return this.tryAuthorization(index + 1);
313
+ createCacheManager() {
314
+ return undefined;
342
315
  }
343
316
  /**
344
317
  * 默认请求头.
@@ -357,7 +330,7 @@ export default class Fetch {
357
330
  }
358
331
  const redirect = encodeURIComponent(window.location.href);
359
332
  if (result?.code === '401') {
360
- const link = (window?.authLogin || '/auth/login') + '?redirect=' + redirect;
333
+ const link = (window.authLogin || '/auth/login') + '?redirect=' + redirect;
361
334
  setTimeout(() => window.location.replace(link), 500);
362
335
  return result;
363
336
  }
@@ -369,6 +342,10 @@ export default class Fetch {
369
342
  getPathPrefix() {
370
343
  return this.pathPrefix || '';
371
344
  }
345
+ /**
346
+ * 分页参数.
347
+ * @param params 入参.
348
+ */
372
349
  pageParams(params) {
373
350
  return {
374
351
  ...params,
@@ -376,12 +353,18 @@ export default class Fetch {
376
353
  current: undefined
377
354
  };
378
355
  }
356
+ /**
357
+ * 分页参数.
358
+ * @param response 响应.
359
+ */
379
360
  async pageTable(response) {
380
361
  if (response.success) {
362
+ const responseData = response.data;
363
+ const records = Array.isArray(responseData) ? responseData : responseData.records || [];
381
364
  return {
382
- data: response.data.records || response.data,
383
- page: response.data.currentPage || 1,
384
- total: response.data.totalSize || response.data.length,
365
+ data: records,
366
+ page: Array.isArray(responseData) ? 1 : responseData.currentPage || 1,
367
+ total: Array.isArray(responseData) ? responseData.length : responseData.totalSize || records.length,
385
368
  success: true
386
369
  };
387
370
  }
@@ -392,6 +375,10 @@ export default class Fetch {
392
375
  success: true
393
376
  };
394
377
  }
378
+ /**
379
+ * 休眠.
380
+ * @param value 时间戳.
381
+ */
395
382
  async sleep(value) {
396
383
  return new Promise((resolve) => {
397
384
  setTimeout(() => {
@@ -0,0 +1,23 @@
1
+ import type { JsonResponse } from './api';
2
+ export interface CacheManager {
3
+ /**
4
+ * 读取缓存.
5
+ * @param method 方法.
6
+ * @param path 路径.
7
+ * @param params 参数.
8
+ * @param header 头部参数.
9
+ * @param body 请求体.
10
+ * @returns 缓存数据.
11
+ */
12
+ get<T>(method: string, path: string, params: string, header: Record<string, string>, body?: unknown): Promise<JsonResponse<T>>;
13
+ /**
14
+ * 写入缓存.
15
+ * @param method 方法.
16
+ * @param path 路径.
17
+ * @param params 参数.
18
+ * @param header 头部参数.
19
+ * @param body 请求体.
20
+ * @param data 缓存数据.
21
+ */
22
+ put<T>(method: string, path: string, params: string, header: Record<string, string>, body: unknown, data: JsonResponse<T>): Promise<JsonResponse<T>>;
23
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "beer-network",
3
3
  "private": false,
4
- "version": "1.3.3",
4
+ "version": "1.4.1",
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"
package/next-headers.d.ts DELETED
@@ -1,13 +0,0 @@
1
- type HeadersLike = {
2
- get(name: string): string | null;
3
- };
4
- type CookiesLike = {
5
- get(name: string): {
6
- value: string;
7
- } | undefined;
8
- getAll?: () => Array<any>;
9
- has?: (name: string) => boolean;
10
- };
11
- export declare function getNodeHeaders(): Promise<HeadersLike>;
12
- export declare function getNodeCookies(): CookiesLike;
13
- export {};
package/next-headers.js DELETED
@@ -1,45 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- /* eslint-disable no-eval */
3
- function loadNextHeadersModule() {
4
- try {
5
- const req = eval('require');
6
- return req('next/headers');
7
- }
8
- catch {
9
- return null;
10
- }
11
- }
12
- function emptyHeaders() {
13
- return {
14
- get() {
15
- return null;
16
- }
17
- };
18
- }
19
- function emptyCookies() {
20
- return {
21
- get() {
22
- return undefined;
23
- },
24
- getAll() {
25
- return [];
26
- },
27
- has() {
28
- return false;
29
- }
30
- };
31
- }
32
- export async function getNodeHeaders() {
33
- const mod = loadNextHeadersModule();
34
- if (mod?.headers) {
35
- return mod.headers();
36
- }
37
- return emptyHeaders();
38
- }
39
- export function getNodeCookies() {
40
- const mod = loadNextHeadersModule();
41
- if (mod?.cookies) {
42
- return mod.cookies();
43
- }
44
- return emptyCookies();
45
- }