mblabs-roccato-backend-commons 0.0.1

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.
Files changed (90) hide show
  1. package/.editorconfig +9 -0
  2. package/.gitattributes +1 -0
  3. package/README.md +29 -0
  4. package/commitlint.config.js +4 -0
  5. package/dist/database/entities/account.js +49 -0
  6. package/dist/database/entities/base.js +43 -0
  7. package/dist/database/entities/index.js +10 -0
  8. package/dist/database/entities/profile.js +35 -0
  9. package/dist/index.js +17 -0
  10. package/dist/interfaces/aws.js +2 -0
  11. package/dist/interfaces/azure.js +2 -0
  12. package/dist/interfaces/firebase.js +2 -0
  13. package/dist/interfaces/gcp.js +2 -0
  14. package/dist/interfaces/grafana.js +2 -0
  15. package/dist/interfaces/http.js +2 -0
  16. package/dist/interfaces/keycloak.js +2 -0
  17. package/dist/interfaces/rabbit.js +2 -0
  18. package/dist/interfaces/redis.js +2 -0
  19. package/dist/interfaces/sendgrid.js +2 -0
  20. package/dist/services/aws/cloudwatch.js +110 -0
  21. package/dist/services/aws/index.js +14 -0
  22. package/dist/services/aws/pinpoint.js +120 -0
  23. package/dist/services/aws/s3.js +155 -0
  24. package/dist/services/aws/secret-manager.js +44 -0
  25. package/dist/services/azure/application-insights.js +72 -0
  26. package/dist/services/azure/communication.js +66 -0
  27. package/dist/services/azure/index.js +14 -0
  28. package/dist/services/azure/keyvault.js +33 -0
  29. package/dist/services/azure/storage-blob.js +67 -0
  30. package/dist/services/date.js +86 -0
  31. package/dist/services/file.js +59 -0
  32. package/dist/services/firebase.js +43 -0
  33. package/dist/services/gcp/drive.js +27 -0
  34. package/dist/services/gcp/index.js +12 -0
  35. package/dist/services/gcp/secrets.js +35 -0
  36. package/dist/services/gcp/sheets.js +30 -0
  37. package/dist/services/grafana.js +51 -0
  38. package/dist/services/http.js +76 -0
  39. package/dist/services/index.js +36 -0
  40. package/dist/services/keycloak.js +55 -0
  41. package/dist/services/rabbit.js +44 -0
  42. package/dist/services/redis.js +54 -0
  43. package/dist/services/sendgrid.js +33 -0
  44. package/dist/utils/parse-secret.js +15 -0
  45. package/eslint.config.mjs +149 -0
  46. package/package.json +78 -0
  47. package/src/database/entities/account.ts +35 -0
  48. package/src/database/entities/base.ts +30 -0
  49. package/src/database/entities/index.ts +7 -0
  50. package/src/database/entities/profile.ts +21 -0
  51. package/src/index.ts +1 -0
  52. package/src/interfaces/aws.ts +168 -0
  53. package/src/interfaces/azure.ts +153 -0
  54. package/src/interfaces/firebase.ts +29 -0
  55. package/src/interfaces/gcp.ts +58 -0
  56. package/src/interfaces/grafana.ts +33 -0
  57. package/src/interfaces/http.ts +36 -0
  58. package/src/interfaces/keycloak.ts +50 -0
  59. package/src/interfaces/rabbit.ts +13 -0
  60. package/src/interfaces/redis.ts +14 -0
  61. package/src/interfaces/sendgrid.ts +28 -0
  62. package/src/services/aws/cloudwatch.ts +107 -0
  63. package/src/services/aws/index.ts +11 -0
  64. package/src/services/aws/pinpoint.ts +160 -0
  65. package/src/services/aws/s3.ts +165 -0
  66. package/src/services/aws/secret-manager.ts +40 -0
  67. package/src/services/azure/application-insights.ts +31 -0
  68. package/src/services/azure/communication.ts +64 -0
  69. package/src/services/azure/index.ts +11 -0
  70. package/src/services/azure/keyvault.ts +24 -0
  71. package/src/services/azure/storage-blob.ts +84 -0
  72. package/src/services/date.ts +112 -0
  73. package/src/services/file.ts +74 -0
  74. package/src/services/firebase.ts +33 -0
  75. package/src/services/gcp/drive.ts +20 -0
  76. package/src/services/gcp/index.ts +9 -0
  77. package/src/services/gcp/secrets.ts +28 -0
  78. package/src/services/gcp/sheets.ts +23 -0
  79. package/src/services/grafana.ts +40 -0
  80. package/src/services/http.ts +75 -0
  81. package/src/services/index.ts +50 -0
  82. package/src/services/keycloak.ts +46 -0
  83. package/src/services/rabbit.ts +39 -0
  84. package/src/services/redis.ts +46 -0
  85. package/src/services/sendgrid.ts +22 -0
  86. package/src/utils/parse-secret.ts +15 -0
  87. package/tsconfig-paths.js +8 -0
  88. package/tsconfig.base.json +49 -0
  89. package/tsconfig.json +14 -0
  90. package/tsup.config.ts +16 -0
