ag-common 0.0.289 → 0.0.293

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.
@@ -49,6 +49,7 @@ export interface IRequestCommon {
49
49
  url: LocationSubset;
50
50
  lang: TLang;
51
51
  userAgent: string;
52
+ defaultHost: string;
52
53
  }
53
54
  export interface IStateCommon<TRequest extends IRequestCommon> extends IInitialStateCommon {
54
55
  request: TRequest;
@@ -64,24 +65,32 @@ export interface IStateCommon<TRequest extends IRequestCommon> extends IInitialS
64
65
  * @param param0
65
66
  * @returns
66
67
  */
67
- export declare const getClientOrServerReqHref: ({ href, query, forceServer, userAgent, }: {
68
- /**
69
- * will use window if possible
70
- */
71
- href?: string | undefined;
72
- /**
73
- * pass in query string params
74
- */
75
- query?: Record<string, string> | undefined;
68
+ export declare const getClientOrServerReqHref: ({ url: { href, query }, forceServer, userAgent, darkMode, defaultHost, }: {
69
+ url: {
70
+ /**
71
+ * parse querystring keyvalues
72
+ */
73
+ query?: Record<string, string> | undefined;
74
+ /**
75
+ * full url
76
+ */
77
+ href?: string | undefined;
78
+ };
76
79
  /**
77
80
  * if true, wont use window location. default false
78
81
  */
79
82
  forceServer?: boolean | undefined;
80
83
  /** will use navigator if possible */
81
84
  userAgent?: string | undefined;
85
+ /** will use window.matchMedia */
86
+ darkMode?: boolean | undefined;
87
+ defaultHost: string;
82
88
  }) => {
83
89
  url: LocationSubset;
84
90
  userAgent: string;
91
+ darkMode: boolean;
92
+ lang: TLang;
93
+ defaultHost: string;
85
94
  };
86
95
  /**
87
96
  * get server side parsed url
@@ -108,6 +117,9 @@ export declare const getServerReq: ({ defaultHost, pathname, query, headers, }:
108
117
  }) => {
109
118
  url: LocationSubset;
110
119
  userAgent: string;
120
+ darkMode: boolean;
121
+ lang: TLang;
122
+ defaultHost: string;
111
123
  };
112
124
  export interface INextCtx {
113
125
  req?: {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getServerReq = exports.getClientOrServerReqHref = void 0;
4
+ const i18n_1 = require("../../common/helpers/i18n");
4
5
  const string_1 = require("../../common/helpers/string");
5
6
  const object_1 = require("../../common/helpers/object");
6
7
  const url_1 = require("url");
@@ -18,22 +19,32 @@ const calculateServerHref = ({ host, pathname, }) => {
18
19
  href += host + pathname;
19
20
  return decodeURIComponent(href);
20
21
  };
22
+ const getRenderLanguage = ({ defaultHost, url, }) => {
23
+ var _a, _b, _c, _d;
24
+ const prefixReg = new RegExp(`(.*?).(local|${defaultHost.toLowerCase()})`, 'gim');
25
+ const host = (_c = (_b = (_a = url.host) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : '';
26
+ const prefix = host.trim().length !== 0 && ((_d = prefixReg.exec(host)) === null || _d === void 0 ? void 0 : _d[1]);
27
+ if (!prefix) {
28
+ return 'en';
29
+ }
30
+ return (0, i18n_1.getValidatedLang)(prefix);
31
+ };
21
32
  /**
22
33
  * get parsed url. use on client/SSR. defaults to window location if possible
23
34
  * @param param0
24
35
  * @returns
25
36
  */
26
- const getClientOrServerReqHref = ({ href, query, forceServer = false, userAgent, }) => {
27
- if (typeof window !== 'undefined' && !forceServer) {
28
- href = window.location.href;
29
- }
30
- if (typeof navigator !== 'undefined' && !forceServer) {
31
- if (navigator.userAgent) {
32
- userAgent = navigator.userAgent;
37
+ const getClientOrServerReqHref = ({ url: { href, query }, forceServer = false, userAgent, darkMode, defaultHost, }) => {
38
+ if (typeof window !== 'undefined') {
39
+ if (!forceServer) {
40
+ href = window.location.href;
33
41
  }
42
+ darkMode =
43
+ window.matchMedia &&
44
+ window.matchMedia('(prefers-color-scheme: dark)').matches;
34
45
  }
35
- if (!userAgent) {
36
- userAgent = '?';
46
+ if (typeof navigator !== 'undefined') {
47
+ userAgent = navigator.userAgent;
37
48
  }
38
49
  if (!href) {
39
50
  throw new Error('no href');
@@ -49,7 +60,13 @@ const getClientOrServerReqHref = ({ href, query, forceServer = false, userAgent,
49
60
  protocol: parsed.protocol || '',
50
61
  query: Object.assign(Object.assign({}, query), (0, string_1.stringToObject)(parsed.query || '', '=', '&')),
51
62
  };
52
- return { url, userAgent };
63
+ return {
64
+ url,
65
+ userAgent: userAgent !== null && userAgent !== void 0 ? userAgent : '?',
66
+ darkMode: darkMode !== null && darkMode !== void 0 ? darkMode : false,
67
+ lang: getRenderLanguage({ url, defaultHost }),
68
+ defaultHost,
69
+ };
53
70
  };
54
71
  exports.getClientOrServerReqHref = getClientOrServerReqHref;
55
72
  /**
@@ -66,10 +83,13 @@ const getServerReq = ({ defaultHost, pathname, query, headers, }) => {
66
83
  ? undefined
67
84
  : (0, object_1.castStringlyObject)(query);
68
85
  const ret = (0, exports.getClientOrServerReqHref)({
69
- href,
70
- query: parsedQuery,
86
+ url: {
87
+ href,
88
+ query: parsedQuery,
89
+ },
71
90
  forceServer: true,
72
91
  userAgent: (_a = headers['user-agent']) === null || _a === void 0 ? void 0 : _a.toLowerCase(),
92
+ defaultHost,
73
93
  });
74
94
  return ret;
75
95
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.289",
3
+ "version": "0.0.293",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",
@@ -32,7 +32,7 @@
32
32
  "styled-components": ">=5"
33
33
  },
34
34
  "devDependencies": {
35
- "@babel/core": "7.18.6",
35
+ "@babel/core": "7.18.9",
36
36
  "@storybook/addon-actions": "6.5.9",
37
37
  "@storybook/addon-docs": "6.5.9",
38
38
  "@storybook/addon-essentials": "6.5.9",
@@ -42,14 +42,14 @@
42
42
  "@storybook/react": "6.5.9",
43
43
  "@storybook/theming": "6.5.9",
44
44
  "@types/jsonwebtoken": "8.5.8",
45
- "@types/node": "18.0.3",
45
+ "@types/node": "18.0.6",
46
46
  "@types/react": "18.0.15",
47
47
  "@types/react-dom": "18.0.6",
48
48
  "@types/styled-components": "5.1.25",
49
- "@typescript-eslint/eslint-plugin": "5.30.6",
50
- "@typescript-eslint/parser": "5.30.6",
49
+ "@typescript-eslint/eslint-plugin": "5.30.7",
50
+ "@typescript-eslint/parser": "5.30.7",
51
51
  "cross-env": "7.0.3",
52
- "eslint": "8.19.0",
52
+ "eslint": "8.20.0",
53
53
  "eslint-config-airbnb-typescript": "17.0.0",
54
54
  "eslint-config-prettier": "8.5.0",
55
55
  "eslint-plugin-import": "2.26.0",