lesgo 2.1.4 → 2.1.5

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.
@@ -1,9 +1,5 @@
1
1
  declare const _default: {
2
2
  region: string;
3
- queues: {
4
- alias: string;
5
- name: string;
6
- url: string;
7
- }[];
3
+ queueAliases: string[];
8
4
  };
9
5
  export default _default;
@@ -1,18 +1,9 @@
1
1
  var _a;
2
2
  import awsConfig from './aws';
3
- import appConfig from './app';
4
- const sqsQueueAliases =
5
- ((_a = process.env.LESGO_AWS_SQS_QUEUE_ALIASES) === null || _a === void 0
6
- ? void 0
7
- : _a.split(',')) || [];
8
- const region = process.env.LESGO_AWS_SQS_REGION || awsConfig.region;
9
3
  export default {
10
4
  region: process.env.LESGO_AWS_SQS_REGION || awsConfig.region,
11
- queues: sqsQueueAliases.map(q => ({
12
- alias: q,
13
- name: `${appConfig.stackName}-${q}`,
14
- url: `https://sqs.${region}.amazonaws.com/${
15
- awsConfig.accountId
16
- }/${`${appConfig.stackName}-${q}`}`,
17
- })),
5
+ queueAliases:
6
+ ((_a = process.env.LESGO_AWS_SQS_QUEUE_ALIASES) === null || _a === void 0
7
+ ? void 0
8
+ : _a.split(',')) || [],
18
9
  };
@@ -1,4 +1,5 @@
1
- import { ConnectionOptions } from 'mysql2/promise';
1
+ import { ConnectionOptions, FieldPacket, QueryResult } from 'mysql2/promise';
2
2
  import { RDSAuroraMySQLProxyClientOptions } from '../../types/aws';
3
- declare const query: (sql: string, preparedValues?: any[], connOptions?: ConnectionOptions, clientOpts?: RDSAuroraMySQLProxyClientOptions) => Promise<[import("mysql2/promise").QueryResult, import("mysql2/typings/mysql/lib/protocol/packets/FieldPacket").FieldPacket[]]>;
3
+ type QueryReturn<T> = [T, FieldPacket[]];
4
+ declare const query: <T = QueryResult>(sql: string, preparedValues?: any[], connOptions?: ConnectionOptions, clientOpts?: RDSAuroraMySQLProxyClientOptions) => Promise<QueryReturn<T>>;
4
5
  export default query;
