ag-common 0.0.6 → 0.0.10
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.
- package/LICENSE.md +7 -0
- package/README.md +22 -0
- package/dist/api/helpers/dynamo.js +11 -6
- package/dist/api/helpers/validations.js +2 -2
- package/dist/common/helpers/array.js +2 -4
- package/dist/common/helpers/string.d.ts +9 -0
- package/dist/common/helpers/string.js +17 -2
- package/dist/ui/components/LoginButton/index.js +1 -1
- package/dist/ui/helpers/axiosHelper.d.ts +11 -0
- package/dist/ui/helpers/axiosHelper.js +82 -0
- package/dist/ui/helpers/cookie.js +1 -1
- package/dist/ui/helpers/index.d.ts +1 -0
- package/dist/ui/helpers/index.js +1 -0
- package/package.json +5 -2
package/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright 2021 Andrei Gec
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# ag-common
|
|
2
|
+
|
|
3
|
+
> Various reusable libaries for: common ts/js / api (aws cognito, aws-cdk) / ui (react)
|
|
4
|
+
|
|
5
|
+
[![NPM Version][npm-image]][npm-url]
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i -S ag-common
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Please raise any issues or requests you might have [here](https://github.com/andreigec/ag-common/issues)
|
|
16
|
+
|
|
17
|
+
## License
|
|
18
|
+
|
|
19
|
+
[ISC](./LICENSE.md)
|
|
20
|
+
|
|
21
|
+
[npm-image]: https://img.shields.io/npm/v/ag-common.svg
|
|
22
|
+
[npm-url]: https://www.npmjs.com/package/ag-common
|
|
@@ -28,7 +28,7 @@ const putDynamo = (item, tableName) => __awaiter(void 0, void 0, void 0, functio
|
|
|
28
28
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
29
|
Item: item,
|
|
30
30
|
};
|
|
31
|
-
(0, log_1.info)(`running dynamo put=${JSON.stringify(params)}`);
|
|
31
|
+
(0, log_1.info)(`running dynamo put=${JSON.stringify(params, null, 2)}`);
|
|
32
32
|
// write the todo to the database
|
|
33
33
|
const put = yield exports.dynamoDb.put(params).promise();
|
|
34
34
|
if (put.$response.error &&
|
|
@@ -138,7 +138,7 @@ const getItemDynamo = ({ tableName, pkName, pkValue, }) => __awaiter(void 0, voi
|
|
|
138
138
|
try {
|
|
139
139
|
const res = yield exports.dynamoDb.get(params).promise();
|
|
140
140
|
const ret = res.Item;
|
|
141
|
-
(0, log_1.debug)(`got dynamo getitem=${JSON.stringify(params)}`);
|
|
141
|
+
(0, log_1.debug)(`got dynamo getitem=${JSON.stringify(params, null, 2)}`);
|
|
142
142
|
return ret;
|
|
143
143
|
}
|
|
144
144
|
catch (e) {
|
|
@@ -158,14 +158,19 @@ const getItemsDynamo = ({ tableName, items, }) => __awaiter(void 0, void 0, void
|
|
|
158
158
|
},
|
|
159
159
|
},
|
|
160
160
|
};
|
|
161
|
+
const dbRaw = new aws_sdk_1.default.DynamoDB({ apiVersion: '2012-10-08' });
|
|
161
162
|
try {
|
|
162
|
-
const res = yield
|
|
163
|
-
(0, log_1.debug)(`got dynamo getitems=${JSON.stringify(res)}`);
|
|
164
|
-
let ret = ((_f = (_e = res.Responses) === null || _e === void 0 ? void 0 : _e[tableName]) === null || _f === void 0 ? void 0 : _f.map((s) => s)) || [];
|
|
163
|
+
const res = yield dbRaw.batchGetItem(params).promise();
|
|
164
|
+
(0, log_1.debug)(`got dynamo getitems=${JSON.stringify(res, null, 2)}`);
|
|
165
|
+
let ret = ((_f = (_e = res.Responses) === null || _e === void 0 ? void 0 : _e[tableName]) === null || _f === void 0 ? void 0 : _f.map((s) => aws_sdk_1.default.DynamoDB.Converter.unmarshall(s))) || [];
|
|
165
166
|
return ret;
|
|
166
167
|
}
|
|
167
168
|
catch (e) {
|
|
168
|
-
|
|
169
|
+
let msg = `error with getitems query:` +
|
|
170
|
+
JSON.stringify(params, null, 2) +
|
|
171
|
+
'\n' +
|
|
172
|
+
e.toString();
|
|
173
|
+
(0, log_1.error)(msg);
|
|
169
174
|
throw e;
|
|
170
175
|
}
|
|
171
176
|
});
|
|
@@ -68,12 +68,12 @@ const getAndValidateToken = ({ tokenRaw, COGNITO_USER_POOL_ID, }) => __awaiter(v
|
|
|
68
68
|
error: (0, api_1.returnCode)(403, 'auth failed'),
|
|
69
69
|
};
|
|
70
70
|
}
|
|
71
|
-
token = tokenRaw.
|
|
71
|
+
token = tokenRaw.substring(tokenRaw.indexOf(' ') + 1);
|
|
72
72
|
let subject;
|
|
73
73
|
try {
|
|
74
74
|
yield jwtVerify({ token, COGNITO_USER_POOL_ID });
|
|
75
75
|
const decoded = (0, jsonwebtoken_1.decode)(token);
|
|
76
|
-
(0, log_1.debug)(`decoded=${JSON.stringify(decoded)}`);
|
|
76
|
+
(0, log_1.debug)(`decoded=${JSON.stringify(decoded, null, 2)}`);
|
|
77
77
|
subject = decoded === null || decoded === void 0 ? void 0 : decoded.sub;
|
|
78
78
|
if (!subject) {
|
|
79
79
|
const mess = 'user should have responded with subject (sub) field';
|
|
@@ -27,10 +27,8 @@ const chunk = (array, max) => {
|
|
|
27
27
|
let row = [];
|
|
28
28
|
for (const k in array) {
|
|
29
29
|
const item = array[k];
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
30
|
+
row.push(item);
|
|
31
|
+
if (row.length >= max) {
|
|
34
32
|
rows.push(row);
|
|
35
33
|
row = [];
|
|
36
34
|
}
|
|
@@ -6,4 +6,13 @@ export interface ISite {
|
|
|
6
6
|
siteUrl: string;
|
|
7
7
|
niceSiteUrl: string;
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* removes protocol, and trailing slashes
|
|
11
|
+
*/
|
|
9
12
|
export declare const niceUrl: (siteUrl: string) => string | ISite;
|
|
13
|
+
/**
|
|
14
|
+
* string -> String
|
|
15
|
+
* @param str
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export declare function toTitleCase(str: string): string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = void 0;
|
|
3
|
+
exports.toTitleCase = exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = void 0;
|
|
4
4
|
const csvJSON = (csv) => {
|
|
5
5
|
const lines = csv.split('\n');
|
|
6
6
|
const result = [];
|
|
@@ -47,12 +47,15 @@ function truncate(str, n, ellip) {
|
|
|
47
47
|
return str.length > n ? str.substr(0, n - 1) + ellip : str;
|
|
48
48
|
}
|
|
49
49
|
exports.truncate = truncate;
|
|
50
|
+
/**
|
|
51
|
+
* removes protocol, and trailing slashes
|
|
52
|
+
*/
|
|
50
53
|
const niceUrl = (siteUrl) => {
|
|
51
54
|
if (!siteUrl) {
|
|
52
55
|
return siteUrl;
|
|
53
56
|
}
|
|
54
57
|
let niceSiteUrl = siteUrl
|
|
55
|
-
.
|
|
58
|
+
.substring(siteUrl.indexOf(':') + 1)
|
|
56
59
|
.replace('sc-domain:', '')
|
|
57
60
|
.replace('https://', '')
|
|
58
61
|
.replace('http://', '');
|
|
@@ -60,3 +63,15 @@ const niceUrl = (siteUrl) => {
|
|
|
60
63
|
return { siteUrl, niceSiteUrl };
|
|
61
64
|
};
|
|
62
65
|
exports.niceUrl = niceUrl;
|
|
66
|
+
/**
|
|
67
|
+
* string -> String
|
|
68
|
+
* @param str
|
|
69
|
+
* @returns
|
|
70
|
+
*/
|
|
71
|
+
function toTitleCase(str) {
|
|
72
|
+
if (!str) {
|
|
73
|
+
return str;
|
|
74
|
+
}
|
|
75
|
+
return str.replace(/\w\S*/g, (txt) => txt && txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase());
|
|
76
|
+
}
|
|
77
|
+
exports.toTitleCase = toTitleCase;
|
|
@@ -21,7 +21,7 @@ const LoginButton = ({ className, text = text_1.getstarted, invert, title, saveP
|
|
|
21
21
|
const lp = loginPath(!savePath || typeof window === 'undefined'
|
|
22
22
|
? undefined
|
|
23
23
|
: {
|
|
24
|
-
redirect: window.location.href.
|
|
24
|
+
redirect: window.location.href.substring(window.location.origin.length),
|
|
25
25
|
});
|
|
26
26
|
return (react_1.default.createElement(Button_1.Button, { lang: lang, title: title, invert: invert, className: className, onKeyPress: () => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
27
|
yield pushPath(lp);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AxiosResponse } from 'axios';
|
|
2
|
+
export declare const axiosHelper: <TOut>({ verb, url, data, headers, timeout, retryMax, }: {
|
|
3
|
+
headers?: {
|
|
4
|
+
[a: string]: string;
|
|
5
|
+
} | undefined;
|
|
6
|
+
verb: 'put' | 'post' | 'get' | 'patch' | 'delete';
|
|
7
|
+
url: string;
|
|
8
|
+
data?: unknown;
|
|
9
|
+
timeout?: number | undefined;
|
|
10
|
+
retryMax?: number | undefined;
|
|
11
|
+
}) => Promise<AxiosResponse<TOut, any>>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.axiosHelper = void 0;
|
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
|
17
|
+
const object_1 = require("../../common/helpers/object");
|
|
18
|
+
const axiosHelper = ({ verb, url, data, headers, timeout = 30000, retryMax = 0, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
let retry = 0;
|
|
20
|
+
do {
|
|
21
|
+
try {
|
|
22
|
+
const setHeaders = Object.assign({ accept: 'application/json' }, headers);
|
|
23
|
+
if (verb === 'get') {
|
|
24
|
+
const ret = yield axios_1.default.get(url, {
|
|
25
|
+
headers: setHeaders,
|
|
26
|
+
timeout,
|
|
27
|
+
timeoutErrorMessage: `${url} timeout`,
|
|
28
|
+
});
|
|
29
|
+
return ret;
|
|
30
|
+
}
|
|
31
|
+
let noBody = false;
|
|
32
|
+
let func = axios_1.default.post;
|
|
33
|
+
if (verb === 'put') {
|
|
34
|
+
func = axios_1.default.put;
|
|
35
|
+
}
|
|
36
|
+
else if (verb === 'post') {
|
|
37
|
+
func = axios_1.default.post;
|
|
38
|
+
}
|
|
39
|
+
else if (verb === 'patch') {
|
|
40
|
+
func = axios_1.default.patch;
|
|
41
|
+
}
|
|
42
|
+
else if (verb === 'delete') {
|
|
43
|
+
func = axios_1.default.delete;
|
|
44
|
+
noBody = true;
|
|
45
|
+
}
|
|
46
|
+
let ret;
|
|
47
|
+
if (noBody) {
|
|
48
|
+
ret = yield func(url, {
|
|
49
|
+
headers: setHeaders,
|
|
50
|
+
timeout,
|
|
51
|
+
timeoutErrorMessage: `${url} timeout`,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
56
|
+
if (data && (0, object_1.isJson)(data)) {
|
|
57
|
+
setHeaders['Content-Type'] = 'application/json';
|
|
58
|
+
}
|
|
59
|
+
ret = yield func(url, data, { headers: setHeaders });
|
|
60
|
+
}
|
|
61
|
+
return ret;
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
const em = e;
|
|
65
|
+
// jwt expired or bad response
|
|
66
|
+
// 403 returned for old token - will be refreshed
|
|
67
|
+
if (em.code === '401' || em.code === '403') {
|
|
68
|
+
// eslint-disable-next-line no-console
|
|
69
|
+
console.log('auth expired, reset');
|
|
70
|
+
// retry current page
|
|
71
|
+
window.location.reload();
|
|
72
|
+
retry = retryMax;
|
|
73
|
+
}
|
|
74
|
+
if (retry >= retryMax) {
|
|
75
|
+
throw em;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
retry += 1;
|
|
79
|
+
} while (retry <= retryMax);
|
|
80
|
+
throw new Error('unexpected');
|
|
81
|
+
});
|
|
82
|
+
exports.axiosHelper = axiosHelper;
|
|
@@ -26,7 +26,7 @@ function getCookie({ cname, cookieDocument, }) {
|
|
|
26
26
|
const ca = ca1.split(';').map((t) => t.trim());
|
|
27
27
|
for (const c of ca) {
|
|
28
28
|
if (c.indexOf(name) === 0) {
|
|
29
|
-
return c.
|
|
29
|
+
return c.substr(name.length, c.length);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
return undefined;
|
package/dist/ui/helpers/index.js
CHANGED
|
@@ -10,6 +10,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./axiosHelper"), exports);
|
|
13
14
|
__exportStar(require("./browserHelpers"), exports);
|
|
14
15
|
__exportStar(require("./callOpenApi"), exports);
|
|
15
16
|
__exportStar(require("./cookie"), exports);
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ag-common",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"author": "Andrei Gec <@andreigec> (https://gec.dev/)",
|
|
7
7
|
"repository": "github:andreigec/ag-common",
|
|
8
|
+
"license": "ISC",
|
|
8
9
|
"dependencies": {
|
|
9
10
|
"aws-lambda": "1.0.7",
|
|
10
11
|
"styled-components": "5.3.3",
|
|
@@ -50,6 +51,8 @@
|
|
|
50
51
|
"start": "tsc --watch"
|
|
51
52
|
},
|
|
52
53
|
"files": [
|
|
53
|
-
"dist/**/*"
|
|
54
|
+
"dist/**/*",
|
|
55
|
+
"README.md",
|
|
56
|
+
"LICENSE.md"
|
|
54
57
|
]
|
|
55
58
|
}
|