idea-aws 4.4.13 → 4.4.15

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,6 +1,6 @@
1
1
  import { SignedURL } from 'idea-toolbox';
2
2
  import { DynamoDB } from './dynamoDB';
3
- import { S3 } from './s3';
3
+ import { S3, SignedURLOptions } from './s3';
4
4
  /**
5
5
  * A custom class that takes advantage of DynamoDB and S3 to easily manage attachments.
6
6
  */
@@ -8,20 +8,42 @@ export declare class Attachments {
8
8
  protected ddb: DynamoDB;
9
9
  protected s3: S3;
10
10
  /**
11
- * The bucket where from to retrieve the attachments. Fallback to IDEA's default one.
11
+ * The bucket where from to retrieve the attachments. Fallback to IDEA's default one (CDK).
12
12
  */
13
- protected S3_ATTACHMENTS_BUCKET: string;
13
+ bucket: string;
14
14
  /**
15
- * The prefix for attachment IDs. Fallback to IDEA's default one.
15
+ * The folder where from to retrieve the attachments. Fallback to IDEA's default one (CDK).
16
16
  */
17
- protected IUID_ATTACHMENTS_PREFIX: string;
18
- constructor(ddb: DynamoDB, s3: S3);
17
+ folder: string;
19
18
  /**
20
- * Get a signedURL to put an attachment.
19
+ * The default prefix for attachment IDs.
21
20
  */
22
- put(project: string, teamId: string): Promise<SignedURL>;
21
+ prefix: string;
22
+ constructor(ddb: DynamoDB, s3: S3, options?: AttachmentsInitOptions);
23
23
  /**
24
- * Get a signedURL to retrieve an attachment.
24
+ * Get a `SignedURL` to upload an attachment.
25
25
  */
26
- get(attachmentId: string): Promise<SignedURL>;
26
+ put(options?: AttachmentsOptions): Promise<SignedURL>;
27
+ /**
28
+ * Get a `SignedURL` to download an attachment.
29
+ */
30
+ get(attachmentId: string, options?: AttachmentsOptions): Promise<SignedURL>;
31
+ }
32
+ /**
33
+ * Options when creating a new instance of Attachments.
34
+ */
35
+ export interface AttachmentsInitOptions {
36
+ /**
37
+ * Whether to enable the compatibility mode to older versions (temporary, it will be removed).
38
+ */
39
+ compatibility?: 'v1';
40
+ }
41
+ /**
42
+ * Options when preparing for the upload/download of an attachment.
43
+ */
44
+ export interface AttachmentsOptions extends SignedURLOptions {
45
+ /**
46
+ * The attachment ID prefix, if different from the internal attribute `prefix`.
47
+ */
48
+ prefix?: string;
27
49
  }
@@ -1,37 +1,52 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Attachments = void 0;
4
+ const genericController_1 = require("./genericController");
4
5
  /**
5
6
  * A custom class that takes advantage of DynamoDB and S3 to easily manage attachments.
6
7
  */
