idea-aws 4.4.1 → 4.4.3
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/.prettierrc +9 -0
- package/.vscode/settings.json +26 -0
- package/HOW-TO-RELEASE.md +8 -0
- package/build.sh +21 -0
- package/dist/src/attachments.d.ts +27 -0
- package/dist/src/attachments.js +39 -0
- package/dist/src/cognito.d.ts +177 -0
- package/dist/src/cognito.js +412 -0
- package/dist/src/comprehend.d.ts +34 -0
- package/dist/src/comprehend.js +58 -0
- package/dist/src/dynamoDB.d.ts +108 -0
- package/dist/src/dynamoDB.js +296 -0
- package/dist/src/genericController.d.ts +60 -0
- package/dist/src/genericController.js +89 -0
- package/dist/src/lambdaLogger.d.ts +13 -0
- package/dist/src/lambdaLogger.js +43 -0
- package/dist/src/metrics.d.ts +31 -0
- package/dist/src/metrics.js +45 -0
- package/dist/src/resourceController.d.ts +232 -0
- package/dist/src/resourceController.js +561 -0
- package/dist/src/s3.d.ts +225 -0
- package/dist/src/s3.js +180 -0
- package/dist/src/secretsManager.d.ts +15 -0
- package/dist/src/secretsManager.js +48 -0
- package/dist/src/ses.d.ts +161 -0
- package/dist/src/ses.js +196 -0
- package/dist/src/sns.d.ts +60 -0
- package/dist/src/sns.js +94 -0
- package/dist/src/ssm.d.ts +22 -0
- package/dist/src/ssm.js +54 -0
- package/dist/src/streamController.d.ts +11 -0
- package/dist/src/streamController.js +20 -0
- package/dist/src/translate.d.ts +61 -0
- package/dist/src/translate.js +155 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/custom.css +4 -0
- package/docs/assets/highlight.css +50 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/Attachments.html +201 -0
- package/docs/classes/Cognito.html +713 -0
- package/docs/classes/Comprehend.html +176 -0
- package/docs/classes/DynamoDB.html +584 -0
- package/docs/classes/GenericController.html +262 -0
- package/docs/classes/HandledError.html +219 -0
- package/docs/classes/LambdaLogger.html +220 -0
- package/docs/classes/ResourceController.html +957 -0
- package/docs/classes/S3.html +391 -0
- package/docs/classes/SES.html +335 -0
- package/docs/classes/SNS.html +185 -0
- package/docs/classes/SecretsManager.html +159 -0
- package/docs/classes/StreamController.html +284 -0
- package/docs/classes/SystemsManager.html +184 -0
- package/docs/classes/Translate.html +239 -0
- package/docs/classes/UnhandledError.html +252 -0
- package/docs/functions/cleanFilename.html +93 -0
- package/docs/index.html +95 -0
- package/docs/interfaces/BasicEmailData.html +145 -0
- package/docs/interfaces/CognitoGroup.html +122 -0
- package/docs/interfaces/CognitoUserGeneric.html +125 -0
- package/docs/interfaces/CopyObjectOptions.html +132 -0
- package/docs/interfaces/CreateDownloadURLFromDataOptions.html +163 -0
- package/docs/interfaces/CreateUserOptions.html +122 -0
- package/docs/interfaces/DeleteObjectOptions.html +122 -0
- package/docs/interfaces/DetectSentimentParameters.html +121 -0
- package/docs/interfaces/EmailAttachment.html +176 -0
- package/docs/interfaces/EmailData.html +188 -0
- package/docs/interfaces/GetObjectOptions.html +133 -0
- package/docs/interfaces/HeadObjectOptions.html +132 -0
- package/docs/interfaces/InternalAPIRequestParams.html +207 -0
- package/docs/interfaces/ListObjectsOptions.html +122 -0
- package/docs/interfaces/PutObjectOptions.html +173 -0
- package/docs/interfaces/ResourceControllerOptions.html +142 -0
- package/docs/interfaces/SESParams.html +152 -0
- package/docs/interfaces/SNSCreateEndpointParams.html +132 -0
- package/docs/interfaces/SNSPublishParams.html +142 -0
- package/docs/interfaces/SignedURLOptions.html +123 -0
- package/docs/interfaces/TemplatedEmailData.html +186 -0
- package/docs/interfaces/TranslateParameters.html +140 -0
- package/docs/modules.html +130 -0
- package/docs/variables/LOG_LEVELS_PRIORITY.html +81 -0
- package/package.json +2 -2
- package/src/attachments.ts +41 -0
- package/src/cognito.ts +511 -0
- package/src/comprehend.ts +52 -0
- package/src/dynamoDB.ts +311 -0
- package/src/genericController.ts +103 -0
- package/src/lambdaLogger.ts +39 -0
- package/src/metrics.ts +45 -0
- package/src/resourceController.ts +645 -0
- package/src/s3.ts +334 -0
- package/src/secretsManager.ts +24 -0
- package/src/ses.ts +313 -0
- package/src/sns.ts +118 -0
- package/src/ssm.ts +33 -0
- package/src/streamController.ts +25 -0
- package/src/translate.ts +174 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,412 @@
|
|
|
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.Cognito = void 0;
|
|
27
|
+
const CognitoIP = __importStar(require("@aws-sdk/client-cognito-identity-provider"));
|
|
28
|
+
const idea_toolbox_1 = require("idea-toolbox");
|
|
29
|
+
/**
|
|
30
|
+
* A wrapper for AWS Cognito.
|
|
31
|
+
*/
|
|
32
|
+
class Cognito {
|
|
33
|
+
constructor(options = {}) {
|
|
34
|
+
this.cognito = new CognitoIP.CognitoIdentityProviderClient({ region: options.region });
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Change the region in which to find the user pool.
|
|
38
|
+
* Default: the runner's (e.g. Lambda function) region.
|
|
39
|
+
*/
|
|
40
|
+
setRegion(region) {
|
|
41
|
+
// there is no quick way to change the region without re-creating the object
|
|
42
|
+
this.cognito = new CognitoIP.CognitoIdentityProviderClient({ region });
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get the attributes of the user, from the authorizer claims.
|
|
46
|
+
* @param claims authorizer claims
|
|
47
|
+
* @return user's data
|
|
48
|
+
* @deprecated use idea-toolbox's CognitoUser instead
|
|
49
|
+
*/
|
|
50
|
+
getUserByClaims(claims) {
|
|
51
|
+
if (!claims)
|
|
52
|
+
return null;
|
|
53
|
+
const user = {};
|
|
54
|
+
// add any additional cognito attribute available in cognito
|
|
55
|
+
for (const p in claims)
|
|
56
|
+
if (p.startsWith('cognito:'))
|
|
57
|
+
user[p.slice(8)] = claims[p];
|
|
58
|
+
// map the important attributes with reserved names
|
|
59
|
+
user.userId = claims.sub;
|
|
60
|
+
user.email = claims.email;
|
|
61
|
+
return user;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Map the complex structure returned by Cognito for a user's attributes in a simple key-value object.
|
|
65
|
+
*/
|
|
66
|
+
mapCognitoUserAttributesAsPlainObject(user) {
|
|
67
|
+
const userAttributes = {};
|
|
68
|
+
(user.Attributes ?? user.UserAttributes ?? []).forEach((a) => (userAttributes[a.Name] = a.Value));
|
|
69
|
+
if (!userAttributes.userId)
|
|
70
|
+
userAttributes.userId = userAttributes.sub;
|
|
71
|
+
return userAttributes;
|
|
72
|
+
}
|
|
73
|
+
//
|
|
74
|
+
// ADMIN
|
|
75
|
+
//
|
|
76
|
+
/**
|
|
77
|
+
* Identify a user by its email address, returning its attributes.
|
|
78
|
+
*/
|
|
79
|
+
async getUserByEmail(email, userPoolId) {
|
|
80
|
+
const command = new CognitoIP.AdminGetUserCommand({ UserPoolId: userPoolId, Username: email });
|
|
81
|
+
try {
|
|
82
|
+
const user = await this.cognito.send(command);
|
|
83
|
+
return this.mapCognitoUserAttributesAsPlainObject(user);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (error.name === 'UserNotFoundException')
|
|
87
|
+
throw new Error('User not found');
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Identify a user by its userId (sub), returning its attributes.
|
|
93
|
+
*/
|
|
94
|
+
async getUserBySub(sub, userPoolId) {
|
|
95
|
+
// as of today, there is no a direct way to find a user by its sub: we need to run a query against the users base
|
|
96
|
+
const command = new CognitoIP.ListUsersCommand({ UserPoolId: userPoolId, Filter: `sub = "${sub}"`, Limit: 1 });
|
|
97
|
+
const { Users } = await this.cognito.send(command);
|
|
98
|
+
if (Users.length < 1)
|
|
99
|
+
throw new Error('User not found');
|
|
100
|
+
return this.mapCognitoUserAttributesAsPlainObject(Users[0]);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* List all the users of the pool.
|
|
104
|
+
*/
|
|
105
|
+
async listUsers(userPoolId, options = { users: [] }) {
|
|
106
|
+
const params = { UserPoolId: userPoolId };
|
|
107
|
+
if (options.pagination)
|
|
108
|
+
params.PaginationToken = options.pagination;
|
|
109
|
+
const { Users, PaginationToken: pagination } = await this.cognito.send(new CognitoIP.ListUsersCommand(params));
|
|
110
|
+
const users = options.users.concat(Users.map(u => new idea_toolbox_1.CognitoUser(this.mapCognitoUserAttributesAsPlainObject(u))));
|
|
111
|
+
if (pagination)
|
|
112
|
+
return await this.listUsers(userPoolId, { pagination, users });
|
|
113
|
+
else
|
|
114
|
+
return users;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* List all the users of the pool, including the information about the groups they're in.
|
|
118
|
+
* Note: it's slower than the alternative `getAllUsers`: use it only when needed.
|
|
119
|
+
*/
|
|
120
|
+
async listUsersWithGroupsDetail(cognitoUserPoolId) {
|
|
121
|
+
const groups = await this.listGroups(cognitoUserPoolId);
|
|
122
|
+
const users = [];
|
|
123
|
+
for (const group of groups) {
|
|
124
|
+
const usersOfGroup = await this.listUsersInGroup(group.name, cognitoUserPoolId);
|
|
125
|
+
usersOfGroup.forEach(userInGroup => {
|
|
126
|
+
const userAlreadyInOutputList = users.find(u => u.userId === userInGroup.userId);
|
|
127
|
+
if (userAlreadyInOutputList)
|
|
128
|
+
userAlreadyInOutputList.groups.push(group.name);
|
|
129
|
+
else {
|
|
130
|
+
userInGroup.groups.push(group.name);
|
|
131
|
+
users.push(userInGroup);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return users;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Create a new user (by its email) in the pool specified.
|
|
139
|
+
* @return userId of the new user
|
|
140
|
+
*/
|
|
141
|
+
async createUser(cognitoUserOrEmail, userPoolId, options = {}) {
|
|
142
|
+
const email = typeof cognitoUserOrEmail === 'string'
|
|
143
|
+
? cognitoUserOrEmail
|
|
144
|
+
: cognitoUserOrEmail.email;
|
|
145
|
+
if ((0, idea_toolbox_1.isEmpty)(email, 'email'))
|
|
146
|
+
throw new Error('INVALID_EMAIL');
|
|
147
|
+
const UserAttributes = [
|
|
148
|
+
{ Name: 'email', Value: email },
|
|
149
|
+
{ Name: 'email_verified', Value: 'true' }
|
|
150
|
+
];
|
|
151
|
+
if (typeof cognitoUserOrEmail === 'object') {
|
|
152
|
+
const user = cognitoUserOrEmail;
|
|
153
|
+
UserAttributes.push({ Name: 'name', Value: user.name });
|
|
154
|
+
UserAttributes.push({ Name: 'picture', Value: user.picture || '' });
|
|
155
|
+
Object.keys(user.attributes).forEach(a => UserAttributes.push({ Name: 'custom:'.concat(a), Value: String(user.attributes[a]) }));
|
|
156
|
+
}
|
|
157
|
+
const params = { UserPoolId: userPoolId, Username: email, UserAttributes };
|
|
158
|
+
if (options.skipNotification)
|
|
159
|
+
params.MessageAction = 'SUPPRESS';
|
|
160
|
+
if (options.temporaryPassword)
|
|
161
|
+
params.TemporaryPassword = options.temporaryPassword;
|
|
162
|
+
const { User } = await this.cognito.send(new CognitoIP.AdminCreateUserCommand(params));
|
|
163
|
+
const userId = this.mapCognitoUserAttributesAsPlainObject(User).sub;
|
|
164
|
+
if (!userId)
|
|
165
|
+
throw new Error('Creation failed');
|
|
166
|
+
return userId;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Resend the password to a user who never logged in.
|
|
170
|
+
*/
|
|
171
|
+
async resendPassword(email, userPoolId, options = {}) {
|
|
172
|
+
if ((0, idea_toolbox_1.isEmpty)(email, 'email'))
|
|
173
|
+
throw new Error('Invalid email');
|
|
174
|
+
const params = {
|
|
175
|
+
UserPoolId: userPoolId,
|
|
176
|
+
Username: email,
|
|
177
|
+
MessageAction: 'RESEND'
|
|
178
|
+
};
|
|
179
|
+
if (options.temporaryPassword)
|
|
180
|
+
params.TemporaryPassword = options.temporaryPassword;
|
|
181
|
+
await this.cognito.send(new CognitoIP.AdminCreateUserCommand(params));
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Set a new password for a specific user identified by its email (admin-only).
|
|
185
|
+
* If not specified, the password is generated randomly, and the user must change it at the first login.
|
|
186
|
+
*/
|
|
187
|
+
async setPassword(email, userPoolId, options = {}) {
|
|
188
|
+
if ((0, idea_toolbox_1.isEmpty)(email, 'email'))
|
|
189
|
+
throw new Error('Invalid email');
|
|
190
|
+
const RANDOM_PASSWORD_LENGTH = 8;
|
|
191
|
+
const password = options.password ??
|
|
192
|
+
Math.random()
|
|
193
|
+
.toString(36)
|
|
194
|
+
.slice(2, 2 + RANDOM_PASSWORD_LENGTH);
|
|
195
|
+
const params = {
|
|
196
|
+
UserPoolId: userPoolId,
|
|
197
|
+
Username: email,
|
|
198
|
+
Password: password,
|
|
199
|
+
Permanent: options.permanent
|
|
200
|
+
};
|
|
201
|
+
await this.cognito.send(new CognitoIP.AdminSetUserPasswordCommand(params));
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Delete a user by its email (username), in the pool specified.
|
|
205
|
+
*/
|
|
206
|
+
async deleteUser(email, userPoolId) {
|
|
207
|
+
if ((0, idea_toolbox_1.isEmpty)(email, 'email'))
|
|
208
|
+
throw new Error('Invalid email');
|
|
209
|
+
const command = new CognitoIP.AdminDeleteUserCommand({ UserPoolId: userPoolId, Username: email });
|
|
210
|
+
await this.cognito.send(command);
|
|
211
|
+
}
|
|
212
|
+
//
|
|
213
|
+
// USER
|
|
214
|
+
//
|
|
215
|
+
/**
|
|
216
|
+
* Sign in a user of a specific pool through username and password.
|
|
217
|
+
*/
|
|
218
|
+
async signIn(email, password, userPoolId, userPoolClientId) {
|
|
219
|
+
const command = new CognitoIP.AdminInitiateAuthCommand({
|
|
220
|
+
UserPoolId: userPoolId,
|
|
221
|
+
ClientId: userPoolClientId,
|
|
222
|
+
AuthFlow: 'ADMIN_NO_SRP_AUTH',
|
|
223
|
+
AuthParameters: { USERNAME: email, PASSWORD: password }
|
|
224
|
+
});
|
|
225
|
+
const { AuthenticationResult } = await this.cognito.send(command);
|
|
226
|
+
if (!AuthenticationResult)
|
|
227
|
+
throw new Error('Sign-in failed');
|
|
228
|
+
return AuthenticationResult;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Given a username and a refresh token (and pool data), refresh the session and return the new tokens.
|
|
232
|
+
*/
|
|
233
|
+
async refreshSession(email, refreshToken, userPoolId, userPoolClientId) {
|
|
234
|
+
const command = new CognitoIP.AdminInitiateAuthCommand({
|
|
235
|
+
UserPoolId: userPoolId,
|
|
236
|
+
ClientId: userPoolClientId,
|
|
237
|
+
AuthFlow: 'REFRESH_TOKEN_AUTH',
|
|
238
|
+
AuthParameters: { USERNAME: email, REFRESH_TOKEN: refreshToken }
|
|
239
|
+
});
|
|
240
|
+
const { AuthenticationResult } = await this.cognito.send(command);
|
|
241
|
+
if (!AuthenticationResult)
|
|
242
|
+
throw new Error('Refresh failed');
|
|
243
|
+
return AuthenticationResult;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Change the email address (== username) associated to a user.
|
|
247
|
+
*/
|
|
248
|
+
async updateEmail(email, newEmail, userPoolId) {
|
|
249
|
+
if ((0, idea_toolbox_1.isEmpty)(newEmail, 'email'))
|
|
250
|
+
throw new Error('Invalid new email');
|
|
251
|
+
const command = new CognitoIP.AdminUpdateUserAttributesCommand({
|
|
252
|
+
UserPoolId: userPoolId,
|
|
253
|
+
Username: email,
|
|
254
|
+
UserAttributes: [
|
|
255
|
+
{ Name: 'email', Value: newEmail },
|
|
256
|
+
{ Name: 'email_verified', Value: 'true' }
|
|
257
|
+
]
|
|
258
|
+
});
|
|
259
|
+
await this.cognito.send(command);
|
|
260
|
+
// sign out the user from all its devices and resolve
|
|
261
|
+
await this.globalSignOut(newEmail, userPoolId);
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Change the password to sign in for a user.
|
|
265
|
+
*/
|
|
266
|
+
async updatePassword(email, oldPassword, newPassword, userPoolId, userPoolClientId) {
|
|
267
|
+
if (newPassword.length < 8)
|
|
268
|
+
throw new Error('Invalid new password');
|
|
269
|
+
const tokensForPasswordChange = await this.signIn(email, oldPassword, userPoolId, userPoolClientId);
|
|
270
|
+
const command = new CognitoIP.ChangePasswordCommand({
|
|
271
|
+
AccessToken: tokensForPasswordChange.AccessToken,
|
|
272
|
+
PreviousPassword: oldPassword,
|
|
273
|
+
ProposedPassword: newPassword
|
|
274
|
+
});
|
|
275
|
+
await this.cognito.send(command);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Send to a user the instructions to change the password.
|
|
279
|
+
*/
|
|
280
|
+
async forgotPassword(email, userPoolClientId) {
|
|
281
|
+
const command = new CognitoIP.ForgotPasswordCommand({ Username: email, ClientId: userPoolClientId });
|
|
282
|
+
const { CodeDeliveryDetails } = await this.cognito.send(command);
|
|
283
|
+
return CodeDeliveryDetails;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Complete the flow of a password forgot.
|
|
287
|
+
*/
|
|
288
|
+
async confirmForgotPassword(email, newPassword, confirmationCode, userPoolClientId) {
|
|
289
|
+
const command = new CognitoIP.ConfirmForgotPasswordCommand({
|
|
290
|
+
ClientId: userPoolClientId,
|
|
291
|
+
Username: email,
|
|
292
|
+
ConfirmationCode: confirmationCode,
|
|
293
|
+
Password: newPassword
|
|
294
|
+
});
|
|
295
|
+
await this.cognito.send(command);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Update a (Cognito)User's attributes, excluding the attributes that require specific methods.
|
|
299
|
+
*/
|
|
300
|
+
async updateUser(user, userPoolId) {
|
|
301
|
+
const UserAttributes = [
|
|
302
|
+
{ Name: 'name', Value: user.name },
|
|
303
|
+
{ Name: 'picture', Value: user.picture ?? '' }
|
|
304
|
+
];
|
|
305
|
+
Object.keys(user.attributes).forEach(customAttribute => UserAttributes.push({
|
|
306
|
+
Name: 'custom:'.concat(customAttribute),
|
|
307
|
+
Value: String(user.attributes[customAttribute])
|
|
308
|
+
}));
|
|
309
|
+
const command = new CognitoIP.AdminUpdateUserAttributesCommand({
|
|
310
|
+
UserPoolId: userPoolId,
|
|
311
|
+
Username: user.email,
|
|
312
|
+
UserAttributes
|
|
313
|
+
});
|
|
314
|
+
await this.cognito.send(command);
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Sign out the user from all devices.
|
|
318
|
+
*/
|
|
319
|
+
async globalSignOut(email, userPoolId) {
|
|
320
|
+
const command = new CognitoIP.AdminUserGlobalSignOutCommand({ Username: email, UserPoolId: userPoolId });
|
|
321
|
+
await this.cognito.send(command);
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Confirm and conclude a registration, usign a confirmation code.
|
|
325
|
+
*/
|
|
326
|
+
async confirmSignUp(email, confirmationCode, userPoolClientId) {
|
|
327
|
+
if (!email)
|
|
328
|
+
throw new Error('Invalid email');
|
|
329
|
+
if (!confirmationCode)
|
|
330
|
+
throw new Error('Invalid confirmation code');
|
|
331
|
+
if (!userPoolClientId)
|
|
332
|
+
throw new Error('Invalid client ID');
|
|
333
|
+
const command = new CognitoIP.ConfirmSignUpCommand({
|
|
334
|
+
Username: email,
|
|
335
|
+
ConfirmationCode: confirmationCode,
|
|
336
|
+
ClientId: userPoolClientId
|
|
337
|
+
});
|
|
338
|
+
await this.cognito.send(command);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* List the groups of the user pool.
|
|
342
|
+
*/
|
|
343
|
+
async listGroups(userPoolId, options = { groups: [] }) {
|
|
344
|
+
const params = { UserPoolId: userPoolId };
|
|
345
|
+
if (options.pagination)
|
|
346
|
+
params.NextToken = options.pagination;
|
|
347
|
+
const res = await this.cognito.send(new CognitoIP.ListGroupsCommand(params));
|
|
348
|
+
const pagination = res.NextToken;
|
|
349
|
+
const groups = options.groups.concat(res.Groups.map(g => ({ name: g.GroupName, description: g.Description })));
|
|
350
|
+
if (pagination)
|
|
351
|
+
return await this.listGroups(userPoolId, { pagination, groups });
|
|
352
|
+
else
|
|
353
|
+
return groups;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Create a new group in the user pool.
|
|
357
|
+
*/
|
|
358
|
+
async createGroup(groupName, userPoolId) {
|
|
359
|
+
const command = new CognitoIP.CreateGroupCommand({ GroupName: groupName, UserPoolId: userPoolId });
|
|
360
|
+
await this.cognito.send(command);
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Delete a group from the user pool.
|
|
364
|
+
*/
|
|
365
|
+
async deleteGroup(groupName, userPoolId) {
|
|
366
|
+
const command = new CognitoIP.DeleteGroupCommand({ GroupName: groupName, UserPoolId: userPoolId });
|
|
367
|
+
await this.cognito.send(command);
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* List the users part of a group in the user pool.
|
|
371
|
+
*/
|
|
372
|
+
async listUsersInGroup(group, userPoolId, options = { users: [] }) {
|
|
373
|
+
const params = {
|
|
374
|
+
UserPoolId: userPoolId,
|
|
375
|
+
GroupName: group
|
|
376
|
+
};
|
|
377
|
+
if (options.pagination)
|
|
378
|
+
params.NextToken = options.pagination;
|
|
379
|
+
const res = await this.cognito.send(new CognitoIP.ListUsersInGroupCommand(params));
|
|
380
|
+
const pagination = res.NextToken;
|
|
381
|
+
const users = options.users.concat(res.Users.map(u => new idea_toolbox_1.CognitoUser(this.mapCognitoUserAttributesAsPlainObject(u))));
|
|
382
|
+
if (pagination)
|
|
383
|
+
return await this.listUsersInGroup(group, userPoolId, { pagination, users });
|
|
384
|
+
else
|
|
385
|
+
return users;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Add a user (by email) to a group in the user pool.
|
|
389
|
+
*/
|
|
390
|
+
async addUserToGroup(email, group, userPoolId) {
|
|
391
|
+
const user = new idea_toolbox_1.CognitoUser(await this.getUserByEmail(email, userPoolId));
|
|
392
|
+
const command = new CognitoIP.AdminAddUserToGroupCommand({
|
|
393
|
+
UserPoolId: userPoolId,
|
|
394
|
+
GroupName: group,
|
|
395
|
+
Username: user.userId
|
|
396
|
+
});
|
|
397
|
+
await this.cognito.send(command);
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Remove a user (by email) from a group in the user pool.
|
|
401
|
+
*/
|
|
402
|
+
async removeUserFromGroup(email, group, userPoolId) {
|
|
403
|
+
const user = new idea_toolbox_1.CognitoUser(await this.getUserByEmail(email, userPoolId));
|
|
404
|
+
const command = new CognitoIP.AdminRemoveUserFromGroupCommand({
|
|
405
|
+
UserPoolId: userPoolId,
|
|
406
|
+
GroupName: group,
|
|
407
|
+
Username: user.userId
|
|
408
|
+
});
|
|
409
|
+
await this.cognito.send(command);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
exports.Cognito = Cognito;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as AmazonComprehend from '@aws-sdk/client-comprehend';
|
|
2
|
+
import { Sentiment } from 'idea-toolbox';
|
|
3
|
+
/**
|
|
4
|
+
* A wrapper for Amazon Comprehend.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Comprehend {
|
|
7
|
+
protected comprehend: AmazonComprehend.ComprehendClient;
|
|
8
|
+
constructor(options?: {
|
|
9
|
+
region?: string;
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* Inspects text and returns an inference of the prevailing sentiment (POSITIVE, NEUTRAL, MIXED, or NEGATIVE).
|
|
13
|
+
*/
|
|
14
|
+
detectSentiment(params: DetectSentimentParameters): Promise<Sentiment>;
|
|
15
|
+
/**
|
|
16
|
+
* Determines the dominant language of the input text.
|
|
17
|
+
*/
|
|
18
|
+
detectDominantLanguage(params: {
|
|
19
|
+
text: string;
|
|
20
|
+
}): Promise<string>;
|
|
21
|
+
}
|
|
22
|
+
export interface DetectSentimentParameters {
|
|
23
|
+
/**
|
|
24
|
+
* The language of the input contents. You can specify any of the primary languages supported by Amazon Comprehend.
|
|
25
|
+
* All contents must be in the same language. Required.
|
|
26
|
+
* Valid Values: en | es | fr | de | it | pt | ar | hi | ja | ko | zh | zh-TW
|
|
27
|
+
*/
|
|
28
|
+
language: string;
|
|
29
|
+
/**
|
|
30
|
+
* The text to analyze. Required.
|
|
31
|
+
* A UTF-8 text string. Each string must contain fewer that 5,000 bytes of UTF-8 encoded characters.
|
|
32
|
+
*/
|
|
33
|
+
text: string;
|
|
34
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
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.Comprehend = void 0;
|
|
27
|
+
const AmazonComprehend = __importStar(require("@aws-sdk/client-comprehend"));
|
|
28
|
+
/**
|
|
29
|
+
* A wrapper for Amazon Comprehend.
|
|
30
|
+
*/
|
|
31
|
+
class Comprehend {
|
|
32
|
+
constructor(options = {}) {
|
|
33
|
+
this.comprehend = new AmazonComprehend.ComprehendClient({ region: options.region });
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Inspects text and returns an inference of the prevailing sentiment (POSITIVE, NEUTRAL, MIXED, or NEGATIVE).
|
|
37
|
+
*/
|
|
38
|
+
async detectSentiment(params) {
|
|
39
|
+
if (!params.language || !params.text)
|
|
40
|
+
throw new Error('Missing some parameters');
|
|
41
|
+
const command = new AmazonComprehend.DetectSentimentCommand({ LanguageCode: params.language, Text: params.text });
|
|
42
|
+
const { Sentiment } = await this.comprehend.send(command);
|
|
43
|
+
return Sentiment;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Determines the dominant language of the input text.
|
|
47
|
+
*/
|
|
48
|
+
async detectDominantLanguage(params) {
|
|
49
|
+
if (!params.text)
|
|
50
|
+
throw new Error('Missing text');
|
|
51
|
+
const command = new AmazonComprehend.DetectDominantLanguageCommand({ Text: params.text });
|
|
52
|
+
const { Languages } = await this.comprehend.send(command);
|
|
53
|
+
if (!Languages.length)
|
|
54
|
+
throw new Error('Not found');
|
|
55
|
+
return Languages[0].LanguageCode;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.Comprehend = Comprehend;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import * as DDB from '@aws-sdk/lib-dynamodb';
|
|
2
|
+
import * as DDBUtils from '@aws-sdk/util-dynamodb';
|
|
3
|
+
import { LambdaLogger } from './lambdaLogger';
|
|
4
|
+
/**
|
|
5
|
+
* A wrapper for AWS DynamoDB.
|
|
6
|
+
*/
|
|
7
|
+
export declare class DynamoDB {
|
|
8
|
+
protected dynamo: DDB.DynamoDBDocument;
|
|
9
|
+
protected logger: LambdaLogger;
|
|
10
|
+
constructor();
|
|
11
|
+
/**
|
|
12
|
+
* Convert a JSON object from DynamoDB format to simple JSON.
|
|
13
|
+
* @data the data in DynamoDB's original format to convert in plain objects
|
|
14
|
+
* @options the options to use to convert the data
|
|
15
|
+
*/
|
|
16
|
+
unmarshall(data: Record<string, any>, options?: DDBUtils.unmarshallOptions): Record<string, any>;
|
|
17
|
+
/**
|
|
18
|
+
* Returns an IUNID: IDEA's Unique Nano IDentifier, which is an id unique through an IDEA's AWS account and region.
|
|
19
|
+
* Note: no need of an auth check for external uses: the permissions depend from the context in which it's executed.
|
|
20
|
+
* @param project project code
|
|
21
|
+
* @return the IUNID
|
|
22
|
+
*/
|
|
23
|
+
IUNID(project: string): Promise<string>;
|
|
24
|
+
protected IUNIDHelper(project: string, attempt: number, maxAttempts: number): Promise<string>;
|
|
25
|
+
/**
|
|
26
|
+
* Manage atomic counters (atomic autoincrement values) in IDEA's projects.
|
|
27
|
+
* They key of an atomic counter should be composed as the following: `DynamoDBTableName_uniqueKey`.
|
|
28
|
+
* @param key the key of the counter
|
|
29
|
+
*/
|
|
30
|
+
getAtomicCounterByKey(key: string): Promise<number>;
|
|
31
|
+
/**
|
|
32
|
+
* Get an item of a DynamoDB table.
|
|
33
|
+
* @param params the params to apply to DynamoDB's function
|
|
34
|
+
*/
|
|
35
|
+
get(params: DDB.GetCommandInput): Promise<any>;
|
|
36
|
+
/**
|
|
37
|
+
* Put an item in a DynamoDB table.
|
|
38
|
+
* @param params the params to apply to DynamoDB's function
|
|
39
|
+
*/
|
|
40
|
+
put(params: DDB.PutCommandInput): Promise<DDB.PutCommandOutput>;
|
|
41
|
+
/**
|
|
42
|
+
* Update an item of a DynamoDB table.
|
|
43
|
+
* @param params the params to apply to DynamoDB's function
|
|
44
|
+
*/
|
|
45
|
+
update(params: DDB.UpdateCommandInput): Promise<DDB.UpdateCommandOutput>;
|
|
46
|
+
/**
|
|
47
|
+
* Delete an item of a DynamoDB table.
|
|
48
|
+
* @param params the params to apply to DynamoDB's function
|
|
49
|
+
*/
|
|
50
|
+
delete(params: DDB.DeleteCommandInput): Promise<DDB.DeleteCommandOutput>;
|
|
51
|
+
/**
|
|
52
|
+
* Get group of items based on their keys from DynamoDB table, avoiding the limits of DynamoDB's BatchGetItem.
|
|
53
|
+
* @param table the target DynamoDB table
|
|
54
|
+
* @param keys the keys of the objects to retrieve
|
|
55
|
+
* @param ignoreErr if set, ignore the errors and continue the bulk op.
|
|
56
|
+
*/
|
|
57
|
+
batchGet(table: string, keys: Record<string, any>[], ignoreErr?: boolean): Promise<any[]>;
|
|
58
|
+
protected batchGetHelper(table: string, keys: Record<string, any>[], resultElements: Record<string, any>[], ignoreErr: boolean, currentChunk?: number, chunkSize?: number): Promise<Record<string, any>[]>;
|
|
59
|
+
/**
|
|
60
|
+
* Put an array of items in a DynamoDB table, avoiding the limits of DynamoDB's BatchWriteItem.
|
|
61
|
+
* In case of errors, it will retry with a random back-off mechanism until the timeout.
|
|
62
|
+
* Therefore, in case of timeout, there may be some elements written and some not.
|
|
63
|
+
* @param table the target DynamoDB table
|
|
64
|
+
* @param items the objects to insert
|
|
65
|
+
*/
|
|
66
|
+
batchPut(table: string, items: Record<string, any>[]): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Delete an array of items from a DynamoDB table, avoiding the limits of DynamoDB's BatchWriteItem.
|
|
69
|
+
* In case of errors, it will retry with a random back-off mechanism until the timeout.
|
|
70
|
+
* Therefore, in case of timeout, there may be some elements deleted and some not.
|
|
71
|
+
* @param table the target DynamoDB table
|
|
72
|
+
* @param keys the keys to delete
|
|
73
|
+
*/
|
|
74
|
+
batchDelete(table: string, keys: Record<string, any>[]): Promise<void>;
|
|
75
|
+
protected batchWriteHelper(table: string, itemsOrKeys: Record<string, any>[], isPut: boolean, currentChunk?: number, chunkSize?: number): Promise<void>;
|
|
76
|
+
protected batchWriteChunkWithRetries(table: string, params: DDB.BatchWriteCommandInput): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Query a DynamoDB table, avoiding the limits of DynamoDB's Query.
|
|
79
|
+
* @param params the params to apply to DynamoDB's function
|
|
80
|
+
*/
|
|
81
|
+
query(params: DDB.QueryCommandInput): Promise<any[]>;
|
|
82
|
+
/**
|
|
83
|
+
* Scan a DynamoDB table, avoiding the limits of DynamoDB's Query.
|
|
84
|
+
* @param params the params to apply to DynamoDB's function
|
|
85
|
+
*/
|
|
86
|
+
scan(params: DDB.ScanCommandInput): Promise<any[]>;
|
|
87
|
+
protected queryScanHelper(params: DDB.QueryCommandInput | DDB.ScanCommandInput, items: Record<string, any>[], isQuery: boolean): Promise<Record<string, any>[]>;
|
|
88
|
+
/**
|
|
89
|
+
* Query a DynamoDB table in the traditional way (no pagination or data mapping).
|
|
90
|
+
* @param params the params to apply to DynamoDB's function
|
|
91
|
+
*/
|
|
92
|
+
queryClassic(params: DDB.QueryCommandInput): Promise<DDB.QueryCommandOutput>;
|
|
93
|
+
/**
|
|
94
|
+
* Scan a DynamoDB table in the traditional way (no pagination or data mapping).
|
|
95
|
+
* @param params the params to apply to DynamoDB's function
|
|
96
|
+
*/
|
|
97
|
+
scanClassic(params: DDB.ScanCommandInput): Promise<DDB.ScanCommandOutput>;
|
|
98
|
+
/**
|
|
99
|
+
* Execute a series of write operations in a single transaction.
|
|
100
|
+
* @param ops the operations to execute in the transaction
|
|
101
|
+
*/
|
|
102
|
+
transactWrites(ops: {
|
|
103
|
+
ConditionCheck?: any;
|
|
104
|
+
Put?: any;
|
|
105
|
+
Delete?: any;
|
|
106
|
+
Update?: any;
|
|
107
|
+
}[]): Promise<void>;
|
|
108
|
+
}
|