idea-aws 4.3.5 → 4.4.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.
package/dist/src/s3.d.ts DELETED
@@ -1,225 +0,0 @@
1
- import * as AWSS3 from '@aws-sdk/client-s3';
2
- import { BodyDataTypes } from '@aws-sdk/lib-storage';
3
- import { SignedURL } from 'idea-toolbox';
4
- import { LambdaLogger } from './lambdaLogger';
5
- /**
6
- * A wrapper for AWS Simple Storage Service.
7
- */
8
- export declare class S3 {
9
- protected s3: AWSS3.S3Client;
10
- protected logger: LambdaLogger;
11
- protected DEFAULT_DOWNLOAD_BUCKET_PREFIX: string;
12
- protected DEFAULT_DOWNLOAD_BUCKET: string;
13
- protected DEFAULT_DOWNLOAD_BUCKET_SEC_TO_EXP: number;
14
- protected DEFAULT_UPLOAD_BUCKET_SEC_TO_EXP: number;
15
- constructor();
16
- /**
17
- * Create a download link of a piece of data (through S3).
18
- * *Practically*, it uploads the file on an S3 bucket, generating and returning a url to it.
19
- */
20
- createDownloadURLFromData(data: BodyDataTypes, options?: CreateDownloadURLFromDataOptions): Promise<SignedURL>;
21
- /**
22
- * Get a signed URL to put a file on a S3 bucket.
23
- */
24
- signedURLPut(bucket: string, key: string, options?: SignedURLOptions): Promise<SignedURL>;
25
- /**
26
- * Get a signed URL to get a file on a S3 bucket.
27
- */
28
- signedURLGet(bucket: string, key: string, options?: SignedURLOptions): Promise<SignedURL>;
29
- /**
30
- * Make a copy of an object of the bucket.
31
- */
32
- copyObject(options: CopyObjectOptions): Promise<void>;
33
- /**
34
- * Get an object from a S3 bucket.
35
- */
36
- getObject(options: GetObjectOptions): Promise<AWSS3.GetObjectCommandOutput>;
37
- /**
38
- * Get an object from a S3 bucket and parse the content as a JSON object.
39
- */
40
- getObjectAsJSON(options: GetObjectOptions): Promise<any>;
41
- /**
42
- * Get an object from a S3 bucket and convert the content to string.
43
- */
44
- getObjectAsText(options: GetObjectOptions): Promise<string>;
45
- /**
46
- * Put an object in a S3 bucket.
47
- */
48
- putObject(options: PutObjectOptions): Promise<AWSS3.PutObjectOutput>;
49
- /**
50
- * Delete an object from an S3 bucket.
51
- */
52
- deleteObject(options: DeleteObjectOptions): Promise<AWSS3.PutObjectOutput>;
53
- /**
54
- * List the objects of an S3 bucket.
55
- */
56
- listObjects(options: ListObjectsOptions): Promise<AWSS3.ListObjectsOutput>;
57
- /**
58
- * List the objects keys of an S3 bucket.
59
- */
60
- listObjectsKeys(options: ListObjectsOptions): Promise<string[]>;
61
- /**
62
- * Check whether an object exists in an S3 bucket.
63
- */
64
- doesObjectExist(options: HeadObjectOptions): Promise<boolean>;
65
- }
66
- /**
67
- * Options for creating a download URL.
68
- */
69
- export interface CreateDownloadURLFromDataOptions {
70
- /**
71
- * Downloads bucket; default: `idea-downloads`.
72
- */
73
- bucket?: string;
74
- /**
75
- * Folder (e.g. the project name); default: `common`.
76
- */
77
- prefix?: string;
78
- /**
79
- * The unique filepath in which to store the file; default: _random_.
80
- */
81
- key?: string;
82
- /**
83
- * Content type, e.g. application/json; default: _guessed_.
84
- */
85
- contentType?: string;
86
- /**
87
- * Seconds to URL expiration; default: `180`.
88
- */
89
- secToExp?: number;
90
- /**
91
- * The suggested name for the file once it's downloaded/saved.
92
- * Note: the string is cleaned to ensure maximum compatibility with every OS.
93
- */
94
- filename?: string;
95
- }
96
- /**
97
- * Options for generating a signed URL.
98
- */
99
- export interface SignedURLOptions {
100
- /**
101
- * Seconds to URL expiration; default: `180` for GET, `300` for PUT.
102
- */
103
- secToExp?: number;
104
- /**
105
- * The suggested name for the file once it's downloaded/saved.
106
- * Note: the string is cleaned to ensure maximum compatibility with every OS.
107
- */
108
- filename?: string;
109
- }
110
- /**
111
- * Options for copying an object.
112
- */
113
- export interface CopyObjectOptions {
114
- /**
115
- * The source path (complete with the bucket name).
116
- */
117
- copySource: string;
118
- /**
119
- * The bucket in which to copy the file.
120
- */
121
- bucket: string;
122
- /**
123
- * The complete filepath of the bucket in which to copy the file.
124
- */
125
- key: string;
126
- }
127
- /**
128
- * Options for getting an object.
129
- */
130
- export interface GetObjectOptions {
131
- /**
132
- * The bucket from which to acquire the file.
133
- */
134
- bucket: string;
135
- /**
136
- * The complete filepath (within the bucket) from which to acquire the file.
137
- */
138
- key: string;
139
- /**
140
- * The suggested name for the file once it's downloaded/saved.
141
- * Note: the string is cleaned to ensure maximum compatibility with every OS.
142
- */
143
- filename?: string;
144
- }
145
- /**
146
- * Options for getting the head (main metadata, without the content) of an object.
147
- */
148
- export interface HeadObjectOptions {
149
- /**
150
- * The bucket from which to acquire the file.
151
- */
152
- bucket: string;
153
- /**
154
- * The complete filepath (within the bucket) from which to acquire the file.
155
- */
156
- key: string;
157
- /**
158
- * If set, the request will fail in case the object is empty (`ContentLength === 0`).
159
- */
160
- emptyMeansNotFound?: boolean;
161
- }
162
- /**
163
- * Options for putting an object.
164
- */
165
- export interface PutObjectOptions {
166
- /**
167
- * The bucket in which to copy the file.
168
- */
169
- bucket: string;
170
- /**
171
- * The complete filepath of the bucket in which to copy the file.
172
- */
173
- key: string;
174
- /**
175
- * The content of the file.
176
- */
177
- body: any;
178
- /**
179
- * Content type (e.g. image/png).
180
- */
181
- contentType?: string;
182
- /**
183
- * Access-control list (e.g. public-read).
184
- */
185
- acl?: string;
186
- /**
187
- * A set of metadata as attributes
188
- */
189
- metadata?: any;
190
- /**
191
- * The suggested name for the file once it's downloaded/saved.
192
- * Note: the string is cleaned to ensure maximum compatibility with every OS.
193
- */
194
- filename?: string;
195
- }
196
- /**
197
- * Options for deleting an object.
198
- */
199
- export interface DeleteObjectOptions {
200
- /**
201
- * The bucket from which to delete the file.
202
- */
203
- bucket: string;
204
- /**
205
- * The complete filepath to the file to delete.
206
- */
207
- key: string;
208
- }
209
- /**
210
- * Options for listing a bucket's objects.
211
- */
212
- export interface ListObjectsOptions {
213
- /**
214
- * The bucket from which to list the objects.
215
- */
216
- bucket: string;
217
- /**
218
- * The prefix to filter the objects to select, based on the key.
219
- */
220
- prefix?: string;
221
- }
222
- /**
223
- * Clean a filename to be compatible with most OS.
224
- */
225
- export declare const cleanFilename: (filename: string) => string;
package/dist/src/s3.js DELETED
@@ -1,180 +0,0 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.cleanFilename = exports.S3 = void 0;
27
- const AWSS3 = __importStar(require("@aws-sdk/client-s3"));
28
- const lib_storage_1 = require("@aws-sdk/lib-storage");
29
- const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
30
- const idea_toolbox_1 = require("idea-toolbox");
31
- const lambdaLogger_1 = require("./lambdaLogger");
32
- /**
33
- * A wrapper for AWS Simple Storage Service.
34
- */
35
- class S3 {
36
- constructor() {
37
- this.logger = new lambdaLogger_1.LambdaLogger();
38
- this.DEFAULT_DOWNLOAD_BUCKET_PREFIX = 'common';
39
- this.DEFAULT_DOWNLOAD_BUCKET = 'idea-downloads';
40
- this.DEFAULT_DOWNLOAD_BUCKET_SEC_TO_EXP = 180;
41
- this.DEFAULT_UPLOAD_BUCKET_SEC_TO_EXP = 300;
42
- this.s3 = new AWSS3.S3Client();
43
- }
44
- /**
45
- * Create a download link of a piece of data (through S3).
46
- * *Practically*, it uploads the file on an S3 bucket, generating and returning a url to it.
47
- */
48
- async createDownloadURLFromData(data, options = {}) {
49
- // if needed, randomly generates the key
50
- if (!options.key)
51
- options.key = Date.now().toString().concat(Math.random().toString(36).slice(2));
52
- options.key = `${options.prefix ?? this.DEFAULT_DOWNLOAD_BUCKET_PREFIX}/${options.key}`;
53
- options.bucket = options.bucket ?? this.DEFAULT_DOWNLOAD_BUCKET;
54
- options.secToExp = options.secToExp ?? this.DEFAULT_DOWNLOAD_BUCKET_SEC_TO_EXP;
55
- const params = { Bucket: options.bucket, Key: options.key, Body: data, ContentType: options.contentType };
56
- const upload = new lib_storage_1.Upload({ client: this.s3, params });
57
- await upload.done();
58
- return this.signedURLGet(options.bucket, options.key, { secToExp: options.secToExp, filename: options.filename });
59
- }
60
- /**
61
- * Get a signed URL to put a file on a S3 bucket.
62
- */
63
- async signedURLPut(bucket, key, options = {}) {
64
- const putParams = { Bucket: bucket, Key: key };
65
- if (options.filename)
66
- putParams.ContentDisposition = `attachment; filename ="${(0, exports.cleanFilename)(options.filename)}"`;
67
- const expiresIn = options.secToExp ?? this.DEFAULT_UPLOAD_BUCKET_SEC_TO_EXP;
68
- const url = await (0, s3_request_presigner_1.getSignedUrl)(this.s3, new AWSS3.PutObjectCommand(putParams), { expiresIn });
69
- return new idea_toolbox_1.SignedURL({ url });
70
- }
71
- /**
72
- * Get a signed URL to get a file on a S3 bucket.
73
- */
74
- async signedURLGet(bucket, key, options = {}) {
75
- const getParams = { Bucket: bucket, Key: key };
76
- if (options.filename)
77
- getParams.ResponseContentDisposition = `attachment; filename ="${(0, exports.cleanFilename)(options.filename)}"`;
78
- const expiresIn = options.secToExp ?? this.DEFAULT_DOWNLOAD_BUCKET_SEC_TO_EXP;
79
- const url = await (0, s3_request_presigner_1.getSignedUrl)(this.s3, new AWSS3.GetObjectCommand(getParams), { expiresIn });
80
- return new idea_toolbox_1.SignedURL({ url });
81
- }
82
- /**
83
- * Make a copy of an object of the bucket.
84
- */
85
- async copyObject(options) {
86
- this.logger.trace(`S3 copy object: ${options.key}`);
87
- const command = new AWSS3.CopyObjectCommand({
88
- CopySource: options.copySource,
89
- Bucket: options.bucket,
90
- Key: options.key
91
- });
92
- await this.s3.send(command);
93
- }
94
- /**
95
- * Get an object from a S3 bucket.
96
- */
97
- async getObject(options) {
98
- this.logger.trace(`S3 get object: ${options.key}`);
99
- const params = { Bucket: options.bucket, Key: options.key };
100
- if (options.filename)
101
- params.ResponseContentDisposition = `attachment; filename ="${(0, exports.cleanFilename)(options.filename)}"`;
102
- const command = new AWSS3.GetObjectCommand(params);
103
- return await this.s3.send(command);
104
- }
105
- /**
106
- * Get an object from a S3 bucket and parse the content as a JSON object.
107
- */
108
- async getObjectAsJSON(options) {
109
- const result = await this.getObject(options);
110
- return JSON.parse(await result.Body.transformToString('utf-8'));
111
- }
112
- /**
113
- * Get an object from a S3 bucket and convert the content to string.
114
- */
115
- async getObjectAsText(options) {
116
- const result = await this.getObject(options);
117
- return await result.Body.transformToString('utf-8');
118
- }
119
- /**
120
- * Put an object in a S3 bucket.
121
- */
122
- async putObject(options) {
123
- const params = { Bucket: options.bucket, Key: options.key, Body: options.body };
124
- if (options.contentType)
125
- params.ContentType = options.contentType;
126
- if (options.acl)
127
- params.ACL = options.acl;
128
- if (options.metadata)
129
- params.Metadata = options.metadata;
130
- if (options.filename)
131
- params.ContentDisposition = `attachment; filename ="${(0, exports.cleanFilename)(options.filename)}"`;
132
- this.logger.trace(`S3 put object: ${options.key}`);
133
- return await this.s3.send(new AWSS3.PutObjectCommand(params));
134
- }
135
- /**
136
- * Delete an object from an S3 bucket.
137
- */
138
- async deleteObject(options) {
139
- this.logger.trace(`S3 delete object: ${options.key}`);
140
- const deleteCommand = new AWSS3.DeleteObjectCommand({ Bucket: options.bucket, Key: options.key });
141
- return await this.s3.send(deleteCommand);
142
- }
143
- /**
144
- * List the objects of an S3 bucket.
145
- */
146
- async listObjects(options) {
147
- this.logger.trace(`S3 list object: ${options.prefix}`);
148
- const command = new AWSS3.ListObjectsCommand({ Bucket: options.bucket, Prefix: options.prefix });
149
- return await this.s3.send(command);
150
- }
151
- /**
152
- * List the objects keys of an S3 bucket.
153
- */
154
- async listObjectsKeys(options) {
155
- const result = await this.listObjects(options);
156
- return result.Contents.map(obj => obj.Key);
157
- }
158
- /**
159
- * Check whether an object exists in an S3 bucket.
160
- */
161
- async doesObjectExist(options) {
162
- try {
163
- const command = new AWSS3.HeadObjectCommand({ Bucket: options.bucket, Key: options.key });
164
- const { ContentLength } = await this.s3.send(command);
165
- if (options.emptyMeansNotFound)
166
- return ContentLength > 0;
167
- else
168
- return true;
169
- }
170
- catch (err) {
171
- return false;
172
- }
173
- }
174
- }
175
- exports.S3 = S3;
176
- /**
177
- * Clean a filename to be compatible with most OS.
178
- */
179
- const cleanFilename = (filename) => filename.replace(/[^a-z0-9-.\s]/gi, '_');
180
- exports.cleanFilename = cleanFilename;
@@ -1,15 +0,0 @@
1
- import * as AWSSecretsManager from '@aws-sdk/client-secrets-manager';
2
- /**
3
- * A wrapper for AWS Secrets manager.
4
- */
5
- export declare class SecretsManager {
6
- protected sm: AWSSecretsManager.SecretsManagerClient;
7
- protected cache: Map<string, string>;
8
- constructor();
9
- /**
10
- * Get a secret string from the Secret Manager by its id.
11
- */
12
- getStringById(secretId: string, options?: {
13
- noCache?: boolean;
14
- }): Promise<string>;
15
- }
@@ -1,48 +0,0 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.SecretsManager = void 0;
27
- const AWSSecretsManager = __importStar(require("@aws-sdk/client-secrets-manager"));
28
- /**
29
- * A wrapper for AWS Secrets manager.
30
- */
31
- class SecretsManager {
32
- constructor() {
33
- this.cache = new Map();
34
- this.sm = new AWSSecretsManager.SecretsManagerClient();
35
- }
36
- /**
37
- * Get a secret string from the Secret Manager by its id.
38
- */
39
- async getStringById(secretId, options = {}) {
40
- if (!options.noCache && this.cache.has(secretId))
41
- return this.cache.get(secretId);
42
- const command = new AWSSecretsManager.GetSecretValueCommand({ SecretId: secretId });
43
- const { SecretString } = await this.sm.send(command);
44
- this.cache.set(secretId, SecretString);
45
- return SecretString;
46
- }
47
- }
48
- exports.SecretsManager = SecretsManager;
package/dist/src/ses.d.ts DELETED
@@ -1,161 +0,0 @@
1
- /// <reference types="node" />
2
- import * as AWSSES from '@aws-sdk/client-sesv2';
3
- import { SentMessageInfo as NodemailerSentMessageInfo } from 'nodemailer';
4
- import { Headers } from 'nodemailer/lib/mailer';
5
- import { LambdaLogger } from './lambdaLogger';
6
- /**
7
- * A wrapper for AWS Simple Email Service.
8
- */
9
- export declare class SES {
10
- protected ses: AWSSES.SESv2Client;
11
- protected logger: LambdaLogger;
12
- constructor(options?: {
13
- region?: string;
14
- });
15
- getTemplate(templateName: string): Promise<AWSSES.EmailTemplateContent>;
16
- setTemplate(templateName: string, subject: string, content: string, isHTML?: boolean): Promise<void>;
17
- deleteTemplate(templateName: string): Promise<void>;
18
- testTemplate(templateName: string, data: {
19
- [variable: string]: any;
20
- }): Promise<string>;
21
- /**
22
- * Send a templated email through AWS Simple Email Service.
23
- */
24
- sendTemplatedEmail(emailData: TemplatedEmailData, sesParams: SESParams): Promise<AWSSES.SendEmailCommandOutput>;
25
- /**
26
- * Send an email through AWS Simple Email Service.
27
- * It supports IDEA's teams custom configuration.
28
- */
29
- sendEmail(emailData: EmailData, sesParams: SESParams): Promise<AWSSES.SendEmailResponse | NodemailerSentMessageInfo>;
30
- private searchForCustomSESConfigByTeamId;
31
- private sendEmailWithSES;
32
- private sendEmailWithNodemailer;
33
- private prepareEmailDestination;
34
- private prepareEmailMessage;
35
- }
36
- /**
37
- * The basic data to send an email.
38
- */
39
- export interface BasicEmailData {
40
- /**
41
- * Array of TO email addresses.
42
- */
43
- toAddresses: string[];
44
- /**
45
- * Array of CC email addresses.
46
- */
47
- ccAddresses?: string[];
48
- /**
49
- * Array of BCC email addresses.
50
- */
51
- bccAddresses?: string[];
52
- /**
53
- * Array of Reply-To email addresses.
54
- */
55
- replyToAddresses?: string[];
56
- }
57
- /**
58
- * The data to send an email.
59
- */
60
- export interface EmailData extends BasicEmailData {
61
- /**
62
- * Subject of the email.
63
- */
64
- subject: string;
65
- /**
66
- * HTML content of the email.
67
- */
68
- html?: string;
69
- /**
70
- * Text content of the email
71
- */
72
- text?: string;
73
- /**
74
- * The array of attachments. Ref. https://community.nodemailer.com/using-attachments/
75
- */
76
- attachments?: EmailAttachment[];
77
- }
78
- /**
79
- * Email attachment interface of Nodemailer.
80
- */
81
- export interface EmailAttachment {
82
- /**
83
- * String, Buffer or a Stream contents for the attachmentent
84
- */
85
- content?: string | Buffer;
86
- /**
87
- * Path to a file or an URL (data uris are allowed as well) if you want to stream the file instead of including it
88
- * (better for larger attachments).
89
- */
90
- path?: string;
91
- /**
92
- * Filename to be reported as the name of the attached file, use of unicode is allowed.
93
- * If you do not want to use a filename, set this value as false, otherwise a filename is generated automatically .
94
- */
95
- filename?: string | false;
96
- /**
97
- * If set and content is string, then encodes the content to a Buffer using the specified encoding.
98
- * Example values: base64, hex, binary etc. Useful if you want to use binary attachments in a JSON formatted e-mail.
99
- */
100
- encoding?: string;
101
- /**
102
- * Optional content type for the attachment, if not set will be derived from the filename property
103
- */
104
- contentType?: string;
105
- /**
106
- * Additional headers
107
- */
108
- headers?: Headers;
109
- /**
110
- * Optional value that overrides entire node content in the mime message.
111
- * If used then all other options set for this node are ignored.
112
- */
113
- raw?: string | Buffer;
114
- }
115
- /**
116
- * The data to send a templated email.
117
- * Note: templated email don't support attachments by now.
118
- */
119
- export interface TemplatedEmailData extends BasicEmailData {
120
- /**
121
- * The template to use for sending the email.
122
- * To reference variables, use placeholders such as `{{myVar}}`.
123
- */
124
- template: string;
125
- /**
126
- * An object containing key-value pairs of variable-content to substitute.
127
- * It supports handlebars.js templating.
128
- */
129
- templateData: {
130
- [variable: string]: any;
131
- };
132
- /**
133
- * The name of the configuration set to use for the sending.
134
- */
135
- configurationSet?: string;
136
- }
137
- /**
138
- * SES configuration.
139
- */
140
- export interface SESParams {
141
- /**
142
- * The source (from) email address.
143
- */
144
- source: string;
145
- /**
146
- * The optional name of the source (e.g. Matteo Carbone).
147
- */
148
- sourceName?: string;
149
- /**
150
- * The SES source ARN to use, in case the source doesn't directly match a SES validated email address.
151
- */
152
- sourceArn?: string;
153
- /**
154
- * The SES region to use, in case it differs from the one of the Lambda function running the command.
155
- */
156
- region?: string;
157
- /**
158
- * If set, a custom SES configuration to use for the team will be searched in the table `idea_teamsSES`.
159
- */
160
- teamId?: string;
161
- }