ag-common 0.0.325 → 0.0.329

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.
@@ -0,0 +1,5 @@
1
+ export declare const setCF: (region: string) => void;
2
+ export declare const invalidateCloudfrontPath: ({ path, distributionId, }: {
3
+ path: string;
4
+ distributionId: string;
5
+ }) => Promise<void>;
@@ -0,0 +1,35 @@
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.invalidateCloudfrontPath = exports.setCF = void 0;
16
+ const cloudfront_1 = __importDefault(require("aws-sdk/clients/cloudfront"));
17
+ let cf = new cloudfront_1.default();
18
+ const setCF = (region) => {
19
+ cf = new cloudfront_1.default({ region });
20
+ };
21
+ exports.setCF = setCF;
22
+ const invalidateCloudfrontPath = ({ path, distributionId, }) => __awaiter(void 0, void 0, void 0, function* () {
23
+ const params = {
24
+ DistributionId: distributionId,
25
+ InvalidationBatch: {
26
+ CallerReference: 'scraper',
27
+ Paths: {
28
+ Quantity: 1,
29
+ Items: [path],
30
+ },
31
+ },
32
+ };
33
+ yield cf.createInvalidation(params).promise();
34
+ });
35
+ exports.invalidateCloudfrontPath = invalidateCloudfrontPath;
@@ -2,16 +2,17 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateSSMParams = exports.generateTableRef = void 0;
4
4
  const aws_cdk_lib_1 = require("aws-cdk-lib");
5
+ const ssm_1 = require("./ssm");
5
6
  const generateTableRef = ({ stack, baseKey, hasStream, hasGSI, }) => {
6
7
  const tableStreamArn = !hasStream
7
8
  ? undefined
8
- : aws_cdk_lib_1.aws_ssm.StringParameter.valueForStringParameter(stack, `${baseKey}/streamArn`);
9
+ : (0, ssm_1.loadSsmString)({ stack, path: `${baseKey}/streamArn` });
9
10
  const globalIndexes = !hasGSI
10
11
  ? undefined
11
- : aws_cdk_lib_1.Fn.split(',', aws_cdk_lib_1.aws_ssm.StringParameter.valueForStringParameter(stack, `${baseKey}/gsi`));
12
+ : (0, ssm_1.loadSsmStringList)({ stack, path: `${baseKey}/gsi` });
12
13
  const shortName = aws_cdk_lib_1.aws_ssm.StringParameter.valueFromLookup(stack, `${baseKey}/shortName`);
13
14
  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
+ tableArn: (0, ssm_1.loadSsmString)({ stack, path: `${baseKey}/arn` }),
15
16
  tableStreamArn,
16
17
  globalIndexes,
17
18
  });
@@ -22,43 +23,25 @@ const generateTableRef = ({ stack, baseKey, hasStream, hasGSI, }) => {
22
23
  };
23
24
  exports.generateTableRef = generateTableRef;
24
25
  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
- });
26
+ (0, ssm_1.saveToSsm)({ stack, path: `${baseKey}/arn`, value: table.tableArn });
33
27
  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,
28
+ (0, ssm_1.saveToSsm)({
29
+ stack,
30
+ path: `${baseKey}/streamArn`,
31
+ value: table.tableStreamArn,
41
32
  });
42
33
  }
43
34
  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,
35
+ (0, ssm_1.saveToSsm)({
36
+ stack,
37
+ path: `${baseKey}/gsi`,
38
+ value: gsi.join(','),
52
39
  });
53
40
  }
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,
41
+ (0, ssm_1.saveToSsm)({
42
+ stack,
43
+ path: `${baseKey}/shortName`,
44
+ value: shortName,
62
45
  });
63
46
  };
64
47
  exports.generateSSMParams = generateSSMParams;
@@ -1,5 +1,6 @@
1
1
  export * from './api';
2
2
  export * from './aws';
3
+ export * from './cloudfront';
3
4
  export * from './dynamo';
