emailengine-app 2.76.0 → 2.77.0
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/.github/workflows/release.yaml +24 -19
- package/.ncurc.js +14 -2
- package/.release-please-manifest.json +3 -0
- package/CHANGELOG.md +23 -0
- package/README.md +1 -1
- package/data/google-crawlers.json +1 -1
- package/encrypt.js +71 -4
- package/lib/address-strategies.js +18 -0
- package/lib/api-reference/code-samples.js +194 -0
- package/lib/api-reference/format.js +179 -0
- package/lib/api-reference/index.js +205 -0
- package/lib/api-reference/model.js +336 -0
- package/lib/api-reference/schema-tree.js +366 -0
- package/lib/api-routes/account-routes.js +117 -37
- package/lib/api-routes/behavior-notes.js +54 -0
- package/lib/api-routes/blocklist-routes.js +9 -9
- package/lib/api-routes/changes-routes.js +6 -4
- package/lib/api-routes/chat-routes.js +3 -3
- package/lib/api-routes/delivery-test-routes.js +24 -6
- package/lib/api-routes/export-routes.js +14 -12
- package/lib/api-routes/gateway-routes.js +11 -11
- package/lib/api-routes/license-routes.js +7 -7
- package/lib/api-routes/mailbox-routes.js +18 -9
- package/lib/api-routes/message-routes.js +55 -35
- package/lib/api-routes/oauth2-app-routes.js +22 -16
- package/lib/api-routes/outbox-routes.js +7 -7
- package/lib/api-routes/pubsub-routes.js +3 -3
- package/lib/api-routes/settings-routes.js +10 -9
- package/lib/api-routes/stats-routes.js +3 -3
- package/lib/api-routes/submit-routes.js +58 -8
- package/lib/api-routes/template-routes.js +13 -13
- package/lib/api-routes/token-routes.js +15 -10
- package/lib/api-routes/webhook-route-routes.js +5 -5
- package/lib/consts.js +7 -0
- package/lib/email-client/base-client.js +66 -12
- package/lib/enum-descriptions.js +130 -0
- package/lib/imapproxy/imap-server.js +30 -26
- package/lib/imapproxy/upstream-client.js +38 -0
- package/lib/oauth2-apps.js +9 -3
- package/lib/openapi/build-document.js +318 -0
- package/lib/openapi/index.js +95 -0
- package/lib/openapi/joi-schema.js +523 -0
- package/lib/routes-ui.js +5 -1
- package/lib/schemas.js +76 -17
- package/lib/swagger-options.js +173 -0
- package/lib/tokens.js +69 -5
- package/lib/ui-routes/admin-entities-routes.js +3 -0
- package/lib/ui-routes/dashboard-routes.js +2 -26
- package/lib/ui-routes/reference-routes.js +195 -0
- package/lib/ui-routes/route-helpers.js +11 -0
- package/package.json +9 -11
- package/prepare-bundle-deps.sh +28 -0
- package/release-please-config.json +11 -0
- package/sbom.json +0 -1
- package/server.js +10 -2
- package/static/css/flyonui.css +1 -1
- package/static/css/src/app.tailwind.css +90 -0
- package/static/js/ee-client.js +238 -8
- package/static/js/reference.js +736 -0
- package/static/js/ui.js +61 -33
- package/static/licenses.html +4 -4
- package/translations/messages.pot +27 -27
- package/views/config/ai.hbs +1 -1
- package/views/config/document-store/chat.hbs +5 -5
- package/views/config/document-store/index.hbs +2 -2
- package/views/config/document-store/pre-processing/index.hbs +1 -1
- package/views/config/email-processing.hbs +1 -1
- package/views/config/imap-proxy.hbs +1 -1
- package/views/config/license.hbs +1 -1
- package/views/config/oauth/index.hbs +3 -3
- package/views/config/security.hbs +1 -1
- package/views/config/smtp.hbs +2 -2
- package/views/dashboard.hbs +1 -1
- package/views/layout/app.hbs +18 -7
- package/views/partials/oauth_form.hbs +4 -4
- package/views/partials/reference/anchor-link.hbs +20 -0
- package/views/partials/reference/disclosure.hbs +37 -0
- package/views/partials/reference/nav.hbs +93 -0
- package/views/partials/reference/operation.hbs +127 -0
- package/views/partials/reference/params.hbs +20 -0
- package/views/partials/reference/schema-node.hbs +99 -0
- package/views/partials/reference/schema.hbs +19 -0
- package/views/partials/reference/section-heading.hbs +16 -0
- package/views/partials/reference/token-alert.hbs +24 -0
- package/views/partials/reference/try-field.hbs +31 -0
- package/views/partials/reference/try-it.hbs +78 -0
- package/views/partials/side_menu.hbs +1 -1
- package/views/partials/ui/badge.hbs +4 -2
- package/views/partials/ui/page-header.hbs +1 -1
- package/views/partials/ui/search-input.hbs +31 -0
- package/views/partials/ui/setting-ref.hbs +1 -1
- package/views/partials/ui/stat-card.hbs +4 -2
- package/views/partials/ui/tab.hbs +6 -1
- package/views/partials/ui/tabs.hbs +2 -2
- package/views/reference/index.hbs +76 -0
- package/views/reference/redirect.hbs +49 -0
- package/views/reference/tag.hbs +28 -0
- package/views/reference/token.hbs +49 -0
- package/views/tokens/index.hbs +9 -1
- package/views/webhooks/index.hbs +3 -3
- package/workers/api.js +37 -215
- package/views/swagger/index.hbs +0 -82
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Client behavior for the server-rendered API reference (/admin/reference).
|
|
4
|
+
//
|
|
5
|
+
// The page arrives as finished HTML - operations, schemas, examples and code samples
|
|
6
|
+
// are all rendered by lib/api-reference/ on the server. Nothing here builds page
|
|
7
|
+
// content; this file only adds what genuinely needs a browser: deferred syntax
|
|
8
|
+
// highlighting, the sidebar filter and its keyboard handling, opening the collapsed
|
|
9
|
+
// groups a property deep link lands in, remembering the reader's code sample language,
|
|
10
|
+
// and the try-it request runner.
|
|
11
|
+
|
|
12
|
+
(() => {
|
|
13
|
+
// Responses larger than this are truncated before being inserted into the DOM.
|
|
14
|
+
// A message export or a full settings dump can be megabytes, and highlighting
|
|
15
|
+
// that much text locks up the tab.
|
|
16
|
+
const MAX_RESPONSE_CHARS = 200000;
|
|
17
|
+
|
|
18
|
+
// Shape of the curl command copied from a try-it panel. Both match
|
|
19
|
+
// lib/api-reference/code-samples.js, so a copied command is indistinguishable from the
|
|
20
|
+
// generated curl sample above the same operation.
|
|
21
|
+
const TOKEN_ENV_VAR = 'EMAILENGINE_TOKEN';
|
|
22
|
+
const INDENT = ' ';
|
|
23
|
+
|
|
24
|
+
// The try-it token, shared by every reference page.
|
|
25
|
+
//
|
|
26
|
+
// sessionStorage, not localStorage: the point is to survive navigation between the
|
|
27
|
+
// overview and the group pages, which it does, while the credential still dies with
|
|
28
|
+
// the tab. A minted token only lives an hour anyway, and an access token in permanent
|
|
29
|
+
// storage is an exfiltration target for any future XSS on the admin UI.
|
|
30
|
+
const TOKEN_KEY = 'eeRefToken';
|
|
31
|
+
|
|
32
|
+
const tokenStore = {
|
|
33
|
+
read() {
|
|
34
|
+
let raw;
|
|
35
|
+
try {
|
|
36
|
+
raw = window.sessionStorage.getItem(TOKEN_KEY);
|
|
37
|
+
} catch (err) {
|
|
38
|
+
return null; // storage blocked (private mode, cookie policy)
|
|
39
|
+
}
|
|
40
|
+
if (!raw) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let entry;
|
|
45
|
+
try {
|
|
46
|
+
entry = JSON.parse(raw);
|
|
47
|
+
} catch (err) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!entry || !entry.token || (entry.expires && Date.now() >= entry.expires)) {
|
|
52
|
+
tokenStore.clear();
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return entry;
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
write(token, expires) {
|
|
59
|
+
try {
|
|
60
|
+
// `expires` doubles as the "this page minted it" marker: only the mint path
|
|
61
|
+
// sets one, so a pasted token is exactly an entry without it
|
|
62
|
+
window.sessionStorage.setItem(TOKEN_KEY, JSON.stringify({ token, expires: expires || null }));
|
|
63
|
+
} catch (err) {
|
|
64
|
+
// storage blocked - the token still works for this page load
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
clear() {
|
|
69
|
+
try {
|
|
70
|
+
window.sessionStorage.removeItem(TOKEN_KEY);
|
|
71
|
+
} catch (err) {
|
|
72
|
+
// nothing to do
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const minutesLeft = expires => Math.max(0, Math.round((expires - Date.now()) / 60000));
|
|
78
|
+
|
|
79
|
+
// Repaints the always-visible status in the sidebar, and shows the Clear control on
|
|
80
|
+
// the overview page only while there is something to clear.
|
|
81
|
+
const paintTokenStatus = () => {
|
|
82
|
+
const entry = tokenStore.read();
|
|
83
|
+
|
|
84
|
+
const state = document.getElementById('ref-token-state');
|
|
85
|
+
if (state) {
|
|
86
|
+
state.textContent = entry ? (entry.expires ? `${minutesLeft(entry.expires)} min left` : 'Active') : 'Not set';
|
|
87
|
+
state.className = entry ? 'text-success text-2xs' : 'text-base-content/50 text-2xs';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const clearWrap = document.getElementById('ref-token-clear-wrap');
|
|
91
|
+
if (clearWrap) {
|
|
92
|
+
clearWrap.classList.toggle('hidden', !entry);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The "no token set" banner. It is only in the DOM when a token is actually
|
|
96
|
+
// required (reference/token-alert.hbs skips it when `disableTokens` is on), so
|
|
97
|
+
// its absence means there is nothing to warn about.
|
|
98
|
+
const alert = document.getElementById('ref-token-alert');
|
|
99
|
+
if (alert) {
|
|
100
|
+
alert.classList.toggle('hidden', !!entry);
|
|
101
|
+
alert.classList.toggle('flex', !entry);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// A tag page carries up to ~150 code blocks, several of them tens of kilobytes,
|
|
106
|
+
// so highlighting all of them up front stalls the page for seconds. Each block is
|
|
107
|
+
// highlighted the first time it becomes visible instead. That also covers blocks
|
|
108
|
+
// inside collapsed <details> and inactive tab panels for free: those have no
|
|
109
|
+
// layout box until they open, so the observer fires exactly then.
|
|
110
|
+
const highlightBlock = block => {
|
|
111
|
+
if (block.dataset.eeHighlighted) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
block.dataset.eeHighlighted = '1';
|
|
115
|
+
if (window.hljs) {
|
|
116
|
+
window.hljs.highlightElement(block);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const initHighlighting = () => {
|
|
121
|
+
const blocks = document.querySelectorAll('pre code[class*="language-"]');
|
|
122
|
+
if (!blocks.length) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const observer = new IntersectionObserver(
|
|
127
|
+
entries => {
|
|
128
|
+
for (const entry of entries) {
|
|
129
|
+
if (entry.isIntersecting) {
|
|
130
|
+
highlightBlock(entry.target);
|
|
131
|
+
observer.unobserve(entry.target);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
{ rootMargin: '200px' }
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
blocks.forEach(block => observer.observe(block));
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// FlyonUI binds its component autoInit to the window `load` event, not DOMContentLoaded.
|
|
142
|
+
// Anything that drives a tab has to wait for that: a click dispatched earlier lands on a
|
|
143
|
+
// button whose handler is not attached yet and silently does nothing, which is exactly
|
|
144
|
+
// how it fails - no error, the tab just stays where it was.
|
|
145
|
+
const whenComponentsReady = fn => {
|
|
146
|
+
if (document.readyState === 'complete') {
|
|
147
|
+
fn();
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
window.addEventListener('load', fn);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// Deep links point at a single schema property ("#accountCreate.body.imap.tls"), which
|
|
154
|
+
// can sit inside collapsed <details> groups and an inactive response tab - neither of
|
|
155
|
+
// which the browser can scroll to, because they have no layout box. Everything on the
|
|
156
|
+
// way down is opened first, then the row is scrolled into view. The highlight itself is
|
|
157
|
+
// :target CSS and needs nothing here.
|
|
158
|
+
const revealTarget = () => {
|
|
159
|
+
// No decoding: every anchor the model emits is [A-Za-z0-9_.-], so the fragment and
|
|
160
|
+
// the id are the same string, and anything else simply does not match an element
|
|
161
|
+
const target = document.getElementById(window.location.hash.slice(1));
|
|
162
|
+
if (!target) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
for (let element = target.parentElement; element; element = element.parentElement) {
|
|
167
|
+
if (element.tagName === 'DETAILS') {
|
|
168
|
+
element.open = true;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Response and code-sample panels are plain hidden divs driven by the FlyonUI
|
|
172
|
+
// tab plugin, so the tab button is what has to be activated. The panel already
|
|
173
|
+
// names its button in aria-labelledby, so that is read rather than recomposing
|
|
174
|
+
// ui/tab's "<target>-tab" id convention here.
|
|
175
|
+
if (element.getAttribute('role') === 'tabpanel' && element.classList.contains('hidden')) {
|
|
176
|
+
const tab = document.getElementById(element.getAttribute('aria-labelledby') || '');
|
|
177
|
+
if (tab) {
|
|
178
|
+
tab.click();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
target.scrollIntoView({ block: 'start' });
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// Resolved once - the platform cannot change, and the shortcut check runs on every
|
|
187
|
+
// keystroke anywhere on the page
|
|
188
|
+
const IS_MAC = /mac/i.test((navigator.userAgentData && navigator.userAgentData.platform) || navigator.platform || '');
|
|
189
|
+
|
|
190
|
+
// True while the caret is somewhere the user is typing, so a bare-letter shortcut
|
|
191
|
+
// does not swallow the keystroke.
|
|
192
|
+
const isEditable = element => !!element && (element.isContentEditable || /^(input|textarea|select)$/i.test(element.tagName));
|
|
193
|
+
|
|
194
|
+
// Sidebar filter. Every operation is already in the DOM (hidden until the box has
|
|
195
|
+
// input), so narrowing the list is plain matching - there is no index to fetch.
|
|
196
|
+
// A hit matches on more than it shows: reference/nav.hbs puts the operation id and
|
|
197
|
+
// every parameter and request-body property name in data-search, so "mailbox" finds
|
|
198
|
+
// the endpoints that accept one, not just the ones with the word in their path.
|
|
199
|
+
//
|
|
200
|
+
// Keyboard navigation moves real DOM focus between the result links rather than
|
|
201
|
+
// tracking a selected index behind aria-activedescendant. The results are already
|
|
202
|
+
// links, so focus gives Enter-to-open, the browser's own focus ring and a correct
|
|
203
|
+
// screen reader announcement for free, without claiming this is a listbox when the
|
|
204
|
+
// markup is a list of links.
|
|
205
|
+
const initFilter = () => {
|
|
206
|
+
const input = document.getElementById('ref-filter');
|
|
207
|
+
const results = document.getElementById('ref-filter-results');
|
|
208
|
+
const tagList = document.getElementById('ref-tag-list');
|
|
209
|
+
const empty = document.getElementById('ref-filter-empty');
|
|
210
|
+
const hint = document.getElementById('ref-filter-hint');
|
|
211
|
+
|
|
212
|
+
if (!input || !results || !tagList || !hint) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const count = hint.querySelector('[role="status"]');
|
|
217
|
+
|
|
218
|
+
// Resolved once: 82 rows, and the arrow keys would otherwise re-query the DOM for
|
|
219
|
+
// every one of them on every keypress while a key is held down
|
|
220
|
+
const hits = Array.from(results.querySelectorAll('.ee-ref-hit')).map(el => ({
|
|
221
|
+
el,
|
|
222
|
+
link: el.querySelector('a'),
|
|
223
|
+
text: (el.dataset.search || '').toLowerCase()
|
|
224
|
+
}));
|
|
225
|
+
|
|
226
|
+
const visibleLinks = () => hits.filter(hit => !hit.el.classList.contains('hidden')).map(hit => hit.link);
|
|
227
|
+
|
|
228
|
+
const apply = () => {
|
|
229
|
+
const terms = input.value.trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
230
|
+
const filtering = terms.length > 0;
|
|
231
|
+
|
|
232
|
+
tagList.classList.toggle('hidden', filtering);
|
|
233
|
+
results.classList.toggle('hidden', !filtering);
|
|
234
|
+
hint.classList.toggle('hidden', !filtering);
|
|
235
|
+
hint.classList.toggle('flex', filtering);
|
|
236
|
+
|
|
237
|
+
if (!filtering) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
let visible = 0;
|
|
242
|
+
for (const hit of hits) {
|
|
243
|
+
const match = terms.every(term => hit.text.includes(term));
|
|
244
|
+
hit.el.classList.toggle('hidden', !match);
|
|
245
|
+
if (match) {
|
|
246
|
+
visible++;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
empty.classList.toggle('hidden', visible > 0);
|
|
251
|
+
count.textContent = `${visible} ${visible === 1 ? 'endpoint' : 'endpoints'}`;
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// The input and the visible results form a single cycle, so moving up from the
|
|
255
|
+
// first result - or down past the last - lands back in the field instead of
|
|
256
|
+
// trapping focus in the list.
|
|
257
|
+
const move = offset => {
|
|
258
|
+
const links = visibleLinks();
|
|
259
|
+
if (!links.length) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const current = links.indexOf(document.activeElement);
|
|
264
|
+
if (current < 0) {
|
|
265
|
+
// focus is in the field: Down enters at the top, Up at the bottom
|
|
266
|
+
links[offset > 0 ? 0 : links.length - 1].focus();
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const next = current + offset;
|
|
271
|
+
if (next < 0 || next >= links.length) {
|
|
272
|
+
input.focus();
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
links[next].focus();
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
const reset = () => {
|
|
280
|
+
input.value = '';
|
|
281
|
+
apply();
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// Shared by both listeners below - only their Escape behavior genuinely differs
|
|
285
|
+
const arrowNav = event => {
|
|
286
|
+
if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp') {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
event.preventDefault();
|
|
290
|
+
move(event.key === 'ArrowDown' ? 1 : -1);
|
|
291
|
+
return true;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
input.addEventListener('input', apply);
|
|
295
|
+
|
|
296
|
+
input.addEventListener('keydown', event => {
|
|
297
|
+
if (arrowNav(event)) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (event.key === 'Enter') {
|
|
302
|
+
// Typing and pressing Enter opens the top hit, so the common case never
|
|
303
|
+
// needs the arrow keys
|
|
304
|
+
const [first] = visibleLinks();
|
|
305
|
+
if (first) {
|
|
306
|
+
event.preventDefault();
|
|
307
|
+
first.click();
|
|
308
|
+
}
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (event.key === 'Escape') {
|
|
313
|
+
if (input.value) {
|
|
314
|
+
event.preventDefault();
|
|
315
|
+
reset();
|
|
316
|
+
} else {
|
|
317
|
+
input.blur();
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
results.addEventListener('keydown', event => {
|
|
323
|
+
if (arrowNav(event)) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (event.key === 'Escape') {
|
|
328
|
+
event.preventDefault();
|
|
329
|
+
input.focus();
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
// Focus shortcuts. Ctrl+K (Cmd+K on macOS) is the common one; "/" matches what
|
|
334
|
+
// most documentation sites bind, and is ignored while the caret is in a field so
|
|
335
|
+
// it can still be typed into the filter itself.
|
|
336
|
+
window.addEventListener('keydown', event => {
|
|
337
|
+
const hotkey = (IS_MAC ? event.metaKey : event.ctrlKey) && !event.altKey && event.key.toLowerCase() === 'k';
|
|
338
|
+
const slash = event.key === '/' && !event.metaKey && !event.ctrlKey && !event.altKey && !isEditable(document.activeElement);
|
|
339
|
+
|
|
340
|
+
if (!hotkey && !slash) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
event.preventDefault();
|
|
345
|
+
input.focus();
|
|
346
|
+
input.select();
|
|
347
|
+
});
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
// Code sample language, remembered across operations and page loads.
|
|
351
|
+
//
|
|
352
|
+
// localStorage, not sessionStorage (which is what the try-it token uses): this is a
|
|
353
|
+
// display preference, not a credential, and losing it when the tab closes is exactly
|
|
354
|
+
// the annoyance the setting exists to fix.
|
|
355
|
+
//
|
|
356
|
+
// The stored value is the sample's id (`curl`, `node`, `python`, `custom-0`), carried on
|
|
357
|
+
// the button as data-tab-key. Not the label: that is display text, so rewording one
|
|
358
|
+
// would silently reset everybody's choice, and a hand-written x-codeSamples tab labelled
|
|
359
|
+
// "curl" would take the generated tab's place on operations that have both. An operation
|
|
360
|
+
// that does not offer the remembered id - hand-written samples exist per operation - just
|
|
361
|
+
// keeps its own default rather than landing on an unrelated tab.
|
|
362
|
+
const SAMPLE_LANGUAGE_KEY = 'eeRefSampleLanguage';
|
|
363
|
+
|
|
364
|
+
const initSampleTabs = () => {
|
|
365
|
+
const groups = document.querySelectorAll('[data-sample-tabs]');
|
|
366
|
+
if (!groups.length) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
let preferred;
|
|
371
|
+
try {
|
|
372
|
+
preferred = window.localStorage.getItem(SAMPLE_LANGUAGE_KEY);
|
|
373
|
+
} catch (err) {
|
|
374
|
+
preferred = null;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
whenComponentsReady(() => {
|
|
378
|
+
if (preferred) {
|
|
379
|
+
for (const group of groups) {
|
|
380
|
+
const tab = group.querySelector(`[data-tab-key="${CSS.escape(preferred)}"]`);
|
|
381
|
+
if (tab && tab.getAttribute('aria-selected') !== 'true') {
|
|
382
|
+
tab.click();
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Bound only after the restore pass, so those synthetic clicks do not each
|
|
388
|
+
// rewrite the same value - a 13-operation page would otherwise fire a dozen
|
|
389
|
+
// storage writes on load. Clicks before this point cannot reach a tab anyway:
|
|
390
|
+
// FlyonUI is not initialized yet.
|
|
391
|
+
document.addEventListener('click', event => {
|
|
392
|
+
const tab = event.target.closest('[data-sample-tabs] [role="tab"]');
|
|
393
|
+
if (!tab || !tab.dataset.tabKey) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
try {
|
|
398
|
+
window.localStorage.setItem(SAMPLE_LANGUAGE_KEY, tab.dataset.tabKey);
|
|
399
|
+
} catch (err) {
|
|
400
|
+
// storage blocked - the choice still applies to this page
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
const badgeVariant = status => {
|
|
407
|
+
if (status >= 200 && status < 300) {
|
|
408
|
+
return 'badge-success';
|
|
409
|
+
}
|
|
410
|
+
if (status >= 400 && status < 500) {
|
|
411
|
+
return 'badge-warning';
|
|
412
|
+
}
|
|
413
|
+
if (status >= 500) {
|
|
414
|
+
return 'badge-error';
|
|
415
|
+
}
|
|
416
|
+
return 'badge-neutral';
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
const buildRequest = form => {
|
|
420
|
+
let path = form.dataset.path;
|
|
421
|
+
|
|
422
|
+
for (const input of form.querySelectorAll('[data-in="path"]')) {
|
|
423
|
+
const value = input.value.trim();
|
|
424
|
+
if (!value) {
|
|
425
|
+
throw new Error(`Path parameter "${input.dataset.param}" is required`);
|
|
426
|
+
}
|
|
427
|
+
path = path.replace(`{${input.dataset.param}}`, encodeURIComponent(value));
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const query = new URLSearchParams();
|
|
431
|
+
for (const input of form.querySelectorAll('[data-in="query"]')) {
|
|
432
|
+
const value = input.value.trim();
|
|
433
|
+
if (value) {
|
|
434
|
+
query.append(input.dataset.param, value);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const headers = {};
|
|
439
|
+
for (const input of form.querySelectorAll('[data-in="header"]')) {
|
|
440
|
+
const value = input.value.trim();
|
|
441
|
+
if (value) {
|
|
442
|
+
headers[input.dataset.param] = value;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// The token held for this tab, set once on the overview page. Only ever sent to
|
|
447
|
+
// this instance's own API, by the request the user explicitly triggered.
|
|
448
|
+
const entry = tokenStore.read();
|
|
449
|
+
if (entry) {
|
|
450
|
+
headers.Authorization = `Bearer ${entry.token}`;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
let body;
|
|
454
|
+
const bodyField = form.querySelector('[data-body]');
|
|
455
|
+
if (bodyField && bodyField.value.trim()) {
|
|
456
|
+
try {
|
|
457
|
+
JSON.parse(bodyField.value);
|
|
458
|
+
} catch (err) {
|
|
459
|
+
throw new Error(`Request body is not valid JSON: ${err.message}`);
|
|
460
|
+
}
|
|
461
|
+
body = bodyField.value;
|
|
462
|
+
headers['Content-Type'] = 'application/json';
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const queryString = query.toString();
|
|
466
|
+
|
|
467
|
+
return {
|
|
468
|
+
url: path + (queryString ? `?${queryString}` : ''),
|
|
469
|
+
method: (form.dataset.method || 'get').toUpperCase(),
|
|
470
|
+
headers,
|
|
471
|
+
body
|
|
472
|
+
};
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
const renderResponse = async (form, response, elapsed) => {
|
|
476
|
+
const result = form.querySelector('.ee-ref-try-result');
|
|
477
|
+
const codeBadge = form.querySelector('.ee-ref-try-code');
|
|
478
|
+
const timeEl = form.querySelector('.ee-ref-try-time');
|
|
479
|
+
const bodyEl = form.querySelector('.ee-ref-try-body');
|
|
480
|
+
|
|
481
|
+
codeBadge.className = `ee-ref-try-code badge badge-sm ${badgeVariant(response.status)}`;
|
|
482
|
+
codeBadge.textContent = `${response.status} ${response.statusText}`.trim();
|
|
483
|
+
|
|
484
|
+
const contentType = response.headers.get('content-type') || '';
|
|
485
|
+
timeEl.textContent = `${elapsed} ms${contentType ? ` - ${contentType.split(';')[0]}` : ''}`;
|
|
486
|
+
|
|
487
|
+
let text = await response.text();
|
|
488
|
+
let truncated = false;
|
|
489
|
+
if (text.length > MAX_RESPONSE_CHARS) {
|
|
490
|
+
text = text.slice(0, MAX_RESPONSE_CHARS);
|
|
491
|
+
truncated = true;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (contentType.includes('json')) {
|
|
495
|
+
try {
|
|
496
|
+
text = JSON.stringify(JSON.parse(text), null, 2);
|
|
497
|
+
} catch (err) {
|
|
498
|
+
// a truncated or non-conforming body is shown as received
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (truncated) {
|
|
503
|
+
text += '\n\n... response truncated for display';
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// textContent, never innerHTML: the body is whatever the API returned
|
|
507
|
+
bodyEl.textContent = text;
|
|
508
|
+
delete bodyEl.dataset.eeHighlighted;
|
|
509
|
+
bodyEl.removeAttribute('data-highlighted');
|
|
510
|
+
result.classList.remove('hidden');
|
|
511
|
+
highlightBlock(bodyEl);
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
// Single-quoted shell strings cannot contain a single quote, so each one closes the
|
|
515
|
+
// string, adds an escaped quote and reopens it. Mirrors shellQuote() in
|
|
516
|
+
// lib/api-reference/code-samples.js, which does the same for the generated samples.
|
|
517
|
+
const shellQuote = value => `'${String(value).replace(/'/g, `'\\''`)}'`;
|
|
518
|
+
|
|
519
|
+
// Serializes the request the form would actually send as a runnable curl command. The
|
|
520
|
+
// generated samples above an operation come from its schema examples, so they drift
|
|
521
|
+
// from the form as soon as anything is edited; this is built from the same
|
|
522
|
+
// buildRequest() the Send button uses, so the two cannot disagree.
|
|
523
|
+
//
|
|
524
|
+
// The access token is replaced with the $EMAILENGINE_TOKEN placeholder the generated
|
|
525
|
+
// samples read, rather than the token held for this tab. That one is usually a
|
|
526
|
+
// throwaway minted for the reference page, so pasting it into a snippet produces a
|
|
527
|
+
// command that stops working within the hour - and a live credential should not be
|
|
528
|
+
// the thing that ends up in a ticket or a chat message.
|
|
529
|
+
const curlForRequest = request => {
|
|
530
|
+
const lines = [
|
|
531
|
+
`curl -X ${request.method} ${shellQuote(window.location.origin + request.url)}`,
|
|
532
|
+
// double quotes, so the shell expands the variable
|
|
533
|
+
`${INDENT}-H "Authorization: Bearer $${TOKEN_ENV_VAR}"`
|
|
534
|
+
];
|
|
535
|
+
|
|
536
|
+
for (const [name, value] of Object.entries(request.headers)) {
|
|
537
|
+
if (name !== 'Authorization') {
|
|
538
|
+
lines.push(`${INDENT}-H ${shellQuote(`${name}: ${value}`)}`);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
if (request.body) {
|
|
543
|
+
lines.push(`${INDENT}-d ${shellQuote(request.body)}`);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
return lines.join(' \\\n');
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
// Both entry points below - send and copy-as-curl - read the same form and report the
|
|
550
|
+
// same validation failures in the same place, so they share the preamble. Returns null
|
|
551
|
+
// when the form is not ready to be turned into a request, having said why.
|
|
552
|
+
const prepareRequest = form => {
|
|
553
|
+
const status = form.querySelector('.ee-ref-try-status');
|
|
554
|
+
status.textContent = '';
|
|
555
|
+
|
|
556
|
+
try {
|
|
557
|
+
return buildRequest(form);
|
|
558
|
+
} catch (err) {
|
|
559
|
+
status.textContent = err.message;
|
|
560
|
+
return null;
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
// One delegated listener for every try-it form on the page, matching how
|
|
565
|
+
// static/js/ui.js handles the copy buttons.
|
|
566
|
+
const initTryIt = () => {
|
|
567
|
+
document.addEventListener('click', event => {
|
|
568
|
+
const button = event.target.closest('.ee-ref-try-curl');
|
|
569
|
+
if (!button) {
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
const request = prepareRequest(button.closest('.ee-ref-try'));
|
|
574
|
+
if (request) {
|
|
575
|
+
window.uiCopyText(curlForRequest(request), button);
|
|
576
|
+
}
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
document.addEventListener('submit', async event => {
|
|
580
|
+
const form = event.target.closest('.ee-ref-try');
|
|
581
|
+
if (!form) {
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
event.preventDefault();
|
|
586
|
+
|
|
587
|
+
const request = prepareRequest(form);
|
|
588
|
+
if (!request) {
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
const status = form.querySelector('.ee-ref-try-status');
|
|
593
|
+
const button = form.querySelector('button[type="submit"]');
|
|
594
|
+
|
|
595
|
+
window.uiButtonBusy(button, true);
|
|
596
|
+
const started = performance.now();
|
|
597
|
+
|
|
598
|
+
try {
|
|
599
|
+
const response = await fetch(request.url, {
|
|
600
|
+
method: request.method,
|
|
601
|
+
headers: request.headers,
|
|
602
|
+
body: request.body
|
|
603
|
+
});
|
|
604
|
+
await renderResponse(form, response, Math.round(performance.now() - started));
|
|
605
|
+
} catch (err) {
|
|
606
|
+
status.textContent = `Request failed: ${err.message}`;
|
|
607
|
+
} finally {
|
|
608
|
+
window.uiButtonBusy(button, false);
|
|
609
|
+
}
|
|
610
|
+
});
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
// Feedback line on the access-token page; elsewhere (the "no token set" banner) there
|
|
614
|
+
// is no such element and a toast carries the message instead.
|
|
615
|
+
const say = (message, ok) => {
|
|
616
|
+
const feedback = document.getElementById('ref-token-feedback');
|
|
617
|
+
if (feedback) {
|
|
618
|
+
feedback.textContent = message;
|
|
619
|
+
feedback.className = ok ? 'text-success text-sm' : 'text-base-content/70 text-sm';
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
window.showToast(message, ok ? 'check-circle' : 'alert-triangle');
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
const revokeMinted = async token => {
|
|
626
|
+
try {
|
|
627
|
+
await window.uiPostJson('/admin/reference/token/revoke', { token });
|
|
628
|
+
return true;
|
|
629
|
+
} catch (err) {
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
const activate = async (token, expires, message) => {
|
|
635
|
+
// Replacing a minted token would otherwise leave it working in Redis for the rest of
|
|
636
|
+
// its hour with nothing pointing at it
|
|
637
|
+
const previous = tokenStore.read();
|
|
638
|
+
if (previous && previous.expires && previous.token !== token) {
|
|
639
|
+
await revokeMinted(previous.token);
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
tokenStore.write(token, expires);
|
|
643
|
+
const field = document.getElementById('ref-access-token');
|
|
644
|
+
if (field) {
|
|
645
|
+
field.value = '';
|
|
646
|
+
}
|
|
647
|
+
paintTokenStatus();
|
|
648
|
+
say(message, true);
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
// Minting is reachable from the access-token page AND from the banner on every other
|
|
652
|
+
// reference page, so it is bound by class through one delegated listener rather than
|
|
653
|
+
// by id on a single button.
|
|
654
|
+
const initMint = () => {
|
|
655
|
+
document.addEventListener('click', async event => {
|
|
656
|
+
const btn = event.target.closest('.ref-token-mint');
|
|
657
|
+
if (!btn) {
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
event.preventDefault();
|
|
662
|
+
window.uiButtonBusy(btn, true);
|
|
663
|
+
|
|
664
|
+
try {
|
|
665
|
+
const data = await window.uiPostJson('/admin/reference/token');
|
|
666
|
+
if (!data || !data.success) {
|
|
667
|
+
throw new Error((data && data.message) || 'Request failed');
|
|
668
|
+
}
|
|
669
|
+
await activate(data.token, data.expires, `Temporary token minted, valid for ${minutesLeft(data.expires)} minutes.`);
|
|
670
|
+
} catch (err) {
|
|
671
|
+
say(`Could not mint a token: ${err.message}`, false);
|
|
672
|
+
} finally {
|
|
673
|
+
window.uiButtonBusy(btn, false);
|
|
674
|
+
}
|
|
675
|
+
});
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
// The paste/clear controls exist only on the access-token page.
|
|
679
|
+
const initTokenForm = () => {
|
|
680
|
+
const field = document.getElementById('ref-access-token');
|
|
681
|
+
if (!field) {
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// Reflect what is already held for this tab
|
|
686
|
+
const existing = tokenStore.read();
|
|
687
|
+
if (existing) {
|
|
688
|
+
say(existing.expires ? `A temporary token is active, ${minutesLeft(existing.expires)} minutes left.` : 'A token is active.', true);
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
document.getElementById('ref-token-set').addEventListener('click', () => {
|
|
692
|
+
const token = field.value.trim();
|
|
693
|
+
if (!token) {
|
|
694
|
+
say('Paste a token first.', false);
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
activate(token, null, 'Token set for this tab.').catch(err => say(err.message, false));
|
|
698
|
+
});
|
|
699
|
+
|
|
700
|
+
document.getElementById('ref-token-clear').addEventListener('click', async () => {
|
|
701
|
+
const entry = tokenStore.read();
|
|
702
|
+
|
|
703
|
+
// Drop it locally first, so the field is cleared even if the revoke call fails
|
|
704
|
+
tokenStore.clear();
|
|
705
|
+
field.value = '';
|
|
706
|
+
paintTokenStatus();
|
|
707
|
+
|
|
708
|
+
// A token this page minted is deleted for real rather than left working in
|
|
709
|
+
// Redis until it expires. A pasted token belongs to the user - only forget it.
|
|
710
|
+
if (!entry || !entry.expires) {
|
|
711
|
+
say('Token cleared.', false);
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
say((await revokeMinted(entry.token)) ? 'Temporary token cleared and revoked.' : 'Token cleared here, but revoking it on the server failed.', false);
|
|
716
|
+
});
|
|
717
|
+
};
|
|
718
|
+
|
|
719
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
720
|
+
initHighlighting();
|
|
721
|
+
initFilter();
|
|
722
|
+
initSampleTabs();
|
|
723
|
+
initTryIt();
|
|
724
|
+
initMint();
|
|
725
|
+
initTokenForm();
|
|
726
|
+
paintTokenStatus();
|
|
727
|
+
|
|
728
|
+
// On load rather than here: a target inside an inactive response tab needs the tab
|
|
729
|
+
// switched, which needs FlyonUI
|
|
730
|
+
whenComponentsReady(revealTarget);
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
// Same-page property links only change the fragment, so the load-time pass does not
|
|
734
|
+
// run for them
|
|
735
|
+
window.addEventListener('hashchange', revealTarget);
|
|
736
|
+
})();
|