beer-network 1.2.8 → 1.2.9

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.
@@ -0,0 +1,13 @@
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(): HeadersLike;
12
+ export declare function getNodeCookies(): CookiesLike;
13
+ export {};
@@ -0,0 +1,43 @@
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 function getNodeHeaders() {
33
+ const mod = loadNextHeadersModule();
34
+ if (mod?.headers)
35
+ return mod.headers();
36
+ return emptyHeaders();
37
+ }
38
+ export function getNodeCookies() {
39
+ const mod = loadNextHeadersModule();
40
+ if (mod?.cookies)
41
+ return mod.cookies();
42
+ return emptyCookies();
43
+ }
package/api.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Session } from './session';
2
+ import { getNodeHeaders } from './NextHeadersLazy';
2
3
  export default class Fetch {
3
4
  constructor(pathPrefix) {
4
5
  this.isNextJS = undefined;
@@ -293,9 +294,7 @@ export default class Fetch {
293
294
  return undefined;
294
295
  }
295
296
  if (this.isNextJS === true) {
296
- // eslint-disable-next-line global-require
297
- const nextModule = require('next/headers');
298
- const nodeHeaders = await nextModule.headers();
297
+ const nodeHeaders = getNodeHeaders();
299
298
  return nodeHeaders.get('x-access-token')
300
299
  ?? nodeHeaders.get('access-token')
301
300
  ?? nodeHeaders.get('authorization')
@@ -305,7 +304,7 @@ export default class Fetch {
305
304
  }
306
305
  if (this.isNextJS === undefined) {
307
306
  try {
308
- await import('next/headers');
307
+ getNodeHeaders();
309
308
  this.isNextJS = true;
310
309
  }
311
310
  catch {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "beer-network",
3
3
  "private": false,
4
- "version": "1.2.8",
4
+ "version": "1.2.9",
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"