@@ -0,0 +1,10 @@
1
+ import { ChangeMessageVisibilityCommandInput } from '@aws-sdk/client-sqs';
2
+ import { ClientOptions } from '../../types/aws';
3
+ import { Queue } from './getQueueUrl';
4
+ export interface changeMessageVisibilityOptions extends Partial<Omit<ChangeMessageVisibilityCommandInput, 'QueueUrl' | 'ReceiptHandle' | 'VisibilityTimeout'>> {
5
+ QueueUrl?: string;
6
+ ReceiptHandle?: string;
7
+ VisibilityTimeout?: number;
8
+ }
9
+ declare const changeMessageVisibility: (queue: string | Queue, receiptHandle: string, visibilityTimeout: number, opts?: changeMessageVisibilityOptions, clientOpts?: ClientOptions) => Promise<void>;
10
+ export default changeMessageVisibility;
@@ -0,0 +1,80 @@
1
+ var __awaiter =
2
+ (this && this.__awaiter) ||
3
+ function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) {
5
+ return value instanceof P
6
+ ? value
7
+ : new P(function (resolve) {
8
+ resolve(value);
9
+ });
10
+ }
11
+ return new (P || (P = Promise))(function (resolve, reject) {
12
+ function fulfilled(value) {
13
+ try {
14
+ step(generator.next(value));
15
+ } catch (e) {
16
+ reject(e);
17
+ }
18
+ }
19
+ function rejected(value) {
20
+ try {
21
+ step(generator['throw'](value));
22
+ } catch (e) {
23
+ reject(e);
24
+ }
25
+ }
26
+ function step(result) {
27
+ result.done
28
+ ? resolve(result.value)
29
+ : adopt(result.value).then(fulfilled, rejected);
30
+ }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ import { ChangeMessageVisibilityCommand } from '@aws-sdk/client-sqs';
35
+ import LesgoException from '../../exceptions/LesgoException';
36
+ import { logger, validateFields } from '../../utils';
37
+ import getClient from './getClient';
38
+ import getQueueUrl from './getQueueUrl';
39
+ const FILE = 'lesgo.services.SQSService.changeMessageVisibility';
40
+ const changeMessageVisibility = (
41
+ queue,
42
+ receiptHandle,
43
+ visibilityTimeout,
44
+ opts,
45
+ clientOpts
46
+ ) =>
47
+ __awaiter(void 0, void 0, void 0, function* () {
48
+ const queueUrl = getQueueUrl(queue);
49
+ const input = validateFields({ receiptHandle, visibilityTimeout }, [
50
+ { key: 'receiptHandle', type: 'string', required: true },
51
+ { key: 'visibilityTimeout', type: 'number', required: true },
52
+ ]);
53
+ const client = getClient(clientOpts);
54
+ const commandInput = Object.assign(
55
+ {
56
+ QueueUrl: queueUrl,
57
+ ReceiptHandle: input.receiptHandle,
58
+ VisibilityTimeout: input.visibilityTimeout,
59
+ },
60
+ opts
61
+ );
62
+ try {
63
+ yield client.send(new ChangeMessageVisibilityCommand(commandInput));
64
+ logger.debug(`${FILE}::MESSAGE_VISIBILITY_UPDATED`, {
65
+ commandInput,
66
+ });
67
+ } catch (error) {
68
+ throw new LesgoException(
69
+ 'Error occurred updating message visibility timeout',
70
+ `${FILE}::MESSAGE_VISIBILITY_TIMEOUT_UPDATED_ERROR`,
71
+ 500,
72
+ {
73
+ error,
74
+ commandInput,
75
+ opts,
76
+ }
77
+ );
78
+ }
79
+ });
80
+ export default changeMessageVisibility;
@@ -0,0 +1,8 @@
1
+ import { GetQueueAttributesCommandInput } from '@aws-sdk/client-sqs';
2
+ import { ClientOptions } from '../../types/aws';
3
+ import { Queue } from './getQueueUrl';
4
+ export interface GetQueueCountOptions extends Partial<Omit<GetQueueAttributesCommandInput, 'QueueUrl'>> {
5
+ QueueUrl?: string;
6
+ }
7
+ declare const getQueueCount: (queue: string | Queue, opts?: GetQueueCountOptions, clientOpts?: ClientOptions) => Promise<import("@aws-sdk/client-sqs").GetQueueAttributesCommandOutput>;
8
+ export default getQueueCount;
@@ -0,0 +1,76 @@
1
+ var __awaiter =
2
+ (this && this.__awaiter) ||
3
+ function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) {
5
+ return value instanceof P
6
+ ? value
7
+ : new P(function (resolve) {
8
+ resolve(value);
9
+ });
10
+ }
11
+ return new (P || (P = Promise))(function (resolve, reject) {
12
+ function fulfilled(value) {
13
+ try {
14
+ step(generator.next(value));
15
+ } catch (e) {
16
+ reject(e);
17
+ }
18
+ }
19
+ function rejected(value) {
20
+ try {
21
+ step(generator['throw'](value));
22
+ } catch (e) {
23
+ reject(e);
24
+ }
25
+ }
26
+ function step(result) {
27
+ result.done
28
+ ? resolve(result.value)
29
+ : adopt(result.value).then(fulfilled, rejected);
30
+ }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ import { GetQueueAttributesCommand } from '@aws-sdk/client-sqs';
35
+ import LesgoException from '../../exceptions/LesgoException';
36
+ import { logger } from '../../utils';
37
+ import getClient from './getClient';
38
+ import getQueueUrl from './getQueueUrl';
39
+ const FILE = 'lesgo.services.SQSService.getQueueCount';
40
+ const getQueueCount = (queue, opts, clientOpts) =>
41
+ __awaiter(void 0, void 0, void 0, function* () {
42
+ const queueUrl = getQueueUrl(queue);
43
+ const client = getClient(clientOpts);
44
+ const commandInput = Object.assign(
45
+ {
46
+ QueueUrl: queueUrl,
47
+ AttributeNames: [
48
+ 'ApproximateNumberOfMessages',
49
+ 'ApproximateNumberOfMessagesNotVisible',
50
+ ],
51
+ },
52
+ opts
53
+ );
54
+ try {
55
+ const data = yield client.send(
56
+ new GetQueueAttributesCommand(commandInput)
57
+ );
58
+ logger.debug(`${FILE}::QUEUE_COUNT`, {
59
+ data,
60
+ commandInput,
61
+ });
62
+ return data;
63
+ } catch (error) {
64
+ throw new LesgoException(
65
+ 'Error occurred getting count from queue',
66
+ `${FILE}::QUEUE_COUNT_ERROR`,
67
+ 500,
68
+ {
69
+ error,
70
+ commandInput,
71
+ opts,
72
+ }
73
+ );
74
+ }
75
+ });
76
+ export default getQueueCount;
@@ -1,5 +1,6 @@
1
1
  import { LesgoException } from '../../exceptions';
2
2
  import sqsConfig from '../../config/sqs';
3
+ import { convertQueueAliasToObject } from '../../utils/sqs';
3
4
  const FILE = 'lesgo.services.SQSService.getQueueUrl';
4
5
  export default queue => {
5
6
  if (
@@ -10,7 +11,8 @@ export default queue => {
10
11
  ) {
11
12
  return queue.url;
12
13
  }
13
- const configQueue = sqsConfig.queues.find(q => q.alias === queue);
14
+ const queues = convertQueueAliasToObject(sqsConfig.queueAliases);
15
+ const configQueue = queues.find(q => q.alias === queue);
14
16
  if (!configQueue) {
15
17
  throw new LesgoException(
16
18
  `Queue with alias ${queue} not found in config`,
@@ -1,3 +1,4 @@
1
+ export { default as changeMessageVisibility } from './changeMessageVisibility';
1
2
  export { default as deleteMessage } from './deleteMessage';
2
3
  export { default as dispatch } from './dispatch';
3
4
  export { default as getClient } from './getClient';
@@ -1,3 +1,4 @@
1
+ export { default as changeMessageVisibility } from './changeMessageVisibility';
1
2
  export { default as deleteMessage } from './deleteMessage';
2
3
  export { default as dispatch } from './dispatch';
3
4
  export { default as getClient } from './getClient';
@@ -11,7 +11,7 @@ interface ValidateEncryptionFieldsOptions {
11
11
  export declare const validateEncryptionAlgorithm: (algorithm: EncryptionAlgorithm) => void;
12
12
  export declare const validateSecretKey: (secretKey?: string, algorithm?: EncryptionAlgorithm) => void;
13
13
  declare const validateEncryptionFields: (text: string, opts?: ValidateEncryptionFieldsOptions) => {
14
- validText: any;
14
+ validText: string;
15
15
  validAlgorithm: EncryptionAlgorithm;
16
16
  validSecretKey: string;
17
17
  validIvLength: number;
@@ -1,4 +1,4 @@
1
1
  import { ConnectionOptions } from 'mysql2/promise';
2
2
  import { RDSAuroraMySQLProxyClientOptions } from '../../../../types/aws';
3
- declare const query: (sql: string, preparedValues?: any[], connOptions?: ConnectionOptions, clientOpts?: RDSAuroraMySQLProxyClientOptions) => Promise<import("mysql2/promise").QueryResult>;
3
+ declare const query: <T>(sql: string, preparedValues?: any[], connOptions?: ConnectionOptions, clientOpts?: RDSAuroraMySQLProxyClientOptions) => Promise<T>;
4
4
  export default query;
@@ -7,3 +7,4 @@ export { default as isEmail } from './isEmail';
7
7
  export { default as isEmpty } from './isEmpty';
8
8
  export { default as logger } from './logger';
9
9
  export { default as validateFields } from './validateFields';
10
+ export { default as safePromise } from './safePromise';
@@ -7,3 +7,4 @@ export { default as isEmail } from './isEmail';
7
7
  export { default as isEmpty } from './isEmpty';
8
8
  export { default as logger } from './logger';
9
9
  export { default as validateFields } from './validateFields';
10
+ export { default as safePromise } from './safePromise';
@@ -0,0 +1,20 @@
1
+ /**
2
+ *
3
+ * @param promise
4
+ * @returns [err, res]
5
+ * @reference https://github.com/arthurfiorette/proposal-safe-assignment-operator
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { safePromise } from 'lesgo/utils';
10
+ *
11
+ * const [err, res] = await safePromise(fetch('https://example.com/'));
12
+ * if (err) {
13
+ * console.error(err);
14
+ * } else {
15
+ * console.log(res);
16
+ * }
17
+ * ```
18
+ */
19
+ declare const safePromise: <T>(promise: Promise<T>) => Promise<[unknown, T | null]>;
20
+ export default safePromise;
@@ -0,0 +1,61 @@
1
+ var __awaiter =
2
+ (this && this.__awaiter) ||
3
+ function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) {
5
+ return value instanceof P
6
+ ? value
7
+ : new P(function (resolve) {
8
+ resolve(value);
9
+ });
10
+ }
11
+ return new (P || (P = Promise))(function (resolve, reject) {
12
+ function fulfilled(value) {
13
+ try {
14
+ step(generator.next(value));
15
+ } catch (e) {
16
+ reject(e);
17
+ }
18
+ }
19
+ function rejected(value) {
20
+ try {
21
+ step(generator['throw'](value));
22
+ } catch (e) {
23
+ reject(e);
24
+ }
25
+ }
26
+ function step(result) {
27
+ result.done
28
+ ? resolve(result.value)
29
+ : adopt(result.value).then(fulfilled, rejected);
30
+ }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ /**
35
+ *
36
+ * @param promise
37
+ * @returns [err, res]
38
+ * @reference https://github.com/arthurfiorette/proposal-safe-assignment-operator
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * import { safePromise } from 'lesgo/utils';
43
+ *
44
+ * const [err, res] = await safePromise(fetch('https://example.com/'));
45
+ * if (err) {
46
+ * console.error(err);
47
+ * } else {
48
+ * console.log(res);
49
+ * }
50
+ * ```
51
+ */
52
+ const safePromise = promise =>
53
+ __awaiter(void 0, void 0, void 0, function* () {
54
+ try {
55
+ const res = yield promise;
56
+ return [null, res];
57
+ } catch (err) {
58
+ return [err, null];
59
+ }
60
+ });
61
+ export default safePromise;
@@ -0,0 +1,5 @@
1
+ import { Queue } from '../../services/SQSService/getQueueUrl';
2
+ import { ClientOptions } from '../../types/aws';
3
+ import { changeMessageVisibilityOptions } from '../../services/SQSService/changeMessageVisibility';
4
+ export declare const changeMessageVisibility: (queue: string | Queue, receiptHandle: string, visibilityTimeout: number, opts?: changeMessageVisibilityOptions, clientOpts?: ClientOptions) => Promise<void>;
5
+ export default changeMessageVisibility;
@@ -0,0 +1,51 @@
1
+ var __awaiter =
2
+ (this && this.__awaiter) ||
3
+ function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) {
5
+ return value instanceof P
6
+ ? value
7
+ : new P(function (resolve) {
8
+ resolve(value);
9
+ });
10
+ }
11
+ return new (P || (P = Promise))(function (resolve, reject) {
12
+ function fulfilled(value) {
13
+ try {
14
+ step(generator.next(value));
15
+ } catch (e) {
16
+ reject(e);
17
+ }
18
+ }
19
+ function rejected(value) {
20
+ try {
21
+ step(generator['throw'](value));
22
+ } catch (e) {
23
+ reject(e);
24
+ }
25
+ }
26
+ function step(result) {
27
+ result.done
28
+ ? resolve(result.value)
29
+ : adopt(result.value).then(fulfilled, rejected);
30
+ }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ import changeMessageVisibilityService from '../../services/SQSService/changeMessageVisibility';
35
+ export const changeMessageVisibility = (
36
+ queue,
37
+ receiptHandle,
38
+ visibilityTimeout,
39
+ opts,
40
+ clientOpts
41
+ ) =>
42
+ __awaiter(void 0, void 0, void 0, function* () {
43
+ return changeMessageVisibilityService(
44
+ queue,
45
+ receiptHandle,
46
+ visibilityTimeout,
47
+ opts,
48
+ clientOpts
49
+ );
50
+ });
51
+ export default changeMessageVisibility;
@@ -0,0 +1,3 @@
1
+ import { Queue } from '../../services/SQSService/getQueueUrl';
2
+ export declare const convertQueueAliasToObject: (queueAlias: string | string[]) => Queue | Queue[];
3
+ export default convertQueueAliasToObject;
@@ -0,0 +1,16 @@
1
+ import appConfig from '../../config/app';
2
+ import awsConfig from '../../config/aws';
3
+ const convert = queueAlias => ({
4
+ alias: queueAlias,
5
+ name: `${appConfig.stackName}-${queueAlias}`,
6
+ url: `https://sqs.${awsConfig.region}.amazonaws.com/${
7
+ awsConfig.accountId
8
+ }/${`${appConfig.stackName}-${queueAlias}`}`,
9
+ });
10
+ export const convertQueueAliasToObject = queueAlias => {
11
+ if (Array.isArray(queueAlias)) {
12
+ return queueAlias.map(q => convert(q));
13
+ }
14
+ return convert(queueAlias);
15
+ };
16
+ export default convertQueueAliasToObject;
@@ -0,0 +1,5 @@
1
+ import { GetQueueCountOptions } from '../../services/SQSService/getQueueCount';
2
+ import { Queue } from '../../services/SQSService/getQueueUrl';
3
+ import { ClientOptions } from '../../types/aws';
4
+ export declare const getQueueCount: (queue: string | Queue, opts?: GetQueueCountOptions, clientOpts?: ClientOptions) => Promise<import("@aws-sdk/client-sqs").GetQueueAttributesCommandOutput>;
5
+ export default getQueueCount;
@@ -0,0 +1,39 @@
1
+ var __awaiter =
2
+ (this && this.__awaiter) ||
3
+ function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) {
5
+ return value instanceof P
6
+ ? value
7
+ : new P(function (resolve) {
8
+ resolve(value);
9
+ });
10
+ }
11
+ return new (P || (P = Promise))(function (resolve, reject) {
12
+ function fulfilled(value) {
13
+ try {
14
+ step(generator.next(value));
15
+ } catch (e) {
16
+ reject(e);
17
+ }
18
+ }
19
+ function rejected(value) {
20
+ try {
21
+ step(generator['throw'](value));
22
+ } catch (e) {
23
+ reject(e);
24
+ }
25
+ }
26
+ function step(result) {
27
+ result.done
28
+ ? resolve(result.value)
29
+ : adopt(result.value).then(fulfilled, rejected);
30
+ }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ import getQueueCountService from '../../services/SQSService/getQueueCount';
35
+ export const getQueueCount = (queue, opts, clientOpts) =>
36
+ __awaiter(void 0, void 0, void 0, function* () {
37
+ return getQueueCountService(queue, opts, clientOpts);
38
+ });
39
+ export default getQueueCount;
@@ -1,4 +1,7 @@
1
+ export { default as changeMessageVisibility } from './changeMessageVisibility';
2
+ export { default as convertQueueAliasToObject } from './convertQueueAliasToObject';
1
3
  export { default as deleteMessage } from './deleteMessage';
2
4
  export { default as dispatch } from './dispatch';
3
5
  export { default as getClient } from './getClient';
6
+ export { default as getQueueCount } from './getQueueCount';
4
7
  export { default as receiveMessages } from './receiveMessages';
@@ -1,4 +1,7 @@
1
+ export { default as changeMessageVisibility } from './changeMessageVisibility';
2
+ export { default as convertQueueAliasToObject } from './convertQueueAliasToObject';
1
3
  export { default as deleteMessage } from './deleteMessage';
2
4
  export { default as dispatch } from './dispatch';
3
5
  export { default as getClient } from './getClient';
6
+ export { default as getQueueCount } from './getQueueCount';
4
7
  export { default as receiveMessages } from './receiveMessages';
@@ -1,6 +1,4 @@
1
- export type Params = {
2
- [key: string]: any;
3
- };
1
+ export type Params = Record<PropertyKey, any>;
4
2
  export type Field = {
5
3
  key: string;
6
4
  type: string;
@@ -16,7 +14,5 @@ export type Field = {
16
14
  * @returns An object containing the validated fields.
17
15
  * @throws {LesgoException} If a required field is missing or if a field has an invalid type.
18
16
  */
19
- declare const validateFields: (params: Params, validFields: Field[]) => {
20
- [key: string]: any;
21
- };
17
+ declare const validateFields: <T extends Params>(params: T, validFields: Field[]) => T;
22
18
  export default validateFields;
@@ -25,9 +25,10 @@ const validateFields = (params, validFields) => {
25
25
  const validated = {};
26
26
  validFields.forEach(field => {
27
27
  const { required, type, key, isCollection, enumValues = [] } = field;
28
+ const value = params[key];
28
29
  if (required) {
29
- if (typeof params[key] === 'object') {
30
- if (Array.isArray(params[key]) && params[key].length === 0) {
30
+ if (typeof value === 'object') {
31
+ if (Array.isArray(value) && value.length === 0) {
31
32
  throw new LesgoException(
32
33
  `Missing required '${key}'`,
33
34
  `${FILE}::MISSING_REQUIRED_${key.toUpperCase()}`,
@@ -36,8 +37,8 @@ const validateFields = (params, validFields) => {
36
37
  );
37
38
  }
38
39
  }
39
- if (!params[key]) {
40
- if (typeof params[key] !== 'number') {
40
+ if (!value) {
41
+ if (typeof value !== 'number') {
41
42
  throw new LesgoException(
42
43
  `Missing required '${key}'`,
43
44
  `${FILE}::MISSING_REQUIRED_${key.toUpperCase()}`,
@@ -49,19 +50,18 @@ const validateFields = (params, validFields) => {
49
50
  }
50
51
  if (isCollection) {
51
52
  try {
52
- validateFields({ [key]: params[key] }, [
53
- { key, required, type: 'array' },
54
- ]);
53
+ validateFields({ [key]: value }, [{ key, required, type: 'array' }]);
55
54
  } catch (_) {
56
55
  throw new LesgoException(
57
56
  `Invalid type for '${key}', expecting collection of '${type}'`,
58
57
  `${FILE}::INVALID_TYPE_${key.toUpperCase()}`,
59
58
  500,
60
- { field, value: params[key] }
59
+ { field, value }
61
60
  );
62
61
  }
63
62
  }
64
- (isCollection ? params[key] || [] : [params[key]]).forEach(paramsItem => {
63
+ const paramsItems = isCollection ? value || [] : [value];
64
+ paramsItems.forEach(paramsItem => {
65
65
  if (
66
66
  (type === 'string' &&
67
67
  typeof paramsItem !== 'undefined' &&
@@ -101,8 +101,8 @@ const validateFields = (params, validFields) => {
101
101
  );
102
102
  }
103
103
  });
104
- if (typeof params[key] !== 'undefined') {
105
- validated[key] = params[key];
104
+ if (typeof value !== 'undefined') {
105
+ validated[key] = value;
106
106
  }
107
107
  });
108
108
  return validated;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lesgo",
3
- "version": "2.1.4",
3
+ "version": "2.1.5",
4
4
  "description": "Core framework for lesgo node.js serverless framework.",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",