ag-common 0.0.146 → 0.0.147

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.
@@ -36,3 +36,17 @@ export declare function paramsToObject(entries: any): Record<string, string>;
36
36
  * @returns
37
37
  */
38
38
  export declare function objectToString(obj: Record<string, string>, joinKeyValue: string, joinKeys: string): string;
39
+ /**
40
+ * run func over values in object. can be used to cast value type string[]|string to string
41
+ * @param orig
42
+ * @param castF
43
+ * @returns
44
+ */
45
+ export declare const castObject: <TIn, TOut>(orig: Record<string, TIn>, castF: (t: TIn) => TOut) => Record<string, TOut>;
46
+ export declare const removeUndefValuesFromObject: <T>(orig: Record<string, T>) => Record<string, T>;
47
+ /**
48
+ * cast Record<string,string[]|string> to Record<string,string>. can be used for querystring params
49
+ * @param orig
50
+ * @returns
51
+ */
52
+ export declare const castStringlyObject: (orig: Record<string, string | string[] | undefined>) => Record<string, string>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.objectToString = exports.paramsToObject = exports.objectAlphaSort = exports.objectToArray = exports.getObjectKeysAsNumber = exports.objectKeysToLowerCase = exports.isJson = exports.tryJsonParse = void 0;
3
+ exports.castStringlyObject = exports.removeUndefValuesFromObject = exports.castObject = exports.objectToString = exports.paramsToObject = exports.objectAlphaSort = exports.objectToArray = exports.getObjectKeysAsNumber = exports.objectKeysToLowerCase = exports.isJson = exports.tryJsonParse = void 0;
4
4
  const _1 = require(".");
5
5
  const tryJsonParse = (str, defaultValue) => {
6
6
  if (!str) {
@@ -114,3 +114,42 @@ function objectToString(obj, joinKeyValue, joinKeys) {
114
114
  return ret;
115
115
  }
116
116
  exports.objectToString = objectToString;
117
+ /**
118
+ * run func over values in object. can be used to cast value type string[]|string to string
119
+ * @param orig
120
+ * @param castF
121
+ * @returns
122
+ */
123
+ const castObject = (orig, castF) => {
124
+ const ret = {};
125
+ Object.entries(orig).forEach(([k, v]) => {
126
+ ret[k] = castF(v);
127
+ });
128
+ return ret;
129
+ };
130
+ exports.castObject = castObject;
131
+ const removeUndefValuesFromObject = (orig) => {
132
+ const ret = {};
133
+ Object.entries(orig).forEach(([k, v]) => {
134
+ if (v !== null && v !== undefined) {
135
+ ret[k] = v;
136
+ }
137
+ });
138
+ return ret;
139
+ };
140
+ exports.removeUndefValuesFromObject = removeUndefValuesFromObject;
141
+ /**
142
+ * cast Record<string,string[]|string> to Record<string,string>. can be used for querystring params
143
+ * @param orig
144
+ * @returns
145
+ */
146
+ const castStringlyObject = (orig) => {
147
+ const noundef = (0, exports.removeUndefValuesFromObject)(orig);
148
+ return (0, exports.castObject)(noundef, (s) => {
149
+ if (Array.isArray(s)) {
150
+ return s.join(',');
151
+ }
152
+ return s;
153
+ });
154
+ };
155
+ exports.castStringlyObject = castStringlyObject;
@@ -63,14 +63,31 @@ export interface IStateCommon<TRequest extends IRequestCommon> extends IInitialS
63
63
  cookieDocument: string | undefined;
64
64
  }
65
65
  /**
66
- * get parsed url. will use window if possible
66
+ * get parsed url.
67
67
  * @param param0
68
68
  * @returns
69
69
  */
70
- export declare const getClientOrServerReqHref: ({ href }: {
70
+ export declare const getClientOrServerReqHref: ({ href, }: {
71
+ /**
72
+ * will use window if possible
73
+ */
71
74
  href?: string | undefined;
72
75
  }) => LocationSubset;
73
- export declare const getServerReq: ({ host, pathname, }: {
74
- pathname: string;
76
+ /**
77
+ * get parsed url from nextjs server host,pathname
78
+ * @param param0 * @returns
79
+ */
80
+ export declare const getServerReq: ({ host, pathname, query, }: {
81
+ /**
82
+ * eg ctx?.req?.headers?.host
83
+ */
75
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>;
76
93
  }) => LocationSubset;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getServerReq = exports.getClientOrServerReqHref = void 0;
4
4
  const url_1 = require("url");
5
5
  const string_1 = require("../../common/helpers/string");
6
+ const object_1 = require("../../common/helpers/object");
6
7
  const calculateServerHref = ({ host, pathname, }) => {
7
8
  if (!host) {
8
9
  return undefined;
@@ -18,11 +19,11 @@ const calculateServerHref = ({ host, pathname, }) => {
18
19
  return decodeURIComponent(href);
19
20
  };
20
21
  /**
21
- * get parsed url. will use window if possible
22
+ * get parsed url.
22
23
  * @param param0
23
24
  * @returns
24
25
  */
25
- const getClientOrServerReqHref = ({ href }) => {
26
+ const getClientOrServerReqHref = ({ href, }) => {
26
27
  if (typeof window !== 'undefined') {
27
28
  href = window.location.href;
28
29
  }
@@ -43,11 +44,19 @@ const getClientOrServerReqHref = ({ href }) => {
43
44
  return ret;
44
45
  };
45
46
  exports.getClientOrServerReqHref = getClientOrServerReqHref;
46
- const getServerReq = ({ host, pathname, }) => {
47
+ /**
48
+ * get parsed url from nextjs server host,pathname
49
+ * @param param0 * @returns
50
+ */
51
+ const getServerReq = ({ host, pathname, query, }) => {
47
52
  const href = calculateServerHref({
48
53
  host,
49
54
  pathname,
50
55
  });
51
- return (0, exports.getClientOrServerReqHref)({ href });
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
+ return ret;
52
61
  };
53
62
  exports.getServerReq = getServerReq;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.146",
3
+ "version": "0.0.147",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",