sendproof-core 0.1.0.alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +55 -0
- data/lib/sendproof/assets.rb +70 -0
- data/lib/sendproof/attribution.rb +98 -0
- data/lib/sendproof/capture.rb +66 -0
- data/lib/sendproof/clock.rb +54 -0
- data/lib/sendproof/configuration.rb +241 -0
- data/lib/sendproof/data_store.rb +34 -0
- data/lib/sendproof/dynamodb/store.rb +183 -0
- data/lib/sendproof/dynamodb.rb +17 -0
- data/lib/sendproof/elasticsearch/store.rb +24 -0
- data/lib/sendproof/elasticsearch.rb +17 -0
- data/lib/sendproof/firestore/interceptor.rb +38 -0
- data/lib/sendproof/firestore/store.rb +150 -0
- data/lib/sendproof/firestore.rb +99 -0
- data/lib/sendproof/handler.rb +23 -0
- data/lib/sendproof/identity.rb +37 -0
- data/lib/sendproof/limits.rb +113 -0
- data/lib/sendproof/middleware.rb +68 -0
- data/lib/sendproof/mongo/engine.rb +692 -0
- data/lib/sendproof/mongo/guarded.rb +100 -0
- data/lib/sendproof/mongo/plain.rb +45 -0
- data/lib/sendproof/mongo/store.rb +54 -0
- data/lib/sendproof/mongo/subscriber.rb +21 -0
- data/lib/sendproof/mongo.rb +41 -0
- data/lib/sendproof/mongoid/store.rb +58 -0
- data/lib/sendproof/mongoid.rb +65 -0
- data/lib/sendproof/observed_request.rb +108 -0
- data/lib/sendproof/opensearch/store.rb +21 -0
- data/lib/sendproof/opensearch.rb +17 -0
- data/lib/sendproof/operation_matcher.rb +28 -0
- data/lib/sendproof/pool_hooks.rb +80 -0
- data/lib/sendproof/preview_request.rb +44 -0
- data/lib/sendproof/preview_transport.rb +331 -0
- data/lib/sendproof/protocol.rb +81 -0
- data/lib/sendproof/provider.rb +83 -0
- data/lib/sendproof/recorder.rb +298 -0
- data/lib/sendproof/redaction.rb +98 -0
- data/lib/sendproof/redis/commands.rb +76 -0
- data/lib/sendproof/redis/store.rb +205 -0
- data/lib/sendproof/redis.rb +18 -0
- data/lib/sendproof/search/middleware.rb +39 -0
- data/lib/sendproof/search/store.rb +213 -0
- data/lib/sendproof/search.rb +82 -0
- data/lib/sendproof/stores/classify.rb +346 -0
- data/lib/sendproof/stores/guard.rb +127 -0
- data/lib/sendproof/stores/tables.rb +1328 -0
- data/lib/sendproof/stores.rb +12 -0
- data/lib/sendproof/trace_store.rb +110 -0
- data/lib/sendproof/transaction_control.rb +187 -0
- data/lib/sendproof/ui/action-bar.js +91 -0
- data/lib/sendproof/ui/api.js +239 -0
- data/lib/sendproof/ui/app.css +848 -0
- data/lib/sendproof/ui/app.js +742 -0
- data/lib/sendproof/ui/autofill.js +137 -0
- data/lib/sendproof/ui/code-copy.js +71 -0
- data/lib/sendproof/ui/code-picker.js +149 -0
- data/lib/sendproof/ui/commands.js +197 -0
- data/lib/sendproof/ui/components.js +77 -0
- data/lib/sendproof/ui/context-bar.js +82 -0
- data/lib/sendproof/ui/copy.js +92 -0
- data/lib/sendproof/ui/coverage-popover.js +35 -0
- data/lib/sendproof/ui/crumb-fit.js +80 -0
- data/lib/sendproof/ui/deprecation.js +164 -0
- data/lib/sendproof/ui/display-settings.js +175 -0
- data/lib/sendproof/ui/doc-diff.js +209 -0
- data/lib/sendproof/ui/docs-view.js +347 -0
- data/lib/sendproof/ui/favicon.png +0 -0
- data/lib/sendproof/ui/favicon.svg +1 -0
- data/lib/sendproof/ui/field-help.js +52 -0
- data/lib/sendproof/ui/field-modules.js +23 -0
- data/lib/sendproof/ui/fields/controls.js +179 -0
- data/lib/sendproof/ui/fields/dom.js +36 -0
- data/lib/sendproof/ui/fields/infer.js +71 -0
- data/lib/sendproof/ui/fields/lookup-source.js +63 -0
- data/lib/sendproof/ui/fields/lookup.js +285 -0
- data/lib/sendproof/ui/fields/render.js +496 -0
- data/lib/sendproof/ui/fields/select.js +175 -0
- data/lib/sendproof/ui/fields/validate.js +363 -0
- data/lib/sendproof/ui/fields/validation-formats.js +164 -0
- data/lib/sendproof/ui/fields/validation-references.js +68 -0
- data/lib/sendproof/ui/fields/validation-values.js +137 -0
- data/lib/sendproof/ui/generate-menu.js +44 -0
- data/lib/sendproof/ui/generate.js +184 -0
- data/lib/sendproof/ui/header-editor.js +777 -0
- data/lib/sendproof/ui/headers.js +188 -0
- data/lib/sendproof/ui/highlight.js +257 -0
- data/lib/sendproof/ui/history-popover.js +24 -0
- data/lib/sendproof/ui/i18n.js +253 -0
- data/lib/sendproof/ui/icons.js +87 -0
- data/lib/sendproof/ui/index.html +70 -0
- data/lib/sendproof/ui/issues.js +187 -0
- data/lib/sendproof/ui/json-view.js +166 -0
- data/lib/sendproof/ui/lang-icons.js +62 -0
- data/lib/sendproof/ui/layout.js +262 -0
- data/lib/sendproof/ui/listbox.js +334 -0
- data/lib/sendproof/ui/locales/ar-capture.js +366 -0
- data/lib/sendproof/ui/locales/ar-stores.js +116 -0
- data/lib/sendproof/ui/locales/ar.js +561 -0
- data/lib/sendproof/ui/locales/cs-capture.js +442 -0
- data/lib/sendproof/ui/locales/cs-stores.js +116 -0
- data/lib/sendproof/ui/locales/cs.js +689 -0
- data/lib/sendproof/ui/locales/da-capture.js +347 -0
- data/lib/sendproof/ui/locales/da-stores.js +116 -0
- data/lib/sendproof/ui/locales/da.js +522 -0
- data/lib/sendproof/ui/locales/de-capture.js +347 -0
- data/lib/sendproof/ui/locales/de-stores.js +116 -0
- data/lib/sendproof/ui/locales/de.js +519 -0
- data/lib/sendproof/ui/locales/el-capture.js +347 -0
- data/lib/sendproof/ui/locales/el-stores.js +116 -0
- data/lib/sendproof/ui/locales/el.js +520 -0
- data/lib/sendproof/ui/locales/en-capture.js +349 -0
- data/lib/sendproof/ui/locales/en-stores.js +95 -0
- data/lib/sendproof/ui/locales/en.js +522 -0
- data/lib/sendproof/ui/locales/es-capture.js +353 -0
- data/lib/sendproof/ui/locales/es-stores.js +116 -0
- data/lib/sendproof/ui/locales/es.js +525 -0
- data/lib/sendproof/ui/locales/fa-capture.js +348 -0
- data/lib/sendproof/ui/locales/fa-stores.js +116 -0
- data/lib/sendproof/ui/locales/fa.js +521 -0
- data/lib/sendproof/ui/locales/fi-capture.js +347 -0
- data/lib/sendproof/ui/locales/fi-stores.js +116 -0
- data/lib/sendproof/ui/locales/fi.js +523 -0
- data/lib/sendproof/ui/locales/fr-capture.js +353 -0
- data/lib/sendproof/ui/locales/fr-stores.js +116 -0
- data/lib/sendproof/ui/locales/fr.js +525 -0
- data/lib/sendproof/ui/locales/he-capture.js +358 -0
- data/lib/sendproof/ui/locales/he-stores.js +116 -0
- data/lib/sendproof/ui/locales/he.js +529 -0
- data/lib/sendproof/ui/locales/hi-capture.js +348 -0
- data/lib/sendproof/ui/locales/hi-stores.js +116 -0
- data/lib/sendproof/ui/locales/hi.js +523 -0
- data/lib/sendproof/ui/locales/id-capture.js +347 -0
- data/lib/sendproof/ui/locales/id-stores.js +116 -0
- data/lib/sendproof/ui/locales/id.js +521 -0
- data/lib/sendproof/ui/locales/it-capture.js +361 -0
- data/lib/sendproof/ui/locales/it-stores.js +116 -0
- data/lib/sendproof/ui/locales/it.js +523 -0
- data/lib/sendproof/ui/locales/ja-capture.js +346 -0
- data/lib/sendproof/ui/locales/ja-stores.js +116 -0
- data/lib/sendproof/ui/locales/ja.js +521 -0
- data/lib/sendproof/ui/locales/ko-capture.js +346 -0
- data/lib/sendproof/ui/locales/ko-stores.js +116 -0
- data/lib/sendproof/ui/locales/ko.js +522 -0
- data/lib/sendproof/ui/locales/nb-capture.js +349 -0
- data/lib/sendproof/ui/locales/nb-stores.js +116 -0
- data/lib/sendproof/ui/locales/nb.js +521 -0
- data/lib/sendproof/ui/locales/nl-capture.js +355 -0
- data/lib/sendproof/ui/locales/nl-stores.js +116 -0
- data/lib/sendproof/ui/locales/nl.js +520 -0
- data/lib/sendproof/ui/locales/pl-capture.js +432 -0
- data/lib/sendproof/ui/locales/pl-stores.js +116 -0
- data/lib/sendproof/ui/locales/pl.js +528 -0
- data/lib/sendproof/ui/locales/pt-BR-capture.js +350 -0
- data/lib/sendproof/ui/locales/pt-BR-stores.js +116 -0
- data/lib/sendproof/ui/locales/pt-BR.js +523 -0
- data/lib/sendproof/ui/locales/pt-PT-capture.js +350 -0
- data/lib/sendproof/ui/locales/pt-PT-stores.js +116 -0
- data/lib/sendproof/ui/locales/pt-PT.js +523 -0
- data/lib/sendproof/ui/locales/ru-capture.js +437 -0
- data/lib/sendproof/ui/locales/ru-stores.js +116 -0
- data/lib/sendproof/ui/locales/ru.js +624 -0
- data/lib/sendproof/ui/locales/sv-capture.js +349 -0
- data/lib/sendproof/ui/locales/sv-stores.js +116 -0
- data/lib/sendproof/ui/locales/sv.js +518 -0
- data/lib/sendproof/ui/locales/th-capture.js +347 -0
- data/lib/sendproof/ui/locales/th-stores.js +116 -0
- data/lib/sendproof/ui/locales/th.js +521 -0
- data/lib/sendproof/ui/locales/tr-capture.js +348 -0
- data/lib/sendproof/ui/locales/tr-stores.js +116 -0
- data/lib/sendproof/ui/locales/tr.js +521 -0
- data/lib/sendproof/ui/locales/uk-capture.js +442 -0
- data/lib/sendproof/ui/locales/uk-stores.js +116 -0
- data/lib/sendproof/ui/locales/uk.js +630 -0
- data/lib/sendproof/ui/locales/vi-capture.js +346 -0
- data/lib/sendproof/ui/locales/vi-stores.js +116 -0
- data/lib/sendproof/ui/locales/vi.js +523 -0
- data/lib/sendproof/ui/locales/zh-Hans-capture.js +347 -0
- data/lib/sendproof/ui/locales/zh-Hans-stores.js +116 -0
- data/lib/sendproof/ui/locales/zh-Hans.js +523 -0
- data/lib/sendproof/ui/locales/zh-Hant-capture.js +346 -0
- data/lib/sendproof/ui/locales/zh-Hant-stores.js +116 -0
- data/lib/sendproof/ui/locales/zh-Hant.js +522 -0
- data/lib/sendproof/ui/manifest.json +866 -0
- data/lib/sendproof/ui/markdown.js +39 -0
- data/lib/sendproof/ui/model-view.js +182 -0
- data/lib/sendproof/ui/navigation.js +233 -0
- data/lib/sendproof/ui/operation-header.js +76 -0
- data/lib/sendproof/ui/operations.js +7 -0
- data/lib/sendproof/ui/overview-view.js +63 -0
- data/lib/sendproof/ui/personas.js +59 -0
- data/lib/sendproof/ui/popover.js +71 -0
- data/lib/sendproof/ui/preview-false.js +108 -0
- data/lib/sendproof/ui/request-editor.js +496 -0
- data/lib/sendproof/ui/response-metadata.js +354 -0
- data/lib/sendproof/ui/response-viewer.js +618 -0
- data/lib/sendproof/ui/result-blocked.js +155 -0
- data/lib/sendproof/ui/result-capture.js +25 -0
- data/lib/sendproof/ui/result-database.js +185 -0
- data/lib/sendproof/ui/result-effects.js +176 -0
- data/lib/sendproof/ui/result-headers.js +106 -0
- data/lib/sendproof/ui/result-logs.js +408 -0
- data/lib/sendproof/ui/result-states.js +213 -0
- data/lib/sendproof/ui/results.js +371 -0
- data/lib/sendproof/ui/router.js +118 -0
- data/lib/sendproof/ui/schema-model.js +336 -0
- data/lib/sendproof/ui/schema-tree.js +344 -0
- data/lib/sendproof/ui/schema.js +397 -0
- data/lib/sendproof/ui/security.js +31 -0
- data/lib/sendproof/ui/seeded-menu.js +69 -0
- data/lib/sendproof/ui/settings.js +93 -0
- data/lib/sendproof/ui/shortcuts-sheet.js +73 -0
- data/lib/sendproof/ui/shortcuts.js +190 -0
- data/lib/sendproof/ui/sidebar.js +289 -0
- data/lib/sendproof/ui/signin.js +259 -0
- data/lib/sendproof/ui/snippets.js +530 -0
- data/lib/sendproof/ui/store-marks.js +47 -0
- data/lib/sendproof/ui/stores.js +131 -0
- data/lib/sendproof/ui/styles.css +971 -0
- data/lib/sendproof/ui/toggletip.js +53 -0
- data/lib/sendproof/ui/uuid.js +10 -0
- data/lib/sendproof/ui/view.js +122 -0
- data/lib/sendproof/ui/views.css +653 -0
- data/lib/sendproof/version.rb +9 -0
- data/lib/sendproof/workbench.rb +102 -0
- data/lib/sendproof-core.rb +3 -0
- data/lib/sendproof.rb +121 -0
- metadata +292 -0
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
// Sendproof workbench entry point. It loads the capabilities and OpenAPI document, renders the
|
|
2
|
+
// shell, and routes between the four views of the approved design: the API overview, Try it,
|
|
3
|
+
// Docs and a model page. Feature modules own their own markup; this file wires events to them.
|
|
4
|
+
import { showSignIn } from './signin.js';
|
|
5
|
+
import { loadFieldModules } from './field-modules.js';
|
|
6
|
+
import { createRequestEditor } from './request-editor.js';
|
|
7
|
+
import { createNavigation } from './navigation.js';
|
|
8
|
+
import { createSidebar } from './sidebar.js';
|
|
9
|
+
import { createResults } from './results.js';
|
|
10
|
+
import { operations, dereference } from './api.js';
|
|
11
|
+
import { esc, shell } from './view.js';
|
|
12
|
+
import { stateBlock, icon, copyTextButton } from './components.js';
|
|
13
|
+
import { formatRequest } from './snippets.js';
|
|
14
|
+
import { createLayout } from './layout.js';
|
|
15
|
+
import { createIssues } from './issues.js';
|
|
16
|
+
import { matchesShortcut, shortcutAttrs, shortcutHint } from './shortcuts.js';
|
|
17
|
+
import { showShortcuts } from './shortcuts-sheet.js';
|
|
18
|
+
import { createCommands } from './commands.js';
|
|
19
|
+
import { applySettings, getSettings, resolvedTheme, tracedSendEnabled, updateSettings } from './settings.js';
|
|
20
|
+
import { current, loadLanguage, t, th } from './i18n.js';
|
|
21
|
+
import { copyText } from './copy.js';
|
|
22
|
+
import { openDisplaySettings } from './display-settings.js';
|
|
23
|
+
import { openHistory } from './history-popover.js';
|
|
24
|
+
import { closePopover } from './popover.js';
|
|
25
|
+
import { openCoverage } from './coverage-popover.js';
|
|
26
|
+
import { documentedExample, newDocsState, renderDocs } from './docs-view.js';
|
|
27
|
+
import { renderModel } from './model-view.js';
|
|
28
|
+
import { renderOverview } from './overview-view.js';
|
|
29
|
+
import { schemaName as nameOfSchema } from './schema-model.js';
|
|
30
|
+
import { bindToggletips } from './toggletip.js';
|
|
31
|
+
import { backTarget, createHistory, followModel, formatRoute, parseRoute } from './router.js';
|
|
32
|
+
import { findOperation, opKey } from './operations.js';
|
|
33
|
+
import { contextBar } from './context-bar.js';
|
|
34
|
+
import { watchCrumbs } from './crumb-fit.js';
|
|
35
|
+
import { openSeededMenu } from './seeded-menu.js';
|
|
36
|
+
import { resolvePersonaAccess } from './personas.js';
|
|
37
|
+
import { carriedValues, replacements } from './deprecation.js';
|
|
38
|
+
|
|
39
|
+
applySettings();
|
|
40
|
+
watchCrumbs();
|
|
41
|
+
const root = document.querySelector('#app');
|
|
42
|
+
for (const node of root.querySelectorAll('[data-i18n]')) node.textContent = t(node.dataset.i18n);
|
|
43
|
+
const loadingShell = root.firstElementChild.cloneNode(true);
|
|
44
|
+
const basePath = new URL('../', document.baseURI).pathname;
|
|
45
|
+
const state = {
|
|
46
|
+
cap: null, doc: null, ops: [], op: null, forms: [], bodyForm: null, body: undefined,
|
|
47
|
+
tab: 'Form', resultTab: 'Response', result: null, history: [], headers: [], documentUrl: null,
|
|
48
|
+
view: 'overview', model: null, modelFrom: null, route: { kind: 'overview' }, samples: [],
|
|
49
|
+
};
|
|
50
|
+
const $ = selector => document.querySelector(selector);
|
|
51
|
+
let documentLoad = 0;
|
|
52
|
+
let startup = 0;
|
|
53
|
+
let layout;
|
|
54
|
+
let signInEntry = location.pathname.endsWith('/signin.html');
|
|
55
|
+
/** Browser history for the workbench's routes (see router.js). */
|
|
56
|
+
let nav;
|
|
57
|
+
/** What each operation's Docs page remembers (open responses, expanded rows) while the session lasts. */
|
|
58
|
+
const docsMemory = new Map();
|
|
59
|
+
const issues = createIssues({ $, selectTab: selectRequestTab });
|
|
60
|
+
|
|
61
|
+
const {
|
|
62
|
+
renderOperation, readRequest, fillSample, autofill, addHeader, removeHeader, showBulkHeaders, toggleHeader,
|
|
63
|
+
generateField, fieldCount,
|
|
64
|
+
} = createRequestEditor({
|
|
65
|
+
state, basePath, $, updateSnippet: () => {}, onLookupPreview: () => run(true), onHeadersChange: refreshIssues,
|
|
66
|
+
sensitiveNames, onFilled: count => renderBarStatus(count),
|
|
67
|
+
});
|
|
68
|
+
const {
|
|
69
|
+
renderResult, renderBar, run, showError, copyFailedRequest, copyCurl, cancel, cancelConfirmation, previewAvailable,
|
|
70
|
+
} = createResults({
|
|
71
|
+
state, basePath, $, readRequest, sensitiveNames, renderOperation, issues, selectView: setPane, onFollowUp: followUp,
|
|
72
|
+
schemaName: schema => nameOfSchema(schema, state.doc), fillable: () => fieldCount() > 0,
|
|
73
|
+
onCompleted: (result, { preview }) => { if (!preview && state.op?.method !== 'GET') refreshIdentity(); },
|
|
74
|
+
});
|
|
75
|
+
const sidebar = createSidebar({ state, $ });
|
|
76
|
+
const { palette } = createNavigation({
|
|
77
|
+
state, $, selectOperation, run, renderOperation, openModel: name => openModel(name), commands: () => commandRegistry.available(),
|
|
78
|
+
});
|
|
79
|
+
const commandRegistry = createCommands({
|
|
80
|
+
state, $, chooseTab: selectRequestTab, chooseResult: selectResultTab, chooseOperation: selectOperation,
|
|
81
|
+
fill: async mode => { cancelConfirmation(); await fillSample(mode); refreshIssues(); },
|
|
82
|
+
preview: () => run(true), send: () => run(false, !!$('[data-action="confirm-send"]')),
|
|
83
|
+
copyResponse: () => $('.response [data-action="copy-response"]')?.click(),
|
|
84
|
+
openPalette: palette, openSheet: () => showShortcuts($('#overlay')), openSidebar: toggleNavigation,
|
|
85
|
+
changePane: setPane, issues, addHeader, bulkEdit: showBulkHeaders, toggleHeader,
|
|
86
|
+
openDisplay: () => $('#display-btn') && openDisplaySettings($('#display-btn'), { onLanguage: rerender }),
|
|
87
|
+
openHistory: () => showHistory(),
|
|
88
|
+
toggleTheme: () => updateSettings({ theme: resolvedTheme() === 'dark' ? 'light' : 'dark' }),
|
|
89
|
+
toggleDocs: () => setView(state.view === 'docs' ? 'try' : 'docs'),
|
|
90
|
+
sidebarTab: tab => { if (matchMedia('(max-width: 56rem)').matches) setNavigation(true); sidebar.selectTab(tab, { focusFilter: true }); },
|
|
91
|
+
modelBack: () => modelBack(),
|
|
92
|
+
generate: (target, menu) => generateField(target, menu),
|
|
93
|
+
toggleTrace: () => setTraced(!tracedSendEnabled(state.cap)),
|
|
94
|
+
traceAvailable: () => state.cap?.observedSend === true && previewAvailable(),
|
|
95
|
+
reloadSpec: () => start(),
|
|
96
|
+
canReload: () => !state.doc,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* After a committing Send (such as signing in or out through the API's own endpoints), ask
|
|
101
|
+
* discovery who we are now, so the top bar, locks and notices never describe a stale session.
|
|
102
|
+
*/
|
|
103
|
+
async function refreshIdentity() {
|
|
104
|
+
try {
|
|
105
|
+
const response = await fetch('./_api/capabilities', { credentials: 'include', redirect: 'error' });
|
|
106
|
+
if (!response.ok) return;
|
|
107
|
+
const capabilities = await response.json();
|
|
108
|
+
if (JSON.stringify(capabilities.identity ?? null) === JSON.stringify(state.cap.identity ?? null)) return;
|
|
109
|
+
state.cap = { ...state.cap, identity: capabilities.identity };
|
|
110
|
+
const focused = document.activeElement?.id;
|
|
111
|
+
renderShell();
|
|
112
|
+
setPane('response');
|
|
113
|
+
if (focused) document.getElementById(focused)?.focus();
|
|
114
|
+
} catch { /* Discovery is advisory here; the result stays as it is. */ }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Settings that change text (language, digits) re-render the shell in place. */
|
|
118
|
+
function rerender() {
|
|
119
|
+
if (!state.doc) return;
|
|
120
|
+
const focused = document.activeElement?.id;
|
|
121
|
+
renderShell();
|
|
122
|
+
if (focused) document.getElementById(focused)?.focus();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function start() {
|
|
126
|
+
const generation = ++startup;
|
|
127
|
+
// The UI language's catalogue loads before anything is drawn in it (English is bundled).
|
|
128
|
+
if (!current().loaded) {
|
|
129
|
+
await loadLanguage(getSettings().language);
|
|
130
|
+
applySettings();
|
|
131
|
+
for (const node of root.querySelectorAll('[data-i18n]')) node.textContent = t(node.dataset.i18n);
|
|
132
|
+
}
|
|
133
|
+
if (!root.firstElementChild?.classList.contains('loading-shell')) {
|
|
134
|
+
layout?.destroy();
|
|
135
|
+
root.replaceChildren(loadingShell.cloneNode(true));
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
const response = await fetch('./_api/capabilities', { credentials: 'include', redirect: 'error' });
|
|
139
|
+
if (!response.ok) throw Object.assign(new Error(t('boot.capabilitiesStatus', { status: response.status })), { status: response.status, url: './_api/capabilities' });
|
|
140
|
+
const capabilities = await response.json();
|
|
141
|
+
if (generation !== startup) return;
|
|
142
|
+
if (capabilities.protocolVersion !== 1 || !capabilities.openApiUrl) throw new Error(t('boot.unsupported'));
|
|
143
|
+
state.cap = capabilities;
|
|
144
|
+
// Ask the host whether it offers seeded accounts to this browser while the document loads, so
|
|
145
|
+
// the shell is drawn once with the answer and the option never appears and then vanishes.
|
|
146
|
+
const personaAccess = resolvePersonaAccess(capabilities);
|
|
147
|
+
// A login entry point must also work when the OpenAPI document requires a session.
|
|
148
|
+
if (signInEntry) {
|
|
149
|
+
signInEntry = false;
|
|
150
|
+
state.cap = await personaAccess;
|
|
151
|
+
if (generation !== startup) return;
|
|
152
|
+
const back = () => { history.replaceState(null, '', new URL('./', location.href)); return start(); };
|
|
153
|
+
showSignIn(root, state.cap, back, back);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
await loadFieldModules(capabilities.fieldModules);
|
|
157
|
+
if (generation !== startup) return;
|
|
158
|
+
if (!(await loadDocument(state.cap.openApiUrl)) || generation !== startup) return;
|
|
159
|
+
state.cap = await personaAccess;
|
|
160
|
+
if (generation !== startup) return;
|
|
161
|
+
routeFromAddress();
|
|
162
|
+
renderShell();
|
|
163
|
+
// "Sign in and retry" runs the same request again once the session exists.
|
|
164
|
+
if (state.retryAfterSignIn && state.cap.identity && state.view === 'try') {
|
|
165
|
+
state.retryAfterSignIn = false;
|
|
166
|
+
run(false);
|
|
167
|
+
}
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (generation !== startup) return;
|
|
170
|
+
showBootError(error);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The OpenAPI document (or capabilities) failed to load (mock 40). */
|
|
175
|
+
function showBootError(error) {
|
|
176
|
+
const canSignIn = [401, 403].includes(error.status) && state.cap?.authProviders?.length;
|
|
177
|
+
const url = error.url || state.cap?.openApiUrl;
|
|
178
|
+
const details = `${error.message}${url ? `\nGET ${url}` : ''}`;
|
|
179
|
+
root.innerHTML = `<div class="app">
|
|
180
|
+
<header class="topbar"><div class="brand"><span class="brand-mark" aria-hidden="true">${icon('mark')}</span><span class="desktop-only">${esc(t('app.name'))}</span></div>
|
|
181
|
+
<span class="crumb-sep desktop-only" aria-hidden="true">/</span><span class="api-switch"><span class="name" lang="en">${esc(state.cap?.apiName || '')}</span></span><span class="spacer"></span></header>
|
|
182
|
+
<div class="workspace">
|
|
183
|
+
<nav class="sidebar" aria-label="${esc(t('side.operations'))}"><div class="nav">${stateBlock({ glyph: 'list', compact: true, title: esc(t('boot.noOperations')), body: esc(t('boot.noOperationsBody')) })}</div><div class="pane-bar side-bar"></div></nav>
|
|
184
|
+
<main class="pane request" id="request" style="grid-column: 2 / -1"><div class="pane-scroll">${stateBlock({
|
|
185
|
+
glyph: 'alert', tone: 'danger', center: true, heading: 'h1', role: 'alert', title: esc(t('boot.title')),
|
|
186
|
+
body: th(error.status ? 'boot.bodyStatus' : 'boot.body', { request: `<code class="ltr">GET ${esc(url ? new URL(url, location.href).pathname : '')}</code>`, status: `<strong>${esc(error.status ?? '')}</strong>`, message: esc(error.message) }),
|
|
187
|
+
why: esc(t(error.status === 401 || error.status === 403 ? 'boot.whyAuth' : 'boot.why')),
|
|
188
|
+
actions: [
|
|
189
|
+
`<button type="button" class="btn btn-primary" data-action="restart" ${shortcutAttrs('reload-spec')}>${icon('refresh', true)}${esc(t('res.tryAgain'))} ${shortcutHint('reload-spec')}</button>`,
|
|
190
|
+
canSignIn ? `<button type="button" class="btn" data-action="signin">${icon('log-in', true)}${esc(t('shell.signIn'))}</button>` : '',
|
|
191
|
+
url ? `<a class="btn" href="${esc(url)}" target="_blank" rel="noopener noreferrer">${esc(t('boot.open', { url: new URL(url, location.href).pathname }))}</a>` : '',
|
|
192
|
+
copyTextButton(t('boot.copy'), { action: 'copy-boot-error', cls: '', data: `data-copy="${esc(details)}"` }),
|
|
193
|
+
].filter(Boolean),
|
|
194
|
+
})}</div></main>
|
|
195
|
+
</div>
|
|
196
|
+
<div class="sr-only" role="status" aria-live="polite" data-request-announcement></div>
|
|
197
|
+
</div>`;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function loadDocument(url) {
|
|
201
|
+
cancel();
|
|
202
|
+
const generation = ++documentLoad;
|
|
203
|
+
const response = await fetch(url, { credentials: 'include', redirect: 'error' });
|
|
204
|
+
if (!response.ok) throw Object.assign(new Error(t('boot.documentStatus', { status: response.status })), { status: response.status, url });
|
|
205
|
+
const document = await response.json();
|
|
206
|
+
if (generation !== documentLoad) return false;
|
|
207
|
+
Object.assign(state, {
|
|
208
|
+
doc: document, documentUrl: url, ops: operations(document), body: undefined, paramValues: {}, json: undefined,
|
|
209
|
+
headers: [], tab: 'Form', bodyMode: 'Form', history: [], lastResponse: undefined, lookupCache: new Map(),
|
|
210
|
+
autofilled: undefined, canUndoAutofill: false, result: null, view: 'overview', model: null, modelFrom: null,
|
|
211
|
+
route: { kind: 'overview' }, samples: [],
|
|
212
|
+
});
|
|
213
|
+
docsMemory.clear();
|
|
214
|
+
state.op = state.ops[0] || null;
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Makes `state` show a route: the operation, the model and where the model came from. A route
|
|
220
|
+
* that names something missing from this document falls back to the overview. Returns the
|
|
221
|
+
* route that is really shown. Operation inputs are kept when the route stays on the same
|
|
222
|
+
* operation (Try it ⇄ Docs) and reset when it moves to another one.
|
|
223
|
+
*/
|
|
224
|
+
function applyRoute(route, { reset } = {}) {
|
|
225
|
+
let shown = route;
|
|
226
|
+
if (route.kind === 'op' || route.kind === 'docs') {
|
|
227
|
+
const op = findOperation(state.ops, route.id);
|
|
228
|
+
if (!op) shown = { kind: 'overview' };
|
|
229
|
+
else {
|
|
230
|
+
if (op !== state.op || reset) resetOperation(op, reset ?? {});
|
|
231
|
+
state.view = route.kind === 'docs' ? 'docs' : 'try';
|
|
232
|
+
}
|
|
233
|
+
} else if (route.kind === 'model') {
|
|
234
|
+
if (!state.doc.components?.schemas?.[route.name]) shown = { kind: 'overview' };
|
|
235
|
+
else {
|
|
236
|
+
const origin = route.from && findOperation(state.ops, route.from.id);
|
|
237
|
+
state.view = 'model';
|
|
238
|
+
state.model = route.name;
|
|
239
|
+
state.modelFrom = origin ? { op: origin, kind: route.from.kind } : null;
|
|
240
|
+
// A model opened from an operation keeps that operation as its context.
|
|
241
|
+
if (origin && origin !== state.op) resetOperation(origin, {});
|
|
242
|
+
// With no operation to mark, the Models tab is where the model is listed.
|
|
243
|
+
if (!origin) { shown = { kind: 'model', name: route.name }; sidebar.setTab('models'); }
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (shown.kind === 'overview') { state.view = 'overview'; state.modelFrom = null; }
|
|
247
|
+
state.route = shown;
|
|
248
|
+
return shown;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Starts a fresh draft for an operation (inputs, result, issues). */
|
|
252
|
+
function resetOperation(op, parameters) {
|
|
253
|
+
if ($('#context')) cancel();
|
|
254
|
+
Object.assign(state, {
|
|
255
|
+
op, body: undefined, autofilled: undefined, canUndoAutofill: false, json: undefined,
|
|
256
|
+
paramValues: { ...parameters }, headers: [], bodyMode: 'Form', result: null,
|
|
257
|
+
tab: Object.keys(parameters).some(key => /^(path|query|cookie):/.test(key)) ? 'Params' : 'Form',
|
|
258
|
+
});
|
|
259
|
+
// Before the shell exists (a deep link on load) renderShell draws everything from state.
|
|
260
|
+
if (!$('#context')) return;
|
|
261
|
+
renderOperation();
|
|
262
|
+
issues.clear();
|
|
263
|
+
renderBar();
|
|
264
|
+
renderResult();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** The scroll offsets of the main and right columns of the page on screen. */
|
|
268
|
+
function currentScroll() {
|
|
269
|
+
const read = name => document.querySelector(`.workspace > .pane:not([hidden]) [data-scroll="${name}"]`)?.scrollTop ?? 0;
|
|
270
|
+
return { main: read('main'), aside: read('aside') };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function restoreScroll(scroll) {
|
|
274
|
+
const write = (name, value) => { const node = document.querySelector(`.workspace > .pane:not([hidden]) [data-scroll="${name}"]`); if (node) node.scrollTop = value ?? 0; };
|
|
275
|
+
write('main', scroll?.main);
|
|
276
|
+
write('aside', scroll?.aside);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Goes to a route: pushes a history entry (saving where the page being left was scrolled) and
|
|
281
|
+
* shows it from the top. `reset` starts a fresh draft of the operation with those parameters.
|
|
282
|
+
*/
|
|
283
|
+
function navigate(route, { reset, focus = true, replace = false } = {}) {
|
|
284
|
+
closePopover();
|
|
285
|
+
nav.saveScroll(currentScroll());
|
|
286
|
+
nav.go(route, { replace });
|
|
287
|
+
const shown = applyRoute(route, { reset });
|
|
288
|
+
if (shown.kind !== route.kind || shown.name !== route.name) nav.go(shown, { replace: true });
|
|
289
|
+
if (route.kind === 'op' || route.kind === 'docs') { if (sidebar.tab !== 'operations') sidebar.selectTab('operations'); }
|
|
290
|
+
applyView();
|
|
291
|
+
restoreScroll();
|
|
292
|
+
if (focus) focusPage();
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Moves focus to the page's heading, so keyboard and screen reader users land on the new page. */
|
|
296
|
+
function focusPage() {
|
|
297
|
+
const heading = state.view === 'try' ? $('#op-title') || $('#request') : $('#doc-pane h1');
|
|
298
|
+
heading?.focus?.({ preventScroll: true });
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** The route a view change on the current operation means. */
|
|
302
|
+
function routeFor(view) {
|
|
303
|
+
if (view === 'overview' || !state.op) return { kind: 'overview' };
|
|
304
|
+
return { kind: view === 'docs' ? 'docs' : 'op', id: opKey(state.op) };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Browser Back and Forward: show the entry's page and put its scroll position back. */
|
|
308
|
+
function onPopState(event) {
|
|
309
|
+
if (!state.doc) return;
|
|
310
|
+
closePopover();
|
|
311
|
+
applyRoute(parseRoute(location.hash));
|
|
312
|
+
applyView();
|
|
313
|
+
restoreScroll(event.state?.scroll);
|
|
314
|
+
setNavigation(false);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** Reads the address on load and shows that page (deep links and reloads). */
|
|
318
|
+
function routeFromAddress() {
|
|
319
|
+
nav = createHistory();
|
|
320
|
+
const route = parseRoute(location.hash);
|
|
321
|
+
const shown = applyRoute(route);
|
|
322
|
+
if (shown.kind !== route.kind || shown.name !== route.name) nav.go(shown, { replace: true });
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function renderShell() {
|
|
326
|
+
layout?.destroy();
|
|
327
|
+
root.innerHTML = shell(state.cap, state.doc.info);
|
|
328
|
+
layout = createLayout($('.workspace'), state.documentUrl);
|
|
329
|
+
if ((state.cap.documents || []).length > 1) {
|
|
330
|
+
$('.api-switch').outerHTML = `<select class="api-switch" aria-label="${esc(t('shell.apiDocument'))}">${state.cap.documents.map(d =>
|
|
331
|
+
`<option value="${esc(d.url)}" ${d.url === state.documentUrl ? 'selected' : ''}>${esc(d.name)}</option>`).join('')}</select>`;
|
|
332
|
+
$('.api-switch').addEventListener('change', async event => {
|
|
333
|
+
const expectedLoad = documentLoad + 1;
|
|
334
|
+
try {
|
|
335
|
+
if (!(await loadDocument(event.target.value))) return;
|
|
336
|
+
renderShell();
|
|
337
|
+
} catch (error) {
|
|
338
|
+
if (documentLoad === expectedLoad) showBootError(error);
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
$('#filter').addEventListener('input', () => sidebar.render());
|
|
343
|
+
$('#filter').addEventListener('keydown', event => {
|
|
344
|
+
if (event.key === 'Escape' && event.target.value) { event.preventDefault(); event.stopPropagation(); clearFilter(); }
|
|
345
|
+
});
|
|
346
|
+
bindToggletips(root);
|
|
347
|
+
sidebar.render();
|
|
348
|
+
renderOperation();
|
|
349
|
+
issues.clear();
|
|
350
|
+
renderBar();
|
|
351
|
+
renderResult();
|
|
352
|
+
applyView();
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Shows the page in state.view. Try it keeps its request and result panes; Docs, Models and the
|
|
357
|
+
* overview fill the same two columns with a reading pane and an aside pane, so the sidebar, the
|
|
358
|
+
* dividers and the head and bottom lines never move between views.
|
|
359
|
+
*/
|
|
360
|
+
function applyView() {
|
|
361
|
+
const workspace = $('.workspace');
|
|
362
|
+
const reading = state.view !== 'try';
|
|
363
|
+
const main = $('#doc-pane');
|
|
364
|
+
const aside = $('#aside-pane');
|
|
365
|
+
workspace.classList.toggle('docs-mode', reading);
|
|
366
|
+
$('#request').hidden = reading;
|
|
367
|
+
$('.response').hidden = reading;
|
|
368
|
+
main.hidden = aside.hidden = !reading;
|
|
369
|
+
$('.mobile-switch').hidden = reading;
|
|
370
|
+
const route = state.route;
|
|
371
|
+
const ui = state.op ? docsMemory.get(opKey(state.op)) ?? docsMemory.set(opKey(state.op), newDocsState()).get(opKey(state.op)) : newDocsState();
|
|
372
|
+
const context = { state, route, ui, samples: state.samples, $, basePath };
|
|
373
|
+
if (state.view === 'docs') { renderDocs(main, aside, context); main.setAttribute('aria-labelledby', 'doc-title'); aside.setAttribute('aria-label', t('docs.aside')); }
|
|
374
|
+
else if (state.view === 'model') { renderModel(main, aside, context); main.setAttribute('aria-labelledby', 'model-title'); aside.setAttribute('aria-label', t('model.aside')); }
|
|
375
|
+
else if (state.view === 'overview') { renderOverview(main, aside, context); main.setAttribute('aria-labelledby', 'ov-title'); aside.setAttribute('aria-label', t('overview.start')); }
|
|
376
|
+
else {
|
|
377
|
+
main.replaceChildren();
|
|
378
|
+
aside.replaceChildren();
|
|
379
|
+
$('#context').innerHTML = contextBar(state.op ? { kind: 'op', op: state.op, view: 'try' } : { kind: 'overview' });
|
|
380
|
+
}
|
|
381
|
+
for (const pane of [main, aside]) for (const node of pane.querySelectorAll('.pane-scroll')) node.dataset.scroll = pane === main ? 'main' : 'aside';
|
|
382
|
+
$('#request .pane-scroll').dataset.scroll = 'main';
|
|
383
|
+
$('.skip-link').setAttribute('href', reading ? '#doc-pane' : '#request');
|
|
384
|
+
layout?.refresh();
|
|
385
|
+
sidebar.render();
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** Switches between the overview, Try it and Docs of the current operation. */
|
|
389
|
+
function setView(view, { focus = false } = {}) {
|
|
390
|
+
closePopover();
|
|
391
|
+
navigate(routeFor(view), { focus });
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** Opens a model page from the Models tab, the palette or a link without an operation. */
|
|
395
|
+
function openModel(name) {
|
|
396
|
+
if (!state.doc.components?.schemas?.[name]) return;
|
|
397
|
+
navigate({ kind: 'model', name });
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** Back from a model page: to the page it was opened from, restoring that page's scroll position. */
|
|
401
|
+
function modelBack() {
|
|
402
|
+
const target = state.view === 'model' ? backTarget(state.route) : null;
|
|
403
|
+
if (!target) return false;
|
|
404
|
+
// When the browser's previous entry is that page, going back restores its scroll position;
|
|
405
|
+
// after a reload or a deep link there is no such entry, so Back pushes the page instead.
|
|
406
|
+
if (nav.previousIs(formatRoute(target.route))) nav.back();
|
|
407
|
+
else navigate(target.route);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function sensitiveNames() {
|
|
411
|
+
return Object.values(state.doc?.components?.securitySchemes || {})
|
|
412
|
+
.map(scheme => dereference(scheme, state.doc))
|
|
413
|
+
.filter(scheme => scheme?.type === 'apiKey' && scheme.name)
|
|
414
|
+
.map(scheme => scheme.name);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Opens an operation in Try it or Docs. `parameters` start a fresh draft with those values (used
|
|
419
|
+
* by "open in" follow-ups); without them the draft is kept when the operation is already open.
|
|
420
|
+
*/
|
|
421
|
+
function selectOperation(index, parameters, view = 'try') {
|
|
422
|
+
const op = state.ops[index];
|
|
423
|
+
if (!op) return;
|
|
424
|
+
cancel();
|
|
425
|
+
setNavigation(false);
|
|
426
|
+
setPane('request');
|
|
427
|
+
navigate({ kind: view === 'docs' ? 'docs' : 'op', id: opKey(op) }, { reset: parameters });
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function followUp(operation, parameters) {
|
|
431
|
+
const index = state.ops.findIndex(item => item.method === operation.method && item.path === operation.path);
|
|
432
|
+
if (index < 0) return;
|
|
433
|
+
selectOperation(index, parameters);
|
|
434
|
+
$('#request').focus();
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** On mobile, one pane at a time: the Request/Result switch. */
|
|
438
|
+
function setPane(view, focus = false) {
|
|
439
|
+
const workspace = $('.workspace');
|
|
440
|
+
if (!workspace) return;
|
|
441
|
+
// The mock's mobile rule hides the other pane by data-view="request" | "result".
|
|
442
|
+
workspace.dataset.view = view === 'response' ? 'result' : view;
|
|
443
|
+
for (const button of document.querySelectorAll('.mobile-switch button'))
|
|
444
|
+
button.setAttribute('aria-pressed', String(button.dataset.view === view));
|
|
445
|
+
if (focus) (view === 'request' ? $('#request') : $('.response')).focus();
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** Opens or closes the sidebar drawer on a phone. The drawer is the same sidebar. */
|
|
449
|
+
function setNavigation(open) {
|
|
450
|
+
const side = $('.sidebar');
|
|
451
|
+
if (!side) return;
|
|
452
|
+
side.classList.toggle('is-drawer', open);
|
|
453
|
+
$('.drawer-scrim')?.remove();
|
|
454
|
+
if (open) side.after(Object.assign(document.createElement('div'), { className: 'drawer-scrim' }));
|
|
455
|
+
const button = $('[data-action="navigation"]');
|
|
456
|
+
button?.setAttribute('aria-expanded', String(open));
|
|
457
|
+
button?.setAttribute('aria-label', t(open ? 'shell.closeOperations' : 'shell.openOperations'));
|
|
458
|
+
if (open) $('#filter').focus();
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function toggleNavigation(open) {
|
|
462
|
+
if (matchMedia('(max-width: 56rem)').matches) {
|
|
463
|
+
setNavigation(open ?? !$('.sidebar').classList.contains('is-drawer'));
|
|
464
|
+
} else {
|
|
465
|
+
const workspace = $('.workspace');
|
|
466
|
+
const show = open ?? workspace.dataset.sidebar === 'closed';
|
|
467
|
+
workspace.dataset.sidebar = show ? 'open' : 'closed';
|
|
468
|
+
layout.refresh();
|
|
469
|
+
if (show && open === true) $('#filter').focus();
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function selectRequestTab(next, { focus = false, report = true } = {}) {
|
|
474
|
+
if (next === 'Body') next = state.bodyMode || 'Form';
|
|
475
|
+
const nextBody = next === 'Form' || next === 'JSON';
|
|
476
|
+
if (state.forms.some(form => form.hasParseErrors())) {
|
|
477
|
+
$('#editor textarea[aria-invalid=true]')?.focus();
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (next === 'Form' && state.json !== undefined) {
|
|
481
|
+
try {
|
|
482
|
+
state.body = state.json.trim() ? JSON.parse(state.json) : undefined;
|
|
483
|
+
state.json = undefined;
|
|
484
|
+
} catch {
|
|
485
|
+
if (state.tab !== 'JSON') { state.tab = 'JSON'; renderOperation(); }
|
|
486
|
+
issues.show([{ path: '$', message: t('req.fixJson') }]);
|
|
487
|
+
$('#json').focus();
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
state.tab = next;
|
|
492
|
+
if (nextBody) state.bodyMode = next;
|
|
493
|
+
if (state.view !== 'try') setView('try');
|
|
494
|
+
renderOperation();
|
|
495
|
+
if (report) refreshIssues();
|
|
496
|
+
if (focus) (nextBody ? $(`[data-body-mode="${next}"]`) : $(`[data-request-tab="${next}"]`))?.focus();
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function selectResultTab(tab, focus = false) {
|
|
500
|
+
if ($('.response').getAttribute('aria-busy') === 'true' && !state.running) return;
|
|
501
|
+
state.resultTab = tab;
|
|
502
|
+
renderResult();
|
|
503
|
+
if (focus) $(`[data-result-tab="${tab}"]`)?.focus();
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function refreshIssues() {
|
|
507
|
+
if (!issues.active) return;
|
|
508
|
+
if (issues.server) { issues.annotate(); return; }
|
|
509
|
+
try {
|
|
510
|
+
readRequest();
|
|
511
|
+
issues.clear();
|
|
512
|
+
} catch (error) {
|
|
513
|
+
if (error.validation) issues.show(error.issues);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** After generated values, the status line says how many and offers Undo. */
|
|
518
|
+
function renderBarStatus(count) {
|
|
519
|
+
if (count) $('.actionbar .status')?.replaceWith(Object.assign(document.createElement('template'), {
|
|
520
|
+
innerHTML: `<span class="status is-ok" role="status">${icon('wand', true)}<span>${esc(t('bar.filled', { n: count }))}</span><button type="button" class="btn btn-ghost btn-xs" data-action="undo-fill" ${shortcutAttrs('autofill-undo')}>${esc(t('bar.undo'))} ${shortcutHint('autofill-undo')}</button></span>`,
|
|
521
|
+
}).content.firstElementChild);
|
|
522
|
+
else renderBar();
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function setTraced(on) {
|
|
526
|
+
updateSettings({ tracedSend: on });
|
|
527
|
+
renderResult();
|
|
528
|
+
$('[data-request-announcement]').textContent = t(on ? 'res.tracedOnDone' : 'res.tracedOffDone');
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function clearFilter() {
|
|
532
|
+
const input = $('#filter');
|
|
533
|
+
input.value = '';
|
|
534
|
+
sidebar.render();
|
|
535
|
+
input.focus();
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function showHistory(button) {
|
|
539
|
+
const anchor = button?.isConnected ? button : $('#history-btn')?.getClientRects().length ? $('#history-btn') : $('.result-bar [data-action="history"]');
|
|
540
|
+
if (!anchor) return palette(true);
|
|
541
|
+
openHistory(anchor, { state, replay: replayHistory });
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function replayHistory(entry) {
|
|
545
|
+
const index = state.ops.findIndex(op => op.method === entry.op.method && op.path === entry.op.path);
|
|
546
|
+
if (index < 0) return;
|
|
547
|
+
selectOperation(index);
|
|
548
|
+
state.body = structuredClone(entry.body);
|
|
549
|
+
state.paramValues = structuredClone(entry.params);
|
|
550
|
+
state.headers = structuredClone(entry.headers || []);
|
|
551
|
+
renderOperation();
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function openSignIn() {
|
|
555
|
+
cancel();
|
|
556
|
+
closePopover();
|
|
557
|
+
layout?.destroy();
|
|
558
|
+
startup++;
|
|
559
|
+
documentLoad++;
|
|
560
|
+
state.history = [];
|
|
561
|
+
state.result = null;
|
|
562
|
+
const back = () => start();
|
|
563
|
+
showSignIn(root, state.cap, back, back);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* "Try it" in the Docs bar: opens the operation with the documented example in the request body,
|
|
568
|
+
* unless the user already typed something there.
|
|
569
|
+
*/
|
|
570
|
+
function openDocumentedExample() {
|
|
571
|
+
const op = state.op;
|
|
572
|
+
navigate(routeFor('try'));
|
|
573
|
+
const body = op && dereference(op.operation.requestBody, state.doc);
|
|
574
|
+
const media = body?.content?.['application/json'] ?? Object.values(body?.content || {})[0];
|
|
575
|
+
if (media?.schema && state.body === undefined && state.json === undefined) {
|
|
576
|
+
state.body = structuredClone(documentedExample(media, state.doc, state.samples));
|
|
577
|
+
renderOperation();
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/** Copies the URL with path parameters shown as {name}, exactly as documented. */
|
|
582
|
+
function copyUrl(button) {
|
|
583
|
+
const source = $('.urlbar .url');
|
|
584
|
+
const url = [...source.childNodes].map(node => node.dataset?.pathParam ? '{' + node.dataset.pathParam + '}' : node.textContent).join('');
|
|
585
|
+
return copyText(url, { button, what: t('copy.what.url'), select: source, notice: $('.urlbar') });
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
const handlers = {
|
|
589
|
+
restart: () => start(),
|
|
590
|
+
navigation: () => toggleNavigation(),
|
|
591
|
+
palette: () => palette(),
|
|
592
|
+
history: button => showHistory(button),
|
|
593
|
+
coverage: button => openCoverage(button, { capabilities: state.cap }),
|
|
594
|
+
// On a phone the docked Preview reason shows its title and the author's reason; "Why?" adds the rest.
|
|
595
|
+
'dock-why': button => { const open = button.getAttribute('aria-expanded') !== 'true'; button.setAttribute('aria-expanded', String(open)); button.closest('[data-preview-dock]')?.classList.toggle('is-open', open); },
|
|
596
|
+
shortcuts: () => showShortcuts($('#overlay')),
|
|
597
|
+
display: button => openDisplaySettings(button, { onLanguage: rerender }),
|
|
598
|
+
close: () => { $('#overlay').innerHTML = ''; },
|
|
599
|
+
signin: () => openSignIn(),
|
|
600
|
+
'signin-retry': () => { state.retryAfterSignIn = true; openSignIn(); },
|
|
601
|
+
'cancel-request': () => cancel(),
|
|
602
|
+
'retry-request': () => state.result?.network && run(state.result.preview),
|
|
603
|
+
'copy-failed-curl': button => copyFailedRequest(button),
|
|
604
|
+
'copy-curl': button => copyCurl(button),
|
|
605
|
+
'copy-url': button => copyUrl(button),
|
|
606
|
+
'copy-boot-error': button => copyText(button.dataset.copy, { button, what: t('copy.what.error') }),
|
|
607
|
+
'cancel-confirm': () => cancelConfirmation(),
|
|
608
|
+
'confirm-send': () => run(false, true),
|
|
609
|
+
issues: () => issues.toggle(),
|
|
610
|
+
sample: button => autofill(button, refreshIssues),
|
|
611
|
+
'undo-fill': async () => { await fillSample('undo'); refreshIssues(); },
|
|
612
|
+
send: () => run(false),
|
|
613
|
+
preview: () => run(true),
|
|
614
|
+
'view-try': () => setView('try', { focus: true }),
|
|
615
|
+
'view-docs': () => setView('docs', { focus: true }),
|
|
616
|
+
'docs-try': () => openDocumentedExample(),
|
|
617
|
+
'seeded-menu': button => openSeededMenu(button, { capabilities: state.cap, onSignedIn: () => refreshIdentity() }),
|
|
618
|
+
'toggle-groups': () => sidebar.toggleAll(),
|
|
619
|
+
'traced-on': () => setTraced(true),
|
|
620
|
+
'traced-off': () => setTraced(false),
|
|
621
|
+
'clear-filter': () => clearFilter(),
|
|
622
|
+
overview: () => setView('overview', { focus: true }),
|
|
623
|
+
'model-back': () => modelBack(),
|
|
624
|
+
'add-header': () => addHeader(),
|
|
625
|
+
'remove-header': button => removeHeader(button.closest('[data-header-row]')?.dataset.headerId),
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
root.addEventListener('click', async event => {
|
|
629
|
+
if (event.target.closest('#editor') || event.target.closest('[data-action="sample"]')) cancelConfirmation();
|
|
630
|
+
// Links that are plain navigation keep the browser's own behaviour for new tabs and windows.
|
|
631
|
+
const plain = !(event.button || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey);
|
|
632
|
+
const crumb = event.target.closest('[data-crumb-route]');
|
|
633
|
+
if (crumb && plain) { event.preventDefault(); navigate(parseRoute(new URL(crumb.href).hash)); return; }
|
|
634
|
+
const tag = event.target.closest('[data-crumb-tag]');
|
|
635
|
+
if (tag) { event.preventDefault(); sidebar.reveal('ops', tag.dataset.crumbTag); return; }
|
|
636
|
+
const modelsCrumb = event.target.closest('[data-crumb-models]');
|
|
637
|
+
if (modelsCrumb) { event.preventDefault(); sidebar.selectTab('models'); $('#side-tab-models')?.focus(); return; }
|
|
638
|
+
const modelGroup = event.target.closest('[data-crumb-model-group]');
|
|
639
|
+
if (modelGroup) { event.preventDefault(); sidebar.reveal('models', modelGroup.dataset.crumbModelGroup); return; }
|
|
640
|
+
// "Open Get balance" on a deprecated operation: the replacement, with matching inputs carried over.
|
|
641
|
+
const replace = event.target.closest('[data-replace-op]');
|
|
642
|
+
if (replace) {
|
|
643
|
+
event.preventDefault();
|
|
644
|
+
const target = state.ops[Number(replace.dataset.replaceOp)];
|
|
645
|
+
const replacement = state.op && replacements(state.op, state.ops, state.doc).find(item => item.op === target);
|
|
646
|
+
selectOperation(Number(replace.dataset.replaceOp), replacement ? carriedValues(replacement, state.paramValues || {}) : undefined, 'try');
|
|
647
|
+
$('#request')?.focus();
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
const op = event.target.closest('[data-op], [data-op-docs]');
|
|
651
|
+
if (op && !(op.matches('a') && !plain)) {
|
|
652
|
+
event.preventDefault();
|
|
653
|
+
const fromDrawer = $('.sidebar').classList.contains('is-drawer');
|
|
654
|
+
const docs = op.dataset.opDocs !== undefined || op.dataset.opView === 'docs' || (state.view === 'docs' && op.closest('.sidebar'));
|
|
655
|
+
selectOperation(Number(op.dataset.op ?? op.dataset.opDocs), undefined, docs ? 'docs' : 'try');
|
|
656
|
+
if (fromDrawer && !docs) $('#request').focus();
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
const picked = event.target.closest('[data-pick-model]');
|
|
660
|
+
if (picked && plain) {
|
|
661
|
+
event.preventDefault();
|
|
662
|
+
setNavigation(false);
|
|
663
|
+
openModel(picked.dataset.pickModel);
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
const model = event.target.closest('[data-model]');
|
|
667
|
+
if (model && plain) {
|
|
668
|
+
event.preventDefault();
|
|
669
|
+
navigate(followModel(state.route, model.dataset.model));
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
const sideTab = event.target.closest('[data-side-tab]');
|
|
673
|
+
if (sideTab) { sidebar.selectTab(sideTab.dataset.sideTab); sideTab.focus(); return; }
|
|
674
|
+
const suggestion = event.target.closest('[data-filter-suggestion]');
|
|
675
|
+
if (suggestion) { $('#filter').value = suggestion.dataset.filterSuggestion; sidebar.render(); $('#filter').focus(); return; }
|
|
676
|
+
const heading = event.target.closest('.nav-heading');
|
|
677
|
+
if (heading) { sidebar.toggleGroup(heading); return; }
|
|
678
|
+
if (event.target.classList?.contains('drawer-scrim')) { setNavigation(false); $('[data-action="navigation"]')?.focus(); return; }
|
|
679
|
+
const pane = event.target.closest('.mobile-switch [data-view]');
|
|
680
|
+
if (pane) { setPane(pane.dataset.view); return; }
|
|
681
|
+
const pathToken = event.target.closest('[data-path-param]');
|
|
682
|
+
if (pathToken) {
|
|
683
|
+
selectRequestTab('Params');
|
|
684
|
+
const parameter = [...document.querySelectorAll('[data-parameter-name]')].find(node => node.dataset.parameterName === pathToken.dataset.pathParam);
|
|
685
|
+
// A path token focuses its field; a lookup or select opens its list too.
|
|
686
|
+
const control = parameter?.querySelector('input, textarea, [role="combobox"], [role="radio"][tabindex="0"]') ?? parameter?.querySelector('.control button');
|
|
687
|
+
control?.focus();
|
|
688
|
+
control?.openOptions?.();
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
const requestTab = event.target.closest('[data-request-tab]');
|
|
692
|
+
if (requestTab) { selectRequestTab(requestTab.dataset.requestTab); $(`[data-request-tab="${requestTab.dataset.requestTab}"]`)?.focus(); return; }
|
|
693
|
+
const bodyMode = event.target.closest('[data-body-mode]');
|
|
694
|
+
if (bodyMode) { selectRequestTab(bodyMode.dataset.bodyMode, { focus: true }); return; }
|
|
695
|
+
const resultTab = event.target.closest('[data-result-tab]');
|
|
696
|
+
if (resultTab) { selectResultTab(resultTab.dataset.resultTab, true); return; }
|
|
697
|
+
const button = event.target.closest('[data-action]');
|
|
698
|
+
const handler = handlers[button?.dataset.action];
|
|
699
|
+
if (handler) {
|
|
700
|
+
if (button.tagName === 'A') event.preventDefault();
|
|
701
|
+
await handler(button);
|
|
702
|
+
}
|
|
703
|
+
});
|
|
704
|
+
root.addEventListener('input', event => {
|
|
705
|
+
if (!event.target.closest('#editor')) return;
|
|
706
|
+
cancelConfirmation();
|
|
707
|
+
if (issues.server) issues.edit(event.target);
|
|
708
|
+
else refreshIssues();
|
|
709
|
+
});
|
|
710
|
+
root.addEventListener('change', event => {
|
|
711
|
+
if (!event.target.closest('#editor')) return;
|
|
712
|
+
cancelConfirmation();
|
|
713
|
+
refreshIssues();
|
|
714
|
+
});
|
|
715
|
+
addEventListener('popstate', onPopState);
|
|
716
|
+
|
|
717
|
+
document.addEventListener('keydown', event => {
|
|
718
|
+
if (!state.doc && matchesShortcut('reload-spec', event) && $('[data-action="restart"]')) { event.preventDefault(); start(); return; }
|
|
719
|
+
if (!$('.workspace') || event.defaultPrevented || event.isComposing) return;
|
|
720
|
+
const tab = event.target.closest?.('[role="tab"]');
|
|
721
|
+
const rtl = document.documentElement.dir === 'rtl';
|
|
722
|
+
const tabKey = ['left', 'right', 'home', 'end'].find(id => matchesShortcut(id, event));
|
|
723
|
+
if (tab && tabKey) {
|
|
724
|
+
const tabs = [...tab.closest('[role="tablist"]').querySelectorAll('[role="tab"]')];
|
|
725
|
+
const index = tabs.indexOf(tab);
|
|
726
|
+
const forward = (tabKey === 'right') !== rtl;
|
|
727
|
+
const next = tabKey === 'home' ? 0 : tabKey === 'end' ? tabs.length - 1 : (index + (forward ? 1 : -1) + tabs.length) % tabs.length;
|
|
728
|
+
event.preventDefault();
|
|
729
|
+
tabs[next].click();
|
|
730
|
+
tabs[next].focus();
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
if (matchesShortcut('escape', event)) {
|
|
734
|
+
if ($('#issues')) issues.close(true);
|
|
735
|
+
else if ($('.sidebar')?.classList.contains('is-drawer')) { setNavigation(false); $('[data-action="navigation"]').focus(); }
|
|
736
|
+
else cancel();
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
commandRegistry.handle(event);
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
start();
|