7
8
  class Attachments {
8
- constructor(ddb, s3) {
9
+ constructor(ddb, s3, options = {}) {
9
10
  this.ddb = ddb;
10
11
  this.s3 = s3;
11
12
  /**
12
- * The bucket where from to retrieve the attachments. Fallback to IDEA's default one.
13
+ * The bucket where from to retrieve the attachments. Fallback to IDEA's default one (CDK).
13
14
  */
14
- this.S3_ATTACHMENTS_BUCKET = process.env.S3_ATTACHMENTS_BUCKET ?? 'idea-attachments';
15
+ this.bucket = process.env.S3_BUCKET_MEDIA;
15
16
  /**
16
- * The prefix for attachment IDs. Fallback to IDEA's default one.
17
+ * The folder where from to retrieve the attachments. Fallback to IDEA's default one (CDK).
17
18
  */
18
- this.IUID_ATTACHMENTS_PREFIX = process.env.IUID_ATTACHMENTS_PREFIX ?? 'ATT';
19
+ this.folder = process.env.S3_ATTACHMENTS_FOLDER;
20
+ /**
21
+ * The default prefix for attachment IDs.
22
+ */
23
+ this.prefix = process.env.PROJECT ? process.env.PROJECT.concat('_ATT') : 'ATT';
24
+ if (options.compatibility === 'v1') {
25
+ this.bucket = process.env.S3_ATTACHMENTS_BUCKET || 'idea-attachments';
26
+ this.folder = null;
27
+ this.prefix = 'ATT';
28
+ }
19
29
  }
20
30
  /**
21
- * Get a signedURL to put an attachment.
31
+ * Get a `SignedURL` to upload an attachment.
22
32
  */
23
- async put(project, teamId) {
24
- const attachmentIdPrefix = this.IUID_ATTACHMENTS_PREFIX.concat('_', project, '_', teamId);
25
- const attachmentId = await this.ddb.IUNID(attachmentIdPrefix);
26
- const signedURL = await this.s3.signedURLPut(this.S3_ATTACHMENTS_BUCKET, attachmentId);
33
+ async put(options = {}) {
34
+ const attachmentId = await this.ddb.IUNID(options.prefix || this.prefix);
35
+ const key = this.folder ? `${this.folder}/${attachmentId}` : attachmentId;
36
+ const signedURL = await this.s3.signedURLPut(this.bucket, key);
27
37
  signedURL.id = attachmentId;
28
38
  return signedURL;
29
39
  }
30
40
  /**
31
- * Get a signedURL to retrieve an attachment.
41
+ * Get a `SignedURL` to download an attachment.
32
42
  */
33
- async get(attachmentId) {
34
- const signedURL = await this.s3.signedURLGet(this.S3_ATTACHMENTS_BUCKET, attachmentId);
43
+ async get(attachmentId, options = {}) {
44
+ if (!attachmentId)
45
+ throw new genericController_1.HandledError('Missing attachment ID');
46
+ if (!attachmentId.startsWith(options.prefix || this.prefix))
47
+ throw new genericController_1.HandledError('Not found');
48
+ const key = this.folder ? `${this.folder}/${attachmentId}` : attachmentId;
49
+ const signedURL = await this.s3.signedURLGet(this.bucket, key);
35
50
  signedURL.id = attachmentId;
36
51
  return signedURL;
37
52
  }
@@ -97,6 +97,14 @@ export declare class Cognito {
97
97
  * Update a (Cognito)User's attributes, excluding the attributes that require specific methods.
98
98
  */
99
99
  updateUser(user: CognitoUser, userPoolId: string): Promise<void>;
100
+ /**
101
+ * Disable a cognito user.
102
+ */
103
+ disableUser(email: string, userPoolId: string): Promise<void>;
104
+ /**
105
+ * Enable a cognito user.
106
+ */
107
+ enableUser(email: string, userPoolId: string): Promise<void>;
100
108
  /**
101
109
  * Sign out the user from all devices.
102
110
  */
@@ -148,10 +156,14 @@ export interface CognitoUserGeneric {
148
156
  * The email (=== username).
149
157
  */
150
158
  email: string;
159
+ /**
160
+ * Whether the user has been disabled.
161
+ */
162
+ disabled: boolean;
151
163
  /**
152
164
  * Cognito can have custom attributes.
153
165
  */
154
- [attribute: string]: string;
166
+ [attribute: string]: string | number | boolean;
155
167
  }
156
168
  /**
157
169
  * Options when creating a new user.
@@ -68,6 +68,7 @@ class Cognito {
68
68
  (user.Attributes ?? user.UserAttributes ?? []).forEach((a) => (userAttributes[a.Name] = a.Value));
69
69
  if (!userAttributes.userId)
70
70
  userAttributes.userId = userAttributes.sub;
71
+ userAttributes.disabled = !user.Enabled;
71
72
  return userAttributes;
72
73
  }
73
74
  //
@@ -316,6 +317,20 @@ class Cognito {
316
317
  });
317
318
  await this.client.send(command);
318
319
  }
320
+ /**
321
+ * Disable a cognito user.
322
+ */
323
+ async disableUser(email, userPoolId) {
324
+ const command = new CognitoIP.AdminDisableUserCommand({ UserPoolId: userPoolId, Username: email });
325
+ await this.client.send(command);
326
+ }
327
+ /**
328
+ * Enable a cognito user.
329
+ */
330
+ async enableUser(email, userPoolId) {
331
+ const command = new CognitoIP.AdminEnableUserCommand({ UserPoolId: userPoolId, Username: email });
332
+ await this.client.send(command);
333
+ }
319
334
  /**
320
335
  * Sign out the user from all devices.
321
336
  */
@@ -15,7 +15,7 @@ export declare class DynamoDB {
15
15
  */
16
16
  unmarshall(data: Record<string, any>, options?: DDBUtils.unmarshallOptions): Record<string, any>;
17
17
  /**
18
- * Returns an IUNID: IDEA's Unique Nano IDentifier, which is an id unique through an IDEA's AWS account and region.
18
+ * Returns an IUNID: IDEA's Unique Nano IDentifier, which is an id unique through an AWS region inside an account.
19
19
  * Note: no need of an auth check for external uses: the permissions depend from the context in which it's executed.
20
20
  * @param project project code
21
21
  * @return the IUNID
@@ -49,7 +49,7 @@ class DynamoDB {
49
49
  return DDBUtils.unmarshall(data, options);
50
50
  }
51
51
  /**
52
- * Returns an IUNID: IDEA's Unique Nano IDentifier, which is an id unique through an IDEA's AWS account and region.
52
+ * Returns an IUNID: IDEA's Unique Nano IDentifier, which is an id unique through an AWS region inside an account.
53
53
  * Note: no need of an auth check for external uses: the permissions depend from the context in which it's executed.
54
54
  * @param project project code
55
55
  * @return the IUNID
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "idea-aws",
3
- "version": "4.4.13",
3
+ "version": "4.4.15",
4
4
  "description": "AWS wrappers to use in IDEA's back-ends",
5
5
  "license": "MPL-2.0",
6
6
  "main": "dist/index.js",
@@ -39,7 +39,7 @@
39
39
  "dependencies": {
40
40
  "@aws-lambda-powertools/metrics": "^1.18.1",
41
41
  "@aws-lambda-powertools/tracer": "^1.18.1",
42
- "idea-toolbox": "^7.0.8",
42
+ "idea-toolbox": "^7.0.11",
43
43
  "nanoid": "^3.3.7",
44
44
  "nodemailer": "^6.9.14",
45
45
  "source-map-support": "^0.5.21"