ag-common 0.0.78 → 0.0.84

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,13 +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, rangeName, rangeValue, }: {
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
19
  rangeName?: string | undefined;
20
- rangeValue?: string | undefined;
20
+ rangeKeys?: string[] | undefined;
21
21
  }) => Promise<{
22
22
  error: string;
23
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,24 @@ 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, rangeName, rangeValue, }) => __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
119
  let params = {
119
120
  TableName: tableName,
120
121
  Key: { [pkName]: key },
121
122
  };
122
123
  if (rangeName) {
123
- params.Key[rangeName] = rangeValue;
124
+ let rangeValue = rangeKeys === null || rangeKeys === void 0 ? void 0 : rangeKeys[i];
125
+ if (rangeValue) {
126
+ params.Key[rangeName] = rangeValue;
127
+ }
124
128
  }
125
129
  const res = yield exports.dynamoDb.delete(params).promise();
126
130
  const newError = (_d = (_c = res.$response) === null || _c === void 0 ? void 0 : _c.error) !== null && _d !== void 0 ? _d : null;
@@ -130,7 +134,7 @@ const batchDelete = ({ tableName, keys, breakOnError = true, pkName, rangeName,
130
134
  breakV = true;
131
135
  }
132
136
  }
133
- }
137
+ }));
134
138
  if ((error === null || error === void 0 ? void 0 : error.length) > 0) {
135
139
  const me = error.join('\n');
136
140
  (0, log_1.error)(`batch write error=${me}`);
@@ -58,7 +58,7 @@ function validateOpenApi({ event, next, authorized, schema, COGNITO_USER_POOL_ID
58
58
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
59
  params: undefined,
60
60
  query: event.queryStringParameters,
61
- body: event.body && JSON.parse(event.body),
61
+ body: (0, object_1.tryJsonParse)(event.body, event.body),
62
62
  headers: (0, object_1.objectKeysToLowerCase)(event === null || event === void 0 ? void 0 : event.headers),
63
63
  };
64
64
  const method = event.requestContext.httpMethod.toLowerCase();
@@ -116,7 +116,7 @@ function validateOpenApi({ event, next, authorized, schema, COGNITO_USER_POOL_ID
116
116
  const res = yield next({
117
117
  params,
118
118
  event,
119
- body: (event.body && JSON.parse(event.body)),
119
+ body: (0, object_1.tryJsonParse)(event.body, event.body),
120
120
  userProfile,
121
121
  lang: (0, i18n_1.getValidatedLang)((_e = event.headers['x-lang']) !== null && _e !== void 0 ? _e : ''),
122
122
  });
@@ -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>>;
@@ -51,6 +51,7 @@ const callOpenApi = ({ func, apiUrl, overrideAuth, refreshToken, logout, newDefa
51
51
  const ae = e;
52
52
  const status = (_c = ae.response) === null || _c === void 0 ? void 0 : _c.status;
53
53
  const errorMessage = [
54
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
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 : '',
55
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 : '',
56
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 : '',
@@ -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.78",
3
+ "version": "0.0.84",
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",