@postalsys/ee-client 1.1.0 → 1.2.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/README.md +53 -30
- package/index.js +539 -5
- package/package.json +3 -10
- package/index.cjs +0 -1190
package/index.js
CHANGED
|
@@ -4,7 +4,11 @@ export class EmailEngineClient {
|
|
|
4
4
|
this.account = options.account;
|
|
5
5
|
this.accessToken = options.accessToken;
|
|
6
6
|
this.container = options.container;
|
|
7
|
-
this.confirmMethod =
|
|
7
|
+
this.confirmMethod =
|
|
8
|
+
options.confirmMethod ||
|
|
9
|
+
((message, _title = 'Confirm', _cancelText = 'Cancel', _okText = 'OK') => confirm(message));
|
|
10
|
+
this.alertMethod =
|
|
11
|
+
options.alertMethod || ((message, _title = 'Notice', _cancelText = null, _okText = 'OK') => alert(message));
|
|
8
12
|
|
|
9
13
|
this.currentFolder = null;
|
|
10
14
|
this.currentMessage = null;
|
|
@@ -13,6 +17,10 @@ export class EmailEngineClient {
|
|
|
13
17
|
this.nextPageCursor = null;
|
|
14
18
|
this.prevPageCursor = null;
|
|
15
19
|
|
|
20
|
+
// Keep-alive timer for sess_ tokens
|
|
21
|
+
this.keepAliveTimer = null;
|
|
22
|
+
this.lastActivity = Date.now();
|
|
23
|
+
|
|
16
24
|
// Get page size from localStorage or options or default
|
|
17
25
|
const savedPageSize =
|
|
18
26
|
typeof window !== 'undefined' && window.localStorage ? localStorage.getItem('ee-client-page-size') : null;
|
|
@@ -21,9 +29,15 @@ export class EmailEngineClient {
|
|
|
21
29
|
if (this.container) {
|
|
22
30
|
this.init();
|
|
23
31
|
}
|
|
32
|
+
|
|
33
|
+
// Start keep-alive timer for sess_ tokens
|
|
34
|
+
this._startKeepAliveTimer();
|
|
24
35
|
}
|
|
25
36
|
|
|
26
37
|
async apiRequest(method, endpoint, data = null) {
|
|
38
|
+
// Update activity timestamp for keep-alive
|
|
39
|
+
this._updateActivity();
|
|
40
|
+
|
|
27
41
|
const url = `${this.apiUrl}${endpoint}`;
|
|
28
42
|
const options = {
|
|
29
43
|
method: method,
|
|
@@ -58,7 +72,18 @@ export class EmailEngineClient {
|
|
|
58
72
|
|
|
59
73
|
const response = await fetchFn(url, options);
|
|
60
74
|
if (!response.ok) {
|
|
61
|
-
|
|
75
|
+
let errorDetails;
|
|
76
|
+
try {
|
|
77
|
+
errorDetails = await response.json();
|
|
78
|
+
} catch (parseError) {
|
|
79
|
+
// If JSON parsing fails, fall back to status text
|
|
80
|
+
errorDetails = { message: response.statusText };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const error = new Error(`API request failed: ${response.statusText}`);
|
|
84
|
+
error.statusCode = response.status;
|
|
85
|
+
error.details = errorDetails;
|
|
86
|
+
throw error;
|
|
62
87
|
}
|
|
63
88
|
|
|
64
89
|
return await response.json();
|
|
@@ -98,9 +123,13 @@ export class EmailEngineClient {
|
|
|
98
123
|
this.nextPageCursor = data.nextPageCursor || null;
|
|
99
124
|
this.prevPageCursor = data.prevPageCursor || null;
|
|
100
125
|
|
|
126
|
+
// Clear active email selection when folder changes
|
|
127
|
+
this.currentMessage = null;
|
|
128
|
+
|
|
101
129
|
if (this.container) {
|
|
102
130
|
this.renderMessageList();
|
|
103
131
|
this.renderFolderList(); // Re-render to update active state
|
|
132
|
+
this.renderMessage(); // Clear message viewer
|
|
104
133
|
}
|
|
105
134
|
|
|
106
135
|
return {
|
|
@@ -239,6 +268,73 @@ export class EmailEngineClient {
|
|
|
239
268
|
}
|
|
240
269
|
}
|
|
241
270
|
|
|
271
|
+
async sendMessage(to, subject, text) {
|
|
272
|
+
try {
|
|
273
|
+
const toAddresses = Array.isArray(to)
|
|
274
|
+
? to.map(addr => (typeof addr === 'string' ? { address: addr } : addr))
|
|
275
|
+
: [typeof to === 'string' ? { address: to } : to];
|
|
276
|
+
|
|
277
|
+
const messageData = {
|
|
278
|
+
to: toAddresses,
|
|
279
|
+
subject: subject,
|
|
280
|
+
text: text
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const response = await this.apiRequest('POST', `/v1/account/${this.account}/submit`, messageData);
|
|
284
|
+
return response;
|
|
285
|
+
} catch (error) {
|
|
286
|
+
console.error('Failed to send message:', error);
|
|
287
|
+
// Try to parse detailed error information
|
|
288
|
+
this._parseApiError(error);
|
|
289
|
+
throw error;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
_parseApiError(error) {
|
|
294
|
+
try {
|
|
295
|
+
// If error has response text, try to parse it
|
|
296
|
+
if (error.message && error.message.includes('API request failed:')) {
|
|
297
|
+
// This is our custom error from apiRequest, the actual response might have more details
|
|
298
|
+
error.isDetailedError = false;
|
|
299
|
+
}
|
|
300
|
+
} catch (parseError) {
|
|
301
|
+
// If parsing fails, just use the original error
|
|
302
|
+
console.error('Failed to parse error details:', parseError);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
_formatSendError(error) {
|
|
307
|
+
// If we have detailed error information from the API
|
|
308
|
+
if (error.details && error.details.fields && Array.isArray(error.details.fields)) {
|
|
309
|
+
const fieldErrors = error.details.fields.map(field => {
|
|
310
|
+
// Try to make field errors more user-friendly
|
|
311
|
+
let message = field.message;
|
|
312
|
+
|
|
313
|
+
// Map technical field names to user-friendly names
|
|
314
|
+
if (message.includes('to[0].address') || message.includes('"address"')) {
|
|
315
|
+
message = message.replace(/to\[\d+\]\.address|"address"/g, 'email address');
|
|
316
|
+
}
|
|
317
|
+
if (message.includes('"subject"')) {
|
|
318
|
+
message = message.replace('"subject"', 'subject');
|
|
319
|
+
}
|
|
320
|
+
if (message.includes('"text"')) {
|
|
321
|
+
message = message.replace('"text"', 'message');
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return message;
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
const mainMessage = error.details.message || 'Failed to send email';
|
|
328
|
+
if (fieldErrors.length > 0) {
|
|
329
|
+
return `${mainMessage}:\n\n• ${fieldErrors.join('\n• ')}`;
|
|
330
|
+
}
|
|
331
|
+
return mainMessage;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Fallback to generic error message
|
|
335
|
+
return 'Failed to send email. Please check your input and try again.';
|
|
336
|
+
}
|
|
337
|
+
|
|
242
338
|
formatDate(dateStr) {
|
|
243
339
|
const date = new Date(dateStr);
|
|
244
340
|
const now = new Date();
|
|
@@ -302,7 +398,7 @@ export class EmailEngineClient {
|
|
|
302
398
|
window.URL.revokeObjectURL(url);
|
|
303
399
|
} catch (error) {
|
|
304
400
|
console.error('Failed to download attachment:', error);
|
|
305
|
-
|
|
401
|
+
this.alertMethod('Failed to download attachment. Please try again.', 'Download Error', null, 'OK');
|
|
306
402
|
}
|
|
307
403
|
}
|
|
308
404
|
|
|
@@ -342,7 +438,7 @@ export class EmailEngineClient {
|
|
|
342
438
|
window.URL.revokeObjectURL(url);
|
|
343
439
|
} catch (error) {
|
|
344
440
|
console.error('Failed to download original message:', error);
|
|
345
|
-
|
|
441
|
+
this.alertMethod('Failed to download original message. Please try again.', 'Download Error', null, 'OK');
|
|
346
442
|
}
|
|
347
443
|
}
|
|
348
444
|
|
|
@@ -363,6 +459,7 @@ export class EmailEngineClient {
|
|
|
363
459
|
color: #333;
|
|
364
460
|
background: #fff;
|
|
365
461
|
border: 1px solid #ddd;
|
|
462
|
+
position: relative;
|
|
366
463
|
}
|
|
367
464
|
|
|
368
465
|
.ee-client * {
|
|
@@ -759,6 +856,185 @@ export class EmailEngineClient {
|
|
|
759
856
|
flex: 1;
|
|
760
857
|
overflow-y: auto;
|
|
761
858
|
}
|
|
859
|
+
|
|
860
|
+
.ee-compose-button {
|
|
861
|
+
position: fixed;
|
|
862
|
+
bottom: 20px;
|
|
863
|
+
right: 20px;
|
|
864
|
+
width: 56px;
|
|
865
|
+
height: 56px;
|
|
866
|
+
background: #007bff;
|
|
867
|
+
border: none;
|
|
868
|
+
border-radius: 50%;
|
|
869
|
+
color: white;
|
|
870
|
+
font-size: 24px;
|
|
871
|
+
cursor: pointer;
|
|
872
|
+
box-shadow: 0 4px 12px rgba(0,123,255,0.3);
|
|
873
|
+
z-index: 1000;
|
|
874
|
+
transition: all 0.2s ease;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
.ee-compose-button:hover {
|
|
878
|
+
background: #0056b3;
|
|
879
|
+
transform: scale(1.05);
|
|
880
|
+
box-shadow: 0 6px 16px rgba(0,123,255,0.4);
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
.ee-compose-modal {
|
|
884
|
+
display: none;
|
|
885
|
+
position: fixed;
|
|
886
|
+
top: 0;
|
|
887
|
+
left: 0;
|
|
888
|
+
width: 100%;
|
|
889
|
+
height: 100%;
|
|
890
|
+
background: rgba(0, 0, 0, 0.5);
|
|
891
|
+
z-index: 2000;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
.ee-compose-modal.show {
|
|
895
|
+
display: flex;
|
|
896
|
+
align-items: center;
|
|
897
|
+
justify-content: center;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
.ee-compose-dialog {
|
|
901
|
+
background: white;
|
|
902
|
+
border-radius: 8px;
|
|
903
|
+
width: 90%;
|
|
904
|
+
max-width: 600px;
|
|
905
|
+
max-height: 80vh;
|
|
906
|
+
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
|
|
907
|
+
display: flex;
|
|
908
|
+
flex-direction: column;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
.ee-compose-header {
|
|
912
|
+
padding: 16px 20px;
|
|
913
|
+
border-bottom: 1px solid #e0e0e0;
|
|
914
|
+
display: flex;
|
|
915
|
+
justify-content: space-between;
|
|
916
|
+
align-items: center;
|
|
917
|
+
flex-shrink: 0;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
.ee-compose-title {
|
|
921
|
+
font-size: 18px;
|
|
922
|
+
font-weight: 600;
|
|
923
|
+
margin: 0;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
.ee-compose-close {
|
|
927
|
+
background: none;
|
|
928
|
+
border: none;
|
|
929
|
+
font-size: 24px;
|
|
930
|
+
color: #666;
|
|
931
|
+
cursor: pointer;
|
|
932
|
+
padding: 0;
|
|
933
|
+
width: 30px;
|
|
934
|
+
height: 30px;
|
|
935
|
+
border-radius: 50%;
|
|
936
|
+
display: flex;
|
|
937
|
+
align-items: center;
|
|
938
|
+
justify-content: center;
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
.ee-compose-close:hover {
|
|
942
|
+
background: #f0f0f0;
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
.ee-compose-form {
|
|
946
|
+
padding: 20px;
|
|
947
|
+
display: flex;
|
|
948
|
+
flex-direction: column;
|
|
949
|
+
gap: 16px;
|
|
950
|
+
flex: 1;
|
|
951
|
+
overflow-y: auto;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
.ee-compose-field {
|
|
955
|
+
display: flex;
|
|
956
|
+
flex-direction: column;
|
|
957
|
+
gap: 4px;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
.ee-compose-label {
|
|
961
|
+
font-weight: 500;
|
|
962
|
+
color: #333;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
.ee-compose-input {
|
|
966
|
+
padding: 8px 12px;
|
|
967
|
+
border: 1px solid #ddd;
|
|
968
|
+
border-radius: 4px;
|
|
969
|
+
font-size: 14px;
|
|
970
|
+
font-family: inherit;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
.ee-compose-input:focus {
|
|
974
|
+
outline: none;
|
|
975
|
+
border-color: #007bff;
|
|
976
|
+
box-shadow: 0 0 0 2px rgba(0,123,255,0.1);
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
.ee-compose-textarea {
|
|
980
|
+
padding: 12px;
|
|
981
|
+
border: 1px solid #ddd;
|
|
982
|
+
border-radius: 4px;
|
|
983
|
+
font-size: 14px;
|
|
984
|
+
font-family: inherit;
|
|
985
|
+
resize: vertical;
|
|
986
|
+
min-height: 200px;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
.ee-compose-textarea:focus {
|
|
990
|
+
outline: none;
|
|
991
|
+
border-color: #007bff;
|
|
992
|
+
box-shadow: 0 0 0 2px rgba(0,123,255,0.1);
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
.ee-compose-actions {
|
|
996
|
+
padding: 16px 20px;
|
|
997
|
+
border-top: 1px solid #e0e0e0;
|
|
998
|
+
display: flex;
|
|
999
|
+
gap: 12px;
|
|
1000
|
+
justify-content: flex-end;
|
|
1001
|
+
flex-shrink: 0;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
.ee-compose-send {
|
|
1005
|
+
background: #007bff;
|
|
1006
|
+
color: white;
|
|
1007
|
+
border: none;
|
|
1008
|
+
padding: 10px 20px;
|
|
1009
|
+
border-radius: 4px;
|
|
1010
|
+
font-size: 14px;
|
|
1011
|
+
font-weight: 500;
|
|
1012
|
+
cursor: pointer;
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
.ee-compose-send:hover:not(:disabled) {
|
|
1016
|
+
background: #0056b3;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
.ee-compose-send:disabled {
|
|
1020
|
+
background: #6c757d;
|
|
1021
|
+
cursor: not-allowed;
|
|
1022
|
+
opacity: 0.6;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
.ee-compose-cancel {
|
|
1026
|
+
background: #6c757d;
|
|
1027
|
+
color: white;
|
|
1028
|
+
border: none;
|
|
1029
|
+
padding: 10px 20px;
|
|
1030
|
+
border-radius: 4px;
|
|
1031
|
+
font-size: 14px;
|
|
1032
|
+
cursor: pointer;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
.ee-compose-cancel:hover {
|
|
1036
|
+
background: #545b62;
|
|
1037
|
+
}
|
|
762
1038
|
`;
|
|
763
1039
|
document.head.appendChild(style);
|
|
764
1040
|
}
|
|
@@ -1074,7 +1350,12 @@ export class EmailEngineClient {
|
|
|
1074
1350
|
});
|
|
1075
1351
|
|
|
1076
1352
|
viewer.querySelector('[data-action="delete"]').addEventListener('click', async () => {
|
|
1077
|
-
const result = await this.confirmMethod(
|
|
1353
|
+
const result = await this.confirmMethod(
|
|
1354
|
+
'Are you sure you want to delete this message? This action cannot be undone.',
|
|
1355
|
+
'Delete Message',
|
|
1356
|
+
'Cancel',
|
|
1357
|
+
'Delete'
|
|
1358
|
+
);
|
|
1078
1359
|
if (result) {
|
|
1079
1360
|
this.deleteMessage(msg.id);
|
|
1080
1361
|
}
|
|
@@ -1099,6 +1380,12 @@ export class EmailEngineClient {
|
|
|
1099
1380
|
this.downloadAttachment(attachmentId, attachment ? attachment.filename : null);
|
|
1100
1381
|
});
|
|
1101
1382
|
});
|
|
1383
|
+
|
|
1384
|
+
// Scroll message viewer back to top when new email is displayed
|
|
1385
|
+
const messageContent = viewer.querySelector('.ee-message-content');
|
|
1386
|
+
if (messageContent) {
|
|
1387
|
+
messageContent.scrollTop = 0;
|
|
1388
|
+
}
|
|
1102
1389
|
}
|
|
1103
1390
|
|
|
1104
1391
|
createLayout() {
|
|
@@ -1126,7 +1413,209 @@ export class EmailEngineClient {
|
|
|
1126
1413
|
<div class="ee-empty-state">Select a message to view</div>
|
|
1127
1414
|
</div>
|
|
1128
1415
|
</div>
|
|
1416
|
+
<button class="ee-compose-button" title="Compose Email">✉</button>
|
|
1417
|
+
<div class="ee-compose-modal">
|
|
1418
|
+
<div class="ee-compose-dialog">
|
|
1419
|
+
<div class="ee-compose-header">
|
|
1420
|
+
<h3 class="ee-compose-title">Compose Email</h3>
|
|
1421
|
+
<button class="ee-compose-close">×</button>
|
|
1422
|
+
</div>
|
|
1423
|
+
<form class="ee-compose-form">
|
|
1424
|
+
<div class="ee-compose-field">
|
|
1425
|
+
<label class="ee-compose-label">To:</label>
|
|
1426
|
+
<input type="email" class="ee-compose-input" name="to" placeholder="recipient@example.com" required>
|
|
1427
|
+
</div>
|
|
1428
|
+
<div class="ee-compose-field">
|
|
1429
|
+
<label class="ee-compose-label">Subject:</label>
|
|
1430
|
+
<input type="text" class="ee-compose-input" name="subject" placeholder="Enter subject">
|
|
1431
|
+
</div>
|
|
1432
|
+
<div class="ee-compose-field">
|
|
1433
|
+
<label class="ee-compose-label">Message:</label>
|
|
1434
|
+
<textarea class="ee-compose-textarea" name="message" placeholder="Type your message here..." required></textarea>
|
|
1435
|
+
</div>
|
|
1436
|
+
</form>
|
|
1437
|
+
<div class="ee-compose-actions">
|
|
1438
|
+
<button type="button" class="ee-compose-cancel">Cancel</button>
|
|
1439
|
+
<button type="button" class="ee-compose-send">Send</button>
|
|
1440
|
+
</div>
|
|
1441
|
+
</div>
|
|
1442
|
+
</div>
|
|
1129
1443
|
`;
|
|
1444
|
+
|
|
1445
|
+
// Wire up compose modal events
|
|
1446
|
+
this.setupComposeModal();
|
|
1447
|
+
|
|
1448
|
+
// Position compose button correctly
|
|
1449
|
+
this.positionComposeButton();
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
setupComposeModal() {
|
|
1453
|
+
if (typeof document === 'undefined' || !this.container) {
|
|
1454
|
+
return;
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
const composeButton = this.container.querySelector('.ee-compose-button');
|
|
1458
|
+
const modal = this.container.querySelector('.ee-compose-modal');
|
|
1459
|
+
const closeButton = this.container.querySelector('.ee-compose-close');
|
|
1460
|
+
const cancelButton = this.container.querySelector('.ee-compose-cancel');
|
|
1461
|
+
const sendButton = this.container.querySelector('.ee-compose-send');
|
|
1462
|
+
const form = this.container.querySelector('.ee-compose-form');
|
|
1463
|
+
|
|
1464
|
+
// Open modal
|
|
1465
|
+
composeButton.addEventListener('click', () => {
|
|
1466
|
+
modal.classList.add('show');
|
|
1467
|
+
// Focus the To field
|
|
1468
|
+
const toField = form.querySelector('input[name="to"]');
|
|
1469
|
+
setTimeout(() => toField.focus(), 100);
|
|
1470
|
+
});
|
|
1471
|
+
|
|
1472
|
+
// Close modal handlers
|
|
1473
|
+
const closeModal = () => {
|
|
1474
|
+
modal.classList.remove('show');
|
|
1475
|
+
form.reset();
|
|
1476
|
+
};
|
|
1477
|
+
|
|
1478
|
+
closeButton.addEventListener('click', closeModal);
|
|
1479
|
+
cancelButton.addEventListener('click', closeModal);
|
|
1480
|
+
|
|
1481
|
+
// Close on backdrop click
|
|
1482
|
+
modal.addEventListener('click', e => {
|
|
1483
|
+
if (e.target === modal) {
|
|
1484
|
+
closeModal();
|
|
1485
|
+
}
|
|
1486
|
+
});
|
|
1487
|
+
|
|
1488
|
+
// Close on Escape key
|
|
1489
|
+
document.addEventListener('keydown', e => {
|
|
1490
|
+
if (e.key === 'Escape' && modal.classList.contains('show')) {
|
|
1491
|
+
closeModal();
|
|
1492
|
+
}
|
|
1493
|
+
});
|
|
1494
|
+
|
|
1495
|
+
// Send email
|
|
1496
|
+
sendButton.addEventListener('click', async () => {
|
|
1497
|
+
const formData = new FormData(form);
|
|
1498
|
+
const to = formData.get('to').trim();
|
|
1499
|
+
const subject = formData.get('subject').trim();
|
|
1500
|
+
const message = formData.get('message').trim();
|
|
1501
|
+
|
|
1502
|
+
if (!to || !message) {
|
|
1503
|
+
// Close modal temporarily to show alert, then reopen
|
|
1504
|
+
modal.classList.remove('show');
|
|
1505
|
+
await this.alertMethod(
|
|
1506
|
+
'Please fill in the recipient and message fields.',
|
|
1507
|
+
'Validation Error',
|
|
1508
|
+
null,
|
|
1509
|
+
'OK'
|
|
1510
|
+
);
|
|
1511
|
+
modal.classList.add('show');
|
|
1512
|
+
// Re-focus the appropriate field
|
|
1513
|
+
const fieldToFocus = !to
|
|
1514
|
+
? form.querySelector('input[name="to"]')
|
|
1515
|
+
: form.querySelector('textarea[name="message"]');
|
|
1516
|
+
setTimeout(() => fieldToFocus.focus(), 100);
|
|
1517
|
+
return;
|
|
1518
|
+
}
|
|
1519
|
+
|
|
1520
|
+
// Disable send button and show loading state
|
|
1521
|
+
const originalText = sendButton.textContent;
|
|
1522
|
+
sendButton.disabled = true;
|
|
1523
|
+
sendButton.textContent = 'Sending...';
|
|
1524
|
+
|
|
1525
|
+
try {
|
|
1526
|
+
await this.sendMessage(to, subject, message);
|
|
1527
|
+
// Close modal before showing success alert
|
|
1528
|
+
closeModal();
|
|
1529
|
+
await this.alertMethod('Email sent successfully!', 'Success', null, 'OK');
|
|
1530
|
+
} catch (error) {
|
|
1531
|
+
console.error('Failed to send email:', error);
|
|
1532
|
+
// Close modal before showing error alert
|
|
1533
|
+
modal.classList.remove('show');
|
|
1534
|
+
const errorMessage = this._formatSendError(error);
|
|
1535
|
+
await this.alertMethod(errorMessage, 'Send Error', null, 'OK');
|
|
1536
|
+
|
|
1537
|
+
// Reopen modal with preserved values after error alert
|
|
1538
|
+
modal.classList.add('show');
|
|
1539
|
+
// Re-focus the To field to allow user to continue editing
|
|
1540
|
+
const toField = form.querySelector('input[name="to"]');
|
|
1541
|
+
setTimeout(() => toField.focus(), 100);
|
|
1542
|
+
} finally {
|
|
1543
|
+
// Re-enable send button
|
|
1544
|
+
sendButton.disabled = false;
|
|
1545
|
+
sendButton.textContent = originalText;
|
|
1546
|
+
}
|
|
1547
|
+
});
|
|
1548
|
+
|
|
1549
|
+
// Handle Enter key in form (Ctrl+Enter to send)
|
|
1550
|
+
form.addEventListener('keydown', e => {
|
|
1551
|
+
if (e.key === 'Enter' && e.ctrlKey) {
|
|
1552
|
+
e.preventDefault();
|
|
1553
|
+
sendButton.click();
|
|
1554
|
+
}
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
positionComposeButton() {
|
|
1559
|
+
if (typeof document === 'undefined' || !this.container) {
|
|
1560
|
+
return;
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1563
|
+
const composeButton = this.container.querySelector('.ee-compose-button');
|
|
1564
|
+
if (!composeButton) {
|
|
1565
|
+
return;
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
const updateButtonPosition = () => {
|
|
1569
|
+
const containerRect = this.container.getBoundingClientRect();
|
|
1570
|
+
const buttonSize = 56; // Button width/height
|
|
1571
|
+
const margin = 20; // Desired margin from edges
|
|
1572
|
+
|
|
1573
|
+
// Calculate the ideal position (bottom-right of container with margin)
|
|
1574
|
+
const idealBottom = window.innerHeight - containerRect.bottom + margin;
|
|
1575
|
+
const idealRight = window.innerWidth - containerRect.right + margin;
|
|
1576
|
+
|
|
1577
|
+
// Ensure button stays within viewport bounds
|
|
1578
|
+
const minBottom = margin;
|
|
1579
|
+
const minRight = margin;
|
|
1580
|
+
|
|
1581
|
+
// Also ensure button stays within container horizontal bounds
|
|
1582
|
+
const maxRight = window.innerWidth - containerRect.left - buttonSize - margin;
|
|
1583
|
+
|
|
1584
|
+
// Calculate final position
|
|
1585
|
+
const bottom = Math.max(minBottom, idealBottom);
|
|
1586
|
+
const right = Math.min(Math.max(minRight, idealRight), maxRight);
|
|
1587
|
+
|
|
1588
|
+
// Apply positioning
|
|
1589
|
+
composeButton.style.bottom = `${bottom}px`;
|
|
1590
|
+
composeButton.style.right = `${right}px`;
|
|
1591
|
+
};
|
|
1592
|
+
|
|
1593
|
+
// Initial positioning
|
|
1594
|
+
updateButtonPosition();
|
|
1595
|
+
|
|
1596
|
+
// Update position on scroll and resize
|
|
1597
|
+
const updateWithThrottle = this.throttle(updateButtonPosition, 16); // ~60fps
|
|
1598
|
+
window.addEventListener('scroll', updateWithThrottle);
|
|
1599
|
+
window.addEventListener('resize', updateWithThrottle);
|
|
1600
|
+
|
|
1601
|
+
// Store cleanup function for potential future use
|
|
1602
|
+
this._composeButtonCleanup = () => {
|
|
1603
|
+
window.removeEventListener('scroll', updateWithThrottle);
|
|
1604
|
+
window.removeEventListener('resize', updateWithThrottle);
|
|
1605
|
+
};
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
throttle(func, limit) {
|
|
1609
|
+
let inThrottle;
|
|
1610
|
+
return function () {
|
|
1611
|
+
const args = arguments;
|
|
1612
|
+
const context = this;
|
|
1613
|
+
if (!inThrottle) {
|
|
1614
|
+
func.apply(context, args);
|
|
1615
|
+
inThrottle = true;
|
|
1616
|
+
setTimeout(() => (inThrottle = false), limit);
|
|
1617
|
+
}
|
|
1618
|
+
};
|
|
1130
1619
|
}
|
|
1131
1620
|
|
|
1132
1621
|
init() {
|
|
@@ -1151,6 +1640,51 @@ export class EmailEngineClient {
|
|
|
1151
1640
|
console.error('Failed to auto-select inbox:', error);
|
|
1152
1641
|
});
|
|
1153
1642
|
}
|
|
1643
|
+
|
|
1644
|
+
_updateActivity() {
|
|
1645
|
+
this.lastActivity = Date.now();
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
_startKeepAliveTimer() {
|
|
1649
|
+
// Only start keep-alive for sess_ tokens
|
|
1650
|
+
if (!this.accessToken || !this.accessToken.startsWith('sess_')) {
|
|
1651
|
+
return;
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
// Clear existing timer if any
|
|
1655
|
+
if (this.keepAliveTimer) {
|
|
1656
|
+
clearInterval(this.keepAliveTimer);
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
// Check every minute if we need to ping
|
|
1660
|
+
this.keepAliveTimer = setInterval(() => {
|
|
1661
|
+
const now = Date.now();
|
|
1662
|
+
const idleTime = now - this.lastActivity;
|
|
1663
|
+
|
|
1664
|
+
// If idle for 5+ minutes, ping to keep token alive
|
|
1665
|
+
if (idleTime >= 5 * 60 * 1000) {
|
|
1666
|
+
this._keepTokenAlive();
|
|
1667
|
+
}
|
|
1668
|
+
}, 60 * 1000); // Check every minute
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
async _keepTokenAlive() {
|
|
1672
|
+
try {
|
|
1673
|
+
// Ping account endpoint to keep token alive
|
|
1674
|
+
await this.apiRequest('GET', `/v1/account/${this.account}`);
|
|
1675
|
+
console.debug('Keep-alive ping sent for sess_ token');
|
|
1676
|
+
} catch (error) {
|
|
1677
|
+
console.warn('Keep-alive ping failed:', error.message);
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
destroy() {
|
|
1682
|
+
// Clean up keep-alive timer
|
|
1683
|
+
if (this.keepAliveTimer) {
|
|
1684
|
+
clearInterval(this.keepAliveTimer);
|
|
1685
|
+
this.keepAliveTimer = null;
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1154
1688
|
}
|
|
1155
1689
|
|
|
1156
1690
|
export function createEmailEngineClient(options) {
|
package/package.json
CHANGED
|
@@ -1,29 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@postalsys/ee-client",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "EmailEngine client library for browser and Node.js applications",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.js",
|
|
7
7
|
"type": "module",
|
|
8
|
-
"exports":
|
|
9
|
-
".": {
|
|
10
|
-
"import": "./index.js",
|
|
11
|
-
"require": "./index.cjs"
|
|
12
|
-
}
|
|
13
|
-
},
|
|
8
|
+
"exports": "./index.js",
|
|
14
9
|
"files": [
|
|
15
10
|
"index.js",
|
|
16
|
-
"index.cjs",
|
|
17
11
|
"index.d.ts",
|
|
18
12
|
"README.md",
|
|
19
13
|
"LICENSE"
|
|
20
14
|
],
|
|
21
15
|
"scripts": {
|
|
22
|
-
"build": "node build.js",
|
|
23
16
|
"lint": "eslint *.js",
|
|
24
17
|
"format": "prettier --write *.js *.json *.md",
|
|
25
18
|
"format:check": "prettier --check *.js *.json *.md",
|
|
26
|
-
"prepublishOnly": "npm run lint && npm run format:check
|
|
19
|
+
"prepublishOnly": "npm run lint && npm run format:check"
|
|
27
20
|
},
|
|
28
21
|
"keywords": [
|
|
29
22
|
"email",
|