package/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ [*]
2
+ charset = utf-8
3
+ end_of_line = lf
4
+ indent_size = 2
5
+ indent_style = space
6
+ insert_final_newline = true
7
+ max_line_length = 120
8
+ tab_width = 4
9
+ trim_trailing_whitespace = true
package/.gitattributes ADDED
@@ -0,0 +1 @@
1
+ eol=lf
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # README #
2
+
3
+ This README would normally document whatever steps are necessary to get your application up and running.
4
+
5
+ ### What is this repository for? ###
6
+
7
+ * Quick summary
8
+ * Version
9
+ * [Learn Markdown](https://bitbucket.org/tutorials/markdowndemo)
10
+
11
+ ### How do I get set up? ###
12
+
13
+ * Summary of set up
14
+ * Configuration
15
+ * Dependencies
16
+ * Database configuration
17
+ * How to run tests
18
+ * Deployment instructions
19
+
20
+ ### Contribution guidelines ###
21
+
22
+ * Writing tests
23
+ * Code review
24
+ * Other guidelines
25
+
26
+ ### Who do I talk to? ###
27
+
28
+ * Repo owner or admin
29
+ * Other community or team contact
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ extends: [ '@commitlint/config-conventional' ],
3
+ ignores: [ (message) => message.includes('chore(release):') ],
4
+ };
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
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
+ const typeorm_1 = require("typeorm");
16
+ const base_1 = __importDefault(require("@database/entities/base"));
17
+ const profile_1 = __importDefault(require("@database/entities/profile"));
18
+ let AccountEntity = class AccountEntity extends base_1.default {
19
+ constructor(partial) {
20
+ super();
21
+ Object.assign(this, partial);
22
+ }
23
+ };
24
+ __decorate([
25
+ (0, typeorm_1.Column)({ type: 'uuid', nullable: true }),
26
+ __metadata("design:type", String)
27
+ ], AccountEntity.prototype, "tenantId", void 0);
28
+ __decorate([
29
+ (0, typeorm_1.Column)({ type: 'varchar', nullable: true }),
30
+ __metadata("design:type", String)
31
+ ], AccountEntity.prototype, "name", void 0);
32
+ __decorate([
33
+ (0, typeorm_1.Column)({ type: 'varchar', nullable: true }),
34
+ __metadata("design:type", String)
35
+ ], AccountEntity.prototype, "email", void 0);
36
+ __decorate([
37
+ (0, typeorm_1.Column)({ type: 'varchar', nullable: true }),
38
+ __metadata("design:type", String)
39
+ ], AccountEntity.prototype, "cellphone", void 0);
40
+ __decorate([
41
+ (0, typeorm_1.ManyToOne)(() => profile_1.default),
42
+ (0, typeorm_1.JoinColumn)({ name: 'accountId', referencedColumnName: 'id' }),
43
+ __metadata("design:type", profile_1.default)
44
+ ], AccountEntity.prototype, "profile", void 0);
45
+ AccountEntity = __decorate([
46
+ (0, typeorm_1.Entity)('account'),
47
+ __metadata("design:paramtypes", [Object])
48
+ ], AccountEntity);
49
+ exports.default = AccountEntity;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const typeorm_1 = require("typeorm");
13
+ class BaseEntity {
14
+ }
15
+ exports.default = BaseEntity;
16
+ __decorate([
17
+ (0, typeorm_1.PrimaryGeneratedColumn)('uuid'),
18
+ __metadata("design:type", String)
19
+ ], BaseEntity.prototype, "id", void 0);
20
+ __decorate([
21
+ (0, typeorm_1.Column)({ type: 'varchar', nullable: true }),
22
+ __metadata("design:type", String)
23
+ ], BaseEntity.prototype, "createdBy", void 0);
24
+ __decorate([
25
+ (0, typeorm_1.CreateDateColumn)({ type: 'timestamptz' }),
26
+ __metadata("design:type", Date)
27
+ ], BaseEntity.prototype, "createdAt", void 0);
28
+ __decorate([
29
+ (0, typeorm_1.Column)({ type: 'varchar', nullable: true }),
30
+ __metadata("design:type", String)
31
+ ], BaseEntity.prototype, "updatedBy", void 0);
32
+ __decorate([
33
+ (0, typeorm_1.UpdateDateColumn)({ type: 'timestamptz' }),
34
+ __metadata("design:type", Date)
35
+ ], BaseEntity.prototype, "updatedAt", void 0);
36
+ __decorate([
37
+ (0, typeorm_1.Column)({ type: 'varchar', nullable: true }),
38
+ __metadata("design:type", String)
39
+ ], BaseEntity.prototype, "deletedBy", void 0);
40
+ __decorate([
41
+ (0, typeorm_1.DeleteDateColumn)({ type: 'timestamptz', nullable: true }),
42
+ __metadata("design:type", Date)
43
+ ], BaseEntity.prototype, "deletedAt", void 0);
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ProfileEntity = exports.AccountEntity = void 0;
7
+ const account_1 = __importDefault(require("@database/entities/account"));
8
+ exports.AccountEntity = account_1.default;
9
+ const profile_1 = __importDefault(require("@database/entities/profile"));
10
+ exports.ProfileEntity = profile_1.default;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
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
+ const typeorm_1 = require("typeorm");
16
+ const base_1 = __importDefault(require("@database/entities/base"));
17
+ let ProfileEntity = class ProfileEntity extends base_1.default {
18
+ constructor(partial) {
19
+ super();
20
+ Object.assign(this, partial);
21
+ }
22
+ };
23
+ __decorate([
24
+ (0, typeorm_1.Column)({ type: 'uuid', nullable: true }),
25
+ __metadata("design:type", String)
26
+ ], ProfileEntity.prototype, "tenantId", void 0);
27
+ __decorate([
28
+ (0, typeorm_1.Column)({ type: 'varchar', nullable: true }),
29
+ __metadata("design:type", String)
30
+ ], ProfileEntity.prototype, "name", void 0);
31
+ ProfileEntity = __decorate([
32
+ (0, typeorm_1.Entity)('profile'),
33
+ __metadata("design:paramtypes", [Object])
34
+ ], ProfileEntity);
35
+ exports.default = ProfileEntity;
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("@services"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,110 @@
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
+ const client_cloudwatch_logs_1 = require("@aws-sdk/client-cloudwatch-logs");
13
+ class AmazonCloudwatchService {
14
+ createLogEvent(req) {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ const client = new client_cloudwatch_logs_1.CloudWatchLogs({
17
+ credentials: {
18
+ accessKeyId: req.credentials.accessKey,
19
+ secretAccessKey: req.credentials.secretKey,
20
+ },
21
+ region: req.credentials.region,
22
+ });
23
+ yield this.createLogGroup(req);
24
+ yield this.createLogStream(req);
25
+ yield client.putLogEvents({
26
+ logGroupName: req.group,
27
+ logStreamName: req.stream,
28
+ logEvents: req.events,
29
+ });
30
+ });
31
+ }
32
+ getLogGroupByName(req) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ const logGroups = yield this.listApplicationLogGroups(req);
35
+ return logGroups.find((logGroup) => logGroup.logGroupName === req.group);
36
+ });
37
+ }
38
+ getLogStreamByName(req) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ const logStreams = yield this.listApplicationLogStreams(req);
41
+ return logStreams.find((logStream) => logStream.logStreamName === req.stream);
42
+ });
43
+ }
44
+ listApplicationLogGroups(req) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ const client = new client_cloudwatch_logs_1.CloudWatchLogs({
47
+ credentials: {
48
+ accessKeyId: req.credentials.accessKey,
49
+ secretAccessKey: req.credentials.secretKey,
50
+ },
51
+ region: req.credentials.region,
52
+ });
53
+ const { logGroups } = yield client.describeLogGroups({
54
+ logGroupNamePrefix: req.application,
55
+ });
56
+ return logGroups;
57
+ });
58
+ }
59
+ listApplicationLogStreams(req) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ const client = new client_cloudwatch_logs_1.CloudWatchLogs({
62
+ credentials: {
63
+ accessKeyId: req.credentials.accessKey,
64
+ secretAccessKey: req.credentials.secretKey,
65
+ },
66
+ region: req.credentials.region,
67
+ });
68
+ const { logStreams } = yield client.describeLogStreams({
69
+ logGroupName: req.group,
70
+ });
71
+ return logStreams;
72
+ });
73
+ }
74
+ createLogGroup(req) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ const logGroup = yield this.getLogGroupByName(req);
77
+ if (!logGroup) {
78
+ const client = new client_cloudwatch_logs_1.CloudWatchLogs({
79
+ credentials: {
80
+ accessKeyId: req.credentials.accessKey,
81
+ secretAccessKey: req.credentials.secretKey,
82
+ },
83
+ region: req.credentials.region,
84
+ });
85
+ yield client.createLogGroup({
86
+ logGroupName: req.group,
87
+ });
88
+ }
89
+ });
90
+ }
91
+ createLogStream(req) {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ const logStream = yield this.getLogStreamByName(req);
94
+ if (!logStream) {
95
+ const client = new client_cloudwatch_logs_1.CloudWatchLogs({
96
+ credentials: {
97
+ accessKeyId: req.credentials.accessKey,
98
+ secretAccessKey: req.credentials.secretKey,
99
+ },
100
+ region: req.credentials.region,
101
+ });
102
+ yield client.createLogStream({
103
+ logGroupName: req.group,
104
+ logStreamName: req.stream,
105
+ });
106
+ }
107
+ });
108
+ }
109
+ }
110
+ exports.default = new AmazonCloudwatchService();
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AmazonSecretManagerService = exports.AmazonS3Service = exports.AmazonPinpointService = exports.AmazonCloudwatchService = void 0;
7
+ const cloudwatch_1 = __importDefault(require("@services/aws/cloudwatch"));
8
+ exports.AmazonCloudwatchService = cloudwatch_1.default;
9
+ const pinpoint_1 = __importDefault(require("@services/aws/pinpoint"));
10
+ exports.AmazonPinpointService = pinpoint_1.default;
11
+ const s3_1 = __importDefault(require("@services/aws/s3"));
12
+ exports.AmazonS3Service = s3_1.default;
13
+ const secret_manager_1 = __importDefault(require("@services/aws/secret-manager"));
14
+ exports.AmazonSecretManagerService = secret_manager_1.default;
@@ -0,0 +1,120 @@
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
+ const client_pinpoint_1 = require("@aws-sdk/client-pinpoint");
13
+ class AmazonPinpointService {
14
+ sendMail(req) {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ const client = new client_pinpoint_1.PinpointClient({
17
+ credentials: {
18
+ accessKeyId: req.credentials.accessKey,
19
+ secretAccessKey: req.credentials.secretKey,
20
+ },
21
+ region: req.credentials.region,
22
+ });
23
+ const recipient = Array.isArray(req.emails) ? req.emails : [req.emails];
24
+ const addresses = recipient.reduce((acc, curr) => (Object.assign(Object.assign({}, acc), { [curr]: {
25
+ ChannelType: 'EMAIL',
26
+ } })), {});
27
+ const substitutions = Object.entries(req.variables || {}).reduce((acc, [key, value]) => (Object.assign(Object.assign({}, acc), { [key]: [value] })), {});
28
+ const payload = {
29
+ ApplicationId: req.applicationId,
30
+ MessageRequest: {
31
+ Addresses: addresses,
32
+ MessageConfiguration: {
33
+ EmailMessage: {
34
+ FromAddress: req.from,
35
+ Substitutions: substitutions,
36
+ SimpleEmail: {
37
+ Subject: {
38
+ Charset: 'utf-8',
39
+ Data: req.subject,
40
+ },
41
+ TextPart: {
42
+ Charset: 'utf-8',
43
+ Data: req.text,
44
+ },
45
+ },
46
+ },
47
+ },
48
+ },
49
+ };
50
+ yield client.send(new client_pinpoint_1.SendMessagesCommand(payload));
51
+ });
52
+ }
53
+ sendTemplateMail(req) {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ const client = new client_pinpoint_1.PinpointClient({
56
+ credentials: {
57
+ accessKeyId: req.credentials.accessKey,
58
+ secretAccessKey: req.credentials.secretKey,
59
+ },
60
+ region: req.credentials.region,
61
+ });
62
+ const recipient = Array.isArray(req.emails) ? req.emails : [req.emails];
63
+ const addresses = recipient.reduce((acc, curr) => (Object.assign(Object.assign({}, acc), { [curr]: {
64
+ ChannelType: 'EMAIL',
65
+ } })), {});
66
+ const substitutions = Object.entries(req.variables || {}).reduce((acc, [key, value]) => (Object.assign(Object.assign({}, acc), { [key]: [value] })), {});
67
+ const payload = {
68
+ ApplicationId: req.applicationId,
69
+ MessageRequest: {
70
+ Addresses: addresses,
71
+ MessageConfiguration: {
72
+ EmailMessage: {
73
+ FromAddress: req.from,
74
+ Substitutions: substitutions,
75
+ },
76
+ },
77
+ TemplateConfiguration: {
78
+ EmailTemplate: {
79
+ Name: req.template,
80
+ Version: 'latest',
81
+ },
82
+ },
83
+ },
84
+ };
85
+ yield client.send(new client_pinpoint_1.SendMessagesCommand(payload));
86
+ });
87
+ }
88
+ sendSMS(req) {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ const client = new client_pinpoint_1.PinpointClient({
91
+ credentials: {
92
+ accessKeyId: req.credentials.accessKey,
93
+ secretAccessKey: req.credentials.secretKey,
94
+ },
95
+ region: req.credentials.region,
96
+ });
97
+ const recipient = Array.isArray(req.phones) ? req.phones : [req.phones];
98
+ const addresses = recipient.reduce((acc, curr) => (Object.assign(Object.assign({}, acc), { [curr]: {
99
+ ChannelType: 'SMS',
100
+ } })), {});
101
+ const substitutions = Object.entries(req.variables || {}).reduce((acc, [key, value]) => (Object.assign(Object.assign({}, acc), { [key]: [value] })), {});
102
+ const payload = {
103
+ ApplicationId: req.applicationId,
104
+ MessageRequest: {
105
+ Addresses: addresses,
106
+ MessageConfiguration: {
107
+ SMSMessage: {
108
+ Body: req.text,
109
+ MessageType: req.type,
110
+ SenderId: req.sender,
111
+ Substitutions: substitutions,
112
+ },
113
+ },
114
+ },
115
+ };
116
+ yield client.send(new client_pinpoint_1.SendMessagesCommand(payload));
117
+ });
118
+ }
119
+ }
120
+ exports.default = new AmazonPinpointService();
@@ -0,0 +1,155 @@
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
+ const client_s3_1 = require("@aws-sdk/client-s3");
16
+ const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
17
+ const file_1 = __importDefault(require("@services/file"));
18
+ class AmazonS3Service {
19
+ uploadBase64(req) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ var _a, _b, _c, _d, _e, _f, _g;
22
+ const client = new client_s3_1.S3({
23
+ credentials: {
24
+ accessKeyId: req.credentials.accessKey,
25
+ secretAccessKey: req.credentials.secretKey,
26
+ },
27
+ region: req.credentials.region,
28
+ });
29
+ const { buffer, extension } = file_1.default.getBase64Metadata(req.file);
30
+ const key = ((_a = req.options) === null || _a === void 0 ? void 0 : _a.filename)
31
+ ? (_b = req.options) === null || _b === void 0 ? void 0 : _b.filename
32
+ : file_1.default.getUniqueFilename(extension);
33
+ const payload = {
34
+ Key: this.getBucketPath(key, (_c = req.options) === null || _c === void 0 ? void 0 : _c.folder),
35
+ Body: buffer,
36
+ ContentEncoding: (_d = req.options) === null || _d === void 0 ? void 0 : _d.contentEncoding,
37
+ ContentType: (_e = req.options) === null || _e === void 0 ? void 0 : _e.contentType,
38
+ Bucket: req.bucket,
39
+ ACL: ((_f = req.options) === null || _f === void 0 ? void 0 : _f.isPrivate) ? 'private' : 'public-read',
40
+ };
41
+ yield client.putObject(payload);
42
+ const { url } = yield this.getSignedUrl({
43
+ bucket: req.bucket,
44
+ credentials: req.credentials,
45
+ filename: key,
46
+ folder: (_g = req.options) === null || _g === void 0 ? void 0 : _g.folder,
47
+ isUpload: false,
48
+ });
49
+ return {
50
+ key: payload.Key,
51
+ bucket: req.bucket,
52
+ url,
53
+ };
54
+ });
55
+ }
56
+ uploadBuffer(req) {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ var _a, _b, _c, _d, _e, _f, _g;
59
+ const client = new client_s3_1.S3({
60
+ credentials: {
61
+ accessKeyId: req.credentials.accessKey,
62
+ secretAccessKey: req.credentials.secretKey,
63
+ },
64
+ region: req.credentials.region,
65
+ });
66
+ const key = ((_a = req.options) === null || _a === void 0 ? void 0 : _a.filename)
67
+ ? (_b = req.options) === null || _b === void 0 ? void 0 : _b.filename
68
+ : file_1.default.getUniqueFilename('');
69
+ const payload = {
70
+ Key: this.getBucketPath(key, (_c = req.options) === null || _c === void 0 ? void 0 : _c.folder),
71
+ Body: req.file,
72
+ ContentEncoding: (_d = req.options) === null || _d === void 0 ? void 0 : _d.contentEncoding,
73
+ ContentType: (_e = req.options) === null || _e === void 0 ? void 0 : _e.contentType,
74
+ Bucket: req.bucket,
75
+ ACL: ((_f = req.options) === null || _f === void 0 ? void 0 : _f.isPrivate) ? 'private' : 'public-read',
76
+ };
77
+ yield client.putObject(payload);
78
+ const { url } = yield this.getSignedUrl({
79
+ bucket: req.bucket,
80
+ credentials: req.credentials,
81
+ filename: key,
82
+ folder: (_g = req.options) === null || _g === void 0 ? void 0 : _g.folder,
83
+ isUpload: false,
84
+ });
85
+ return {
86
+ key: payload.Key,
87
+ bucket: req.bucket,
88
+ url,
89
+ };
90
+ });
91
+ }
92
+ deleteFile(req) {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ const client = new client_s3_1.S3({
95
+ credentials: {
96
+ accessKeyId: req.credentials.accessKey,
97
+ secretAccessKey: req.credentials.secretKey,
98
+ },
99
+ region: req.credentials.region,
100
+ });
101
+ const payload = {
102
+ Key: this.getBucketPath(req.filename, req.folder),
103
+ Bucket: req.bucket,
104
+ };
105
+ yield client.deleteObject(payload);
106
+ });
107
+ }
108
+ listFiles(req) {
109
+ return __awaiter(this, void 0, void 0, function* () {
110
+ const client = new client_s3_1.S3({
111
+ credentials: {
112
+ accessKeyId: req.credentials.accessKey,
113
+ secretAccessKey: req.credentials.secretKey,
114
+ },
115
+ region: req.credentials.region,
116
+ });
117
+ const { Contents } = yield client.listObjectsV2({
118
+ Bucket: req.bucket,
119
+ Prefix: req.folder,
120
+ });
121
+ return {
122
+ files: Contents !== null && Contents !== void 0 ? Contents : [],
123
+ };
124
+ });
125
+ }
126
+ getSignedUrl(req) {
127
+ return __awaiter(this, void 0, void 0, function* () {
128
+ const client = new client_s3_1.S3({
129
+ credentials: {
130
+ accessKeyId: req.credentials.accessKey,
131
+ secretAccessKey: req.credentials.secretKey,
132
+ },
133
+ region: req.credentials.region,
134
+ });
135
+ const payload = {
136
+ Key: this.getBucketPath(req.filename, req.folder),
137
+ Bucket: req.bucket,
138
+ };
139
+ const method = req.isUpload
140
+ ? new client_s3_1.PutObjectCommand(payload)
141
+ : new client_s3_1.GetObjectCommand(payload);
142
+ const url = yield (0, s3_request_presigner_1.getSignedUrl)(client, method);
143
+ return {
144
+ url,
145
+ };
146
+ });
147
+ }
148
+ getBucketPath(filename, folder = '') {
149
+ if (folder) {
150
+ return `${folder}/${filename}`;
151
+ }
152
+ return filename;
153
+ }
154
+ }
155
+ exports.default = new AmazonS3Service();