4
5
  export * from './enforceDynamoProvisionCap';
5
6
  export * from './dynamoInfra';
@@ -7,5 +8,6 @@ export * from './openApiHelpers';
7
8
  export * from './s3';
8
9
  export * from './ses';
9
10
  export * from './sqs';
11
+ export * from './ssm';
10
12
  export * from './validateOpenApi';
11
13
  export * from './validations';
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./api"), exports);
18
18
  __exportStar(require("./aws"), exports);
19
+ __exportStar(require("./cloudfront"), exports);
19
20
  __exportStar(require("./dynamo"), exports);
20
21
  __exportStar(require("./enforceDynamoProvisionCap"), exports);
21
22
  __exportStar(require("./dynamoInfra"), exports);
@@ -23,5 +24,6 @@ __exportStar(require("./openApiHelpers"), exports);
23
24
  __exportStar(require("./s3"), exports);
24
25
  __exportStar(require("./ses"), exports);
25
26
  __exportStar(require("./sqs"), exports);
27
+ __exportStar(require("./ssm"), exports);
26
28
  __exportStar(require("./validateOpenApi"), exports);
27
29
  __exportStar(require("./validations"), exports);
@@ -0,0 +1,23 @@
1
+ import { Stack } from 'aws-cdk-lib';
2
+ /**
3
+ * save a value to ssm
4
+ * @param param0
5
+ */
6
+ export declare const saveToSsm: ({ stack, path, value, }: {
7
+ /** eg `/${NODE_ENV}/${shortStackName}/service/key` */
8
+ path: string;
9
+ stack: Stack;
10
+ value: string;
11
+ }) => void;
12
+ /** load a string from ssm */
13
+ export declare const loadSsmString: ({ stack, path, }: {
14
+ stack: Stack;
15
+ /** eg `/${NODE_ENV}/${shortStackName}/service/key` */
16
+ path: string;
17
+ }) => string;
18
+ /** load a csv from ssm */
19
+ export declare const loadSsmStringList: ({ stack, path, }: {
20
+ stack: Stack;
21
+ /** eg `/${NODE_ENV}/${shortStackName}/service/key` */
22
+ path: string;
23
+ }) => string[];
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadSsmStringList = exports.loadSsmString = exports.saveToSsm = void 0;
4
+ const aws_cdk_lib_1 = require("aws-cdk-lib");
5
+ const common_1 = require("../../common");
6
+ /**
7
+ * save a value to ssm
8
+ * @param param0
9
+ */
10
+ const saveToSsm = ({ stack, path, value, }) => {
11
+ const name = (0, common_1.toBase64)(path);
12
+ new aws_cdk_lib_1.aws_ssm.StringParameter(stack, name, {
13
+ allowedPattern: '.*',
14
+ description: `${name} (cdk)`,
15
+ parameterName: path,
16
+ stringValue: value,
17
+ tier: aws_cdk_lib_1.aws_ssm.ParameterTier.STANDARD,
18
+ });
19
+ };
20
+ exports.saveToSsm = saveToSsm;
21
+ /** load a string from ssm */
22
+ const loadSsmString = ({ stack, path, }) => aws_cdk_lib_1.aws_ssm.StringParameter.valueForStringParameter(stack, path);
23
+ exports.loadSsmString = loadSsmString;
24
+ /** load a csv from ssm */
25
+ const loadSsmStringList = ({ stack, path, }) => aws_cdk_lib_1.Fn.split(',', (0, exports.loadSsmString)({ stack, path }));
26
+ exports.loadSsmStringList = loadSsmStringList;
@@ -34,13 +34,16 @@ const jwtVerify = ({ token, jwksUri, issuer, }) => __awaiter(void 0, void 0, voi
34
34
  }
