dsh-mailgun 0.1.4 → 0.1.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.
- package/README.md +24 -4
- package/lib/api.js +29 -2
- package/lib/client.js +143 -33
- package/lib/settings-web.js +20 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,9 +13,14 @@ send-only sending service out of the account list that `dsh-email` manages.
|
|
|
13
13
|
|
|
14
14
|
One Mailgun account is one API key and one sending domain, so the plugin
|
|
15
15
|
holds a list of accounts rather than a single one. Add them on the harness
|
|
16
|
-
**Settings → Mailgun** page: one
|
|
16
|
+
**Settings → Mailgun** page: one row per account, each with a label, the
|
|
17
17
|
sending domain, the region, the From address and the API key.
|
|
18
18
|
|
|
19
|
+
A saved account is an **accordion item**: the collapsed line carries the
|
|
20
|
+
label, the sending domain and the badges, and the form opens when the line
|
|
21
|
+
is clicked. Collapsed is the standard state, also for an account that was
|
|
22
|
+
just added.
|
|
23
|
+
|
|
19
24
|
The **Add Mailgun account** form stays open after it saves, so a second
|
|
20
25
|
account is one more click away instead of another trip through the button.
|
|
21
26
|
Several accounts may carry the same label — each one still gets its own id,
|
|
@@ -47,13 +52,28 @@ another one.
|
|
|
47
52
|
|
|
48
53
|
## Install
|
|
49
54
|
|
|
50
|
-
|
|
51
|
-
|
|
55
|
+
From the Plugin Manager's **Discover** tab, or directly from npm:
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
dsh plugin --profile web add dsh-mailgun
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The package is also on npm as [`dsh-mailgun`](https://www.npmjs.com/package/dsh-mailgun),
|
|
62
|
+
so `npm install dsh-mailgun` works too. To run it straight from a checkout
|
|
63
|
+
instead:
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
dsh plugin --profile desktop add "link:/path/to/dsh-mailgun"
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### From a checkout
|
|
70
|
+
|
|
71
|
+
The plugin can also be registered as a local bundle in a profile:
|
|
52
72
|
|
|
53
73
|
```yaml
|
|
54
74
|
# ~/.dsh/profiles/desktop/package.json
|
|
55
75
|
"dependencies": {
|
|
56
|
-
"dsh-mailgun": "link:/
|
|
76
|
+
"dsh-mailgun": "link:/path/to/dsh-mailgun"
|
|
57
77
|
},
|
|
58
78
|
"dsh": { "profile": { "bundles": [ ..., "dsh-mailgun" ] } }
|
|
59
79
|
```
|
package/lib/api.js
CHANGED
|
@@ -45,7 +45,18 @@ export async function call(options) {
|
|
|
45
45
|
const { base, path, apiKey, query, form, method = 'GET', signal } = options;
|
|
46
46
|
const url = new URL(base + path);
|
|
47
47
|
for (const [key, value] of Object.entries(query ?? {})) {
|
|
48
|
-
if (value
|
|
48
|
+
if (value === undefined || value === null || value === '') continue;
|
|
49
|
+
// A list is sent as one repeated parameter per value. Mailgun reads
|
|
50
|
+
// `?event=accepted&event=delivered` as two filters, while a single
|
|
51
|
+
// `?event=accepted,delivered` is one unknown event name and matches
|
|
52
|
+
// nothing at all.
|
|
53
|
+
if (Array.isArray(value)) {
|
|
54
|
+
for (const item of value) {
|
|
55
|
+
if (item !== undefined && item !== null && item !== '') url.searchParams.append(key, String(item));
|
|
56
|
+
}
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
url.searchParams.set(key, String(value));
|
|
49
60
|
}
|
|
50
61
|
const headers = { Authorization: 'Basic ' + Buffer.from('api:' + apiKey).toString('base64') };
|
|
51
62
|
let body;
|
|
@@ -149,7 +160,9 @@ export async function listEvents(options) {
|
|
|
149
160
|
path: '/' + encodeURIComponent(domain) + '/events',
|
|
150
161
|
apiKey,
|
|
151
162
|
query: {
|
|
152
|
-
event
|
|
163
|
+
// One parameter per event name; a comma-separated list in a single
|
|
164
|
+
// parameter is one unknown name to Mailgun.
|
|
165
|
+
event: eventNames(event),
|
|
153
166
|
recipient,
|
|
154
167
|
limit: Math.min(Math.max(Number(limit) || 25, 1), 300),
|
|
155
168
|
ascending: ascending ? 'yes' : 'no',
|
|
@@ -158,6 +171,20 @@ export async function listEvents(options) {
|
|
|
158
171
|
});
|
|
159
172
|
}
|
|
160
173
|
|
|
174
|
+
/**
|
|
175
|
+
* Split an event filter into its names.
|
|
176
|
+
*
|
|
177
|
+
* @param {string|string[]|undefined} event - one name, a comma-separated list, or a list.
|
|
178
|
+
* @returns {string[]|undefined} the names, or undefined when no filter was given.
|
|
179
|
+
*/
|
|
180
|
+
export function eventNames(event) {
|
|
181
|
+
if (event === undefined || event === null || event === '') return undefined;
|
|
182
|
+
const names = (Array.isArray(event) ? event : String(event).split(','))
|
|
183
|
+
.map((name) => String(name).trim())
|
|
184
|
+
.filter((name) => name !== '');
|
|
185
|
+
return names.length === 0 ? undefined : names;
|
|
186
|
+
}
|
|
187
|
+
|
|
161
188
|
/**
|
|
162
189
|
* List the sending domains on the account.
|
|
163
190
|
*
|
package/lib/client.js
CHANGED
|
@@ -91,6 +91,8 @@
|
|
|
91
91
|
addMore: 'The form stays open, so you can add the next account right away.',
|
|
92
92
|
removed: 'Removed.',
|
|
93
93
|
keyAccepted: 'Key accepted.',
|
|
94
|
+
unnamed: 'Unnamed account',
|
|
95
|
+
noDomain: 'No sending domain',
|
|
94
96
|
},
|
|
95
97
|
zh: {
|
|
96
98
|
title: 'Mailgun',
|
|
@@ -119,6 +121,8 @@
|
|
|
119
121
|
addMore: '表单会保持打开,可以马上添加下一个账户。',
|
|
120
122
|
removed: '已移除。',
|
|
121
123
|
keyAccepted: '密钥有效。',
|
|
124
|
+
unnamed: '未命名账户',
|
|
125
|
+
noDomain: '未设置发信域名',
|
|
122
126
|
},
|
|
123
127
|
};
|
|
124
128
|
|
|
@@ -131,6 +135,16 @@
|
|
|
131
135
|
'.mgsec{display:flex;flex-direction:column;gap:14px;padding:4px 0}',
|
|
132
136
|
'.mgsec_intro{font-size:12px;line-height:18px;color:var(--dsw-alias-text-secondary,#6b7280);margin:0}',
|
|
133
137
|
'.mgsec_card{display:flex;flex-direction:column;gap:10px;border:1px solid var(--dsw-alias-border-subtle,#e5e7eb);border-radius:10px;padding:14px}',
|
|
138
|
+
'.mgsec_head{display:flex;align-items:center;gap:10px;width:100%;text-align:left;font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}',
|
|
139
|
+
'.mgsec_head:hover .mgsec_title{text-decoration:underline}',
|
|
140
|
+
'.mgsec_head:focus-visible{outline:2px solid var(--dsw-alias-accent-primary,#2563eb);outline-offset:2px;border-radius:6px}',
|
|
141
|
+
'.mgsec_chevron{font-size:11px;line-height:16px;color:var(--dsw-alias-text-secondary,#6b7280);transition:transform .15s ease;transform:rotate(0deg)}',
|
|
142
|
+
'.mgsec_chevron_open{transform:rotate(90deg)}',
|
|
143
|
+
'.mgsec_headmain{display:flex;flex-direction:column;gap:2px;min-width:0;flex:1}',
|
|
144
|
+
'.mgsec_title{font-size:13px;line-height:18px;font-weight:600;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}',
|
|
145
|
+
'.mgsec_sub{font-size:12px;line-height:16px;color:var(--dsw-alias-text-secondary,#6b7280);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}',
|
|
146
|
+
'.mgsec_badges{display:flex;gap:6px;flex-wrap:wrap;justify-content:flex-end}',
|
|
147
|
+
'.mgsec_body{display:flex;flex-direction:column;gap:10px;border-top:1px solid var(--dsw-alias-border-subtle,#e5e7eb);padding-top:10px}',
|
|
134
148
|
'.mgsec_row{display:flex;flex-direction:column;gap:4px}',
|
|
135
149
|
'.mgsec_label{font-size:12px;line-height:16px;color:var(--dsw-alias-text-secondary,#6b7280)}',
|
|
136
150
|
'.mgsec_input{font:inherit;font-size:13px;line-height:20px;padding:7px 9px;border-radius:7px;border:1px solid var(--dsw-alias-border-subtle,#d1d5db);background:var(--dsw-alias-surface-default,#fff);color:var(--dsw-alias-text-primary,#111827);min-width:0}',
|
|
@@ -151,6 +165,31 @@
|
|
|
151
165
|
'.mgsec_empty{font-size:12px;line-height:18px;color:var(--dsw-alias-text-secondary,#6b7280);margin:0}',
|
|
152
166
|
].join('');
|
|
153
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Unwrap one backend answer.
|
|
170
|
+
*
|
|
171
|
+
* The backend answers with `{ ok: true, value: … }`. A bare
|
|
172
|
+
* payload — and a payload wrapped one time too often — is
|
|
173
|
+
* accepted too, so a different shape can never leave the
|
|
174
|
+
* account list silently empty.
|
|
175
|
+
*
|
|
176
|
+
* @param {object} body - the parsed answer.
|
|
177
|
+
* @param {string} failure - the message for a failed answer.
|
|
178
|
+
* @returns {object} the payload.
|
|
179
|
+
*/
|
|
180
|
+
function payloadOf(body, failure) {
|
|
181
|
+
if (!body || typeof body !== 'object') throw new Error(failure);
|
|
182
|
+
if (body.ok === false) {
|
|
183
|
+
throw new Error((body.error && body.error.message) || failure);
|
|
184
|
+
}
|
|
185
|
+
var value = body.ok === true ? body.value : body;
|
|
186
|
+
if (value && typeof value === 'object' && !Array.isArray(value.accounts)
|
|
187
|
+
&& value.value && typeof value.value === 'object') {
|
|
188
|
+
value = value.value;
|
|
189
|
+
}
|
|
190
|
+
return value;
|
|
191
|
+
}
|
|
192
|
+
|
|
154
193
|
/**
|
|
155
194
|
* Read the current account list from the backend.
|
|
156
195
|
*
|
|
@@ -160,10 +199,11 @@
|
|
|
160
199
|
return fetch(SETTINGS_URL, { headers: { accept: 'application/json' } })
|
|
161
200
|
.then(function (response) { return response.json(); })
|
|
162
201
|
.then(function (body) {
|
|
163
|
-
|
|
164
|
-
|
|
202
|
+
var value = payloadOf(body, 'could not read the Mailgun settings');
|
|
203
|
+
if (!value || !Array.isArray(value.accounts)) {
|
|
204
|
+
throw new Error('the Mailgun settings could not be read: the answer carried no account list');
|
|
165
205
|
}
|
|
166
|
-
return
|
|
206
|
+
return value;
|
|
167
207
|
});
|
|
168
208
|
}
|
|
169
209
|
|
|
@@ -181,10 +221,10 @@
|
|
|
181
221
|
})
|
|
182
222
|
.then(function (response) { return response.json(); })
|
|
183
223
|
.then(function (body) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
return body
|
|
224
|
+
// The whole result is returned: `save` carries the fresh
|
|
225
|
+
// view, `test` carries the domain list. Reading only the
|
|
226
|
+
// view would report an empty domain list.
|
|
227
|
+
return payloadOf(body, 'the Mailgun settings could not be saved');
|
|
188
228
|
});
|
|
189
229
|
}
|
|
190
230
|
|
|
@@ -205,13 +245,24 @@
|
|
|
205
245
|
senderName: account.senderName || '',
|
|
206
246
|
});
|
|
207
247
|
var apiKeyState = React.useState('');
|
|
208
|
-
var
|
|
209
|
-
|
|
248
|
+
var busyState = React.useState(false);
|
|
249
|
+
// The card is an accordion item, and collapsed is its standard
|
|
250
|
+
// state: a saved account is a summary line, and the form opens
|
|
251
|
+
// only when the user asks for it.
|
|
252
|
+
var openState = React.useState(false);
|
|
253
|
+
var notice = { current: null };
|
|
210
254
|
|
|
211
255
|
var draft = state[0];
|
|
212
256
|
var setDraft = state[1];
|
|
213
257
|
var apiKey = apiKeyState[0];
|
|
214
258
|
var setApiKey = apiKeyState[1];
|
|
259
|
+
// `useState` returns a pair: the value and the setter. Holding the
|
|
260
|
+
// pair in `busy` and calling it would throw a TypeError before
|
|
261
|
+
// the request was ever sent, which is why nothing saved.
|
|
262
|
+
var busy = busyState[0];
|
|
263
|
+
var setBusy = busyState[1];
|
|
264
|
+
var open = openState[0];
|
|
265
|
+
var setOpen = openState[1];
|
|
215
266
|
|
|
216
267
|
function patch(field, value) {
|
|
217
268
|
setDraft(function (previous) {
|
|
@@ -228,7 +279,7 @@
|
|
|
228
279
|
}
|
|
229
280
|
|
|
230
281
|
function save() {
|
|
231
|
-
|
|
282
|
+
setBusy(true);
|
|
232
283
|
props.onSave({
|
|
233
284
|
action: 'save',
|
|
234
285
|
account: {
|
|
@@ -247,11 +298,11 @@
|
|
|
247
298
|
report(t('saved'), true);
|
|
248
299
|
})
|
|
249
300
|
.catch(function (error) { report(error.message, false); })
|
|
250
|
-
.finally(function () {
|
|
301
|
+
.finally(function () { setBusy(false); });
|
|
251
302
|
}
|
|
252
303
|
|
|
253
304
|
function test() {
|
|
254
|
-
|
|
305
|
+
setBusy(true);
|
|
255
306
|
props.onTest(account.id)
|
|
256
307
|
.then(function (domains) {
|
|
257
308
|
report(
|
|
@@ -261,18 +312,41 @@
|
|
|
261
312
|
);
|
|
262
313
|
})
|
|
263
314
|
.catch(function (error) { report(error.message, false); })
|
|
264
|
-
.finally(function () {
|
|
315
|
+
.finally(function () { setBusy(false); });
|
|
265
316
|
}
|
|
266
317
|
|
|
267
318
|
function remove() {
|
|
268
|
-
|
|
319
|
+
setBusy(true);
|
|
269
320
|
props.onRemove(account.id)
|
|
270
321
|
.then(function () { report(t('removed'), true); })
|
|
271
322
|
.catch(function (error) { report(error.message, false); })
|
|
272
|
-
.finally(function () {
|
|
323
|
+
.finally(function () { setBusy(false); });
|
|
273
324
|
}
|
|
274
325
|
|
|
275
326
|
return h('div', { className: 'mgsec_card' },
|
|
327
|
+
// The summary line: it is the button that opens and closes
|
|
328
|
+
// the account, so the whole row is the accordion handle.
|
|
329
|
+
h('button', {
|
|
330
|
+
className: 'mgsec_head',
|
|
331
|
+
type: 'button',
|
|
332
|
+
'aria-expanded': open,
|
|
333
|
+
onClick: function () { setOpen(!open); },
|
|
334
|
+
},
|
|
335
|
+
h('span', { className: 'mgsec_chevron' + (open ? ' mgsec_chevron_open' : ''), 'aria-hidden': true }, '▸'),
|
|
336
|
+
h('span', { className: 'mgsec_headmain' },
|
|
337
|
+
h('span', { className: 'mgsec_title' }, draft.label !== '' ? draft.label : (draft.domain !== '' ? draft.domain : t('unnamed'))),
|
|
338
|
+
h('span', { className: 'mgsec_sub' }, draft.domain !== '' ? draft.domain : t('noDomain'))),
|
|
339
|
+
h('span', { className: 'mgsec_badges' },
|
|
340
|
+
props.isDefault
|
|
341
|
+
? h('span', { className: 'mgsec_badge mgsec_badge_ok' }, t('defaultAccount'))
|
|
342
|
+
: null,
|
|
343
|
+
h('span', { className: 'mgsec_badge' + (account.hasKey ? ' mgsec_badge_ok' : '') },
|
|
344
|
+
account.hasKey ? t('keyStored') : t('noKeyYet')),
|
|
345
|
+
h('span', { className: 'mgsec_badge' }, draft.region === 'eu' ? t('regionEu') : t('regionUs')),
|
|
346
|
+
),
|
|
347
|
+
),
|
|
348
|
+
open
|
|
349
|
+
? h('div', { className: 'mgsec_body' },
|
|
276
350
|
h('div', { className: 'mgsec_row' },
|
|
277
351
|
h('label', { className: 'mgsec_label' }, t('label')),
|
|
278
352
|
h('input', {
|
|
@@ -336,13 +410,15 @@
|
|
|
336
410
|
account.hasKey ? t('keyStored') : t('noKeyYet')),
|
|
337
411
|
),
|
|
338
412
|
h('div', { className: 'mgsec_actions' },
|
|
339
|
-
h('button', { className: 'mgsec_btn mgsec_btn_primary', type: 'button', disabled: busy
|
|
340
|
-
h('button', { className: 'mgsec_btn', type: 'button', disabled: busy
|
|
413
|
+
h('button', { className: 'mgsec_btn mgsec_btn_primary', type: 'button', disabled: busy, onClick: save }, t('save')),
|
|
414
|
+
h('button', { className: 'mgsec_btn', type: 'button', disabled: busy, onClick: test }, t('test')),
|
|
341
415
|
!props.isDefault
|
|
342
|
-
? h('button', { className: 'mgsec_btn', type: 'button', disabled: busy
|
|
416
|
+
? h('button', { className: 'mgsec_btn', type: 'button', disabled: busy, onClick: props.onMakeDefault }, t('makeDefault'))
|
|
343
417
|
: h('span', { className: 'mgsec_badge mgsec_badge_ok' }, t('defaultAccount')),
|
|
344
|
-
h('button', { className: 'mgsec_btn mgsec_btn_danger', type: 'button', disabled: busy
|
|
418
|
+
h('button', { className: 'mgsec_btn mgsec_btn_danger', type: 'button', disabled: busy, onClick: remove }, t('remove')),
|
|
345
419
|
),
|
|
420
|
+
)
|
|
421
|
+
: null,
|
|
346
422
|
);
|
|
347
423
|
}
|
|
348
424
|
|
|
@@ -364,8 +440,14 @@
|
|
|
364
440
|
var newState = React.useState({ label: '', domain: '', region: 'us', from: '', senderName: '', apiKey: '' });
|
|
365
441
|
var draft = newState[0];
|
|
366
442
|
var setDraft = newState[1];
|
|
367
|
-
var
|
|
368
|
-
var
|
|
443
|
+
var addingState = React.useState(false);
|
|
444
|
+
var busyState = React.useState(false);
|
|
445
|
+
// Same pair rule as in the card: the value and the setter are two
|
|
446
|
+
// different things, and calling the pair would throw.
|
|
447
|
+
var adding = addingState[0];
|
|
448
|
+
var setAdding = addingState[1];
|
|
449
|
+
var busy = busyState[0];
|
|
450
|
+
var setBusy = busyState[1];
|
|
369
451
|
|
|
370
452
|
React.useEffect(function () {
|
|
371
453
|
var alive = true;
|
|
@@ -379,10 +461,38 @@
|
|
|
379
461
|
return loadView().then(function (value) { setView(value); return value; });
|
|
380
462
|
}
|
|
381
463
|
|
|
464
|
+
/**
|
|
465
|
+
* Change one field of the add form.
|
|
466
|
+
*
|
|
467
|
+
* The update is functional, so two fields edited before the
|
|
468
|
+
* next render cannot overwrite each other with a stale copy
|
|
469
|
+
* of the draft.
|
|
470
|
+
*
|
|
471
|
+
* @param {string} field - the field name.
|
|
472
|
+
* @param {string} value - the new value.
|
|
473
|
+
*/
|
|
474
|
+
function patch(field, value) {
|
|
475
|
+
setDraft(function (previous) {
|
|
476
|
+
var next = {};
|
|
477
|
+
for (var key in previous) next[key] = previous[key];
|
|
478
|
+
next[field] = value;
|
|
479
|
+
return next;
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
|
|
382
483
|
function save(payload) {
|
|
383
484
|
return postAction(payload).then(function (value) {
|
|
384
|
-
|
|
385
|
-
|
|
485
|
+
// The list is read back from the backend after every
|
|
486
|
+
// action: the GET is the one answer that carries the
|
|
487
|
+
// account list, and it is what the section shows.
|
|
488
|
+
return refresh()
|
|
489
|
+
.then(function () { return value; })
|
|
490
|
+
.catch(function () {
|
|
491
|
+
// The read-back failed; the answer of the action
|
|
492
|
+
// still carries a view, so use it.
|
|
493
|
+
if (value && value.view) setView(value.view);
|
|
494
|
+
return value;
|
|
495
|
+
});
|
|
386
496
|
});
|
|
387
497
|
}
|
|
388
498
|
|
|
@@ -405,7 +515,7 @@
|
|
|
405
515
|
}
|
|
406
516
|
|
|
407
517
|
function addAccount() {
|
|
408
|
-
|
|
518
|
+
setBusy(true);
|
|
409
519
|
save({
|
|
410
520
|
action: 'save',
|
|
411
521
|
account: {
|
|
@@ -425,7 +535,7 @@
|
|
|
425
535
|
setNotice({ message: t('added'), ok: true });
|
|
426
536
|
})
|
|
427
537
|
.catch(function (error) { setNotice({ message: error.message, ok: false }); })
|
|
428
|
-
.finally(function () {
|
|
538
|
+
.finally(function () { setBusy(false); });
|
|
429
539
|
}
|
|
430
540
|
|
|
431
541
|
var accounts = (view && view.accounts) || [];
|
|
@@ -463,7 +573,7 @@
|
|
|
463
573
|
className: 'mgsec_input',
|
|
464
574
|
value: draft.label,
|
|
465
575
|
placeholder: 'Bold & Digital',
|
|
466
|
-
onChange: function (event) {
|
|
576
|
+
onChange: function (event) { patch('label', event.target.value); },
|
|
467
577
|
}),
|
|
468
578
|
),
|
|
469
579
|
h('div', { className: 'mgsec_grid' },
|
|
@@ -473,7 +583,7 @@
|
|
|
473
583
|
className: 'mgsec_input',
|
|
474
584
|
value: draft.domain,
|
|
475
585
|
placeholder: 'mg.example.com',
|
|
476
|
-
onChange: function (event) {
|
|
586
|
+
onChange: function (event) { patch('domain', event.target.value); },
|
|
477
587
|
}),
|
|
478
588
|
),
|
|
479
589
|
h('div', { className: 'mgsec_row' },
|
|
@@ -481,7 +591,7 @@
|
|
|
481
591
|
h('select', {
|
|
482
592
|
className: 'mgsec_input',
|
|
483
593
|
value: draft.region,
|
|
484
|
-
onChange: function (event) {
|
|
594
|
+
onChange: function (event) { patch('region', event.target.value); },
|
|
485
595
|
}, ['us', 'eu'].map(function (value) {
|
|
486
596
|
return h('option', { key: value, value: value }, t(value === 'eu' ? 'regionEu' : 'regionUs'));
|
|
487
597
|
})),
|
|
@@ -493,7 +603,7 @@
|
|
|
493
603
|
className: 'mgsec_input',
|
|
494
604
|
value: draft.from,
|
|
495
605
|
placeholder: 'noreply@example.com',
|
|
496
|
-
onChange: function (event) {
|
|
606
|
+
onChange: function (event) { patch('from', event.target.value); },
|
|
497
607
|
}),
|
|
498
608
|
),
|
|
499
609
|
h('div', { className: 'mgsec_row' },
|
|
@@ -502,7 +612,7 @@
|
|
|
502
612
|
className: 'mgsec_input',
|
|
503
613
|
value: draft.senderName,
|
|
504
614
|
placeholder: 'Bold & Digital',
|
|
505
|
-
onChange: function (event) {
|
|
615
|
+
onChange: function (event) { patch('senderName', event.target.value); },
|
|
506
616
|
}),
|
|
507
617
|
),
|
|
508
618
|
h('div', { className: 'mgsec_row' },
|
|
@@ -512,17 +622,17 @@
|
|
|
512
622
|
type: 'password',
|
|
513
623
|
value: draft.apiKey,
|
|
514
624
|
placeholder: 'key-…',
|
|
515
|
-
onChange: function (event) {
|
|
625
|
+
onChange: function (event) { patch('apiKey', event.target.value); },
|
|
516
626
|
}),
|
|
517
627
|
),
|
|
518
628
|
h('div', { className: 'mgsec_actions' },
|
|
519
629
|
h('button', { className: 'mgsec_btn mgsec_btn_primary', type: 'button', disabled: busy[0], onClick: addAccount }, t('add')),
|
|
520
|
-
h('button', { className: 'mgsec_btn', type: 'button', onClick: function () {
|
|
630
|
+
h('button', { className: 'mgsec_btn', type: 'button', onClick: function () { setAdding(false); } }, t('cancel')),
|
|
521
631
|
),
|
|
522
632
|
h('p', { className: 'mgsec_empty' }, t('addMore')),
|
|
523
633
|
)
|
|
524
634
|
: h('div', { className: 'mgsec_actions' },
|
|
525
|
-
h('button', { className: 'mgsec_btn', type: 'button', onClick: function () {
|
|
635
|
+
h('button', { className: 'mgsec_btn', type: 'button', onClick: function () { setAdding(true); } }, t('addAccount')),
|
|
526
636
|
),
|
|
527
637
|
);
|
|
528
638
|
}
|
package/lib/settings-web.js
CHANGED
|
@@ -92,7 +92,17 @@ export class MailgunSettingsBackend {
|
|
|
92
92
|
async save(payload) {
|
|
93
93
|
const source = await this.store.read();
|
|
94
94
|
const existing = Array.isArray(source.accounts) ? source.accounts : [];
|
|
95
|
-
const
|
|
95
|
+
const raw = payload?.account ?? {};
|
|
96
|
+
let row = cleanAccount(raw);
|
|
97
|
+
// A request that names an existing account and nothing else — the
|
|
98
|
+
// "Make default" action sends only the id — must not blank the
|
|
99
|
+
// account it points at. The stored row is the base; the fields the
|
|
100
|
+
// browser did send win.
|
|
101
|
+
const named = existing.find((account) => account.id === row.id);
|
|
102
|
+
if (named !== undefined
|
|
103
|
+
&& row.label === '' && row.domain === '' && row.from === '' && row.senderName === '') {
|
|
104
|
+
row = { ...named, id: row.id };
|
|
105
|
+
}
|
|
96
106
|
if (row.label === '' && row.domain === '') {
|
|
97
107
|
throw new Error('An account needs at least a label or a sending domain.');
|
|
98
108
|
}
|
|
@@ -236,7 +246,15 @@ export class MailgunSettingsBackend {
|
|
|
236
246
|
*/
|
|
237
247
|
async fetch(request) {
|
|
238
248
|
if (request.method === 'GET') {
|
|
239
|
-
|
|
249
|
+
// The same envelope as the POST and as the raw route: the browser
|
|
250
|
+
// reads one shape everywhere. A bare payload here made the client
|
|
251
|
+
// treat the answer as a failure, so the account list stayed empty.
|
|
252
|
+
try {
|
|
253
|
+
return this.json({ ok: true, value: await this.view() });
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
return this.json({ ok: false, error: { message: error instanceof Error ? error.message : String(error) } }, 503);
|
|
257
|
+
}
|
|
240
258
|
}
|
|
241
259
|
if (request.method !== 'POST') {
|
|
242
260
|
return this.json({ ok: false, error: { message: 'Use GET or POST' } }, 405);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mailgun",
|
|
3
3
|
"description": "Send email through the Mailgun HTTP API with per-message open and click tracking, read the delivery and engagement events, list the sending domains and query the domain counters.",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.8",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"exports": {
|