ag-common 0.0.147 → 0.0.151

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.
@@ -63,23 +63,31 @@ export interface IStateCommon<TRequest extends IRequestCommon> extends IInitialS
63
63
  cookieDocument: string | undefined;
64
64
  }
65
65
  /**
66
- * get parsed url.
66
+ * get parsed url. use on client/SSR. defaults to window location if possible
67
67
  * @param param0
68
68
  * @returns
69
69
  */
70
- export declare const getClientOrServerReqHref: ({ href, }: {
70
+ export declare const getClientOrServerReqHref: ({ href, query, forceServer, }: {
71
71
  /**
72
72
  * will use window if possible
73
73
  */
74
74
  href?: string | undefined;
75
+ /**
76
+ * pass in query string params
77
+ */
78
+ query?: Record<string, string> | undefined;
79
+ /**
80
+ * if true, wont use window location. default false
81
+ */
82
+ forceServer?: boolean | undefined;
75
83
  }) => LocationSubset;
76
84
  /**
77
- * get parsed url from nextjs server host,pathname
85
+ * get server side parsed url
78
86
  * @param param0 * @returns
79
87
  */
80
88
  export declare const getServerReq: ({ host, pathname, query, }: {
81
89
  /**
82
- * eg ctx?.req?.headers?.host
90
+ * eg ctx?.req?.headers?.host OR (user provided host eg test.com)
83
91
  */
84
92
  host: string;
85
93
  /**
@@ -91,3 +99,12 @@ export declare const getServerReq: ({ host, pathname, query, }: {
91
99
  */
92
100
  query: Record<string, string | string[] | undefined>;
93
101
  }) => LocationSubset;
102
+ export interface INextCtx {
103
+ req?: {
104
+ headers?: {
105
+ host?: string;
106
+ };
107
+ };
108
+ asPath?: string;
109
+ query: Record<string, string | string[] | undefined>;
110
+ }
@@ -19,12 +19,12 @@ const calculateServerHref = ({ host, pathname, }) => {
19
19
  return decodeURIComponent(href);
20
20
  };
21
21
  /**
22
- * get parsed url.
22
+ * get parsed url. use on client/SSR. defaults to window location if possible
23
23
  * @param param0
24
24
  * @returns
25
25
  */
26
- const getClientOrServerReqHref = ({ href, }) => {
27
- if (typeof window !== 'undefined') {
26
+ const getClientOrServerReqHref = ({ href, query, forceServer = false, }) => {
27
+ if (typeof window !== 'undefined' && !forceServer) {
28
28
  href = window.location.href;
29
29
  }
30
30
  if (!href) {
@@ -39,13 +39,13 @@ const getClientOrServerReqHref = ({ href, }) => {
39
39
  path: `${parsed.path}${parsed.hash || ''}`,
40
40
  pathname: parsed.pathname || '',
41
41
  protocol: parsed.protocol || '',
42
- query: (0, string_1.stringToObject)(parsed.query || '', '=', '&'),
42
+ query: Object.assign(Object.assign({}, query), (0, string_1.stringToObject)(parsed.query || '', '=', '&')),
43
43
  };
44
44
  return ret;
45
45
  };
46
46
  exports.getClientOrServerReqHref = getClientOrServerReqHref;
47
47
  /**
48
- * get parsed url from nextjs server host,pathname
48
+ * get server side parsed url
49
49
  * @param param0 * @returns
50
50
  */
51
51
  const getServerReq = ({ host, pathname, query, }) => {
@@ -53,10 +53,14 @@ const getServerReq = ({ host, pathname, query, }) => {
53
53
  host,
54
54
  pathname,
55
55
  });
56
- const ret = (0, exports.getClientOrServerReqHref)({ href });
57
- if (query && Object.keys(query).length > 0) {
58
- ret.query = Object.assign(Object.assign({}, ret.query), (0, object_1.castStringlyObject)(query));
59
- }
56
+ const parsedQuery = !query || Object.keys(query).length === 0
57
+ ? undefined
58
+ : (0, object_1.castStringlyObject)(query);
59
+ const ret = (0, exports.getClientOrServerReqHref)({
60
+ href,
61
+ query: parsedQuery,
62
+ forceServer: true,
63
+ });
60
64
  return ret;
61
65
  };
62
66
  exports.getServerReq = getServerReq;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.147",
3
+ "version": "0.0.151",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",