ag-common 0.0.67 → 0.0.71

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.
@@ -2,7 +2,7 @@ import { APIGatewayProxyResult, DYNAMOKEYS } from '../types';
2
2
  export declare const returnCode: <T>(statusCode: number, body?: T | undefined, extraHeaders?: {
3
3
  [a: string]: string;
4
4
  } | undefined, fullSiteUrl?: string | undefined) => APIGatewayProxyResult;
5
- export declare const stripPKs: <T>(r: T, removePK?: boolean) => T;
5
+ export declare const stripPKs: <T>(r: T, excludePK?: boolean) => T;
6
6
  export declare const generateDynamoPKS: ({ type, L1, L2, L3, L4, L5, L6, additionalPKValues, }: {
7
7
  type: string;
8
8
  L1: string;
@@ -34,7 +34,12 @@ const returnCode = (statusCode, body, extraHeaders, fullSiteUrl) => {
34
34
  };
35
35
  exports.returnCode = returnCode;
36
36
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
- const stripPKs = (r, removePK = true) => {
37
+ const stripPKs = (r,
38
+ /**
39
+ * default true
40
+ * if true, will keep PK
41
+ */
42
+ excludePK = true) => {
38
43
  if (!r) {
39
44
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
45
  return null;
@@ -42,7 +47,7 @@ const stripPKs = (r, removePK = true) => {
42
47
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
43
48
  // @ts-ignore
44
49
  const { PK, PK1, PK2, PK3, PK4, PK5, L1, L2, L3, L4, L5, L6 } = r, rest = __rest(r, ["PK", "PK1", "PK2", "PK3", "PK4", "PK5", "L1", "L2", "L3", "L4", "L5", "L6"]);
45
- if (removePK) {
50
+ if (!excludePK) {
46
51
  //@ts-ignore
47
52
  rest.PK = PK;
48
53
  }
@@ -11,4 +11,13 @@ export declare const openApiImpl: (p: {
11
11
  certificate: certmgr.ICertificate;
12
12
  hostedZone: route53.IHostedZone;
13
13
  shortStackName: string;
14
+ /**
15
+ * defaults:
16
+ * allowOrigins: apigw.Cors.ALL_ORIGINS,
17
+ allowHeaders: apigw.Cors.DEFAULT_HEADERS,
18
+ */
19
+ cors?: {
20
+ allowOrigins: string[];
21
+ allowHeaders: string[];
22
+ };
14
23
  }) => void;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.openApiImpl = void 0;
4
4
  /* eslint-disable no-new */
5
5
  const aws_cdk_lib_1 = require("aws-cdk-lib");
6
+ const __1 = require("../..");
6
7
  const distinctBy_1 = require("../../common/helpers/distinctBy");
7
8
  const log_1 = require("../../common/helpers/log");
8
9
  // eslint-disable-next-line
@@ -11,11 +12,12 @@ const getPaths = (schema) => Object.entries(schema.paths).map(([fullPath, verbs]
11
12
  pathList: fullPath.split('/').filter((s) => s),
12
13
  verbs: Object.keys(verbs),
13
14
  }));
14
- const setUpApiGw = ({ stack, NODE_ENV, baseUrl, certificate, hostedZone, shortStackName, }) => {
15
+ const setUpApiGw = ({ stack, NODE_ENV, baseUrl, certificate, hostedZone, shortStackName, cors = {
16
+ allowOrigins: aws_cdk_lib_1.aws_apigateway.Cors.ALL_ORIGINS,
17
+ allowHeaders: aws_cdk_lib_1.aws_apigateway.Cors.DEFAULT_HEADERS,
18
+ }, }) => {
15
19
  const api = new aws_cdk_lib_1.aws_apigateway.RestApi(stack, `${shortStackName}-api-${NODE_ENV}`, {
16
- defaultCorsPreflightOptions: {
17
- allowOrigins: aws_cdk_lib_1.aws_apigateway.Cors.ALL_ORIGINS,
18
- },
20
+ defaultCorsPreflightOptions: Object.assign({}, (cors || {})),
19
21
  });
20
22
  const dn = new aws_cdk_lib_1.aws_apigateway.DomainName(stack, 'domain', {
21
23
  domainName: `api.${baseUrl}`,
@@ -33,7 +35,7 @@ const setUpApiGw = ({ stack, NODE_ENV, baseUrl, certificate, hostedZone, shortSt
33
35
  return api;
34
36
  };
35
37
  const getDynamoPermissions = ({ lambdaPermissions, pathV, verb, seenPermissions, }) => {
36
- const pathCompute = pathV + '-' + verb;
38
+ const pathCompute = pathV + '/' + verb;
37
39
  const lp = lambdaPermissions === null || lambdaPermissions === void 0 ? void 0 : lambdaPermissions[pathCompute];
38
40
  if (lp) {
39
41
  seenPermissions[pathCompute] = true;
@@ -48,30 +50,25 @@ const getDynamoPermissions = ({ lambdaPermissions, pathV, verb, seenPermissions,
48
50
  //
49
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);
50
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);
53
+ const policies = [...(def.policies || []), ...((lp === null || lp === void 0 ? void 0 : lp.policies) || [])].filter(__1.notEmpty);
51
54
  const tables = [...readTables, ...writeTables];
52
55
  const environment = {};
53
56
  Object.values(tables).forEach((v) => {
54
57
  environment[v.shortName] = v.table.tableName;
55
58
  });
56
- return { environment, readTables, writeTables };
59
+ return { environment, readTables, writeTables, policies };
57
60
  };
58
- const addApiPaths = (api, subs, apiRoots) => {
59
- let apiPath;
60
- subs.forEach((s, i) => {
61
- if (i === 0) {
62
- if (apiRoots[s]) {
63
- apiPath = apiRoots[s];
64
- }
65
- else {
66
- apiPath = api.root.addResource(s);
67
- apiRoots[s] = apiPath;
68
- }
61
+ const addApiPaths = (api, pathList, apiRoots) => {
62
+ let pathBuild = '';
63
+ let apiPath = api.root;
64
+ pathList.forEach((path) => {
65
+ pathBuild += '/' + path;
66
+ if (apiRoots[pathBuild]) {
67
+ apiPath = apiRoots[pathBuild];
69
68
  }
70
69
  else {
71
- if (!apiPath) {
72
- throw new Error('no apipath');
73
- }
74
- apiPath = apiPath.addResource(s);
70
+ apiRoots[pathBuild] = apiPath.addResource(path);
71
+ apiPath = apiRoots[pathBuild];
75
72
  }
76
73
  });
77
74
  if (!apiPath) {
@@ -92,13 +89,14 @@ const openApiImpl = (p) => {
92
89
  paths.forEach(({ fullPath, verbs, pathList }) => {
93
90
  const apiPath = addApiPaths(api, pathList, apiRoots);
94
91
  verbs.forEach((verb) => {
95
- const { environment, readTables, writeTables } = getDynamoPermissions({
92
+ const { environment, readTables, writeTables, policies } = getDynamoPermissions({
96
93
  lambdaPermissions,
97
94
  pathV: fullPath,
98
95
  verb,
99
96
  seenPermissions,
100
97
  });
101
98
  const lambdaName = lambdaNameSafe(`${p.shortStackName}-${fullPath}-${verb}-${NODE_ENV}`);
99
+ const entry = `${endpointsBase}${fullPath}/${verb.toUpperCase()}.ts`;
102
100
  const lambdaV = new aws_cdk_lib_1.aws_lambda_nodejs.NodejsFunction(stack, lambdaName, {
103
101
  functionName: lambdaName,
104
102
  runtime: aws_cdk_lib_1.aws_lambda.Runtime.NODEJS_14_X,
@@ -107,7 +105,7 @@ const openApiImpl = (p) => {
107
105
  memorySize: 128,
108
106
  timeout: aws_cdk_lib_1.Duration.seconds(30),
109
107
  description: '(cdk)',
110
- entry: `${endpointsBase}${fullPath}/${verb.toUpperCase()}.ts`,
108
+ entry: entry,
111
109
  bundling: {
112
110
  externalModules: ['aws-sdk', 'aws-lambda'],
113
111
  },
@@ -116,6 +114,7 @@ const openApiImpl = (p) => {
116
114
  });
117
115
  readTables.forEach((t) => t.table.grantReadData(lambdaV));
118
116
  writeTables.forEach((t) => t.table.grantReadWriteData(lambdaV));
117
+ policies.forEach((p1) => lambdaV.addToRolePolicy(p1));
119
118
  //
120
119
  apiPath.addMethod(verb.toUpperCase(), new aws_cdk_lib_1.aws_apigateway.LambdaIntegration(lambdaV, {}));
121
120
  });
@@ -1,4 +1,4 @@
1
- import { aws_dynamodb as dynamodb } from 'aws-cdk-lib';
1
+ import { aws_dynamodb as dynamodb, aws_iam as iam } from 'aws-cdk-lib';
2
2
  import { Key } from './aws';
3
3
  export interface DYNAMOKEYS {
4
4
  type: string;
@@ -21,10 +21,12 @@ export interface ILambdaPermissions {
21
21
  [pathHyphenVerb: string]: {
22
22
  reads?: IGeneratedDynamoData[];
23
23
  writes?: IGeneratedDynamoData[];
24
+ policies?: iam.PolicyStatement[];
24
25
  } & {
25
26
  default?: {
26
27
  reads?: IGeneratedDynamoData[];
27
28
  writes?: IGeneratedDynamoData[];
29
+ policies?: iam.PolicyStatement[];
28
30
  };
29
31
  };
30
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.67",
3
+ "version": "0.0.71",
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,10 @@
13
13
  "start": "tsc --watch"
14
14
  },
15
15
  "dependencies": {
16
- "aws-cdk-lib": "2.7.0",
17
- "aws-sdk": "2.1056.0",
16
+ "aws-cdk-lib": "2.8.0",
17
+ "aws-sdk": "2.1058.0",
18
18
  "axios": "0.24.0",
19
- "constructs": "10.0.32",
19
+ "constructs": "10.0.36",
20
20
  "jsonwebtoken": "8.5.1",
21
21
  "jwks-rsa": "2.0.5",
22
22
  "openapi-request-validator": "10.0.0",
@@ -27,8 +27,8 @@
27
27
  "typescript": "4.5.4"
28
28
  },
29
29
  "devDependencies": {
30
- "@types/jsonwebtoken": "8.5.7",
31
- "@types/node": "17.0.8",
30
+ "@types/jsonwebtoken": "8.5.8",
31
+ "@types/node": "17.0.9",
32
32
  "@types/react": "17.0.38",
33
33
  "@types/react-dom": "17.0.11",
34
34
  "@types/styled-components": "5.1.20",