@internxt/sdk 1.15.6 → 1.15.8

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.
@@ -0,0 +1,302 @@
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 __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.Mail = void 0;
40
+ var internxt_crypto_1 = require("internxt-crypto");
41
+ var api_1 = require("./api");
42
+ var crypto_1 = require("./crypto");
43
+ var Mail = /** @class */ (function () {
44
+ function Mail(apiUrl, appDetails, apiSecurity) {
45
+ this.api = api_1.MailApi.client(apiUrl, appDetails, apiSecurity);
46
+ }
47
+ Mail.client = function (apiUrl, appDetails, apiSecurity) {
48
+ return new Mail(apiUrl, appDetails, apiSecurity);
49
+ };
50
+ /**
51
+ * Uploads encrypted keystore to the server
52
+ *
53
+ * @param encryptedKeystore - The encrypted keystore
54
+ * @returns Server response
55
+ */
56
+ Mail.prototype.uploadKeystoreToServer = function (encryptedKeystore) {
57
+ return __awaiter(this, void 0, void 0, function () {
58
+ return __generator(this, function (_a) {
59
+ return [2 /*return*/, this.api.uploadKeystore(encryptedKeystore)];
60
+ });
61
+ });
62
+ };
63
+ /**
64
+ * Creates recovery and encryption keystores and uploads them to the server
65
+ *
66
+ * @param userEmail - The email of the user
67
+ * @param baseKey - The secret key of the user
68
+ * @returns The recovery codes and keys of the user
69
+ */
70
+ Mail.prototype.createAndUploadKeystores = function (userEmail, baseKey) {
71
+ return __awaiter(this, void 0, void 0, function () {
72
+ var _a, encryptionKeystore, recoveryKeystore, recoveryCodes, keys;
73
+ return __generator(this, function (_b) {
74
+ switch (_b.label) {
75
+ case 0: return [4 /*yield*/, (0, crypto_1.createKeystores)(userEmail, baseKey)];
76
+ case 1:
77
+ _a = _b.sent(), encryptionKeystore = _a.encryptionKeystore, recoveryKeystore = _a.recoveryKeystore, recoveryCodes = _a.recoveryCodes, keys = _a.keys;
78
+ return [4 /*yield*/, Promise.all([this.api.uploadKeystore(encryptionKeystore), this.api.uploadKeystore(recoveryKeystore)])];
79
+ case 2:
80
+ _b.sent();
81
+ return [2 /*return*/, { recoveryCodes: recoveryCodes, keys: keys }];
82
+ }
83
+ });
84
+ });
85
+ };
86
+ /**
87
+ * Requests encrypted keystore from the server
88
+ *
89
+ * @param userEmail - The email of the user
90
+ * @param keystoreType - The type of the keystore
91
+ * @returns The encrypted keystore
92
+ */
93
+ Mail.prototype.downloadKeystoreFromServer = function (userEmail, keystoreType) {
94
+ return __awaiter(this, void 0, void 0, function () {
95
+ return __generator(this, function (_a) {
96
+ return [2 /*return*/, this.api.downloadKeystore(userEmail, keystoreType)];
97
+ });
98
+ });
99
+ };
100
+ /**
101
+ * Requests encrypted keystore from the server and opens it
102
+ *
103
+ * @param userEmail - The email of the user
104
+ * @param baseKey - The secret key of the user
105
+ * @returns The hybrid keys of the user
106
+ */
107
+ Mail.prototype.getUserEmailKeys = function (userEmail, baseKey) {
108
+ return __awaiter(this, void 0, void 0, function () {
109
+ var keystore;
110
+ return __generator(this, function (_a) {
111
+ switch (_a.label) {
112
+ case 0: return [4 /*yield*/, this.api.downloadKeystore(userEmail, internxt_crypto_1.KeystoreType.ENCRYPTION)];
113
+ case 1:
114
+ keystore = _a.sent();
115
+ return [2 /*return*/, (0, crypto_1.openKeystore)(keystore, baseKey)];
116
+ }
117
+ });
118
+ });
119
+ };
120
+ /**
121
+ * Requests recovery keystore from the server and opens it
122
+ *
123
+ * @param userEmail - The email of the user
124
+ * @param recoveryCodes - The recovery codes of the user
125
+ * @returns The hybrid keys of the user
126
+ */
127
+ Mail.prototype.recoverUserEmailKeys = function (userEmail, recoveryCodes) {
128
+ return __awaiter(this, void 0, void 0, function () {
129
+ var keystore;
130
+ return __generator(this, function (_a) {
131
+ switch (_a.label) {
132
+ case 0: return [4 /*yield*/, this.api.downloadKeystore(userEmail, internxt_crypto_1.KeystoreType.RECOVERY)];
133
+ case 1:
134
+ keystore = _a.sent();
135
+ return [2 /*return*/, (0, crypto_1.recoverKeys)(keystore, recoveryCodes)];
136
+ }
137
+ });
138
+ });
139
+ };
140
+ /**
141
+ * Request user with corresponding public keys from the server
142
+ *
143
+ * @param userEmail - The email of the user
144
+ * @returns User with corresponding public keys
145
+ */
146
+ Mail.prototype.getUserWithPublicKeys = function (userEmail) {
147
+ return __awaiter(this, void 0, void 0, function () {
148
+ var results;
149
+ return __generator(this, function (_a) {
150
+ switch (_a.label) {
151
+ case 0: return [4 /*yield*/, this.api.getUsersWithPublicKeys([userEmail])];
152
+ case 1:
153
+ results = _a.sent();
154
+ if (!results[0])
155
+ throw new Error("No public keys found for ".concat(userEmail));
156
+ return [2 /*return*/, results[0]];
157
+ }
158
+ });
159
+ });
160
+ };
161
+ /**
162
+ * Request users with corresponding public keys from the server
163
+ *
164
+ * @param emails - The emails of the users
165
+ * @returns Users with corresponding public keys
166
+ */
167
+ Mail.prototype.getSeveralUsersWithPublicKeys = function (emails) {
168
+ return __awaiter(this, void 0, void 0, function () {
169
+ return __generator(this, function (_a) {
170
+ return [2 /*return*/, this.api.getUsersWithPublicKeys(emails)];
171
+ });
172
+ });
173
+ };
174
+ /**
175
+ * Sends the encrypted emails to the server
176
+ *
177
+ * @param emails - The encrypted emails
178
+ * @param params - The public parameters of the email (sender, recipients, CCs, BCCs, etc.)
179
+ * @returns Server response
180
+ */
181
+ Mail.prototype.sendE2EEncryptedEmail = function (emails, params) {
182
+ return __awaiter(this, void 0, void 0, function () {
183
+ return __generator(this, function (_a) {
184
+ return [2 /*return*/, this.api.sendE2EEmails(emails, params)];
185
+ });
186
+ });
187
+ };
188
+ /**
189
+ * Encrypts and sends email(s) to the server
190
+ *
191
+ * @param email - The message to encrypt
192
+ * @param aux - The optional auxilary data to encrypt together with the email (e.g. email sender)
193
+ * @returns Server response
194
+ */
195
+ Mail.prototype.e2eEncryptAndSendEmail = function (email, aux) {
196
+ return __awaiter(this, void 0, void 0, function () {
197
+ var recipientEmails, recipients, encEmails;
198
+ var _a;
199
+ return __generator(this, function (_b) {
200
+ switch (_b.label) {
201
+ case 0:
202
+ recipientEmails = (_a = email.params.recipients) === null || _a === void 0 ? void 0 : _a.map(function (user) { return user.email; });
203
+ if (!recipientEmails)
204
+ throw new Error('No recipients found');
205
+ return [4 /*yield*/, this.api.getUsersWithPublicKeys(recipientEmails)];
206
+ case 1:
207
+ recipients = _b.sent();
208
+ return [4 /*yield*/, (0, crypto_1.encryptEmail)(email, recipients, aux)];
209
+ case 2:
210
+ encEmails = _b.sent();
211
+ return [2 /*return*/, this.api.sendE2EEmails(encEmails, email.params)];
212
+ }
213
+ });
214
+ });
215
+ };
216
+ /**
217
+ * Sends the password-protected email to the server
218
+ *
219
+ * @param email - The password-protected email
220
+ * @param params - The public parameters of the email
221
+ * @returns Server response
222
+ */
223
+ Mail.prototype.sendE2EPasswordProtectedEmail = function (email, params) {
224
+ return __awaiter(this, void 0, void 0, function () {
225
+ return __generator(this, function (_a) {
226
+ return [2 /*return*/, this.api.sendE2EPasswordProtectedEmail(email, params)];
227
+ });
228
+ });
229
+ };
230
+ /**
231
+ * Creates the password-protected E2E email and sends it to the server
232
+ *
233
+ * @param email - The email
234
+ * @param pwd - The password
235
+ * @param aux - The optional auxiliary data to encrypt together with the email (e.g. email sender)
236
+ * @returns Server response
237
+ */
238
+ Mail.prototype.e2ePasswordProtectAndSendEmail = function (email, pwd, aux) {
239
+ return __awaiter(this, void 0, void 0, function () {
240
+ var encEmail;
241
+ return __generator(this, function (_a) {
242
+ switch (_a.label) {
243
+ case 0: return [4 /*yield*/, (0, crypto_1.passwordProtectAndSendEmail)(email, pwd, aux)];
244
+ case 1:
245
+ encEmail = _a.sent();
246
+ return [2 /*return*/, this.api.sendE2EPasswordProtectedEmail(encEmail, email.params)];
247
+ }
248
+ });
249
+ });
250
+ };
251
+ Mail.prototype.getMailboxes = function () {
252
+ return __awaiter(this, void 0, void 0, function () {
253
+ return __generator(this, function (_a) {
254
+ return [2 /*return*/, this.api.getMailboxes()];
255
+ });
256
+ });
257
+ };
258
+ Mail.prototype.listEmails = function (query) {
259
+ return __awaiter(this, void 0, void 0, function () {
260
+ return __generator(this, function (_a) {
261
+ return [2 /*return*/, this.api.listEmails(query)];
262
+ });
263
+ });
264
+ };
265
+ Mail.prototype.getEmail = function (id) {
266
+ return __awaiter(this, void 0, void 0, function () {
267
+ return __generator(this, function (_a) {
268
+ return [2 /*return*/, this.api.getEmail(id)];
269
+ });
270
+ });
271
+ };
272
+ Mail.prototype.deleteEmail = function (id) {
273
+ return __awaiter(this, void 0, void 0, function () {
274
+ return __generator(this, function (_a) {
275
+ return [2 /*return*/, this.api.deleteEmail(id)];
276
+ });
277
+ });
278
+ };
279
+ Mail.prototype.updateEmail = function (id, body) {
280
+ return __awaiter(this, void 0, void 0, function () {
281
+ return __generator(this, function (_a) {
282
+ return [2 /*return*/, this.api.updateEmail(id, body)];
283
+ });
284
+ });
285
+ };
286
+ Mail.prototype.sendEmail = function (body) {
287
+ return __awaiter(this, void 0, void 0, function () {
288
+ return __generator(this, function (_a) {
289
+ return [2 /*return*/, this.api.sendEmail(body)];
290
+ });
291
+ });
292
+ };
293
+ Mail.prototype.saveDraft = function (body) {
294
+ return __awaiter(this, void 0, void 0, function () {
295
+ return __generator(this, function (_a) {
296
+ return [2 /*return*/, this.api.saveDraft(body)];
297
+ });
298
+ });
299
+ };
300
+ return Mail;
301
+ }());
302
+ exports.Mail = Mail;