ag-common 0.0.287 → 0.0.291

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.
@@ -43,15 +43,12 @@ export interface CacheItem<T> {
43
43
  }
44
44
  export interface IInitialStateCommon {
45
45
  openApiCache?: CacheItem<any>[];
46
- headerTitle?: string;
47
- seed?: number;
48
46
  }
49
47
  export interface IRequestCommon {
50
48
  darkMode: boolean;
51
49
  url: LocationSubset;
52
- headerTitle?: string;
53
- seed?: number;
54
50
  lang: TLang;
51
+ userAgent: string;
55
52
  }
56
53
  export interface IStateCommon<TRequest extends IRequestCommon> extends IInitialStateCommon {
57
54
  request: TRequest;
@@ -67,29 +64,41 @@ export interface IStateCommon<TRequest extends IRequestCommon> extends IInitialS
67
64
  * @param param0
68
65
  * @returns
69
66
  */
70
- export declare const getClientOrServerReqHref: ({ href, query, forceServer, }: {
71
- /**
72
- * will use window if possible
73
- */
74
- href?: string | undefined;
75
- /**
76
- * pass in query string params
77
- */
78
- query?: Record<string, string> | undefined;
67
+ export declare const getClientOrServerReqHref: ({ url: { href, query }, forceServer, userAgent, }: {
68
+ url: {
69
+ /**
70
+ * parse querystring keyvalues
71
+ */
72
+ query?: Record<string, string> | undefined;
73
+ /**
74
+ * full url
75
+ */
76
+ href?: string | undefined;
77
+ };
79
78
  /**
80
79
  * if true, wont use window location. default false
81
80
  */
82
81
  forceServer?: boolean | undefined;
83
- }) => LocationSubset;
82
+ /** will use navigator if possible */
83
+ userAgent?: string | undefined;
84
+ }) => {
85
+ url: LocationSubset;
86
+ userAgent: string;
87
+ };
84
88
  /**
85
89
  * get server side parsed url
86
90
  * @param param0 * @returns
87
91
  */
88
- export declare const getServerReq: ({ host, pathname, query, }: {
92
+ export declare const getServerReq: ({ defaultHost, pathname, query, headers, }: {
89
93
  /**
90
- * eg ctx?.req?.headers?.host OR (user provided host eg test.com)
94
+ * eg ctx?.req?.headers || {}
91
95
  */
92
- host: string;
96
+ headers: {
97
+ host?: string;
98
+ 'user-agent'?: string;
99
+ };
100
+ /** what to use if host is not available on headers */
101
+ defaultHost: string;
93
102
  /**
94
103
  * eg ctx.asPath || '/'
95
104
  */
@@ -98,7 +107,10 @@ export declare const getServerReq: ({ host, pathname, query, }: {
98
107
  * eg ctx.query
99
108
  */
100
109
  query: Record<string, string | string[] | undefined>;
101
- }) => LocationSubset;
110
+ }) => {
111
+ url: LocationSubset;
112
+ userAgent: string;
113
+ };
102
114
  export interface INextCtx {
103
115
  req?: {
104
116
  headers?: {
@@ -23,15 +23,23 @@ const calculateServerHref = ({ host, pathname, }) => {
23
23
  * @param param0
24
24
  * @returns
25
25
  */
26
- const getClientOrServerReqHref = ({ href, query, forceServer = false, }) => {
26
+ const getClientOrServerReqHref = ({ url: { href, query }, forceServer = false, userAgent, }) => {
27
27
  if (typeof window !== 'undefined' && !forceServer) {
28
28
  href = window.location.href;
29
29
  }
30
+ if (typeof navigator !== 'undefined' && !forceServer) {
31
+ if (navigator.userAgent) {
32
+ userAgent = navigator.userAgent;
33
+ }
34
+ }
35
+ if (!userAgent) {
36
+ userAgent = '?';
37
+ }
30
38
  if (!href) {
31
39
  throw new Error('no href');
32
40
  }
33
41
  const parsed = (0, url_1.parse)(href);
34
- const ret = {
42
+ const url = {
35
43
  hash: parsed.hash || '',
36
44
  host: parsed.host || '',
37
45
  origin: `${parsed.protocol}//${parsed.host}`,
@@ -41,25 +49,29 @@ const getClientOrServerReqHref = ({ href, query, forceServer = false, }) => {
41
49
  protocol: parsed.protocol || '',
42
50
  query: Object.assign(Object.assign({}, query), (0, string_1.stringToObject)(parsed.query || '', '=', '&')),
43
51
  };
44
- return ret;
52
+ return { url, userAgent };
45
53
  };
46
54
  exports.getClientOrServerReqHref = getClientOrServerReqHref;
47
55
  /**
48
56
  * get server side parsed url
49
57
  * @param param0 * @returns
50
58
  */
51
- const getServerReq = ({ host, pathname, query, }) => {
59
+ const getServerReq = ({ defaultHost, pathname, query, headers, }) => {
60
+ var _a;
52
61
  const href = calculateServerHref({
53
- host,
62
+ host: headers.host || defaultHost,
54
63
  pathname,
55
64
  });
56
65
  const parsedQuery = !query || Object.keys(query).length === 0
57
66
  ? undefined
58
67
  : (0, object_1.castStringlyObject)(query);
59
68
  const ret = (0, exports.getClientOrServerReqHref)({
60
- href,
61
- query: parsedQuery,
69
+ url: {
70
+ href,
71
+ query: parsedQuery,
72
+ },
62
73
  forceServer: true,
74
+ userAgent: (_a = headers['user-agent']) === null || _a === void 0 ? void 0 : _a.toLowerCase(),
63
75
  });
64
76
  return ret;
65
77
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.287",
3
+ "version": "0.0.291",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",