35
35
  jwksClient.getSigningKey(header.kid, (errorV, key) => {
36
36
  if (errorV) {
37
- (0, log_1.error)(`signing key error. jwks=${jwksUri} iss=${issuer}`);
38
- reject(errorV);
37
+ const msg = `signing key error. jwks=${jwksUri} iss=${issuer}` + errorV;
38
+ (0, log_1.error)(msg);
39
+ reject(msg);
39
40
  return;
40
41
  }
41
42
  const signingKey = (key === null || key === void 0 ? void 0 : key.publicKey) || (key === null || key === void 0 ? void 0 : key.rsaPublicKey) || undefined;
42
43
  if (!signingKey) {
43
- callback('no key');
44
+ const msg = `no key error`;
45
+ (0, log_1.error)(msg);
46
+ reject(msg);
44
47
  }
45
48
  else {
46
49
  callback(null, signingKey);
@@ -0,0 +1,2 @@
1
+ export declare const retryHttpCodes: number[];
2
+ export declare const retryHttpMs = 1500;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.retryHttpMs = exports.retryHttpCodes = void 0;
4
+ exports.retryHttpCodes = [429, 408, 502, 503, 504];
5
+ exports.retryHttpMs = 1500;
@@ -34,7 +34,7 @@ function clamp({ value, min, max, }) {
34
34
  }
35
35
  exports.clamp = clamp;
36
36
  function sumArray(array) {
37
- return array.reduce((a, b) => a + b);
37
+ return array.reduce((a, b) => a + b, 0);
38
38
  }
39
39
  exports.sumArray = sumArray;
40
40
  const getRandomInt = (max) => {
@@ -1 +1,2 @@
1
+ export * from './const';
1
2
  export * from './helpers';
@@ -14,4 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./const"), exports);
17
18
  __exportStar(require("./helpers"), exports);
@@ -23,6 +23,7 @@ const axios_1 = __importDefault(require("axios"));
23
23
  * @returns
24
24
  */
25
25
  const axiosHelper = ({ verb, url, body, headers, timeout = 30000, retryMax = 0, onStaleAuth, }) => __awaiter(void 0, void 0, void 0, function* () {
26
+ var _a;
26
27
  let retry = 0;
27
28
  let ret;
28
29
  do {
@@ -70,19 +71,20 @@ const axiosHelper = ({ verb, url, body, headers, timeout = 30000, retryMax = 0,
70
71
  }
71
72
  catch (e) {
72
73
  const em = e;
74
+ const c = Number((_a = em.code) !== null && _a !== void 0 ? _a : '500');
73
75
  // jwt expired or bad response
74
76
  // 403 returned for old token - will be refreshed
75
- if (em.code === '401' || em.code === '403') {
77
+ if (c === 401 || c === 403) {
76
78
  (0, log_1.debug)('auth expired');
77
79
  onStaleAuth === null || onStaleAuth === void 0 ? void 0 : onStaleAuth();
78
80
  retry = retryMax;
79
81
  }
80
- if (retry >= retryMax) {
82
+ if (!common_1.retryHttpCodes.includes(c) || retry >= retryMax) {
81
83
  throw em;
82
84
  }
83
85
  }
84
86
  retry += 1;
85
- yield (0, common_1.sleep)(1000);
87
+ yield (0, common_1.sleep)(common_1.retryHttpMs);
86
88
  } while (retry <= retryMax);
87
89
  throw new Error('unexpected');
88
90
  });
@@ -14,6 +14,7 @@ const cookie_1 = require("../cookie");
14
14
  const sleep_1 = require("../../../common/helpers/sleep");
15
15
  const array_1 = require("../../../common/helpers/array");
16
16
  const useLocalStorage_1 = require("../useLocalStorage");
17
+ const const_1 = require("../../../common/const");
17
18
  /**
18
19
  * get the id_token from provided value, or cookie, or LS
19
20
  */
@@ -57,7 +58,7 @@ function getIdTokenAuthHeader({ overrideAuth, refreshToken, }) {
57
58
  });
58
59
  }
59
60
  const callOpenApi = (p) => __awaiter(void 0, void 0, void 0, function* () {
60
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
61
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
61
62
  const { func, apiUrl, logout, newDefaultApi, headers } = p;
62
63
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
63
64
  let error;
@@ -87,13 +88,13 @@ const callOpenApi = (p) => __awaiter(void 0, void 0, void 0, function* () {
87
88
  catch (e) {
88
89
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
89
90
  const ae = e;
90
- const status = (_a = ae.response) === null || _a === void 0 ? void 0 : _a.status;
91
+ const status = (_b = (_a = ae.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 500;
91
92
  const errorMessage = [
92
93
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
93
- (_d = (_c = (_b = ae.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : '',
94
- (_g = (_f = (_e = ae.response) === null || _e === void 0 ? void 0 : _e.statusText) === null || _f === void 0 ? void 0 : _f.toString()) !== null && _g !== void 0 ? _g : '',
95
- (_k = (_j = (_h = ae.response) === null || _h === void 0 ? void 0 : _h.status) === null || _j === void 0 ? void 0 : _j.toString()) !== null && _k !== void 0 ? _k : '',
96
- (_m = (_l = ae.message) === null || _l === void 0 ? void 0 : _l.toString()) !== null && _m !== void 0 ? _m : '',
94
+ (_e = (_d = (_c = ae.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.toString()) !== null && _e !== void 0 ? _e : '',
95
+ (_h = (_g = (_f = ae.response) === null || _f === void 0 ? void 0 : _f.statusText) === null || _g === void 0 ? void 0 : _g.toString()) !== null && _h !== void 0 ? _h : '',
96
+ (_l = (_k = (_j = ae.response) === null || _j === void 0 ? void 0 : _j.status) === null || _k === void 0 ? void 0 : _k.toString()) !== null && _l !== void 0 ? _l : '',
97
+ (_o = (_m = ae.message) === null || _m === void 0 ? void 0 : _m.toString()) !== null && _o !== void 0 ? _o : '',
97
98
  ]
98
99
  .filter(array_1.notEmpty)
99
100
  .sort((a, b) => (a.length < b.length ? -1 : 1))
@@ -105,13 +106,12 @@ const callOpenApi = (p) => __awaiter(void 0, void 0, void 0, function* () {
105
106
  data: undefined,
106
107
  };
107
108
  }
108
- if (status !== 500 || errorCount === errorMax) {
109
+ if (!const_1.retryHttpCodes.includes(status) || errorCount === errorMax) {
109
110
  error = Object.assign(Object.assign({}, ae), { message: errorMessage });
110
111
  break;
111
112
  }
112
113
  }
113
- // eslint-disable-next-line no-await-in-loop
114
- yield (0, sleep_1.sleep)(2000);
114
+ yield (0, sleep_1.sleep)(const_1.retryHttpMs);
115
115
  }
116
116
  return Object.assign({ data }, (error && { error }));
117
117
  });
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.325",
3
+ "version": "0.0.329",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",
7
7
  "repository": "github:andreigec/ag-common",
8
8
  "license": "ISC",
9
- "packageManager": "yarn@3.2.2",
9
+ "packageManager": "yarn@3.2.3",
10
10
  "scripts": {
11
11
  "format": "npx eslint --ext .ts,.tsx ./src --fix",
12
12
  "build": "rimraf dist && yarn lint && tsc",
@@ -41,8 +41,8 @@
41
41
  "@storybook/addons": "6.5.10",
42
42
  "@storybook/react": "6.5.10",
43
43
  "@storybook/theming": "6.5.10",
44
- "@types/jsonwebtoken": "8.5.8",
45
- "@types/node": "18.7.9",
44
+ "@types/jsonwebtoken": "8.5.9",
45
+ "@types/node": "18.7.13",
46
46
  "@types/react": "18.0.17",
47
47
  "@types/react-dom": "18.0.6",
48
48
  "@types/styled-components": "5.1.26",