ag-common 0.0.147 → 0.0.148

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,31 +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, }: {
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;
75
79
  }) => LocationSubset;
80
+ export interface INextCtx {
81
+ req?: {
82
+ headers?: {
83
+ host?: string;
84
+ };
85
+ };
86
+ asPath?: string;
87
+ query: Record<string, string | string[] | undefined>;
88
+ }
76
89
  /**
77
- * get parsed url from nextjs server host,pathname
90
+ * get parsed url from nextjs server host/pathname/query
78
91
  * @param param0 * @returns
79
92
  */
80
- export declare const getServerReq: ({ host, pathname, query, }: {
81
- /**
82
- * eg ctx?.req?.headers?.host
83
- */
84
- host: string;
85
- /**
86
- * eg ctx.asPath || '/'
87
- */
88
- pathname: string;
89
- /**
90
- * eg ctx.query
91
- */
92
- query: Record<string, string | string[] | undefined>;
93
- }) => LocationSubset;
93
+ export declare const getServerReq: (ctx: INextCtx) => LocationSubset | null;
@@ -19,11 +19,11 @@ 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, }) => {
26
+ const getClientOrServerReqHref = ({ href, query, }) => {
27
27
  if (typeof window !== 'undefined') {
28
28
  href = window.location.href;
29
29
  }
@@ -39,24 +39,28 @@ 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 parsed url from nextjs server host/pathname/query
49
49
  * @param param0 * @returns
50
50
  */
51
- const getServerReq = ({ host, pathname, query, }) => {
51
+ const getServerReq = (ctx) => {
52
+ var _a, _b;
53
+ if (!((_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.req) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b.host) || !(ctx === null || ctx === void 0 ? void 0 : ctx.asPath)) {
54
+ return null;
55
+ }
52
56
  const href = calculateServerHref({
53
- host,
54
- pathname,
57
+ host: ctx.req.headers.host,
58
+ pathname: ctx.asPath,
55
59
  });
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
- }
60
+ const parsedQuery = !ctx.query || Object.keys(ctx.query).length === 0
61
+ ? undefined
62
+ : (0, object_1.castStringlyObject)(ctx.query);
63
+ const ret = (0, exports.getClientOrServerReqHref)({ href, query: parsedQuery });
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.148",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",