ag-common 0.0.3
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/dist/api/helpers/api.d.ts +16 -0
- package/dist/api/helpers/api.js +67 -0
- package/dist/api/helpers/dynamo.d.ts +44 -0
- package/dist/api/helpers/dynamo.js +307 -0
- package/dist/api/helpers/dynamoInfra.d.ts +15 -0
- package/dist/api/helpers/dynamoInfra.js +64 -0
- package/dist/api/helpers/index.d.ts +3 -0
- package/dist/api/helpers/index.js +15 -0
- package/dist/api/helpers/openApiHelpers.d.ts +14 -0
- package/dist/api/helpers/openApiHelpers.js +131 -0
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.js +14 -0
- package/dist/api/types.d.ts +45 -0
- package/dist/api/types.js +2 -0
- package/dist/common/helpers/array.d.ts +9 -0
- package/dist/common/helpers/array.js +51 -0
- package/dist/common/helpers/distinctBy.d.ts +8 -0
- package/dist/common/helpers/distinctBy.js +35 -0
- package/dist/common/helpers/email.d.ts +7 -0
- package/dist/common/helpers/email.js +35 -0
- package/dist/common/helpers/groupBy.d.ts +22 -0
- package/dist/common/helpers/groupBy.js +53 -0
- package/dist/common/helpers/hashCode.d.ts +2 -0
- package/dist/common/helpers/hashCode.js +27 -0
- package/dist/common/helpers/i18n.d.ts +11 -0
- package/dist/common/helpers/i18n.js +14 -0
- package/dist/common/helpers/index.d.ts +12 -0
- package/dist/common/helpers/index.js +24 -0
- package/dist/common/helpers/log.d.ts +5 -0
- package/dist/common/helpers/log.js +25 -0
- package/dist/common/helpers/math.d.ts +11 -0
- package/dist/common/helpers/math.js +65 -0
- package/dist/common/helpers/object.d.ts +17 -0
- package/dist/common/helpers/object.js +54 -0
- package/dist/common/helpers/random.d.ts +1 -0
- package/dist/common/helpers/random.js +26 -0
- package/dist/common/helpers/secondsInNearest.d.ts +8 -0
- package/dist/common/helpers/secondsInNearest.js +39 -0
- package/dist/common/helpers/string.d.ts +9 -0
- package/dist/common/helpers/string.js +62 -0
- package/dist/common/index.d.ts +1 -0
- package/dist/common/index.js +13 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +15 -0
- package/dist/ui/components/Button/index.d.ts +11 -0
- package/dist/ui/components/Button/index.js +45 -0
- package/dist/ui/components/Dropdown/index.d.ts +10 -0
- package/dist/ui/components/Dropdown/index.js +61 -0
- package/dist/ui/components/DropdownList/index.d.ts +9 -0
- package/dist/ui/components/DropdownList/index.js +124 -0
- package/dist/ui/components/FlexColumn/index.d.ts +6 -0
- package/dist/ui/components/FlexColumn/index.js +31 -0
- package/dist/ui/components/FlexRow/index.d.ts +7 -0
- package/dist/ui/components/FlexRow/index.js +42 -0
- package/dist/ui/components/HeadersRaw/index.d.ts +10 -0
- package/dist/ui/components/HeadersRaw/index.js +44 -0
- package/dist/ui/components/Input/index.d.ts +15 -0
- package/dist/ui/components/Input/index.js +54 -0
- package/dist/ui/components/Loader/index.d.ts +4 -0
- package/dist/ui/components/Loader/index.js +69 -0
- package/dist/ui/components/LoginButton/index.d.ts +12 -0
- package/dist/ui/components/LoginButton/index.js +32 -0
- package/dist/ui/components/LoginButton/text.d.ts +3 -0
- package/dist/ui/components/LoginButton/text.js +9 -0
- package/dist/ui/components/Modal/Close.d.ts +5 -0
- package/dist/ui/components/Modal/Close.js +37 -0
- package/dist/ui/components/Modal/index.d.ts +21 -0
- package/dist/ui/components/Modal/index.js +93 -0
- package/dist/ui/components/RowOrColumn/index.d.ts +6 -0
- package/dist/ui/components/RowOrColumn/index.js +40 -0
- package/dist/ui/components/Table/index.d.ts +9 -0
- package/dist/ui/components/Table/index.js +31 -0
- package/dist/ui/components/TextEdit/CheckboxEdit.d.ts +5 -0
- package/dist/ui/components/TextEdit/CheckboxEdit.js +59 -0
- package/dist/ui/components/TextEdit/ColourEdit.d.ts +5 -0
- package/dist/ui/components/TextEdit/ColourEdit.js +48 -0
- package/dist/ui/components/TextEdit/ListboxEdit.d.ts +6 -0
- package/dist/ui/components/TextEdit/ListboxEdit.js +41 -0
- package/dist/ui/components/TextEdit/TextEdit.d.ts +13 -0
- package/dist/ui/components/TextEdit/TextEdit.js +95 -0
- package/dist/ui/components/TextEdit/common.d.ts +5 -0
- package/dist/ui/components/TextEdit/common.js +70 -0
- package/dist/ui/components/TextEdit/images.d.ts +4 -0
- package/dist/ui/components/TextEdit/images.js +21 -0
- package/dist/ui/components/TextEdit/index.d.ts +4 -0
- package/dist/ui/components/TextEdit/index.js +16 -0
- package/dist/ui/components/TextInput/index.d.ts +12 -0
- package/dist/ui/components/TextInput/index.js +30 -0
- package/dist/ui/components/Toast/index.d.ts +11 -0
- package/dist/ui/components/Toast/index.js +42 -0
- package/dist/ui/components/UserImage/index.d.ts +8 -0
- package/dist/ui/components/UserImage/index.js +66 -0
- package/dist/ui/components/index.d.ts +16 -0
- package/dist/ui/components/index.js +28 -0
- package/dist/ui/helpers/browserHelpers.d.ts +1 -0
- package/dist/ui/helpers/browserHelpers.js +23 -0
- package/dist/ui/helpers/callOpenApi.d.ts +13 -0
- package/dist/ui/helpers/callOpenApi.js +85 -0
- package/dist/ui/helpers/cookie.d.ts +14 -0
- package/dist/ui/helpers/cookie.js +110 -0
- package/dist/ui/helpers/debounce.d.ts +6 -0
- package/dist/ui/helpers/debounce.js +33 -0
- package/dist/ui/helpers/dom.d.ts +1 -0
- package/dist/ui/helpers/dom.js +10 -0
- package/dist/ui/helpers/extractAttributes.d.ts +3 -0
- package/dist/ui/helpers/extractAttributes.js +17 -0
- package/dist/ui/helpers/index.d.ts +17 -0
- package/dist/ui/helpers/index.js +29 -0
- package/dist/ui/helpers/lang.d.ts +5 -0
- package/dist/ui/helpers/lang.js +23 -0
- package/dist/ui/helpers/mutex.d.ts +22 -0
- package/dist/ui/helpers/mutex.js +53 -0
- package/dist/ui/helpers/mutexData.d.ts +12 -0
- package/dist/ui/helpers/mutexData.js +46 -0
- package/dist/ui/helpers/plural.d.ts +1 -0
- package/dist/ui/helpers/plural.js +13 -0
- package/dist/ui/helpers/routes.d.ts +34 -0
- package/dist/ui/helpers/routes.js +2 -0
- package/dist/ui/helpers/useLocalStorage.d.ts +5 -0
- package/dist/ui/helpers/useLocalStorage.js +80 -0
- package/dist/ui/helpers/useLockBodyScroll.d.ts +1 -0
- package/dist/ui/helpers/useLockBodyScroll.js +19 -0
- package/dist/ui/helpers/useOnClickOutside.d.ts +7 -0
- package/dist/ui/helpers/useOnClickOutside.js +28 -0
- package/dist/ui/helpers/useOnScroll.d.ts +6 -0
- package/dist/ui/helpers/useOnScroll.js +33 -0
- package/dist/ui/helpers/useOpenApiStore.d.ts +14 -0
- package/dist/ui/helpers/useOpenApiStore.js +145 -0
- package/dist/ui/helpers/useQueryString.d.ts +8 -0
- package/dist/ui/helpers/useQueryString.js +38 -0
- package/dist/ui/helpers/useResize.d.ts +8 -0
- package/dist/ui/helpers/useResize.js +33 -0
- package/dist/ui/index.d.ts +3 -0
- package/dist/ui/index.js +15 -0
- package/dist/ui/styles/colours.d.ts +25 -0
- package/dist/ui/styles/colours.js +51 -0
- package/dist/ui/styles/common.d.ts +13 -0
- package/dist/ui/styles/common.js +94 -0
- package/dist/ui/styles/index.d.ts +4 -0
- package/dist/ui/styles/index.js +16 -0
- package/dist/ui/styles/media.d.ts +5 -0
- package/dist/ui/styles/media.js +8 -0
- package/dist/ui/styles/standaloneStyles.d.ts +6 -0
- package/dist/ui/styles/standaloneStyles.js +69 -0
- package/package.json +40 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { APIGatewayProxyResult } from 'aws-lambda';
|
|
2
|
+
import { DYNAMOKEYS } from '../types';
|
|
3
|
+
export declare const returnCode: <T>(statusCode: number, body?: T | undefined, extraHeaders?: {
|
|
4
|
+
[a: string]: string;
|
|
5
|
+
} | undefined, fullSiteUrl?: string | undefined) => APIGatewayProxyResult;
|
|
6
|
+
export declare const stripPKs: <T>(r: T, excludePK?: boolean) => T;
|
|
7
|
+
export declare const generateDynamoPKS: ({ type, L1, L2, L3, L4, L5, L6, additionalPKValues, }: {
|
|
8
|
+
type: string;
|
|
9
|
+
L1: string;
|
|
10
|
+
L2?: string | undefined;
|
|
11
|
+
L3?: string | undefined;
|
|
12
|
+
L4?: string | undefined;
|
|
13
|
+
L5?: string | undefined;
|
|
14
|
+
L6?: string | undefined;
|
|
15
|
+
additionalPKValues?: string[] | undefined;
|
|
16
|
+
}) => DYNAMOKEYS;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.generateDynamoPKS = exports.stripPKs = exports.returnCode = void 0;
|
|
15
|
+
const array_1 = require("../../common/helpers/array");
|
|
16
|
+
const returnCode = (statusCode, body, extraHeaders, fullSiteUrl) => {
|
|
17
|
+
let headers = {
|
|
18
|
+
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent,User-Agent,Origin',
|
|
19
|
+
'Access-Control-Allow-Methods': '*',
|
|
20
|
+
'Access-Control-Allow-Origin': fullSiteUrl || '*',
|
|
21
|
+
'Access-Control-Allow-Credentials': true, // Required for cookies, authorization headers with HTTPS
|
|
22
|
+
};
|
|
23
|
+
if (extraHeaders && Object.keys(extraHeaders).length > 0) {
|
|
24
|
+
headers = Object.assign(Object.assign({}, headers), extraHeaders);
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
headers,
|
|
28
|
+
statusCode,
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
body: (!body ? undefined : JSON.stringify(body, null, 2)),
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
exports.returnCode = returnCode;
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
+
const stripPKs = (r, excludePK = true) => {
|
|
36
|
+
if (!r) {
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
41
|
+
// @ts-ignore
|
|
42
|
+
const { PK, PK1, PK2, PK3, PK4, L1, L2, L3, L4, L5, L6, TYPE } = r, rest = __rest(r, ["PK", "PK1", "PK2", "PK3", "PK4", "L1", "L2", "L3", "L4", "L5", "L6", "TYPE"]);
|
|
43
|
+
if (!excludePK) {
|
|
44
|
+
//@ts-ignore
|
|
45
|
+
rest.PK = PK;
|
|
46
|
+
}
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
48
|
+
// @ts-ignore
|
|
49
|
+
return rest;
|
|
50
|
+
};
|
|
51
|
+
exports.stripPKs = stripPKs;
|
|
52
|
+
const generateDynamoPKS = ({ type, L1, L2, L3, L4, L5, L6, additionalPKValues, }) => {
|
|
53
|
+
const c = '#';
|
|
54
|
+
const keys = [L1, L2, L3, L4, L5, L6].filter(array_1.notEmpty);
|
|
55
|
+
const additionalPK = additionalPKValues && (additionalPKValues === null || additionalPKValues === void 0 ? void 0 : additionalPKValues.length) > 0
|
|
56
|
+
? `#${additionalPKValues.join(c)}`
|
|
57
|
+
: '';
|
|
58
|
+
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ PK: keys.join(c) + additionalPK, type,
|
|
59
|
+
L1 }, (L2 && { L2 })), (L3 && { L3 })), (L4 && { L4 })), (L5 && { L5 })), (L6 && { L6 })), { PK1: L1, PK2: [keys[0], keys[1]].join(c) }), (keys.length >= 3 && {
|
|
60
|
+
PK3: [keys[0], keys[1], keys[2]].join(c),
|
|
61
|
+
})), (keys.length >= 4 && {
|
|
62
|
+
PK4: [keys[0], keys[1], keys[2], keys[3]].join(c),
|
|
63
|
+
})), (keys.length >= 5 && {
|
|
64
|
+
PK5: [keys[0], keys[1], keys[2], keys[3], keys[4]].join(c),
|
|
65
|
+
}));
|
|
66
|
+
};
|
|
67
|
+
exports.generateDynamoPKS = generateDynamoPKS;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { DocumentClient, Key } from 'aws-sdk/clients/dynamodb';
|
|
2
|
+
import { IQueryDynamo } from '../types';
|
|
3
|
+
export declare let dynamoDb: DocumentClient;
|
|
4
|
+
export declare const setDynamo: (region: string) => void;
|
|
5
|
+
export declare const putDynamo: <T>(item: T, tableName: string) => Promise<{
|
|
6
|
+
error?: string | undefined;
|
|
7
|
+
data?: T | undefined;
|
|
8
|
+
}>;
|
|
9
|
+
export declare const batchWrite: <T extends {}>(tableName: string, itemsIn: T[], breakOnError?: boolean) => Promise<{
|
|
10
|
+
error: string;
|
|
11
|
+
} | {
|
|
12
|
+
error?: undefined;
|
|
13
|
+
}>;
|
|
14
|
+
export declare const batchDelete: ({ tableName, keys, breakOnError, pkName, }: {
|
|
15
|
+
pkName: string;
|
|
16
|
+
breakOnError?: boolean | undefined;
|
|
17
|
+
tableName: string;
|
|
18
|
+
keys: string[];
|
|
19
|
+
}) => Promise<{
|
|
20
|
+
error: string;
|
|
21
|
+
} | {
|
|
22
|
+
error?: undefined;
|
|
23
|
+
}>;
|
|
24
|
+
export declare const scan: <T>(tableName: string) => Promise<T[]>;
|
|
25
|
+
export declare const getItemDynamo: <T>({ tableName, pkName, pkValue, }: {
|
|
26
|
+
pkName: string;
|
|
27
|
+
pkValue: string;
|
|
28
|
+
tableName: string;
|
|
29
|
+
}) => Promise<T>;
|
|
30
|
+
export declare const getItemsDynamo: <T>({ tableName, items, }: {
|
|
31
|
+
items: {
|
|
32
|
+
pkName: string;
|
|
33
|
+
pkValue: string;
|
|
34
|
+
}[];
|
|
35
|
+
tableName: string;
|
|
36
|
+
}) => Promise<T[]>;
|
|
37
|
+
export declare const queryDynamo: <T>({ tableName, pkName, pkValue, pkOperator, skName, skValue, skOperator, indexName, count, startKey: startKeyIn, filterName, filterValue, filterOperator, }: IQueryDynamo) => Promise<{
|
|
38
|
+
Items: T[];
|
|
39
|
+
startKey?: Key | undefined;
|
|
40
|
+
}>;
|
|
41
|
+
export declare const getDynamoTtl: (days: number) => number;
|
|
42
|
+
export declare const wipeTable: (tableName: string) => Promise<{
|
|
43
|
+
errors?: string[] | undefined;
|
|
44
|
+
}>;
|
|
@@ -0,0 +1,307 @@
|
|
|
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.wipeTable = exports.getDynamoTtl = exports.queryDynamo = exports.getItemsDynamo = exports.getItemDynamo = exports.scan = exports.batchDelete = exports.batchWrite = exports.putDynamo = exports.setDynamo = exports.dynamoDb = void 0;
|
|
16
|
+
const aws_sdk_1 = __importDefault(require("aws-sdk"));
|
|
17
|
+
const log_1 = require("../../common/helpers/log");
|
|
18
|
+
const array_1 = require("../../common/helpers/array");
|
|
19
|
+
// eslint-disable-next-line import/no-mutable-exports
|
|
20
|
+
exports.dynamoDb = new aws_sdk_1.default.DynamoDB.DocumentClient();
|
|
21
|
+
const setDynamo = (region) => {
|
|
22
|
+
exports.dynamoDb = new aws_sdk_1.default.DynamoDB.DocumentClient({ region });
|
|
23
|
+
};
|
|
24
|
+
exports.setDynamo = setDynamo;
|
|
25
|
+
const putDynamo = (item, tableName) => __awaiter(void 0, void 0, void 0, function* () {
|
|
26
|
+
const params = {
|
|
27
|
+
TableName: tableName,
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
+
Item: item,
|
|
30
|
+
};
|
|
31
|
+
(0, log_1.info)(`running dynamo put=${JSON.stringify(params)}`);
|
|
32
|
+
// write the todo to the database
|
|
33
|
+
const put = yield exports.dynamoDb.put(params).promise();
|
|
34
|
+
if (put.$response.error &&
|
|
35
|
+
put.$response.error.statusCode &&
|
|
36
|
+
put.$response.error.statusCode >= 400) {
|
|
37
|
+
throw new Error(put.$response.error.message);
|
|
38
|
+
}
|
|
39
|
+
// put never returns into, so just use what we have already
|
|
40
|
+
return { data: item };
|
|
41
|
+
});
|
|
42
|
+
exports.putDynamo = putDynamo;
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
44
|
+
const batchWrite = (tableName, itemsIn, breakOnError = false) => __awaiter(void 0, void 0, void 0, function* () {
|
|
45
|
+
var _a, _b;
|
|
46
|
+
let items = JSON.parse(JSON.stringify(itemsIn));
|
|
47
|
+
(0, log_1.info)(`push to dynamo:${tableName} - count=${itemsIn.length}`);
|
|
48
|
+
const error = [];
|
|
49
|
+
while (items.length > 0) {
|
|
50
|
+
const { part, rest } = (0, array_1.take)(items, 25);
|
|
51
|
+
// eslint-disable-next-line no-param-reassign
|
|
52
|
+
items = rest;
|
|
53
|
+
// eslint-disable-next-line no-await-in-loop
|
|
54
|
+
const res = yield exports.dynamoDb
|
|
55
|
+
.batchWrite({
|
|
56
|
+
RequestItems: {
|
|
57
|
+
[`${tableName}`]: part.map((item) => ({
|
|
58
|
+
PutRequest: { Item: item },
|
|
59
|
+
})),
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
.promise();
|
|
63
|
+
const newError = (_b = (_a = res.$response) === null || _a === void 0 ? void 0 : _a.error) !== null && _b !== void 0 ? _b : null;
|
|
64
|
+
if (newError) {
|
|
65
|
+
error.push(newError);
|
|
66
|
+
if (breakOnError) {
|
|
67
|
+
items = [];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if ((error === null || error === void 0 ? void 0 : error.length) > 0) {
|
|
72
|
+
const me = error.join('\n');
|
|
73
|
+
(0, log_1.error)(`batch write error=${me}`);
|
|
74
|
+
return { error: me };
|
|
75
|
+
}
|
|
76
|
+
return {};
|
|
77
|
+
});
|
|
78
|
+
exports.batchWrite = batchWrite;
|
|
79
|
+
const batchDelete = ({ tableName, keys, breakOnError = true, pkName, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
80
|
+
var _c, _d;
|
|
81
|
+
(0, log_1.info)(`wipe keys dynamo:${tableName} - count=${keys.length}`);
|
|
82
|
+
const error = [];
|
|
83
|
+
let breakV = false;
|
|
84
|
+
for (let key of keys) {
|
|
85
|
+
if (breakV) {
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
const res = yield exports.dynamoDb
|
|
89
|
+
.delete({
|
|
90
|
+
TableName: tableName,
|
|
91
|
+
Key: { [`${pkName}`]: key },
|
|
92
|
+
})
|
|
93
|
+
.promise();
|
|
94
|
+
const newError = (_d = (_c = res.$response) === null || _c === void 0 ? void 0 : _c.error) !== null && _d !== void 0 ? _d : null;
|
|
95
|
+
if (newError) {
|
|
96
|
+
error.push(newError);
|
|
97
|
+
if (breakOnError) {
|
|
98
|
+
breakV = true;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if ((error === null || error === void 0 ? void 0 : error.length) > 0) {
|
|
103
|
+
const me = error.join('\n');
|
|
104
|
+
(0, log_1.error)(`batch write error=${me}`);
|
|
105
|
+
return { error: me };
|
|
106
|
+
}
|
|
107
|
+
return {};
|
|
108
|
+
});
|
|
109
|
+
exports.batchDelete = batchDelete;
|
|
110
|
+
const scan = (tableName) => __awaiter(void 0, void 0, void 0, function* () {
|
|
111
|
+
const Items = [];
|
|
112
|
+
let ExclusiveStartKey;
|
|
113
|
+
do {
|
|
114
|
+
const { Items: newitems, LastEvaluatedKey, $response,
|
|
115
|
+
// eslint-disable-next-line no-await-in-loop
|
|
116
|
+
} = yield exports.dynamoDb
|
|
117
|
+
.scan({ TableName: tableName, ExclusiveStartKey })
|
|
118
|
+
.promise();
|
|
119
|
+
ExclusiveStartKey = LastEvaluatedKey;
|
|
120
|
+
if ($response.error &&
|
|
121
|
+
$response.error.statusCode &&
|
|
122
|
+
$response.error.statusCode >= 400) {
|
|
123
|
+
throw new Error($response.error.message);
|
|
124
|
+
}
|
|
125
|
+
if (newitems) {
|
|
126
|
+
Items.push(...newitems.map((r) => r));
|
|
127
|
+
}
|
|
128
|
+
} while (ExclusiveStartKey);
|
|
129
|
+
(0, log_1.info)(`dynamo scan against ${tableName} ok, count=${Items === null || Items === void 0 ? void 0 : Items.length}`);
|
|
130
|
+
return Items;
|
|
131
|
+
});
|
|
132
|
+
exports.scan = scan;
|
|
133
|
+
const getItemDynamo = ({ tableName, pkName, pkValue, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
134
|
+
const params = {
|
|
135
|
+
Key: { [pkName]: pkValue },
|
|
136
|
+
TableName: tableName,
|
|
137
|
+
};
|
|
138
|
+
try {
|
|
139
|
+
const res = yield exports.dynamoDb.get(params).promise();
|
|
140
|
+
const ret = res.Item;
|
|
141
|
+
(0, log_1.debug)(`got dynamo getitem=${JSON.stringify(params)}`);
|
|
142
|
+
return ret;
|
|
143
|
+
}
|
|
144
|
+
catch (e) {
|
|
145
|
+
(0, log_1.error)(e);
|
|
146
|
+
throw e;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
exports.getItemDynamo = getItemDynamo;
|
|
150
|
+
const getItemsDynamo = ({ tableName, items, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
151
|
+
var _e, _f;
|
|
152
|
+
const params = {
|
|
153
|
+
RequestItems: {
|
|
154
|
+
[tableName]: {
|
|
155
|
+
Keys: items.map(({ pkName, pkValue }) => ({
|
|
156
|
+
[pkName]: { S: pkValue },
|
|
157
|
+
})),
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
try {
|
|
162
|
+
const res = yield exports.dynamoDb.batchGet(params).promise();
|
|
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)) || [];
|
|
165
|
+
return ret;
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
(0, log_1.error)(e);
|
|
169
|
+
throw e;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
exports.getItemsDynamo = getItemsDynamo;
|
|
173
|
+
const queryDynamo = ({ tableName, pkName, pkValue, pkOperator = '=', skName, skValue, skOperator = '=', indexName, count = 1000, startKey: startKeyIn, filterName, filterValue, filterOperator = '=', }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
174
|
+
var _g, _h;
|
|
175
|
+
let startKey = startKeyIn;
|
|
176
|
+
let kce = `#${pkName.toLowerCase()} ${pkOperator} :${pkName.toLowerCase()}`;
|
|
177
|
+
const ean = { [`#${pkName.toLowerCase()}`]: pkName };
|
|
178
|
+
const eav = {
|
|
179
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
180
|
+
[`:${pkName.toLowerCase()}`]: pkValue,
|
|
181
|
+
};
|
|
182
|
+
if (skName) {
|
|
183
|
+
if (skOperator === 'BETWEEN') {
|
|
184
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
185
|
+
let [sp0, sp1] = skValue;
|
|
186
|
+
const skName1 = `${skName.toLowerCase()}1`;
|
|
187
|
+
const skName2 = `${skName.toLowerCase()}2`;
|
|
188
|
+
kce += ` and #${skName.toLowerCase()} ${skOperator} :${skName1.toLowerCase()} AND :${skName2.toLowerCase()}`;
|
|
189
|
+
ean[`#${skName.toLowerCase()}`] = skName;
|
|
190
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
191
|
+
eav[`:${skName1}`] = sp0;
|
|
192
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
193
|
+
eav[`:${skName2}`] = sp1;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
kce += ` and #${skName.toLowerCase()} ${skOperator} :${skName.toLowerCase()}`;
|
|
197
|
+
ean[`#${skName.toLowerCase()}`] = skName;
|
|
198
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
199
|
+
eav[`:${skName.toLowerCase()}`] = skValue;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
let FilterExpression;
|
|
203
|
+
if (filterName) {
|
|
204
|
+
ean[`#${filterName.toLowerCase()}`] = filterName;
|
|
205
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
206
|
+
eav[`:${filterName.toLowerCase()}`] = filterValue;
|
|
207
|
+
FilterExpression = `#${filterName.toLowerCase()} ${filterOperator} :${filterName.toLowerCase()}`;
|
|
208
|
+
if (filterOperator === 'contains') {
|
|
209
|
+
FilterExpression = `contains(#${filterName.toLowerCase()}, :${filterName.toLowerCase()})`;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const Items = [];
|
|
213
|
+
do {
|
|
214
|
+
const params = {
|
|
215
|
+
TableName: tableName,
|
|
216
|
+
KeyConditionExpression: kce,
|
|
217
|
+
ExpressionAttributeNames: ean,
|
|
218
|
+
ExpressionAttributeValues: eav,
|
|
219
|
+
ExclusiveStartKey: startKey,
|
|
220
|
+
FilterExpression,
|
|
221
|
+
};
|
|
222
|
+
if (count > 0) {
|
|
223
|
+
params.Limit = count;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
params.Limit = 1000;
|
|
227
|
+
}
|
|
228
|
+
if (indexName) {
|
|
229
|
+
params.IndexName = indexName;
|
|
230
|
+
}
|
|
231
|
+
let newitems;
|
|
232
|
+
let lek;
|
|
233
|
+
let $response;
|
|
234
|
+
try {
|
|
235
|
+
({
|
|
236
|
+
Items: newitems,
|
|
237
|
+
LastEvaluatedKey: lek,
|
|
238
|
+
$response,
|
|
239
|
+
// eslint-disable-next-line no-await-in-loop
|
|
240
|
+
} = yield exports.dynamoDb.query(params).promise());
|
|
241
|
+
}
|
|
242
|
+
catch (e) {
|
|
243
|
+
(0, log_1.error)('error. query params=', JSON.stringify(params), e);
|
|
244
|
+
throw e;
|
|
245
|
+
}
|
|
246
|
+
startKey = lek;
|
|
247
|
+
if ($response.error && ((_g = $response.error.statusCode) !== null && _g !== void 0 ? _g : 0) >= 400) {
|
|
248
|
+
(0, log_1.error)('error. query params=', JSON.stringify(params));
|
|
249
|
+
throw new Error($response.error.message);
|
|
250
|
+
}
|
|
251
|
+
(0, log_1.debug)(`dynamo query against ${params === null || params === void 0 ? void 0 : params.TableName} ok, count=${newitems === null || newitems === void 0 ? void 0 : newitems.length} ${JSON.stringify(params)}`, ` next startkey=${startKey}`);
|
|
252
|
+
if (newitems) {
|
|
253
|
+
Items.push(...newitems.map((r) => r));
|
|
254
|
+
}
|
|
255
|
+
if (count > 0 && ((_h = newitems === null || newitems === void 0 ? void 0 : newitems.length) !== null && _h !== void 0 ? _h : 0) >= count) {
|
|
256
|
+
return { Items, startKey };
|
|
257
|
+
}
|
|
258
|
+
} while (startKey && Object.keys(startKey).length > 0);
|
|
259
|
+
return { Items, startKey: undefined };
|
|
260
|
+
});
|
|
261
|
+
exports.queryDynamo = queryDynamo;
|
|
262
|
+
const getDynamoTtl = (days) => Math.ceil(new Date().getTime() / 1000) + days * 86400;
|
|
263
|
+
exports.getDynamoTtl = getDynamoTtl;
|
|
264
|
+
const wipeTable = (tableName) => __awaiter(void 0, void 0, void 0, function* () {
|
|
265
|
+
var _j;
|
|
266
|
+
const dbRaw = new aws_sdk_1.default.DynamoDB({ apiVersion: '2012-10-08' });
|
|
267
|
+
let infoV = yield dbRaw
|
|
268
|
+
.describeTable({
|
|
269
|
+
TableName: tableName,
|
|
270
|
+
})
|
|
271
|
+
.promise();
|
|
272
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
273
|
+
// @ts-ignore
|
|
274
|
+
let keyHash = infoV.Table.KeySchema.find((k) => k.KeyType === 'HASH').AttributeName;
|
|
275
|
+
let all = yield dbRaw
|
|
276
|
+
.scan({
|
|
277
|
+
TableName: tableName,
|
|
278
|
+
})
|
|
279
|
+
.promise();
|
|
280
|
+
(0, log_1.warn)(`will delete ${(_j = all === null || all === void 0 ? void 0 : all.Items) === null || _j === void 0 ? void 0 : _j.length} items from ${tableName}`);
|
|
281
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
282
|
+
// @ts-ignore
|
|
283
|
+
const datagr = (0, array_1.chunk)(all.Items, 25);
|
|
284
|
+
let res = yield Promise.all(datagr.map((group) => dbRaw
|
|
285
|
+
.batchWriteItem({
|
|
286
|
+
RequestItems: {
|
|
287
|
+
[`${tableName}`]: group.map((s) => ({
|
|
288
|
+
DeleteRequest: {
|
|
289
|
+
Key: {
|
|
290
|
+
[`${keyHash}`]: s[keyHash],
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
})),
|
|
294
|
+
},
|
|
295
|
+
})
|
|
296
|
+
.promise()));
|
|
297
|
+
let errors = res
|
|
298
|
+
.map((r) => { var _a, _b; return ((_b = (((_a = r === null || r === void 0 ? void 0 : r.$response) === null || _a === void 0 ? void 0 : _a.error) || {})) === null || _b === void 0 ? void 0 : _b.message) || undefined; })
|
|
299
|
+
.filter(array_1.notEmpty);
|
|
300
|
+
if (errors.length > 0) {
|
|
301
|
+
(0, log_1.error)('errors=', JSON.stringify(errors));
|
|
302
|
+
return { errors };
|
|
303
|
+
}
|
|
304
|
+
(0, log_1.warn)(`cleared table ${tableName}`);
|
|
305
|
+
return {};
|
|
306
|
+
});
|
|
307
|
+
exports.wipeTable = wipeTable;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Stack, aws_dynamodb as dynamodb } from 'aws-cdk-lib';
|
|
2
|
+
import { IGeneratedDynamoData } from '../types';
|
|
3
|
+
export declare const generateTableRef: ({ stack, baseKey, hasStream, hasGSI, }: {
|
|
4
|
+
hasStream: boolean;
|
|
5
|
+
hasGSI: boolean;
|
|
6
|
+
stack: Stack;
|
|
7
|
+
baseKey: string;
|
|
8
|
+
}) => IGeneratedDynamoData;
|
|
9
|
+
export declare const generateSSMParams: ({ stack, table, baseKey, gsi, shortName, }: {
|
|
10
|
+
stack: Stack;
|
|
11
|
+
table: dynamodb.ITable;
|
|
12
|
+
baseKey: string;
|
|
13
|
+
gsi?: string[] | undefined;
|
|
14
|
+
shortName: string;
|
|
15
|
+
}) => void;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateSSMParams = exports.generateTableRef = void 0;
|
|
4
|
+
const aws_cdk_lib_1 = require("aws-cdk-lib");
|
|
5
|
+
const generateTableRef = ({ stack, baseKey, hasStream, hasGSI, }) => {
|
|
6
|
+
const tableStreamArn = !hasStream
|
|
7
|
+
? undefined
|
|
8
|
+
: aws_cdk_lib_1.aws_ssm.StringParameter.valueForStringParameter(stack, `${baseKey}/streamArn`);
|
|
9
|
+
const globalIndexes = !hasGSI
|
|
10
|
+
? undefined
|
|
11
|
+
: aws_cdk_lib_1.Fn.split(',', aws_cdk_lib_1.aws_ssm.StringParameter.valueForStringParameter(stack, `${baseKey}/gsi`));
|
|
12
|
+
const shortName = aws_cdk_lib_1.aws_ssm.StringParameter.valueFromLookup(stack, `${baseKey}/shortName`);
|
|
13
|
+
const table = aws_cdk_lib_1.aws_dynamodb.Table.fromTableAttributes(stack, baseKey, {
|
|
14
|
+
tableArn: aws_cdk_lib_1.aws_ssm.StringParameter.valueForStringParameter(stack, `${baseKey}/arn`),
|
|
15
|
+
tableStreamArn,
|
|
16
|
+
globalIndexes,
|
|
17
|
+
});
|
|
18
|
+
return {
|
|
19
|
+
shortName,
|
|
20
|
+
table,
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
exports.generateTableRef = generateTableRef;
|
|
24
|
+
const generateSSMParams = ({ stack, table, baseKey, gsi, shortName, }) => {
|
|
25
|
+
const name1 = `${baseKey}TableArn`;
|
|
26
|
+
new aws_cdk_lib_1.aws_ssm.StringParameter(stack, name1, {
|
|
27
|
+
allowedPattern: '.*',
|
|
28
|
+
description: `${name1} (cdk)`,
|
|
29
|
+
parameterName: `${baseKey}/arn`,
|
|
30
|
+
stringValue: table.tableArn,
|
|
31
|
+
tier: aws_cdk_lib_1.aws_ssm.ParameterTier.STANDARD,
|
|
32
|
+
});
|
|
33
|
+
if (table.tableStreamArn) {
|
|
34
|
+
const name2 = `${baseKey}TableStreamArn`;
|
|
35
|
+
new aws_cdk_lib_1.aws_ssm.StringParameter(stack, name2, {
|
|
36
|
+
allowedPattern: '.*',
|
|
37
|
+
description: `${name2} (cdk)`,
|
|
38
|
+
parameterName: `${baseKey}/streamArn`,
|
|
39
|
+
stringValue: table.tableStreamArn,
|
|
40
|
+
tier: aws_cdk_lib_1.aws_ssm.ParameterTier.STANDARD,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (gsi && (gsi === null || gsi === void 0 ? void 0 : gsi.length) > 0) {
|
|
44
|
+
const name3 = `${baseKey}TableGSI`;
|
|
45
|
+
new aws_cdk_lib_1.aws_ssm.StringParameter(stack, name3, {
|
|
46
|
+
allowedPattern: '.*',
|
|
47
|
+
description: `${name3} (cdk)`,
|
|
48
|
+
parameterName: `${baseKey}/gsi`,
|
|
49
|
+
stringValue: gsi.join(','),
|
|
50
|
+
tier: aws_cdk_lib_1.aws_ssm.ParameterTier.STANDARD,
|
|
51
|
+
type: aws_cdk_lib_1.aws_ssm.ParameterType.STRING,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const name4 = `${baseKey}TableShortName`;
|
|
55
|
+
new aws_cdk_lib_1.aws_ssm.StringParameter(stack, name4, {
|
|
56
|
+
allowedPattern: '.*',
|
|
57
|
+
description: `${name4} (cdk)`,
|
|
58
|
+
parameterName: `${baseKey}/shortName`,
|
|
59
|
+
stringValue: shortName,
|
|
60
|
+
tier: aws_cdk_lib_1.aws_ssm.ParameterTier.STANDARD,
|
|
61
|
+
type: aws_cdk_lib_1.aws_ssm.ParameterType.STRING,
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
exports.generateSSMParams = generateSSMParams;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./api"), exports);
|
|
14
|
+
__exportStar(require("./dynamoInfra"), exports);
|
|
15
|
+
__exportStar(require("./openApiHelpers"), exports);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { aws_certificatemanager as certmgr, aws_route53 as route53 } from 'aws-cdk-lib';
|
|
2
|
+
import { Construct } from 'constructs';
|
|
3
|
+
import { ILambdaPermissions } from '../types';
|
|
4
|
+
export declare const openApiImpl: (p: {
|
|
5
|
+
schema: any;
|
|
6
|
+
stack: Construct;
|
|
7
|
+
NODE_ENV: string;
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
endpointsBase: string;
|
|
10
|
+
lambdaPermissions: ILambdaPermissions;
|
|
11
|
+
certificate: certmgr.ICertificate;
|
|
12
|
+
hostedZone: route53.IHostedZone;
|
|
13
|
+
shortStackName: string;
|
|
14
|
+
}) => void;
|