ag-common 0.0.77 → 0.0.83

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.
@@ -11,11 +11,13 @@ export declare const batchWrite: <T extends {}>(tableName: string, itemsIn: T[],
11
11
  } | {
12
12
  error?: undefined;
13
13
  }>;
14
- export declare const batchDelete: ({ tableName, keys, breakOnError, pkName, }: {
14
+ export declare const batchDelete: ({ tableName, breakOnError, pkName, keys, rangeName, rangeKeys, }: {
15
15
  pkName: string;
16
16
  breakOnError?: boolean | undefined;
17
17
  tableName: string;
18
18
  keys: string[];
19
+ rangeName?: string | undefined;
20
+ rangeKeys?: string[] | undefined;
19
21
  }) => Promise<{
20
22
  error: string;
21
23
  } | {
@@ -17,6 +17,7 @@ const aws_sdk_1 = __importDefault(require("aws-sdk"));
17
17
  const log_1 = require("../../common/helpers/log");
18
18
  const array_1 = require("../../common/helpers/array");
19
19
  const sleep_1 = require("../../common/helpers/sleep");
20
+ const async_1 = require("../../common/helpers/async");
20
21
  // eslint-disable-next-line import/no-mutable-exports
21
22
  exports.dynamoDb = new aws_sdk_1.default.DynamoDB.DocumentClient();
22
23
  const setDynamo = (region) => {
@@ -106,21 +107,26 @@ const batchWrite = (tableName, itemsIn, breakOnError = false) => __awaiter(void
106
107
  return {};
107
108
  });
108
109
  exports.batchWrite = batchWrite;
109
- const batchDelete = ({ tableName, keys, breakOnError = true, pkName, }) => __awaiter(void 0, void 0, void 0, function* () {
110
- var _c, _d;
110
+ const batchDelete = ({ tableName, breakOnError = true, pkName, keys, rangeName, rangeKeys, }) => __awaiter(void 0, void 0, void 0, function* () {
111
111
  (0, log_1.info)(`wipe keys dynamo:${tableName} - count=${keys.length}`);
112
112
  const error = [];
113
113
  let breakV = false;
114
- for (let key of keys) {
114
+ yield (0, async_1.asyncForEach)(keys, (key, i) => __awaiter(void 0, void 0, void 0, function* () {
115
+ var _c, _d;
115
116
  if (breakV) {
116
- break;
117
+ return;
117
118
  }
118
- const res = yield exports.dynamoDb
119
- .delete({
119
+ let params = {
120
120
  TableName: tableName,
121
- Key: { [`${pkName}`]: key },
122
- })
123
- .promise();
121
+ Key: { [pkName]: key },
122
+ };
123
+ if (rangeName) {
124
+ let rangeValue = rangeKeys === null || rangeKeys === void 0 ? void 0 : rangeKeys[i];
125
+ if (rangeValue) {
126
+ params.Key[rangeName] = rangeValue;
127
+ }
128
+ }
129
+ const res = yield exports.dynamoDb.delete(params).promise();
124
130
  const newError = (_d = (_c = res.$response) === null || _c === void 0 ? void 0 : _c.error) !== null && _d !== void 0 ? _d : null;
125
131
  if (newError) {
126
132
  error.push(newError);
@@ -128,7 +134,7 @@ const batchDelete = ({ tableName, keys, breakOnError = true, pkName, }) => __awa
128
134
  breakV = true;
129
135
  }
130
136
  }
131
- }
137
+ }));
132
138
  if ((error === null || error === void 0 ? void 0 : error.length) > 0) {
133
139
  const me = error.join('\n');
134
140
  (0, log_1.error)(`batch write error=${me}`);
@@ -18,6 +18,9 @@ export interface APIGatewayEvent {
18
18
  resource: string;
19
19
  path: string;
20
20
  requestContext: {
21
+ authorizer?: {
22
+ data?: string;
23
+ };
21
24
  connectionId: string;
22
25
  domainName: string;
23
26
  identity: {
@@ -1,13 +1,3 @@
1
- import { AxiosResponse } from 'axios';
2
- import { AxiosWrapper, User } from './jwt';
3
- export interface OverrideAuth {
4
- id_token?: string;
5
- }
6
- export declare const callOpenApi: <T, TDefaultApi>({ func, apiUrl, overrideAuth, refreshToken, logout, newDefaultApi, }: {
7
- func: (f: TDefaultApi) => Promise<AxiosResponse<T, any>>;
8
- apiUrl: string;
9
- overrideAuth?: OverrideAuth | undefined;
10
- logout: () => void;
11
- refreshToken: () => Promise<User | undefined>;
12
- newDefaultApi: (config: any) => TDefaultApi;
13
- }) => Promise<AxiosWrapper<T | undefined>>;
1
+ import { AxiosWrapper } from './jwt';
2
+ import { ICallOpenApi } from './types';
3
+ export declare const callOpenApi: <T, TDefaultApi>({ func, apiUrl, overrideAuth, refreshToken, logout, newDefaultApi, }: ICallOpenApi<T, TDefaultApi>) => Promise<AxiosWrapper<T | undefined>>;
@@ -12,8 +12,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.callOpenApi = void 0;
13
13
  const cookie_1 = require("./cookie");
14
14
  const sleep_1 = require("../../common/helpers/sleep");
15
+ const array_1 = require("../../common/helpers/array");
15
16
  const callOpenApi = ({ func, apiUrl, overrideAuth, refreshToken, logout, newDefaultApi, }) => __awaiter(void 0, void 0, void 0, function* () {
16
- var _a, _b, _c, _d, _e, _f;
17
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
17
18
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
19
  let error;
19
20
  let data;
@@ -49,11 +50,16 @@ const callOpenApi = ({ func, apiUrl, overrideAuth, refreshToken, logout, newDefa
49
50
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
51
  const ae = e;
51
52
  const status = (_c = ae.response) === null || _c === void 0 ? void 0 : _c.status;
52
- const errorMessage = (((_d = ae.response) === null || _d === void 0 ? void 0 : _d.data) ||
53
- ((_e = ae.response) === null || _e === void 0 ? void 0 : _e.statusText) ||
54
- ((_f = ae.response) === null || _f === void 0 ? void 0 : _f.status) ||
55
- ae.message ||
56
- 'ERROR');
53
+ const errorMessage = [
54
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
+ (_f = (_e = (_d = ae.response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.toString()) !== null && _f !== void 0 ? _f : '',
56
+ (_j = (_h = (_g = ae.response) === null || _g === void 0 ? void 0 : _g.statusText) === null || _h === void 0 ? void 0 : _h.toString()) !== null && _j !== void 0 ? _j : '',
57
+ (_m = (_l = (_k = ae.response) === null || _k === void 0 ? void 0 : _k.status) === null || _l === void 0 ? void 0 : _l.toString()) !== null && _m !== void 0 ? _m : '',
58
+ (_p = (_o = ae.message) === null || _o === void 0 ? void 0 : _o.toString()) !== null && _p !== void 0 ? _p : '',
59
+ ]
60
+ .filter(array_1.notEmpty)
61
+ .sort((a, b) => (a.length < b.length ? -1 : 1))
62
+ .join('\n');
57
63
  if (status === 403 || status === 401) {
58
64
  logout();
59
65
  return {
@@ -13,6 +13,8 @@ export * from './mutex';
13
13
  export * from './mutexData';
14
14
  export * from './plural';
15
15
  export * from './routes';
16
+ export * from './types';
17
+ export * from './useCallOpenApi';
16
18
  export * from './useContextMenu';
17
19
  export * from './useLocalStorage';
18
20
  export * from './useLockBodyScroll';
@@ -25,6 +25,8 @@ __exportStar(require("./mutex"), exports);
25
25
  __exportStar(require("./mutexData"), exports);
26
26
  __exportStar(require("./plural"), exports);
27
27
  __exportStar(require("./routes"), exports);
28
+ __exportStar(require("./types"), exports);
29
+ __exportStar(require("./useCallOpenApi"), exports);
28
30
  __exportStar(require("./useContextMenu"), exports);
29
31
  __exportStar(require("./useLocalStorage"), exports);
30
32
  __exportStar(require("./useLockBodyScroll"), exports);
@@ -0,0 +1,13 @@
1
+ import { AxiosResponse } from 'axios';
2
+ import { User } from './jwt';
3
+ export interface OverrideAuth {
4
+ id_token?: string;
5
+ }
6
+ export interface ICallOpenApi<T, TDefaultApi> {
7
+ func: (f: TDefaultApi) => Promise<AxiosResponse<T>>;
8
+ apiUrl: string;
9
+ overrideAuth?: OverrideAuth;
10
+ logout: () => void;
11
+ refreshToken: () => Promise<User | undefined>;
12
+ newDefaultApi: (config: any) => TDefaultApi;
13
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { AxiosWrapper } from './jwt';
2
+ import { ICallOpenApi } from './types';
3
+ declare type AxiosWrapperWrap<T> = AxiosWrapper<T | undefined> & {
4
+ loaded: boolean;
5
+ loadcount: number;
6
+ };
7
+ export declare const useCallOpenApi: <T, TDefaultApi>(p: ICallOpenApi<T, TDefaultApi> & {
8
+ cacheKey: string;
9
+ }) => AxiosWrapperWrap<T>;
10
+ export {};
@@ -0,0 +1,47 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.useCallOpenApi = void 0;
13
+ const react_1 = require("react");
14
+ const callOpenApi_1 = require("./callOpenApi");
15
+ const defaultV = (def) => ({
16
+ loading: false,
17
+ data: def,
18
+ datetime: new Date().getTime(),
19
+ reFetch: () => __awaiter(void 0, void 0, void 0, function* () { }),
20
+ error: undefined,
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
+ url: undefined,
23
+ //
24
+ loaded: false,
25
+ loadcount: 0,
26
+ });
27
+ const useCallOpenApi = (p) => {
28
+ const [data, setData] = (0, react_1.useState)(defaultV(undefined));
29
+ (0, react_1.useEffect)(() => {
30
+ function run() {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ const res = yield (0, callOpenApi_1.callOpenApi)(p);
33
+ setData((d) => (Object.assign(Object.assign({}, res), { loaded: true, loading: false, loadcount: d.loadcount + 1 })));
34
+ });
35
+ }
36
+ const { error, loaded, loading, loadcount } = data;
37
+ if (loaded || loading || (error && loadcount < 2)) {
38
+ return;
39
+ }
40
+ setData((d) => (Object.assign(Object.assign({}, d), { loading: true })));
41
+ void run();
42
+ }, [data, p, setData]);
43
+ return Object.assign(Object.assign({}, data), { reFetch: () => __awaiter(void 0, void 0, void 0, function* () {
44
+ setData(defaultV(undefined));
45
+ }) });
46
+ };
47
+ exports.useCallOpenApi = useCallOpenApi;
@@ -1,7 +1,7 @@
1
1
  import { AxiosResponse } from 'axios';
2
- import { OverrideAuth } from './callOpenApi';
3
2
  import { CacheItems } from './routes';
4
3
  import { AxiosWrapper, User } from './jwt';
4
+ import { OverrideAuth } from './types';
5
5
  export declare const deleteMutexData: (key: string) => void;
6
6
  export declare const setMutexData: ({ key, data, ttlSeconds, }: {
7
7
  key: string;
@@ -42,22 +42,15 @@ exports.setMutexData = setMutexData;
42
42
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
43
  const getMutexData = (key) => mutexData.getData(key);
44
44
  exports.getMutexData = getMutexData;
45
- function mLock({ key, func, ttlSeconds = 3600, logout, refreshToken, apiUrl, newDefaultApi, overrideAuth, }) {
45
+ function mLock(p) {
46
46
  return __awaiter(this, void 0, void 0, function* () {
47
47
  let unlock;
48
48
  try {
49
- unlock = yield mutex.capture(key);
50
- if (mutexData.getData(key)) {
49
+ unlock = yield mutex.capture(p.key);
50
+ if (mutexData.getData(p.key)) {
51
51
  return;
52
52
  }
53
- const newData = yield (0, callOpenApi_1.callOpenApi)({
54
- apiUrl,
55
- func,
56
- logout,
57
- refreshToken,
58
- newDefaultApi,
59
- overrideAuth,
60
- });
53
+ const newData = yield (0, callOpenApi_1.callOpenApi)(p);
61
54
  if (!newData) {
62
55
  return;
63
56
  }
@@ -65,9 +58,13 @@ function mLock({ key, func, ttlSeconds = 3600, logout, refreshToken, apiUrl, new
65
58
  (0, log_1.error)('api error:', newData.error);
66
59
  }
67
60
  else {
68
- mutexData.setData({ key, data: newData, ttlSeconds });
61
+ mutexData.setData({
62
+ key: p.key,
63
+ data: newData,
64
+ ttlSeconds: p.ttlSeconds,
65
+ });
69
66
  }
70
- mutexData.pingSubscribers(key);
67
+ mutexData.pingSubscribers(p.key);
71
68
  }
72
69
  catch (e) {
73
70
  (0, log_1.error)('mutex error:', e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.77",
3
+ "version": "0.0.83",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",
@@ -13,10 +13,8 @@
13
13
  "start": "tsc --watch"
14
14
  },
15
15
  "dependencies": {
16
- "aws-cdk-lib": "2.8.0",
17
16
  "aws-sdk": "2.1060.0",
18
17
  "axios": "0.25.0",
19
- "constructs": "10.0.39",
20
18
  "jsonwebtoken": "8.5.1",
21
19
  "jwks-rsa": "2.0.5",
22
20
  "openapi-request-validator": "10.0.0",
@@ -24,7 +22,9 @@
24
22
  "react-dom": "17.0.2",
25
23
  "react-hot-toast": "2.2.0",
26
24
  "styled-components": "5.3.3",
27
- "typescript": "4.5.4"
25
+ "typescript": "4.5.5",
26
+ "aws-cdk-lib": "2.x",
27
+ "constructs": "10.x"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/jsonwebtoken": "8.5.8",
@@ -32,8 +32,8 @@
32
32
  "@types/react": "17.0.38",
33
33
  "@types/react-dom": "17.0.11",
34
34
  "@types/styled-components": "5.1.20",
35
- "@typescript-eslint/eslint-plugin": "5.9.1",
36
- "@typescript-eslint/parser": "5.9.1",
35
+ "@typescript-eslint/eslint-plugin": "5.10.0",
36
+ "@typescript-eslint/parser": "5.10.0",
37
37
  "eslint": "8.7.0",
38
38
  "eslint-config-airbnb-typescript": "16.1.0",
39
39
  "eslint-config-prettier": "8.3.0",