@kipicore/dbcore 1.1.354 → 1.1.355
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.
- package/dist/configs/aws.d.ts +7 -1
- package/dist/configs/aws.js +59 -3
- package/dist/configs/env.d.ts +1 -0
- package/dist/configs/env.js +2 -0
- package/dist/helpers/s3Uploader.js +5 -3
- package/package.json +5 -2
package/dist/configs/aws.d.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
import { S3Client } from '@aws-sdk/client-s3';
|
|
2
|
-
|
|
2
|
+
import { SESClient } from '@aws-sdk/client-ses';
|
|
3
|
+
import { SNSClient } from '@aws-sdk/client-sns';
|
|
4
|
+
export declare const aws: {
|
|
5
|
+
s3(): Promise<S3Client>;
|
|
6
|
+
ses(): Promise<SESClient>;
|
|
7
|
+
sns(): Promise<SNSClient>;
|
|
8
|
+
};
|
package/dist/configs/aws.js
CHANGED
|
@@ -1,12 +1,68 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// import { S3Client } from '@aws-sdk/client-s3';
|
|
3
|
+
// import { ENV_VARIABLE } from '../configs/env';
|
|
2
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
5
|
+
exports.aws = void 0;
|
|
6
|
+
// export const s3 = new S3Client({
|
|
7
|
+
// region: ENV_VARIABLE.AWS_REGION,
|
|
8
|
+
// credentials: {
|
|
9
|
+
// accessKeyId: ENV_VARIABLE.AWS_ACCESS_KEY_ID!,
|
|
10
|
+
// secretAccessKey: ENV_VARIABLE.AWS_SECRET_ACCESS_KEY!,
|
|
11
|
+
// },
|
|
12
|
+
// });
|
|
4
13
|
const client_s3_1 = require("@aws-sdk/client-s3");
|
|
14
|
+
const client_ses_1 = require("@aws-sdk/client-ses");
|
|
15
|
+
const client_sns_1 = require("@aws-sdk/client-sns");
|
|
16
|
+
const client_sts_1 = require("@aws-sdk/client-sts");
|
|
5
17
|
const env_1 = require("../configs/env");
|
|
6
|
-
|
|
7
|
-
|
|
18
|
+
const REGION = env_1.ENV_VARIABLE.AWS_REGION || 'ap-south-1';
|
|
19
|
+
const sts = new client_sts_1.STSClient({
|
|
20
|
+
region: REGION,
|
|
8
21
|
credentials: {
|
|
9
22
|
accessKeyId: env_1.ENV_VARIABLE.AWS_ACCESS_KEY_ID,
|
|
10
23
|
secretAccessKey: env_1.ENV_VARIABLE.AWS_SECRET_ACCESS_KEY,
|
|
11
24
|
},
|
|
12
25
|
});
|
|
26
|
+
const cached = {
|
|
27
|
+
s3: null,
|
|
28
|
+
ses: null,
|
|
29
|
+
sns: null,
|
|
30
|
+
expiry: 0,
|
|
31
|
+
};
|
|
32
|
+
async function refreshClients() {
|
|
33
|
+
const assumed = await sts.send(new client_sts_1.AssumeRoleCommand({
|
|
34
|
+
RoleArn: env_1.ENV_VARIABLE.AWS_ROLE_ARN,
|
|
35
|
+
RoleSessionName: 'backend-session',
|
|
36
|
+
}));
|
|
37
|
+
if (!assumed.Credentials) {
|
|
38
|
+
throw new Error('Failed to assume role');
|
|
39
|
+
}
|
|
40
|
+
const credentials = {
|
|
41
|
+
accessKeyId: assumed.Credentials.AccessKeyId,
|
|
42
|
+
secretAccessKey: assumed.Credentials.SecretAccessKey,
|
|
43
|
+
sessionToken: assumed.Credentials.SessionToken,
|
|
44
|
+
};
|
|
45
|
+
cached.expiry = new Date(assumed.Credentials.Expiration).getTime() - 60 * 1000;
|
|
46
|
+
cached.s3 = new client_s3_1.S3Client({ region: REGION, credentials });
|
|
47
|
+
cached.ses = new client_ses_1.SESClient({ region: REGION, credentials });
|
|
48
|
+
cached.sns = new client_sns_1.SNSClient({ region: REGION, credentials });
|
|
49
|
+
}
|
|
50
|
+
async function ensureClients() {
|
|
51
|
+
if (!cached.s3 || Date.now() > cached.expiry) {
|
|
52
|
+
await refreshClients();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.aws = {
|
|
56
|
+
async s3() {
|
|
57
|
+
await ensureClients();
|
|
58
|
+
return cached.s3;
|
|
59
|
+
},
|
|
60
|
+
async ses() {
|
|
61
|
+
await ensureClients();
|
|
62
|
+
return cached.ses;
|
|
63
|
+
},
|
|
64
|
+
async sns() {
|
|
65
|
+
await ensureClients();
|
|
66
|
+
return cached.sns;
|
|
67
|
+
},
|
|
68
|
+
};
|
package/dist/configs/env.d.ts
CHANGED
package/dist/configs/env.js
CHANGED
|
@@ -81,6 +81,7 @@ const envValidation = joi_1.default.object()
|
|
|
81
81
|
AWS_ACCESS_KEY_ID: joi_1.default.string().required(),
|
|
82
82
|
AWS_SECRET_ACCESS_KEY: joi_1.default.string().required(),
|
|
83
83
|
AWS_REGION: joi_1.default.string().required(),
|
|
84
|
+
AWS_ROLE_ARN: joi_1.default.string().optional(),
|
|
84
85
|
PDF_MICRO_SERVICE_API_URL: joi_1.default.string().required(),
|
|
85
86
|
SMTP_SERVER: joi_1.default.string().required(),
|
|
86
87
|
SMTP_EMAIL: joi_1.default.string().required(),
|
|
@@ -143,6 +144,7 @@ exports.ENV_VARIABLE = {
|
|
|
143
144
|
AWS_SECRET_ACCESS_KEY: envVar.AWS_SECRET_ACCESS_KEY,
|
|
144
145
|
AWS_REGION: envVar.AWS_REGION,
|
|
145
146
|
PDF_MICRO_SERVICE_API_URL: envVar.PDF_MICRO_SERVICE_API_URL,
|
|
147
|
+
AWS_ROLE_ARN: envVar.AWS_ROLE_ARN,
|
|
146
148
|
SMTP_SERVER: envVar.SMTP_SERVER,
|
|
147
149
|
SMTP_EMAIL: envVar.SMTP_EMAIL,
|
|
148
150
|
SMTP_PORT: envVar.SMTP_PORT,
|
|
@@ -7,12 +7,13 @@ exports.getPresignedUrl = exports.uploadFileToS3 = void 0;
|
|
|
7
7
|
// src/utils/s3Uploader.ts
|
|
8
8
|
const fs_1 = __importDefault(require("fs"));
|
|
9
9
|
const path_1 = __importDefault(require("path"));
|
|
10
|
-
const
|
|
10
|
+
const configs_1 = require("../configs");
|
|
11
11
|
const env_1 = require("../configs/env");
|
|
12
12
|
const client_s3_1 = require("@aws-sdk/client-s3");
|
|
13
13
|
const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
|
|
14
14
|
const uploadFileToS3 = async (localFilePath, s3Key, s3Bucket = env_1.ENV_VARIABLE.AWS_BUCKET_NAME) => {
|
|
15
15
|
try {
|
|
16
|
+
const s3 = await configs_1.aws.s3();
|
|
16
17
|
const fileContent = fs_1.default.readFileSync(localFilePath);
|
|
17
18
|
const contentType = getMimeTypeFromExt(path_1.default.extname(localFilePath));
|
|
18
19
|
const command = new client_s3_1.PutObjectCommand({
|
|
@@ -22,7 +23,7 @@ const uploadFileToS3 = async (localFilePath, s3Key, s3Bucket = env_1.ENV_VARIABL
|
|
|
22
23
|
ContentType: contentType,
|
|
23
24
|
// ACL: 'public-read', // ✅ For public access
|
|
24
25
|
});
|
|
25
|
-
await
|
|
26
|
+
await s3.send(command);
|
|
26
27
|
// Delete local file after successful upload
|
|
27
28
|
fs_1.default.unlinkSync(localFilePath);
|
|
28
29
|
const filePath = `https://${s3Bucket}.s3.${env_1.ENV_VARIABLE.AWS_REGION}.amazonaws.com/${s3Key}`;
|
|
@@ -37,12 +38,13 @@ exports.uploadFileToS3 = uploadFileToS3;
|
|
|
37
38
|
const getPresignedUrl = async (bucket, fileStorage, expiresIn = 60 * 60 * 24) => {
|
|
38
39
|
if (fileStorage.filePath)
|
|
39
40
|
return fileStorage.filePath;
|
|
41
|
+
const s3 = await configs_1.aws.s3();
|
|
40
42
|
const s3Key = `${fileStorage.storagePath}/${fileStorage.storageFileName}`;
|
|
41
43
|
const command = new client_s3_1.GetObjectCommand({
|
|
42
44
|
Bucket: bucket,
|
|
43
45
|
Key: s3Key,
|
|
44
46
|
});
|
|
45
|
-
const signedUrl = await (0, s3_request_presigner_1.getSignedUrl)(
|
|
47
|
+
const signedUrl = await (0, s3_request_presigner_1.getSignedUrl)(s3, command, { expiresIn }); // in seconds
|
|
46
48
|
return signedUrl;
|
|
47
49
|
};
|
|
48
50
|
exports.getPresignedUrl = getPresignedUrl;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kipicore/dbcore",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.355",
|
|
4
4
|
"description": "Reusable DB core package with Postgres, MongoDB, models, services, interfaces, and types",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -38,7 +38,10 @@
|
|
|
38
38
|
"license": "MIT",
|
|
39
39
|
"private": false,
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@aws-sdk/client-s3": "^3.
|
|
41
|
+
"@aws-sdk/client-s3": "^3.1017.0",
|
|
42
|
+
"@aws-sdk/client-ses": "^3.1017.0",
|
|
43
|
+
"@aws-sdk/client-sns": "^3.1017.0",
|
|
44
|
+
"@aws-sdk/client-sts": "^3.1017.0",
|
|
42
45
|
"@aws-sdk/s3-request-presigner": "^3.895.0",
|
|
43
46
|
"axios": "^1.12.2",
|
|
44
47
|
"date-fns": "^4.1.0",
|