emailengine-app 2.76.0 → 2.78.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/.github/workflows/test.yml +80 -0
- package/.ncurc.js +14 -2
- package/.release-please-manifest.json +3 -0
- package/CHANGELOG.md +54 -0
- package/README.md +1 -1
- package/bin/emailengine.js +1 -1
- package/data/google-crawlers.json +1 -1
- package/encrypt.js +72 -5
- package/lib/account/expected-identity.js +88 -0
- package/lib/account.js +75 -26
- package/lib/address-strategies.js +18 -0
- package/lib/api-reference/code-samples.js +194 -0
- package/lib/api-reference/format.js +193 -0
- package/lib/api-reference/index.js +222 -0
- package/lib/api-reference/model.js +338 -0
- package/lib/api-reference/schema-tree.js +366 -0
- package/lib/api-routes/account-routes.js +136 -39
- package/lib/api-routes/behavior-notes.js +57 -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/append-list.js +3 -14
- package/lib/arf-detect.js +23 -1
- package/lib/autodetect-imap-settings.js +40 -16
- package/lib/base32.js +30 -0
- package/lib/bounce-detect.js +182 -62
- package/lib/consts.js +7 -0
- package/lib/egress-filter.js +89 -8
- package/lib/email-client/base-client.js +72 -82
- package/lib/email-client/gmail-client.js +3 -3
- package/lib/email-client/imap/listing-diff.js +3 -3
- package/lib/email-client/imap/mailbox.js +5 -69
- package/lib/email-client/imap-client.js +3 -2
- package/lib/email-client/message-builder.js +13 -13
- package/lib/email-client/outlook-client.js +1 -1
- package/lib/enum-descriptions.js +130 -0
- package/lib/export.js +1 -1
- package/lib/gateway.js +1 -1
- package/lib/get-raw-email.js +1 -1
- package/lib/imapproxy/imap-core/lib/imap-connection.js +2 -2
- package/lib/imapproxy/imap-core/lib/imap-server.js +2 -2
- package/lib/imapproxy/imap-server.js +42 -28
- package/lib/imapproxy/upstream-client.js +38 -0
- package/lib/license-beacon.js +1 -1
- package/lib/llm-pre-process.js +1 -1
- package/lib/msgpack.js +62 -0
- package/lib/oauth/decode-jwt-payload.js +39 -0
- package/lib/oauth/outlook-identity.js +61 -0
- package/lib/oauth/retry-setup.js +38 -0
- package/lib/oauth2-apps.js +10 -4
- 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/outbox.js +66 -3
- package/lib/pre-process.js +1 -1
- package/lib/read-env-value.js +37 -0
- package/lib/routes-ui.js +5 -1
- package/lib/schemas.js +88 -17
- package/lib/swagger-options.js +173 -0
- package/lib/templates.js +1 -1
- package/lib/tokens.js +70 -6
- package/lib/tools.js +117 -48
- package/lib/totp.js +67 -0
- package/lib/ui-routes/account-routes.js +91 -15
- package/lib/ui-routes/admin-config-routes.js +2 -7
- package/lib/ui-routes/admin-entities-routes.js +3 -0
- package/lib/ui-routes/auth-routes.js +6 -24
- package/lib/ui-routes/dashboard-routes.js +2 -26
- package/lib/ui-routes/reference-routes.js +198 -0
- package/lib/ui-routes/route-helpers.js +39 -0
- package/lib/utils/is-email.js +34 -0
- package/lib/utils/to-camel-case.js +17 -0
- package/lib/webhook-egress.js +37 -0
- package/lib/webhooks.js +1 -1
- package/package.json +27 -31
- package/prepare-bundle-deps.sh +28 -0
- package/release-please-config.json +11 -0
- package/sbom.json +1 -1
- package/server.js +11 -3
- package/static/css/flyonui.css +1 -1
- package/static/css/public.css +3 -0
- package/static/css/src/app.tailwind.css +113 -0
- package/static/js/app.js +69 -0
- package/static/js/ee-client.js +238 -8
- package/static/js/reference.js +1021 -0
- package/static/js/ui.js +61 -33
- package/static/licenses.html +658 -1560
- package/translations/README.md +64 -0
- package/translations/de.mo +0 -0
- package/translations/de.po +164 -120
- package/translations/en.po +200 -161
- package/translations/et.mo +0 -0
- package/translations/et.po +163 -120
- package/translations/fr.mo +0 -0
- package/translations/fr.po +163 -120
- package/translations/ja.mo +0 -0
- package/translations/ja.po +163 -120
- package/translations/messages.pot +84 -54
- package/translations/nl.mo +0 -0
- package/translations/nl.po +164 -120
- package/translations/pl.mo +0 -0
- package/translations/pl.po +163 -120
- package/views/account-identity-error.hbs +34 -0
- package/views/accounts/edit.hbs +1 -0
- package/views/accounts/register/imap.hbs +12 -2
- 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 +43 -8
- package/views/oauth-scope-error.hbs +1 -1
- 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 +120 -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 +49 -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 +194 -302
- package/workers/imap.js +1 -1
- package/workers/submit.js +1 -1
- package/workers/webhooks.js +2 -13
- package/views/swagger/index.hbs +0 -82
|
@@ -0,0 +1,1021 @@
|
|
|
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
|
+
// The "no token set" banner sticks below the topbar (reference/token-alert.hbs), so it
|
|
80
|
+
// adds to what a jump target has to clear. Its measured height is published to the
|
|
81
|
+
// stylesheet rather than hard-coded there, because the band wraps to two rows on a narrow
|
|
82
|
+
// viewport - the layout's scroll-padding (.ee-app-chrome) adds it to the scroller's top
|
|
83
|
+
// inset. Kept as a number too, for the scroll spy: that line moves with the same inset,
|
|
84
|
+
// and reading it back out of the cascade on every scrolled frame would force a style
|
|
85
|
+
// recalc to do it.
|
|
86
|
+
let alertHeight = 0;
|
|
87
|
+
|
|
88
|
+
// Written only when it actually changes. Setting an inherited custom property on the root
|
|
89
|
+
// element invalidates style for the whole document - measured at ~7ms on a tag page, and
|
|
90
|
+
// the same with no rule reading the property at all, so this is the cost of the write
|
|
91
|
+
// itself rather than of the 434 rows that resolve it. The observer below makes an
|
|
92
|
+
// unchanged republish easy to hit, since it fires on width too, which moves on every frame
|
|
93
|
+
// of a resize drag while the height sits still.
|
|
94
|
+
const publishAlertHeight = alert => {
|
|
95
|
+
// `hidden` is display: none, so a hidden band measures 0 without a special case
|
|
96
|
+
const height = alert.offsetHeight;
|
|
97
|
+
if (height === alertHeight) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
alertHeight = height;
|
|
102
|
+
document.documentElement.style.setProperty('--ee-sticky-band-h', `${height}px`);
|
|
103
|
+
return true;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// A band that changes height after revealTarget has landed a deep link leaves the section
|
|
107
|
+
// under a band that is opaque, not merely offset - the web font swapping in and rewrapping
|
|
108
|
+
// the warning is the case that hits a cold load. Re-land it, but only while the reader is
|
|
109
|
+
// still exactly where the jump left them: past that they have scrolled on, and yanking the
|
|
110
|
+
// page back would be worse than the offset.
|
|
111
|
+
const relandIfUnmoved = () => {
|
|
112
|
+
if (landedAt !== null && Math.round(window.scrollY) === landedAt) {
|
|
113
|
+
revealTarget();
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// Show and hide are published by paintTokenStatus rather than left to the observer below:
|
|
118
|
+
// both run at DOMContentLoaded, and revealTarget scrolls to the deep link at `load` with
|
|
119
|
+
// whatever margin is in effect by then. This covers the band changing height on its own,
|
|
120
|
+
// which it does when a viewport narrow enough to wrap it is resized.
|
|
121
|
+
const initAlertHeight = () => {
|
|
122
|
+
const alert = document.getElementById('ref-token-alert');
|
|
123
|
+
if (alert) {
|
|
124
|
+
new ResizeObserver(() => {
|
|
125
|
+
if (publishAlertHeight(alert)) {
|
|
126
|
+
relandIfUnmoved();
|
|
127
|
+
}
|
|
128
|
+
}).observe(alert);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// Repaints the always-visible status in the sidebar, and shows the Clear control on
|
|
133
|
+
// the overview page only while there is something to clear.
|
|
134
|
+
const paintTokenStatus = () => {
|
|
135
|
+
const entry = tokenStore.read();
|
|
136
|
+
|
|
137
|
+
const state = document.getElementById('ref-token-state');
|
|
138
|
+
if (state) {
|
|
139
|
+
state.textContent = entry ? (entry.expires ? `${minutesLeft(entry.expires)} min left` : 'Active') : 'Not set';
|
|
140
|
+
state.className = entry ? 'text-success text-2xs' : 'text-base-content/50 text-2xs';
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const clearWrap = document.getElementById('ref-token-clear-wrap');
|
|
144
|
+
if (clearWrap) {
|
|
145
|
+
clearWrap.classList.toggle('hidden', !entry);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// The "no token set" banner. It is only in the DOM when a token is actually
|
|
149
|
+
// required (reference/token-alert.hbs skips it when `disableTokens` is on), so
|
|
150
|
+
// its absence means there is nothing to warn about.
|
|
151
|
+
const alert = document.getElementById('ref-token-alert');
|
|
152
|
+
if (alert) {
|
|
153
|
+
alert.classList.toggle('hidden', !!entry);
|
|
154
|
+
publishAlertHeight(alert);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// A tag page carries up to ~150 code blocks, several of them tens of kilobytes,
|
|
159
|
+
// so highlighting all of them up front stalls the page for seconds. Each block is
|
|
160
|
+
// highlighted the first time it becomes visible instead. That also covers blocks
|
|
161
|
+
// inside collapsed <details> and inactive tab panels for free: those have no
|
|
162
|
+
// layout box until they open, so the observer fires exactly then.
|
|
163
|
+
const highlightBlock = block => {
|
|
164
|
+
if (block.dataset.eeHighlighted) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
block.dataset.eeHighlighted = '1';
|
|
168
|
+
if (window.hljs) {
|
|
169
|
+
window.hljs.highlightElement(block);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const initHighlighting = () => {
|
|
174
|
+
const blocks = document.querySelectorAll('pre code[class*="language-"]');
|
|
175
|
+
if (!blocks.length) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const observer = new IntersectionObserver(
|
|
180
|
+
entries => {
|
|
181
|
+
for (const entry of entries) {
|
|
182
|
+
if (entry.isIntersecting) {
|
|
183
|
+
highlightBlock(entry.target);
|
|
184
|
+
observer.unobserve(entry.target);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
{ rootMargin: '200px' }
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
blocks.forEach(block => observer.observe(block));
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// FlyonUI binds its component autoInit to the window `load` event, not DOMContentLoaded.
|
|
195
|
+
// Anything that drives a tab has to wait for that: a click dispatched earlier lands on a
|
|
196
|
+
// button whose handler is not attached yet and silently does nothing, which is exactly
|
|
197
|
+
// how it fails - no error, the tab just stays where it was.
|
|
198
|
+
const whenComponentsReady = fn => {
|
|
199
|
+
if (document.readyState === 'complete') {
|
|
200
|
+
fn();
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
window.addEventListener('load', fn);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
// Where the last deep-link jump left the page, so a band that grows afterwards can tell
|
|
207
|
+
// "the reader has not moved yet" from "they have scrolled on". Null until one happens.
|
|
208
|
+
let landedAt = null;
|
|
209
|
+
|
|
210
|
+
// Deep links point at a single schema property ("#accountCreate.body.imap.tls"), which
|
|
211
|
+
// can sit inside collapsed <details> groups and an inactive response tab - neither of
|
|
212
|
+
// which the browser can scroll to, because they have no layout box. Everything on the
|
|
213
|
+
// way down is opened first, then the row is scrolled into view. The highlight itself is
|
|
214
|
+
// :target CSS and needs nothing here.
|
|
215
|
+
const revealTarget = () => {
|
|
216
|
+
// No decoding: every anchor the model emits is [A-Za-z0-9_.-], so the fragment and
|
|
217
|
+
// the id are the same string, and anything else simply does not match an element
|
|
218
|
+
const target = document.getElementById(window.location.hash.slice(1));
|
|
219
|
+
if (!target) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
for (let element = target.parentElement; element; element = element.parentElement) {
|
|
224
|
+
if (element.tagName === 'DETAILS') {
|
|
225
|
+
element.open = true;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Response and code-sample panels are plain hidden divs driven by the FlyonUI
|
|
229
|
+
// tab plugin, so the tab button is what has to be activated. The panel already
|
|
230
|
+
// names its button in aria-labelledby, so that is read rather than recomposing
|
|
231
|
+
// ui/tab's "<target>-tab" id convention here.
|
|
232
|
+
if (element.getAttribute('role') === 'tabpanel' && element.classList.contains('hidden')) {
|
|
233
|
+
const tab = document.getElementById(element.getAttribute('aria-labelledby') || '');
|
|
234
|
+
if (tab) {
|
|
235
|
+
tab.click();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
target.scrollIntoView({ block: 'start' });
|
|
241
|
+
landedAt = Math.round(window.scrollY);
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
// Group rows in the nav. As a column they are plain links to the group's page. Below the
|
|
245
|
+
// layout's lg breakpoint the whole nav is a disclosure, and a link there is a bad deal: the
|
|
246
|
+
// tap that was meant to look inside a group loads its page and takes the menu with it. So
|
|
247
|
+
// in that mode the row opens its own operation list instead, and the reader picks the
|
|
248
|
+
// endpoint they actually wanted - which is the only navigation that ends the menu.
|
|
249
|
+
//
|
|
250
|
+
// Which mode the column is in is the layout's own question, answered by the small API
|
|
251
|
+
// static/js/app.js publishes for it.
|
|
252
|
+
const initTagRows = () => {
|
|
253
|
+
const list = document.getElementById('ref-tag-list');
|
|
254
|
+
const nav = window.uiSecondaryNav;
|
|
255
|
+
if (!list || !nav) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
list.addEventListener('click', event => {
|
|
260
|
+
// aria-controls is what a group row has and an operation row does not
|
|
261
|
+
const row = event.target.closest('a[aria-controls]');
|
|
262
|
+
if (!row || !nav.collapsible()) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// A modified click is asking for the group's page in a new tab or window, which is
|
|
267
|
+
// still what the href says. Swallowing it would make the row mean one thing to the
|
|
268
|
+
// left button and another to every other way of opening a link.
|
|
269
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// preventDefault is also what tells the layout's panel handler to leave the menu
|
|
274
|
+
// open: a link that did not navigate has not finished with the reader.
|
|
275
|
+
event.preventDefault();
|
|
276
|
+
|
|
277
|
+
const expand = row.getAttribute('aria-expanded') !== 'true';
|
|
278
|
+
const before = row.getBoundingClientRect().top;
|
|
279
|
+
|
|
280
|
+
// One group at a time, which is the state the page is served in and keeps the menu
|
|
281
|
+
// to a screen or two. Opening all nineteen would put 3000px of navigation above the
|
|
282
|
+
// page, with the control to put it away scrolled off the top.
|
|
283
|
+
for (const open of list.querySelectorAll('a[aria-expanded="true"]')) {
|
|
284
|
+
open.setAttribute('aria-expanded', 'false');
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
row.setAttribute('aria-expanded', String(expand));
|
|
288
|
+
|
|
289
|
+
// Closing a group above this one pulls the list out from under the finger that
|
|
290
|
+
// just tapped it - by the height of whatever was open, which is most of a screen.
|
|
291
|
+
// Put the row back where it was touched.
|
|
292
|
+
const drift = row.getBoundingClientRect().top - before;
|
|
293
|
+
if (drift) {
|
|
294
|
+
if (nav.panel.scrollHeight > nav.panel.clientHeight) {
|
|
295
|
+
nav.panel.scrollTop += drift;
|
|
296
|
+
} else {
|
|
297
|
+
window.scrollBy(0, drift);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
// Crossing back into column mode resets the rows to what the server renders: there
|
|
303
|
+
// lg:block shows the group the page is on and hides every other, so a row still saying
|
|
304
|
+
// "expanded" from a narrow window would announce the opposite of what is on screen -
|
|
305
|
+
// and no click can reconcile it, because the rows are plain links at that width. The
|
|
306
|
+
// active group is the one carrying the spy hook. Watching the toggle's box is how this
|
|
307
|
+
// hears the mode change without naming the breakpoint.
|
|
308
|
+
new ResizeObserver(() => {
|
|
309
|
+
if (nav.collapsible()) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
for (const row of list.querySelectorAll('a[aria-controls]')) {
|
|
314
|
+
const ops = document.getElementById(row.getAttribute('aria-controls'));
|
|
315
|
+
row.setAttribute('aria-expanded', String(!!ops && ops.classList.contains('ee-ref-op-nav')));
|
|
316
|
+
}
|
|
317
|
+
}).observe(nav.toggle);
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
// Resolved once - the platform cannot change, and the shortcut check runs on every
|
|
321
|
+
// keystroke anywhere on the page
|
|
322
|
+
const IS_MAC = /mac/i.test((navigator.userAgentData && navigator.userAgentData.platform) || navigator.platform || '');
|
|
323
|
+
|
|
324
|
+
// True while the caret is somewhere the user is typing, so a bare-letter shortcut
|
|
325
|
+
// does not swallow the keystroke.
|
|
326
|
+
const isEditable = element => !!element && (element.isContentEditable || /^(input|textarea|select)$/i.test(element.tagName));
|
|
327
|
+
|
|
328
|
+
// Sidebar filter. Every operation is already in the DOM (hidden until the box has
|
|
329
|
+
// input), so narrowing the list is plain matching - there is no index to fetch.
|
|
330
|
+
// A hit matches on more than it shows: reference/nav.hbs puts the operation id and
|
|
331
|
+
// every parameter and request-body property name in data-search, so "mailbox" finds
|
|
332
|
+
// the endpoints that accept one, not just the ones with the word in their path.
|
|
333
|
+
//
|
|
334
|
+
// Keyboard navigation moves real DOM focus between the result links rather than
|
|
335
|
+
// tracking a selected index behind aria-activedescendant. The results are already
|
|
336
|
+
// links, so focus gives Enter-to-open, the browser's own focus ring and a correct
|
|
337
|
+
// screen reader announcement for free, without claiming this is a listbox when the
|
|
338
|
+
// markup is a list of links.
|
|
339
|
+
const initFilter = () => {
|
|
340
|
+
const input = document.getElementById('ref-filter');
|
|
341
|
+
const results = document.getElementById('ref-filter-results');
|
|
342
|
+
const tagList = document.getElementById('ref-tag-list');
|
|
343
|
+
const empty = document.getElementById('ref-filter-empty');
|
|
344
|
+
const hint = document.getElementById('ref-filter-hint');
|
|
345
|
+
|
|
346
|
+
if (!input || !results || !tagList || !hint) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const count = hint.querySelector('[role="status"]');
|
|
351
|
+
|
|
352
|
+
// Resolved once: 82 rows, and the arrow keys would otherwise re-query the DOM for
|
|
353
|
+
// every one of them on every keypress while a key is held down
|
|
354
|
+
const hits = Array.from(results.querySelectorAll('.ee-ref-hit')).map(el => ({
|
|
355
|
+
el,
|
|
356
|
+
link: el.querySelector('a'),
|
|
357
|
+
text: (el.dataset.search || '').toLowerCase()
|
|
358
|
+
}));
|
|
359
|
+
|
|
360
|
+
const visibleLinks = () => hits.filter(hit => !hit.el.classList.contains('hidden')).map(hit => hit.link);
|
|
361
|
+
|
|
362
|
+
const apply = () => {
|
|
363
|
+
const terms = input.value.trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
364
|
+
const filtering = terms.length > 0;
|
|
365
|
+
|
|
366
|
+
tagList.classList.toggle('hidden', filtering);
|
|
367
|
+
results.classList.toggle('hidden', !filtering);
|
|
368
|
+
hint.classList.toggle('hidden', !filtering);
|
|
369
|
+
hint.classList.toggle('flex', filtering);
|
|
370
|
+
|
|
371
|
+
if (!filtering) {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
let visible = 0;
|
|
376
|
+
for (const hit of hits) {
|
|
377
|
+
const match = terms.every(term => hit.text.includes(term));
|
|
378
|
+
hit.el.classList.toggle('hidden', !match);
|
|
379
|
+
if (match) {
|
|
380
|
+
visible++;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
empty.classList.toggle('hidden', visible > 0);
|
|
385
|
+
count.textContent = `${visible} ${visible === 1 ? 'endpoint' : 'endpoints'}`;
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
// The input and the visible results form a single cycle, so moving up from the
|
|
389
|
+
// first result - or down past the last - lands back in the field instead of
|
|
390
|
+
// trapping focus in the list.
|
|
391
|
+
const move = offset => {
|
|
392
|
+
const links = visibleLinks();
|
|
393
|
+
if (!links.length) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const current = links.indexOf(document.activeElement);
|
|
398
|
+
if (current < 0) {
|
|
399
|
+
// focus is in the field: Down enters at the top, Up at the bottom
|
|
400
|
+
links[offset > 0 ? 0 : links.length - 1].focus();
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const next = current + offset;
|
|
405
|
+
if (next < 0 || next >= links.length) {
|
|
406
|
+
input.focus();
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
links[next].focus();
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
const reset = () => {
|
|
414
|
+
input.value = '';
|
|
415
|
+
apply();
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
// Shared by both listeners below - only their Escape behavior genuinely differs
|
|
419
|
+
const arrowNav = event => {
|
|
420
|
+
if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp') {
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
event.preventDefault();
|
|
424
|
+
move(event.key === 'ArrowDown' ? 1 : -1);
|
|
425
|
+
return true;
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
input.addEventListener('input', apply);
|
|
429
|
+
|
|
430
|
+
input.addEventListener('keydown', event => {
|
|
431
|
+
if (arrowNav(event)) {
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
if (event.key === 'Enter') {
|
|
436
|
+
// Typing and pressing Enter opens the top hit, so the common case never
|
|
437
|
+
// needs the arrow keys
|
|
438
|
+
const [first] = visibleLinks();
|
|
439
|
+
if (first) {
|
|
440
|
+
event.preventDefault();
|
|
441
|
+
first.click();
|
|
442
|
+
}
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
if (event.key === 'Escape') {
|
|
447
|
+
if (input.value) {
|
|
448
|
+
event.preventDefault();
|
|
449
|
+
reset();
|
|
450
|
+
} else {
|
|
451
|
+
input.blur();
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
results.addEventListener('keydown', event => {
|
|
457
|
+
if (arrowNav(event)) {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (event.key === 'Escape') {
|
|
462
|
+
event.preventDefault();
|
|
463
|
+
input.focus();
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
// Focus shortcuts. Ctrl+K (Cmd+K on macOS) is the common one; "/" matches what
|
|
468
|
+
// most documentation sites bind, and is ignored while the caret is in a field so
|
|
469
|
+
// it can still be typed into the filter itself.
|
|
470
|
+
window.addEventListener('keydown', event => {
|
|
471
|
+
const hotkey = (IS_MAC ? event.metaKey : event.ctrlKey) && !event.altKey && event.key.toLowerCase() === 'k';
|
|
472
|
+
const slash = event.key === '/' && !event.metaKey && !event.ctrlKey && !event.altKey && !isEditable(document.activeElement);
|
|
473
|
+
|
|
474
|
+
if (!hotkey && !slash) {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
event.preventDefault();
|
|
479
|
+
|
|
480
|
+
// Below the layout's lg breakpoint this whole column is a disclosure, and focusing
|
|
481
|
+
// an input inside a display:none subtree does nothing at all - which is the width
|
|
482
|
+
// where a narrow window most needs the filter. Open it on the way to the field.
|
|
483
|
+
if (window.uiSecondaryNav && window.uiSecondaryNav.collapsible()) {
|
|
484
|
+
window.uiSecondaryNav.open();
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
input.focus();
|
|
488
|
+
input.select();
|
|
489
|
+
});
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
// Marks the operation you are currently reading in the sidebar.
|
|
493
|
+
//
|
|
494
|
+
// Slack between the line an operation has to cross and the point it parks at when linked
|
|
495
|
+
// to directly. Without it the two are equal and sub-pixel rounding decides which section
|
|
496
|
+
// a deep link marks; with it, landing on a section always marks that section.
|
|
497
|
+
const SPY_SLACK = 16;
|
|
498
|
+
|
|
499
|
+
const initScrollSpy = () => {
|
|
500
|
+
const list = document.querySelector('.ee-ref-op-nav');
|
|
501
|
+
const panel = list && list.closest('aside');
|
|
502
|
+
if (!panel) {
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// Walked in DOCUMENT order, from the sections rather than from the nav. The two happen
|
|
507
|
+
// to agree today - both come from tag.operations - but only this direction is true by
|
|
508
|
+
// construction, and the scan below is meaningless if the order is wrong. Sorting the
|
|
509
|
+
// nav some other way later would silently break a nav-ordered list.
|
|
510
|
+
//
|
|
511
|
+
// One array of pairs, not a Map plus a copy of its keys: every use wants both halves.
|
|
512
|
+
const entries = [];
|
|
513
|
+
for (const section of document.querySelectorAll('.ee-ref-op[id]')) {
|
|
514
|
+
const link = list.querySelector(`a[href="#${CSS.escape(section.id)}"]`);
|
|
515
|
+
if (link) {
|
|
516
|
+
entries.push({ link, section });
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (!entries.length) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const last = entries[entries.length - 1];
|
|
525
|
+
|
|
526
|
+
// Read from the cascade rather than restated as a number here: a linked section parks
|
|
527
|
+
// at the scroller's own top inset (the sticky chrome, html's scroll-padding-top) plus
|
|
528
|
+
// its scroll-margin, and a spy line that disagreed with the two would mark the wrong
|
|
529
|
+
// row on arrival. Whatever the banner contributes right now is taken back out and
|
|
530
|
+
// re-added per read: that makes this independent of whether the banner has been
|
|
531
|
+
// measured yet, and follows it disappearing when a token is minted, which shortens the
|
|
532
|
+
// inset without the page reloading.
|
|
533
|
+
const staticLine =
|
|
534
|
+
parseFloat(window.getComputedStyle(document.documentElement).scrollPaddingTop || '0') -
|
|
535
|
+
alertHeight +
|
|
536
|
+
parseFloat(window.getComputedStyle(entries[0].section).scrollMarginTop || '0') +
|
|
537
|
+
SPY_SLACK;
|
|
538
|
+
|
|
539
|
+
const current = () => {
|
|
540
|
+
// Recomputed from scratch every time rather than tracked incrementally, so it cannot
|
|
541
|
+
// drift out of step with the page
|
|
542
|
+
const line = staticLine + alertHeight;
|
|
543
|
+
const found = entries.findLast(entry => entry.section.getBoundingClientRect().top <= line) || entries[0];
|
|
544
|
+
|
|
545
|
+
// A final section shorter than the space below it never reaches the line, so it
|
|
546
|
+
// would never light up. Only the pick just before it can be wrong this way, which
|
|
547
|
+
// keeps the scrollHeight read - the one most likely to force layout, on a page whose
|
|
548
|
+
// height moves as code blocks highlight - off all but the last screenful.
|
|
549
|
+
if (found === entries[entries.length - 2] && window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 2) {
|
|
550
|
+
return last;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
return found;
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
// Scrolls the sidebar itself rather than calling scrollIntoView, which also scrolls the
|
|
557
|
+
// window - that would move the sections being measured and could oscillate. Returns the
|
|
558
|
+
// delta instead of applying it so paint() can finish measuring before it writes.
|
|
559
|
+
const panelDelta = link => {
|
|
560
|
+
// Only the full-height column scrolls on its own. Below the layout's lg breakpoint
|
|
561
|
+
// the same markup is a disclosure in normal page flow, with nothing to scroll -
|
|
562
|
+
// asking whether it overflows answers that without naming the breakpoint again.
|
|
563
|
+
if (panel.scrollHeight <= panel.clientHeight) {
|
|
564
|
+
return 0;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// While the filter has input the whole tag list is display:none, so these links
|
|
568
|
+
// have no box at all - and a zero rect against the panel's top reads as "scrolled
|
|
569
|
+
// far above", which would drag the sidebar up a row at a time behind the results.
|
|
570
|
+
if (!link.offsetParent) {
|
|
571
|
+
return 0;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const linkBox = link.getBoundingClientRect();
|
|
575
|
+
const panelBox = panel.getBoundingClientRect();
|
|
576
|
+
|
|
577
|
+
if (linkBox.top < panelBox.top) {
|
|
578
|
+
return linkBox.top - panelBox.top - 8;
|
|
579
|
+
}
|
|
580
|
+
if (linkBox.bottom > panelBox.bottom) {
|
|
581
|
+
return linkBox.bottom - panelBox.bottom + 8;
|
|
582
|
+
}
|
|
583
|
+
return 0;
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
let marked = null;
|
|
587
|
+
|
|
588
|
+
const paint = () => {
|
|
589
|
+
const entry = current();
|
|
590
|
+
if (entry === marked) {
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// Measured before anything is written: setting aria-current restyles the link, so
|
|
595
|
+
// reading its box afterwards would force a synchronous style and layout pass at the
|
|
596
|
+
// exact moment the marker moves.
|
|
597
|
+
const delta = panelDelta(entry.link);
|
|
598
|
+
|
|
599
|
+
if (marked) {
|
|
600
|
+
marked.link.removeAttribute('aria-current');
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
marked = entry;
|
|
604
|
+
// `location` rather than `page`: the tag entry above is the current page, this is a
|
|
605
|
+
// position within it
|
|
606
|
+
entry.link.setAttribute('aria-current', 'location');
|
|
607
|
+
|
|
608
|
+
if (delta) {
|
|
609
|
+
panel.scrollTop += delta;
|
|
610
|
+
}
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
let queued = false;
|
|
614
|
+
const schedule = () => {
|
|
615
|
+
if (queued) {
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
queued = true;
|
|
619
|
+
// Reads happen inside the frame callback, before the browser's own style and layout
|
|
620
|
+
// phase, so the flush they force is work the frame was going to do anyway
|
|
621
|
+
window.requestAnimationFrame(() => {
|
|
622
|
+
queued = false;
|
|
623
|
+
paint();
|
|
624
|
+
});
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
window.addEventListener('scroll', schedule, { passive: true });
|
|
628
|
+
window.addEventListener('resize', schedule, { passive: true });
|
|
629
|
+
paint();
|
|
630
|
+
};
|
|
631
|
+
|
|
632
|
+
// Code sample language, remembered across operations and page loads.
|
|
633
|
+
//
|
|
634
|
+
// localStorage, not sessionStorage (which is what the try-it token uses): this is a
|
|
635
|
+
// display preference, not a credential, and losing it when the tab closes is exactly
|
|
636
|
+
// the annoyance the setting exists to fix.
|
|
637
|
+
//
|
|
638
|
+
// The stored value is the sample's id (`curl`, `node`, `python`, `custom-0`), carried on
|
|
639
|
+
// the button as data-tab-key. Not the label: that is display text, so rewording one
|
|
640
|
+
// would silently reset everybody's choice, and a hand-written x-codeSamples tab labelled
|
|
641
|
+
// "curl" would take the generated tab's place on operations that have both. An operation
|
|
642
|
+
// that does not offer the remembered id - hand-written samples exist per operation - just
|
|
643
|
+
// keeps its own default rather than landing on an unrelated tab.
|
|
644
|
+
const SAMPLE_LANGUAGE_KEY = 'eeRefSampleLanguage';
|
|
645
|
+
|
|
646
|
+
const initSampleTabs = () => {
|
|
647
|
+
const groups = document.querySelectorAll('[data-sample-tabs]');
|
|
648
|
+
if (!groups.length) {
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
let preferred;
|
|
653
|
+
try {
|
|
654
|
+
preferred = window.localStorage.getItem(SAMPLE_LANGUAGE_KEY);
|
|
655
|
+
} catch (err) {
|
|
656
|
+
preferred = null;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
whenComponentsReady(() => {
|
|
660
|
+
if (preferred) {
|
|
661
|
+
for (const group of groups) {
|
|
662
|
+
const tab = group.querySelector(`[data-tab-key="${CSS.escape(preferred)}"]`);
|
|
663
|
+
if (tab && tab.getAttribute('aria-selected') !== 'true') {
|
|
664
|
+
tab.click();
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// Bound only after the restore pass, so those synthetic clicks do not each
|
|
670
|
+
// rewrite the same value - a 13-operation page would otherwise fire a dozen
|
|
671
|
+
// storage writes on load. Clicks before this point cannot reach a tab anyway:
|
|
672
|
+
// FlyonUI is not initialized yet.
|
|
673
|
+
document.addEventListener('click', event => {
|
|
674
|
+
const tab = event.target.closest('[data-sample-tabs] [role="tab"]');
|
|
675
|
+
if (!tab || !tab.dataset.tabKey) {
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
try {
|
|
680
|
+
window.localStorage.setItem(SAMPLE_LANGUAGE_KEY, tab.dataset.tabKey);
|
|
681
|
+
} catch (err) {
|
|
682
|
+
// storage blocked - the choice still applies to this page
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
});
|
|
686
|
+
};
|
|
687
|
+
|
|
688
|
+
const badgeVariant = status => {
|
|
689
|
+
if (status >= 200 && status < 300) {
|
|
690
|
+
return 'badge-success';
|
|
691
|
+
}
|
|
692
|
+
if (status >= 400 && status < 500) {
|
|
693
|
+
return 'badge-warning';
|
|
694
|
+
}
|
|
695
|
+
if (status >= 500) {
|
|
696
|
+
return 'badge-error';
|
|
697
|
+
}
|
|
698
|
+
return 'badge-neutral';
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
const buildRequest = form => {
|
|
702
|
+
let path = form.dataset.path;
|
|
703
|
+
|
|
704
|
+
for (const input of form.querySelectorAll('[data-in="path"]')) {
|
|
705
|
+
const value = input.value.trim();
|
|
706
|
+
if (!value) {
|
|
707
|
+
throw new Error(`Path parameter "${input.dataset.param}" is required`);
|
|
708
|
+
}
|
|
709
|
+
path = path.replace(`{${input.dataset.param}}`, encodeURIComponent(value));
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
const query = new URLSearchParams();
|
|
713
|
+
for (const input of form.querySelectorAll('[data-in="query"]')) {
|
|
714
|
+
const value = input.value.trim();
|
|
715
|
+
if (value) {
|
|
716
|
+
query.append(input.dataset.param, value);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
const headers = {};
|
|
721
|
+
for (const input of form.querySelectorAll('[data-in="header"]')) {
|
|
722
|
+
const value = input.value.trim();
|
|
723
|
+
if (value) {
|
|
724
|
+
headers[input.dataset.param] = value;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// The token held for this tab, set once on the overview page. Only ever sent to
|
|
729
|
+
// this instance's own API, by the request the user explicitly triggered.
|
|
730
|
+
const entry = tokenStore.read();
|
|
731
|
+
if (entry) {
|
|
732
|
+
headers.Authorization = `Bearer ${entry.token}`;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
let body;
|
|
736
|
+
const bodyField = form.querySelector('[data-body]');
|
|
737
|
+
if (bodyField && bodyField.value.trim()) {
|
|
738
|
+
try {
|
|
739
|
+
JSON.parse(bodyField.value);
|
|
740
|
+
} catch (err) {
|
|
741
|
+
throw new Error(`Request body is not valid JSON: ${err.message}`);
|
|
742
|
+
}
|
|
743
|
+
body = bodyField.value;
|
|
744
|
+
headers['Content-Type'] = 'application/json';
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
const queryString = query.toString();
|
|
748
|
+
|
|
749
|
+
return {
|
|
750
|
+
url: path + (queryString ? `?${queryString}` : ''),
|
|
751
|
+
method: (form.dataset.method || 'get').toUpperCase(),
|
|
752
|
+
headers,
|
|
753
|
+
body
|
|
754
|
+
};
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
const renderResponse = async (form, response, elapsed) => {
|
|
758
|
+
const result = form.querySelector('.ee-ref-try-result');
|
|
759
|
+
const codeBadge = form.querySelector('.ee-ref-try-code');
|
|
760
|
+
const timeEl = form.querySelector('.ee-ref-try-time');
|
|
761
|
+
const bodyEl = form.querySelector('.ee-ref-try-body');
|
|
762
|
+
|
|
763
|
+
codeBadge.className = `ee-ref-try-code badge badge-sm ${badgeVariant(response.status)}`;
|
|
764
|
+
codeBadge.textContent = `${response.status} ${response.statusText}`.trim();
|
|
765
|
+
|
|
766
|
+
const contentType = response.headers.get('content-type') || '';
|
|
767
|
+
timeEl.textContent = `${elapsed} ms${contentType ? ` - ${contentType.split(';')[0]}` : ''}`;
|
|
768
|
+
|
|
769
|
+
let text = await response.text();
|
|
770
|
+
let truncated = false;
|
|
771
|
+
if (text.length > MAX_RESPONSE_CHARS) {
|
|
772
|
+
text = text.slice(0, MAX_RESPONSE_CHARS);
|
|
773
|
+
truncated = true;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
if (contentType.includes('json')) {
|
|
777
|
+
try {
|
|
778
|
+
text = JSON.stringify(JSON.parse(text), null, 2);
|
|
779
|
+
} catch (err) {
|
|
780
|
+
// a truncated or non-conforming body is shown as received
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
if (truncated) {
|
|
785
|
+
text += '\n\n... response truncated for display';
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
// textContent, never innerHTML: the body is whatever the API returned
|
|
789
|
+
bodyEl.textContent = text;
|
|
790
|
+
delete bodyEl.dataset.eeHighlighted;
|
|
791
|
+
bodyEl.removeAttribute('data-highlighted');
|
|
792
|
+
result.classList.remove('hidden');
|
|
793
|
+
highlightBlock(bodyEl);
|
|
794
|
+
};
|
|
795
|
+
|
|
796
|
+
// Single-quoted shell strings cannot contain a single quote, so each one closes the
|
|
797
|
+
// string, adds an escaped quote and reopens it. Mirrors shellQuote() in
|
|
798
|
+
// lib/api-reference/code-samples.js, which does the same for the generated samples.
|
|
799
|
+
const shellQuote = value => `'${String(value).replace(/'/g, `'\\''`)}'`;
|
|
800
|
+
|
|
801
|
+
// Serializes the request the form would actually send as a runnable curl command. The
|
|
802
|
+
// generated samples above an operation come from its schema examples, so they drift
|
|
803
|
+
// from the form as soon as anything is edited; this is built from the same
|
|
804
|
+
// buildRequest() the Send button uses, so the two cannot disagree.
|
|
805
|
+
//
|
|
806
|
+
// The access token is replaced with the $EMAILENGINE_TOKEN placeholder the generated
|
|
807
|
+
// samples read, rather than the token held for this tab. That one is usually a
|
|
808
|
+
// throwaway minted for the reference page, so pasting it into a snippet produces a
|
|
809
|
+
// command that stops working within the hour - and a live credential should not be
|
|
810
|
+
// the thing that ends up in a ticket or a chat message.
|
|
811
|
+
const curlForRequest = request => {
|
|
812
|
+
const lines = [
|
|
813
|
+
`curl -X ${request.method} ${shellQuote(window.location.origin + request.url)}`,
|
|
814
|
+
// double quotes, so the shell expands the variable
|
|
815
|
+
`${INDENT}-H "Authorization: Bearer $${TOKEN_ENV_VAR}"`
|
|
816
|
+
];
|
|
817
|
+
|
|
818
|
+
for (const [name, value] of Object.entries(request.headers)) {
|
|
819
|
+
if (name !== 'Authorization') {
|
|
820
|
+
lines.push(`${INDENT}-H ${shellQuote(`${name}: ${value}`)}`);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
if (request.body) {
|
|
825
|
+
lines.push(`${INDENT}-d ${shellQuote(request.body)}`);
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
return lines.join(' \\\n');
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
// Both entry points below - send and copy-as-curl - read the same form and report the
|
|
832
|
+
// same validation failures in the same place, so they share the preamble. Returns null
|
|
833
|
+
// when the form is not ready to be turned into a request, having said why.
|
|
834
|
+
const prepareRequest = form => {
|
|
835
|
+
const status = form.querySelector('.ee-ref-try-status');
|
|
836
|
+
status.textContent = '';
|
|
837
|
+
|
|
838
|
+
try {
|
|
839
|
+
return buildRequest(form);
|
|
840
|
+
} catch (err) {
|
|
841
|
+
status.textContent = err.message;
|
|
842
|
+
return null;
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
|
|
846
|
+
// One delegated listener for every try-it form on the page, matching how
|
|
847
|
+
// static/js/ui.js handles the copy buttons.
|
|
848
|
+
const initTryIt = () => {
|
|
849
|
+
document.addEventListener('click', event => {
|
|
850
|
+
const button = event.target.closest('.ee-ref-try-curl');
|
|
851
|
+
if (!button) {
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
const request = prepareRequest(button.closest('.ee-ref-try'));
|
|
856
|
+
if (request) {
|
|
857
|
+
window.uiCopyText(curlForRequest(request), button);
|
|
858
|
+
}
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
document.addEventListener('submit', async event => {
|
|
862
|
+
const form = event.target.closest('.ee-ref-try');
|
|
863
|
+
if (!form) {
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
event.preventDefault();
|
|
868
|
+
|
|
869
|
+
const request = prepareRequest(form);
|
|
870
|
+
if (!request) {
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
const status = form.querySelector('.ee-ref-try-status');
|
|
875
|
+
const button = form.querySelector('button[type="submit"]');
|
|
876
|
+
|
|
877
|
+
window.uiButtonBusy(button, true);
|
|
878
|
+
const started = performance.now();
|
|
879
|
+
|
|
880
|
+
try {
|
|
881
|
+
const response = await fetch(request.url, {
|
|
882
|
+
method: request.method,
|
|
883
|
+
headers: request.headers,
|
|
884
|
+
body: request.body
|
|
885
|
+
});
|
|
886
|
+
await renderResponse(form, response, Math.round(performance.now() - started));
|
|
887
|
+
} catch (err) {
|
|
888
|
+
status.textContent = `Request failed: ${err.message}`;
|
|
889
|
+
} finally {
|
|
890
|
+
window.uiButtonBusy(button, false);
|
|
891
|
+
}
|
|
892
|
+
});
|
|
893
|
+
};
|
|
894
|
+
|
|
895
|
+
// Feedback line on the access-token page; elsewhere (the "no token set" banner) there
|
|
896
|
+
// is no such element and a toast carries the message instead.
|
|
897
|
+
const say = (message, ok) => {
|
|
898
|
+
const feedback = document.getElementById('ref-token-feedback');
|
|
899
|
+
if (feedback) {
|
|
900
|
+
feedback.textContent = message;
|
|
901
|
+
feedback.className = ok ? 'text-success text-sm' : 'text-base-content/70 text-sm';
|
|
902
|
+
return;
|
|
903
|
+
}
|
|
904
|
+
window.showToast(message, ok ? 'check-circle' : 'alert-triangle');
|
|
905
|
+
};
|
|
906
|
+
|
|
907
|
+
const revokeMinted = async token => {
|
|
908
|
+
try {
|
|
909
|
+
await window.uiPostJson('/admin/reference/token/revoke', { token });
|
|
910
|
+
return true;
|
|
911
|
+
} catch (err) {
|
|
912
|
+
return false;
|
|
913
|
+
}
|
|
914
|
+
};
|
|
915
|
+
|
|
916
|
+
const activate = async (token, expires, message) => {
|
|
917
|
+
// Replacing a minted token would otherwise leave it working in Redis for the rest of
|
|
918
|
+
// its hour with nothing pointing at it
|
|
919
|
+
const previous = tokenStore.read();
|
|
920
|
+
if (previous && previous.expires && previous.token !== token) {
|
|
921
|
+
await revokeMinted(previous.token);
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
tokenStore.write(token, expires);
|
|
925
|
+
const field = document.getElementById('ref-access-token');
|
|
926
|
+
if (field) {
|
|
927
|
+
field.value = '';
|
|
928
|
+
}
|
|
929
|
+
paintTokenStatus();
|
|
930
|
+
say(message, true);
|
|
931
|
+
};
|
|
932
|
+
|
|
933
|
+
// Minting is reachable from the access-token page AND from the banner on every other
|
|
934
|
+
// reference page, so it is bound by class through one delegated listener rather than
|
|
935
|
+
// by id on a single button.
|
|
936
|
+
const initMint = () => {
|
|
937
|
+
document.addEventListener('click', async event => {
|
|
938
|
+
const btn = event.target.closest('.ref-token-mint');
|
|
939
|
+
if (!btn) {
|
|
940
|
+
return;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
event.preventDefault();
|
|
944
|
+
window.uiButtonBusy(btn, true);
|
|
945
|
+
|
|
946
|
+
try {
|
|
947
|
+
const data = await window.uiPostJson('/admin/reference/token');
|
|
948
|
+
if (!data || !data.success) {
|
|
949
|
+
throw new Error((data && data.message) || 'Request failed');
|
|
950
|
+
}
|
|
951
|
+
await activate(data.token, data.expires, `Temporary token minted, valid for ${minutesLeft(data.expires)} minutes.`);
|
|
952
|
+
} catch (err) {
|
|
953
|
+
say(`Could not mint a token: ${err.message}`, false);
|
|
954
|
+
} finally {
|
|
955
|
+
window.uiButtonBusy(btn, false);
|
|
956
|
+
}
|
|
957
|
+
});
|
|
958
|
+
};
|
|
959
|
+
|
|
960
|
+
// The paste/clear controls exist only on the access-token page.
|
|
961
|
+
const initTokenForm = () => {
|
|
962
|
+
const field = document.getElementById('ref-access-token');
|
|
963
|
+
if (!field) {
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
// Reflect what is already held for this tab
|
|
968
|
+
const existing = tokenStore.read();
|
|
969
|
+
if (existing) {
|
|
970
|
+
say(existing.expires ? `A temporary token is active, ${minutesLeft(existing.expires)} minutes left.` : 'A token is active.', true);
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
document.getElementById('ref-token-set').addEventListener('click', () => {
|
|
974
|
+
const token = field.value.trim();
|
|
975
|
+
if (!token) {
|
|
976
|
+
say('Paste a token first.', false);
|
|
977
|
+
return;
|
|
978
|
+
}
|
|
979
|
+
activate(token, null, 'Token set for this tab.').catch(err => say(err.message, false));
|
|
980
|
+
});
|
|
981
|
+
|
|
982
|
+
document.getElementById('ref-token-clear').addEventListener('click', async () => {
|
|
983
|
+
const entry = tokenStore.read();
|
|
984
|
+
|
|
985
|
+
// Drop it locally first, so the field is cleared even if the revoke call fails
|
|
986
|
+
tokenStore.clear();
|
|
987
|
+
field.value = '';
|
|
988
|
+
paintTokenStatus();
|
|
989
|
+
|
|
990
|
+
// A token this page minted is deleted for real rather than left working in
|
|
991
|
+
// Redis until it expires. A pasted token belongs to the user - only forget it.
|
|
992
|
+
if (!entry || !entry.expires) {
|
|
993
|
+
say('Token cleared.', false);
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
say((await revokeMinted(entry.token)) ? 'Temporary token cleared and revoked.' : 'Token cleared here, but revoking it on the server failed.', false);
|
|
998
|
+
});
|
|
999
|
+
};
|
|
1000
|
+
|
|
1001
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
1002
|
+
initHighlighting();
|
|
1003
|
+
initFilter();
|
|
1004
|
+
initTagRows();
|
|
1005
|
+
initScrollSpy();
|
|
1006
|
+
initSampleTabs();
|
|
1007
|
+
initTryIt();
|
|
1008
|
+
initMint();
|
|
1009
|
+
initTokenForm();
|
|
1010
|
+
initAlertHeight();
|
|
1011
|
+
paintTokenStatus();
|
|
1012
|
+
|
|
1013
|
+
// On load rather than here: a target inside an inactive response tab needs the tab
|
|
1014
|
+
// switched, which needs FlyonUI
|
|
1015
|
+
whenComponentsReady(revealTarget);
|
|
1016
|
+
});
|
|
1017
|
+
|
|
1018
|
+
// Same-page property links only change the fragment, so the load-time pass does not
|
|
1019
|
+
// run for them
|
|
1020
|
+
window.addEventListener('hashchange', revealTarget);
|
|
1021
|
+
})();
|