ag-common 0.0.145 → 0.0.149
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,45 @@ 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
|
+
/**
|
|
72
|
+
* will use window if possible
|
|
73
|
+
*/
|
|
71
74
|
href?: string | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* pass in query string params
|
|
77
|
+
*/
|
|
78
|
+
query?: Record<string, string> | undefined;
|
|
72
79
|
}) => LocationSubset;
|
|
73
|
-
|
|
74
|
-
|
|
80
|
+
/**
|
|
81
|
+
* get server side parsed url
|
|
82
|
+
* @param param0 * @returns
|
|
83
|
+
*/
|
|
84
|
+
export declare const getServerReq: ({ host, pathname, query, }: {
|
|
85
|
+
/**
|
|
86
|
+
* eg ctx?.req?.headers?.host
|
|
87
|
+
*/
|
|
75
88
|
host: string;
|
|
89
|
+
/**
|
|
90
|
+
* eg ctx.asPath || '/'
|
|
91
|
+
*/
|
|
92
|
+
pathname: string;
|
|
93
|
+
/**
|
|
94
|
+
* eg ctx.query
|
|
95
|
+
*/
|
|
96
|
+
query: Record<string, string | string[] | undefined>;
|
|
76
97
|
}) => LocationSubset;
|
|
98
|
+
export interface INextCtx {
|
|
99
|
+
req?: {
|
|
100
|
+
headers?: {
|
|
101
|
+
host?: string;
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
asPath?: string;
|
|
105
|
+
query: Record<string, string | string[] | undefined>;
|
|
106
|
+
}
|
|
107
|
+
export declare const getServerReqNextJs: (ctx: INextCtx) => LocationSubset | null;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getServerReq = exports.getClientOrServerReqHref = void 0;
|
|
3
|
+
exports.getServerReqNextJs = 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.
|
|
22
|
+
* get parsed url. use on client/SSR. defaults to window location if possible
|
|
22
23
|
* @param param0
|
|
23
24
|
* @returns
|
|
24
25
|
*/
|
|
25
|
-
const getClientOrServerReqHref = ({ href }) => {
|
|
26
|
+
const getClientOrServerReqHref = ({ href, query, }) => {
|
|
26
27
|
if (typeof window !== 'undefined') {
|
|
27
28
|
href = window.location.href;
|
|
28
29
|
}
|
|
@@ -38,16 +39,34 @@ const getClientOrServerReqHref = ({ href }) => {
|
|
|
38
39
|
path: `${parsed.path}${parsed.hash || ''}`,
|
|
39
40
|
pathname: parsed.pathname || '',
|
|
40
41
|
protocol: parsed.protocol || '',
|
|
41
|
-
query: (0, string_1.stringToObject)(parsed.query || '', '=', '&'),
|
|
42
|
+
query: Object.assign(Object.assign({}, query), (0, string_1.stringToObject)(parsed.query || '', '=', '&')),
|
|
42
43
|
};
|
|
43
44
|
return ret;
|
|
44
45
|
};
|
|
45
46
|
exports.getClientOrServerReqHref = getClientOrServerReqHref;
|
|
46
|
-
|
|
47
|
+
/**
|
|
48
|
+
* get server side parsed url
|
|
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
|
-
|
|
56
|
+
const parsedQuery = !query || Object.keys(query).length === 0
|
|
57
|
+
? undefined
|
|
58
|
+
: (0, object_1.castStringlyObject)(query);
|
|
59
|
+
const ret = (0, exports.getClientOrServerReqHref)({ href, query: parsedQuery });
|
|
60
|
+
return ret;
|
|
52
61
|
};
|
|
53
62
|
exports.getServerReq = getServerReq;
|
|
63
|
+
const getServerReqNextJs = (ctx) => {
|
|
64
|
+
var _a, _b;
|
|
65
|
+
const host = (_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;
|
|
66
|
+
const pathname = ctx === null || ctx === void 0 ? void 0 : ctx.asPath;
|
|
67
|
+
if (!host || !pathname) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
return (0, exports.getServerReq)({ host, pathname, query: ctx.query });
|
|
71
|
+
};
|
|
72
|
+
exports.getServerReqNextJs = getServerReqNextJs;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ag-common",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.149",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"author": "Andrei Gec <@andreigec> (https://gec.dev/)",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"aws-cdk-lib": "2.x",
|
|
17
|
-
"aws-sdk": "2.
|
|
18
|
-
"axios": "0.
|
|
17
|
+
"aws-sdk": "2.1073.0",
|
|
18
|
+
"axios": "0.26.0",
|
|
19
19
|
"constructs": "10.x",
|
|
20
20
|
"jsonwebtoken": "8.5.1",
|
|
21
21
|
"jwks-rsa": "2.0.5",
|