ag-common 0.0.80 → 0.0.86

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.
@@ -34,7 +34,8 @@ const setUpApiGw = ({ stack, NODE_ENV, baseUrl, certificate, hostedZone, shortSt
34
34
  });
35
35
  return api;
36
36
  };
37
- const getDynamoPermissions = ({ lambdaPermissions, pathV, verb, seenPermissions, }) => {
37
+ const setupLambda = ({ lambdaPermissions, pathV, verb, seenPermissions, }) => {
38
+ var _a, _b, _c, _d;
38
39
  const pathCompute = pathV + '/' + verb;
39
40
  const lp = lambdaPermissions === null || lambdaPermissions === void 0 ? void 0 : lambdaPermissions[pathCompute];
40
41
  if (lp) {
@@ -48,11 +49,12 @@ const getDynamoPermissions = ({ lambdaPermissions, pathV, verb, seenPermissions,
48
49
  seenPermissions.default = true;
49
50
  }
50
51
  //
51
- const readTables = (0, distinctBy_1.distinctBy)([...((def === null || def === void 0 ? void 0 : def.reads) || []), ...((lp === null || lp === void 0 ? void 0 : lp.reads) || [])], (s) => s.shortName);
52
- const writeTables = (0, distinctBy_1.distinctBy)([...((def === null || def === void 0 ? void 0 : def.writes) || []), ...((lp === null || lp === void 0 ? void 0 : lp.writes) || [])], (s) => s.shortName);
52
+ const readTables = (0, distinctBy_1.distinctBy)([...(((_a = def === null || def === void 0 ? void 0 : def.dynamo) === null || _a === void 0 ? void 0 : _a.reads) || []), ...(((_b = lp === null || lp === void 0 ? void 0 : lp.dynamo) === null || _b === void 0 ? void 0 : _b.reads) || [])], (s) => s.shortName);
53
+ const writeTables = (0, distinctBy_1.distinctBy)([...(((_c = def === null || def === void 0 ? void 0 : def.dynamo) === null || _c === void 0 ? void 0 : _c.writes) || []), ...(((_d = lp === null || lp === void 0 ? void 0 : lp.dynamo) === null || _d === void 0 ? void 0 : _d.writes) || [])], (s) => s.shortName);
53
54
  const policies = [...(def.policies || []), ...((lp === null || lp === void 0 ? void 0 : lp.policies) || [])].filter(__1.notEmpty);
55
+ const env = Object.assign(Object.assign({}, (def.env || {})), ((lp === null || lp === void 0 ? void 0 : lp.env) || {}));
54
56
  const tables = [...readTables, ...writeTables];
55
- const environment = {};
57
+ const environment = env;
56
58
  Object.values(tables).forEach((v) => {
57
59
  environment[v.shortName] = v.table.tableName;
58
60
  });
@@ -89,7 +91,7 @@ const openApiImpl = (p) => {
89
91
  paths.forEach(({ fullPath, verbs, pathList }) => {
90
92
  const apiPath = addApiPaths(api, pathList, apiRoots);
91
93
  verbs.forEach((verb) => {
92
- const { environment, readTables, writeTables, policies } = getDynamoPermissions({
94
+ const { environment, readTables, writeTables, policies } = setupLambda({
93
95
  lambdaPermissions,
94
96
  pathV: fullPath,
95
97
  verb,
@@ -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
  });
@@ -17,17 +17,17 @@ export interface IGeneratedDynamoData {
17
17
  table: dynamodb.ITable;
18
18
  shortName: string;
19
19
  }
20
- export interface ILambdaPermissions {
21
- [pathHyphenVerb: string]: {
20
+ export interface ILambdaPermission {
21
+ dynamo?: {
22
22
  reads?: IGeneratedDynamoData[];
23
23
  writes?: IGeneratedDynamoData[];
24
- policies?: iam.PolicyStatement[];
25
- } & {
26
- default?: {
27
- reads?: IGeneratedDynamoData[];
28
- writes?: IGeneratedDynamoData[];
29
- policies?: iam.PolicyStatement[];
30
- };
24
+ };
25
+ policies?: iam.PolicyStatement[];
26
+ env?: Record<string, string>;
27
+ }
28
+ export interface ILambdaPermissions {
29
+ [pathHyphenVerb: string]: ILambdaPermission & {
30
+ default?: ILambdaPermission;
31
31
  };
32
32
  }
33
33
  export interface IQueryDynamo {
@@ -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,14 @@
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
+ disabled?: boolean;
14
+ }
@@ -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 (p.disabled || 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.80",
3
+ "version": "0.0.86",
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
- "aws-sdk": "2.1060.0",
16
+ "aws-sdk": "2.1062.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,14 +22,16 @@
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.5"
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",
31
31
  "@types/node": "17.0.10",
32
32
  "@types/react": "17.0.38",
33
33
  "@types/react-dom": "17.0.11",
34
- "@types/styled-components": "5.1.20",
34
+ "@types/styled-components": "5.1.21",
35
35
  "@typescript-eslint/eslint-plugin": "5.10.0",
36
36
  "@typescript-eslint/parser": "5.10.0",
37
37
  "eslint": "8.7.0",