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,39 @@
|
|
|
1
|
+
// A small, safe CommonMark subset for OpenAPI descriptions: paragraphs, bullet and numbered
|
|
2
|
+
// lists, headings, **bold**, *emphasis*, `code` and http(s) links. Everything else is escaped.
|
|
3
|
+
import { esc } from './components.js';
|
|
4
|
+
|
|
5
|
+
/** Inline markup only, for one-line descriptions. */
|
|
6
|
+
export function inline(text) {
|
|
7
|
+
return esc(text)
|
|
8
|
+
.replace(/`([^`]+)`/g, '<code>$1</code>')
|
|
9
|
+
.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
|
|
10
|
+
.replace(/(^|[^*])\*([^*\s][^*]*)\*/g, '$1<em>$2</em>')
|
|
11
|
+
.replace(/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g, '<a class="link" href="$2" target="_blank" rel="noopener noreferrer">$1</a>');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Block markup: paragraphs, lists and headings. */
|
|
15
|
+
export function markdown(text) {
|
|
16
|
+
const blocks = String(text ?? '').replace(/\r\n/g, '\n').trim().split(/\n\s*\n/);
|
|
17
|
+
return blocks.map(block => {
|
|
18
|
+
const lines = block.split('\n');
|
|
19
|
+
if (lines.every(line => /^\s*[-*+]\s+/.test(line))) return `<ul>${lines.map(line => `<li>${inline(line.replace(/^\s*[-*+]\s+/, ''))}</li>`).join('')}</ul>`;
|
|
20
|
+
if (lines.every(line => /^\s*\d+[.)]\s+/.test(line))) return `<ol>${lines.map(line => `<li>${inline(line.replace(/^\s*\d+[.)]\s+/, ''))}</li>`).join('')}</ol>`;
|
|
21
|
+
const heading = /^(#{1,6})\s+(.*)$/.exec(lines[0]);
|
|
22
|
+
if (heading && lines.length === 1) return `<h4>${inline(heading[2])}</h4>`;
|
|
23
|
+
return `<p>${lines.map(inline).join(' ')}</p>`;
|
|
24
|
+
}).join('');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Descriptions often name another operation by its operationId (`BalancesController.Get`). This
|
|
29
|
+
* turns each such code span into a link with the operation's method and name: "GET Get balance".
|
|
30
|
+
* `ops` are the discovered operations; `view` is 'docs' or 'op' for the link's route.
|
|
31
|
+
*/
|
|
32
|
+
export function linkOperationIds(html, ops, view = 'docs') {
|
|
33
|
+
return html.replace(/<code>([A-Za-z_][\w.-]*)<\/code>/g, (match, id) => {
|
|
34
|
+
const index = ops.findIndex(op => op.operation?.operationId === id);
|
|
35
|
+
if (index < 0) return match;
|
|
36
|
+
const op = ops[index];
|
|
37
|
+
return `<a class="op-ref" href="#${view}/${encodeURIComponent(id)}" data-op="${index}"${view === 'docs' ? ' data-op-view="docs"' : ''} title="${esc(id)}"><span class="method m-${esc(op.method.toLowerCase())}">${esc(op.method.toUpperCase())}</span><span lang="en">${esc(op.title)}</span></a>`;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// A model page (mocks/models.js), in the same two columns as Try it and Docs. The main column is
|
|
2
|
+
// the property tree (or a union with its discriminator and variants, or an enum's values); the
|
|
3
|
+
// right column lists the operations and models that use it, then an example or the raw JSON
|
|
4
|
+
// Schema. Where the user came from decides the breadcrumbs and the one Back control, not the
|
|
5
|
+
// sidebar tab.
|
|
6
|
+
import { dereference } from './api.js';
|
|
7
|
+
import { copyButton, esc, icon, method, tab } from './components.js';
|
|
8
|
+
import { contextBar } from './context-bar.js';
|
|
9
|
+
import { copyText } from './copy.js';
|
|
10
|
+
import { formatNumber, t } from './i18n.js';
|
|
11
|
+
import { bindJsonViews, hasCollapsed, jsonView, setAllFolded } from './json-view.js';
|
|
12
|
+
import { inline, markdown } from './markdown.js';
|
|
13
|
+
import {
|
|
14
|
+
discriminatorOf, enumOf, isExpandable, modelExample, modelKind, modelSize, propertiesOf, refName, resolve, typeLabel, usedBy, variantsOf,
|
|
15
|
+
} from './schema-model.js';
|
|
16
|
+
import { bindTrees, hasNested, isAllOpen, newTreeState, propertyTree, setAllOpen } from './schema-tree.js';
|
|
17
|
+
import { opKey } from './operations.js';
|
|
18
|
+
|
|
19
|
+
/** What a model page remembers while the session lasts: the chosen variant, the tree state and the right column's view. */
|
|
20
|
+
const memory = new Map();
|
|
21
|
+
const remembered = (name, scope) => { if (!memory.has(name)) memory.set(name, { variant: null, tree: newTreeState(scope ? `${scope}#model/${name}` : undefined), aside: 'example' }); return memory.get(name); };
|
|
22
|
+
|
|
23
|
+
const where = row => [row.request ? t('used.request') : '', row.codes.length ? t('used.response', { code: row.codes.join(' · ') }) : ''].filter(Boolean).join(' · ');
|
|
24
|
+
const rowTitle = op => op.operation.deprecated ? ' style="text-decoration:line-through"' : '';
|
|
25
|
+
|
|
26
|
+
/** The kind chip: "object · 12 properties", "enum · 9 values", "one of 2 types". */
|
|
27
|
+
function kindLabel(schema, kind, document, size) {
|
|
28
|
+
if (kind.id === 'object') return t('kind.objectN', { n: size });
|
|
29
|
+
if (kind.id === 'enum') return t('kind.enumN', { n: size });
|
|
30
|
+
if (kind.id === 'oneOf' || kind.id === 'anyOf') return t('kind.oneOfN', { n: size });
|
|
31
|
+
return t('kind.' + kind.id);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** An enum's page (mock 96): "Possible enum values", each with its description. */
|
|
35
|
+
function enumBody(schema, document) {
|
|
36
|
+
const en = enumOf(schema, document) || { values: schema.enum || [], desc: {}, described: false };
|
|
37
|
+
const list = en.described
|
|
38
|
+
? `<dl class="enum-dl is-page">${en.values.map(value => `<div><dt><code>${esc(value)}</code></dt>${en.desc[String(value)] ? `<dd lang="en">${inline(en.desc[String(value)])}</dd>` : `<dd class="is-quiet">${esc(t('schema.noDesc'))}</dd>`}</div>`).join('')}</dl>`
|
|
39
|
+
: `<p class="attr-desc is-quiet">${esc(t('model.valuesUndescribed'))}</p><ul class="enum-inline is-page">${en.values.map(value => `<li><code>${esc(value)}</code></li>`).join('')}</ul>`;
|
|
40
|
+
return `<h2>${esc(t('attr.enumValues'))} <span class="count">${formatNumber(en.values.length)} · ${esc(schema.type || 'string')}</span></h2>
|
|
41
|
+
<div class="attr-enum is-page">${list}</div>`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function unionBody(name, schema, document, mem) {
|
|
45
|
+
const variants = variantsOf(schema, document);
|
|
46
|
+
const discriminator = discriminatorOf(schema, document);
|
|
47
|
+
const options = variants.map((variant, index) => ({ raw: variant, name: refName(variant) || resolve(variant, document).title || t('model.option', { n: index + 1 }) }));
|
|
48
|
+
const chosen = options.find(option => option.name === mem.variant) || options[0];
|
|
49
|
+
mem.variant = chosen.name;
|
|
50
|
+
const { props, required } = propertiesOf(chosen.raw, document);
|
|
51
|
+
const count = Object.keys(props).length;
|
|
52
|
+
return {
|
|
53
|
+
chosen,
|
|
54
|
+
html: `${discriminator ? `<h2>${esc(t('model.discriminator'))} <span class="count">${esc(t('model.discBy'))} <code>${esc(discriminator.prop)}</code></span></h2>
|
|
55
|
+
<div class="card disc-table">${discriminator.map.map(([value, target]) => `<div class="row"><code>${esc(discriminator.prop)} = "${esc(value)}"</code><span class="faint" aria-hidden="true">${icon('arrow-right', true)}</span><a class="type-link" href="#model/${encodeURIComponent(target)}" data-model="${esc(target)}">${esc(target)}</a><span class="faint" style="font-size:var(--fs-sm)">${esc(t('docs.props', { n: modelSize({ $ref: `#/components/schemas/${encodeURIComponent(target)}` }, document) }))}</span></div>`).join('')}</div>` : ''}
|
|
56
|
+
<h2>${esc(t('model.variants'))} <span class="count">${esc(t('model.exactlyOne', { n: options.length }))}</span></h2>
|
|
57
|
+
<div class="card">
|
|
58
|
+
<div class="tabs" role="tablist" aria-label="${esc(t('schema.variant'))}" style="padding-inline:0.25rem">${options.map((option, index) => tab(option.name, { selected: option === chosen, count: modelSize(option.raw, document), id: `variant-tab-${index}`, controls: 'variant-panel', data: `data-variant-tab="${esc(option.name)}"`, lang: 'en' })).join('')}</div>
|
|
59
|
+
<div id="variant-panel" role="tabpanel" aria-labelledby="variant-tab-${options.indexOf(chosen)}">
|
|
60
|
+
<div class="card-head"><span>${esc(t('docs.props', { n: count }))} · ${esc(t('docs.requiredN', { n: required.size }))}</span><span class="end">${refName(chosen.raw) ? `<a class="btn btn-ghost btn-xs" href="#model/${encodeURIComponent(chosen.name)}" data-model="${esc(chosen.name)}">${esc(t('model.open', { name: chosen.name }))}${icon('arrow-right', true)}</a>` : ''}</span></div>
|
|
61
|
+
<div data-tree-slot></div>
|
|
62
|
+
</div>
|
|
63
|
+
</div>`,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Renders a model page into the two reading panes.
|
|
69
|
+
* @param {HTMLElement} main the `.doc-pane`
|
|
70
|
+
* @param {HTMLElement} aside the `.aside-pane`
|
|
71
|
+
* @param {{ state: object, route: object, samples: unknown[], bases?: object }} ctx
|
|
72
|
+
*/
|
|
73
|
+
export function renderModel(main, aside, ctx) {
|
|
74
|
+
const { state, route, samples } = ctx;
|
|
75
|
+
const name = route.name;
|
|
76
|
+
const document = state.doc;
|
|
77
|
+
const raw = { $ref: '#/components/schemas/' + encodeURIComponent(name) };
|
|
78
|
+
const schema = dereference(raw, document) || {};
|
|
79
|
+
const kind = modelKind(schema, document);
|
|
80
|
+
const mem = remembered(name, state.documentUrl);
|
|
81
|
+
const size = modelSize(schema, document);
|
|
82
|
+
const from = state.modelFrom?.op;
|
|
83
|
+
let body = '';
|
|
84
|
+
let treeTarget = raw;
|
|
85
|
+
let chosen;
|
|
86
|
+
if (kind.id === 'oneOf' || kind.id === 'anyOf') {
|
|
87
|
+
const union = unionBody(name, schema, document, mem);
|
|
88
|
+
body = union.html;
|
|
89
|
+
chosen = union.chosen;
|
|
90
|
+
treeTarget = chosen.raw;
|
|
91
|
+
} else if (kind.id === 'enum') body = enumBody(schema, document);
|
|
92
|
+
else {
|
|
93
|
+
const { props, required, sources } = propertiesOf(raw, document);
|
|
94
|
+
const count = Object.keys(props).length;
|
|
95
|
+
const label = typeLabel(raw, document);
|
|
96
|
+
body = count || kind.id === 'object' ? `${sources.length ? `<p class="lede">${esc(t('model.includes'))} ${sources.map(source => `<a class="type-link" href="#model/${encodeURIComponent(source)}" data-model="${esc(source)}">${esc(source)}</a>`).join(', ')}</p>` : ''}
|
|
97
|
+
<h2>${esc(t('model.propertiesTitle'))} <span class="count">${esc(t('docs.props', { n: count }))} · ${esc(t('docs.requiredN', { n: required.size }))}</span></h2>
|
|
98
|
+
<div class="card flat">
|
|
99
|
+
${hasNested(raw, document) ? `<div class="card-head"><span>${esc(t('model.nested', { n: Object.values(props).filter(child => isExpandable(child, document)).length }))}</span>
|
|
100
|
+
<span class="end"><button type="button" class="btn btn-ghost btn-xs" data-tree-all>${icon('chevrons-ud', true)}<span>${esc(t('tree.collapseAll'))}</span></button></span></div>` : ''}
|
|
101
|
+
<div data-tree-slot></div>
|
|
102
|
+
</div>` : `<p class="lede"><span class="type">${esc(label.text)}</span></p><div data-tree-slot hidden></div>`;
|
|
103
|
+
}
|
|
104
|
+
const description = schema.description;
|
|
105
|
+
main.innerHTML = `${contextBar({ kind: 'model', route, from, document })}
|
|
106
|
+
<div class="pane-scroll" id="doc-main"><div class="doc-inner">
|
|
107
|
+
<h1 id="model-title" tabindex="-1" lang="en">${esc(name)}</h1>
|
|
108
|
+
<div class="doc-meta"><span class="tag">${esc(kindLabel(schema, kind, document, size))}</span><span class="tag tag-mono">#/components/schemas/${esc(name)}</span>${copyButton(t('model.copyRef'), { action: 'copy-ref', size: 'btn-xs' })}</div>
|
|
109
|
+
<div class="prose" lang="en">${description ? markdown(description) : `<p class="faint">${esc(t('schema.noDescModel'))}</p>`}</div>
|
|
110
|
+
${body}
|
|
111
|
+
</div></div>
|
|
112
|
+
<div class="pane-bar"><span class="bar-text">${icon('box', true)}<span>${esc(t('model.bar'))}</span></span>
|
|
113
|
+
<button type="button" class="btn btn-ghost btn-sm" data-action="copy-schema">${icon('braces', true)}${esc(t('model.copySchema'))}</button></div>`;
|
|
114
|
+
const slot = main.querySelector('[data-tree-slot]');
|
|
115
|
+
if (slot && !slot.hidden) slot.innerHTML = propertyTree(treeTarget, document, { id: 'm', state: mem.tree, label: chosen?.name || name, highlight: route.highlight });
|
|
116
|
+
bindTrees(main);
|
|
117
|
+
// The list head says Collapse all while child lists are open, and Expand all once they're closed.
|
|
118
|
+
const syncAll = () => { const tree = main.querySelector('.stree'); const label = main.querySelector('[data-tree-all] span'); if (tree && label) label.textContent = t(isAllOpen(tree) ? 'tree.collapseAll' : 'tree.expandAll'); };
|
|
119
|
+
syncAll();
|
|
120
|
+
if (!main.dataset.treeSync) { main.dataset.treeSync = ''; main.addEventListener('tree-change', () => syncAll()); }
|
|
121
|
+
|
|
122
|
+
// ---- Right column ----
|
|
123
|
+
const usage = usedBy(name, state.ops, document);
|
|
124
|
+
const list = usage.operations.length ? usage.operations : usage.via;
|
|
125
|
+
const exampleName = chosen && refName(chosen.raw) ? refName(chosen.raw) : name;
|
|
126
|
+
const drawAside = () => {
|
|
127
|
+
const example = modelExample(exampleName, document, samples);
|
|
128
|
+
const schemaView = mem.aside === 'schema';
|
|
129
|
+
const view = schemaView ? jsonView(schema, { label: t('model.jsonSchema') }) : jsonView(example.value, { label: t('docs.example') });
|
|
130
|
+
const row = (op, place, extra = '') => `<a href="#docs/${encodeURIComponent(opKey(op))}" data-op-docs="${state.ops.indexOf(op)}" class="${from === op ? 'is-here' : ''}">${method(op.method)}<span class="label-text" lang="en"${rowTitle(op)}>${esc(op.title)}</span><span class="where">${esc(place)}</span></a>${extra}`;
|
|
131
|
+
aside.innerHTML = `<div class="aside-head">${esc(t('model.usedBy'))} <span class="count">${esc(t('overview.operations', { n: list.length }))}</span></div>
|
|
132
|
+
<div class="pane-scroll" id="aside-scroll"><div class="aside-inner">
|
|
133
|
+
${usage.operations.length ? `<div class="card used-list">${usage.operations.map(item => row(item.op, where(item))).join('')}</div>`
|
|
134
|
+
: usage.via.length ? `<p class="aside-label">${esc(t('used.viaHead'))}</p><div class="card used-list">${usage.via.slice(0, 8).map(item => row(item.op, t('used.via', { name: item.model }))).join('')}</div>`
|
|
135
|
+
: `<p class="faint" style="margin:0;font-size:var(--fs-md)">${esc(t('model.unused'))}</p>`}
|
|
136
|
+
${usage.models.length ? `<p class="aside-label">${esc(t('model.referencedBy'))}</p><div class="card used-list">${usage.models.slice(0, 6).map(other => `<a href="#model/${encodeURIComponent(other)}" data-model="${esc(other)}"><span class="model-ico" style="inline-size:2.125rem;block-size:1.25rem;display:grid;place-items:center;border-radius:var(--r-sm);font:600 0.65625rem/1 var(--mono);color:var(--text-2);background:var(--sunken);border:1px solid var(--line)">${esc(modelKind(document.components.schemas[other], document).glyph)}</span><span class="label-text" lang="en">${esc(other)}</span></a>`).join('')}</div>` : ''}
|
|
137
|
+
<div class="sample-card card" data-viewer>
|
|
138
|
+
<div class="viewer-bar"><div class="segmented" role="group" aria-label="${esc(t('model.show'))}"><button type="button" data-aside="example" aria-pressed="${!schemaView}">${esc(t('docs.example'))}</button><button type="button" data-aside="schema" aria-pressed="${schemaView}">${esc(t('model.jsonSchema'))}</button></div>
|
|
139
|
+
<span class="spacer"></span><span class="lines">${esc(t('json.lines', { n: view.lines }))}</span>
|
|
140
|
+
${view.foldable ? `<button type="button" class="btn btn-ghost btn-xs" data-json-fold-all>${icon('chevrons-ud', true)}<span>${esc(t(view.large ? 'tree.expandAll' : 'tree.collapseAll'))}</span></button>` : ''}
|
|
141
|
+
${copyButton(t(schemaView ? 'model.copySchema' : 'model.copyExample'), { action: 'copy-viewer' })}</div>
|
|
142
|
+
${view.html}
|
|
143
|
+
</div>
|
|
144
|
+
</div></div>
|
|
145
|
+
<div class="pane-bar"><span class="bar-text">${icon('info', true)}<span>${esc(schemaView ? t('model.schemaNote') : t(example.source === 'real' ? 'model.exampleReal' : example.source === 'spec' ? 'model.exampleSpecNote' : 'model.exampleNote'))}</span></span></div>`;
|
|
146
|
+
bindJsonViews(aside);
|
|
147
|
+
aside.dataset.copyText = schemaView ? JSON.stringify(schema, null, 2) : JSON.stringify(example.value, null, 2);
|
|
148
|
+
};
|
|
149
|
+
drawAside();
|
|
150
|
+
|
|
151
|
+
main.onclick = event => {
|
|
152
|
+
const variant = event.target.closest('[data-variant-tab]');
|
|
153
|
+
if (variant) {
|
|
154
|
+
mem.variant = variant.dataset.variantTab;
|
|
155
|
+
renderModel(main, aside, ctx);
|
|
156
|
+
main.querySelector(`[data-variant-tab="${CSS.escape(mem.variant)}"]`)?.focus();
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const ref = event.target.closest('[data-action="copy-ref"]');
|
|
160
|
+
if (ref) { event.stopPropagation(); copyText(`#/components/schemas/${name}`, { button: ref, what: '$ref' }); return; }
|
|
161
|
+
const copySchema = event.target.closest('[data-action="copy-schema"]');
|
|
162
|
+
if (copySchema) { event.stopPropagation(); copyText(JSON.stringify(schema, null, 2), { button: copySchema, what: t('model.jsonSchema') }); return; }
|
|
163
|
+
const all = event.target.closest('[data-tree-all]');
|
|
164
|
+
if (all) {
|
|
165
|
+
const tree = main.querySelector('.stree');
|
|
166
|
+
if (tree) { setAllOpen(tree, !isAllOpen(tree)); all.querySelector('span').textContent = t(isAllOpen(tree) ? 'tree.collapseAll' : 'tree.expandAll'); }
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
aside.onclick = event => {
|
|
170
|
+
const pick = event.target.closest('[data-aside]');
|
|
171
|
+
if (pick) { mem.aside = pick.dataset.aside; drawAside(); aside.querySelector(`[data-aside="${pick.dataset.aside}"]`)?.focus(); return; }
|
|
172
|
+
const fold = event.target.closest('[data-json-fold-all]');
|
|
173
|
+
if (fold) {
|
|
174
|
+
const element = aside.querySelector('[data-json-view]');
|
|
175
|
+
setAllFolded(element, !hasCollapsed(element));
|
|
176
|
+
fold.querySelector('span').textContent = t(hasCollapsed(element) ? 'tree.expandAll' : 'tree.collapseAll');
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
const copy = event.target.closest('[data-action="copy-viewer"]');
|
|
180
|
+
if (copy) { event.stopPropagation(); copyText(aside.dataset.copyText, { button: copy, what: t(mem.aside === 'schema' ? 'model.jsonSchema' : 'copy.what.example'), select: aside.querySelector('.code-view'), notice: aside.querySelector('.sample-card') }); }
|
|
181
|
+
};
|
|
182
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { esc as safe, icon, method } from "./components.js";
|
|
2
|
+
import { formatTime, t } from "./i18n.js";
|
|
3
|
+
import { modelKind, modelSize } from "./schema-model.js";
|
|
4
|
+
import { matchesShortcut, shortcutAttrs, shortcutHint } from "./shortcuts.js";
|
|
5
|
+
|
|
6
|
+
/** The command palette (⌘K): operations, scoped actions, result tabs, models and history. */
|
|
7
|
+
export function createNavigation({
|
|
8
|
+
state,
|
|
9
|
+
$,
|
|
10
|
+
selectOperation,
|
|
11
|
+
run,
|
|
12
|
+
renderOperation,
|
|
13
|
+
openModel = () => {},
|
|
14
|
+
commands = () => [],
|
|
15
|
+
}) {
|
|
16
|
+
function palette(historyOnly = false, initialScope = "All") {
|
|
17
|
+
const overlay = $("#overlay");
|
|
18
|
+
const opener = document.activeElement;
|
|
19
|
+
overlay.innerHTML = `
|
|
20
|
+
<div class="scrim palette-scrim" data-action="close"></div>
|
|
21
|
+
<div class="palette" role="dialog" aria-modal="true" aria-label="${safe(t("palette.label"))}">
|
|
22
|
+
<div class="pop-search">
|
|
23
|
+
${icon("search")}
|
|
24
|
+
<input role="combobox" aria-expanded="true" aria-controls="pal-list"
|
|
25
|
+
aria-label="${safe(t("palette.search"))}"
|
|
26
|
+
placeholder="${safe(t("palette.placeholder"))}">
|
|
27
|
+
<span class="scope">${safe(scopeLabel(historyOnly ? "History" : initialScope))}</span>
|
|
28
|
+
</div>
|
|
29
|
+
<div class="pal-list" id="pal-list" role="listbox" aria-label="${safe(t("palette.results"))}"></div>
|
|
30
|
+
<div class="pop-foot">
|
|
31
|
+
<span>${shortcutHint('updown')} ${safe(t("palette.move"))}</span>
|
|
32
|
+
<span>${shortcutHint('enter')} ${safe(t("palette.run"))}</span>
|
|
33
|
+
<span>${shortcutHint('tab')} ${safe(t("palette.scope"))}</span>
|
|
34
|
+
<span class="spacer"></span><button class="btn btn-ghost btn-xs" type="button" data-palette-shortcuts ${shortcutAttrs('shortcuts')}>${shortcutHint('shortcuts')} ${safe(t("palette.allShortcuts"))}</button>
|
|
35
|
+
</div>
|
|
36
|
+
</div>`;
|
|
37
|
+
|
|
38
|
+
function scopeLabel(value) {
|
|
39
|
+
return value === "All" ? state.op?.title || t("palette.scope.All") : t("palette.scope." + value);
|
|
40
|
+
}
|
|
41
|
+
const input = overlay.querySelector("input");
|
|
42
|
+
const list = overlay.querySelector(".pal-list");
|
|
43
|
+
const app = document.querySelector?.(".app");
|
|
44
|
+
if (app) app.inert = true;
|
|
45
|
+
let scope = historyOnly ? "History" : initialScope;
|
|
46
|
+
let selected = 0;
|
|
47
|
+
let items = [];
|
|
48
|
+
|
|
49
|
+
const highlighted = (value, query) => {
|
|
50
|
+
const text = String(value ?? "");
|
|
51
|
+
const index = query ? text.toLowerCase().indexOf(query) : -1;
|
|
52
|
+
if (index < 0) return safe(text);
|
|
53
|
+
return (
|
|
54
|
+
safe(text.slice(0, index)) +
|
|
55
|
+
"<mark>" +
|
|
56
|
+
safe(text.slice(index, index + query.length)) +
|
|
57
|
+
"</mark>" +
|
|
58
|
+
safe(text.slice(index + query.length))
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const draw = () => {
|
|
63
|
+
const query = input.value.trim().toLowerCase();
|
|
64
|
+
const operations = historyOnly
|
|
65
|
+
? []
|
|
66
|
+
: state.ops.map((op, index) => ({
|
|
67
|
+
group: "Operations",
|
|
68
|
+
label: op.title,
|
|
69
|
+
context: op.group,
|
|
70
|
+
op,
|
|
71
|
+
index,
|
|
72
|
+
}));
|
|
73
|
+
const actions = historyOnly ? [] : commands().map(command => ({
|
|
74
|
+
...command, group: command.group === "Result" ? "Result" : "Actions",
|
|
75
|
+
}));
|
|
76
|
+
const history = state.history.map((entry) => ({
|
|
77
|
+
group: "History",
|
|
78
|
+
label: t("palette.replay", { title: entry.op.title }),
|
|
79
|
+
context: t("palette.fromHistory", { time: formatTime(entry.when) }),
|
|
80
|
+
entry,
|
|
81
|
+
}));
|
|
82
|
+
const schemas = historyOnly
|
|
83
|
+
? []
|
|
84
|
+
: Object.entries(state.doc.components?.schemas || {}).map(
|
|
85
|
+
([name, schema]) => ({
|
|
86
|
+
group: "Models",
|
|
87
|
+
label: name,
|
|
88
|
+
model: name,
|
|
89
|
+
context: t("palette.modelContext", { kind: t("side.kind." + modelKind(schema, state.doc).id), n: modelSize(schema, state.doc) }),
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
items = [...actions.filter(item => item.group !== "Result"), ...operations,
|
|
93
|
+
...actions.filter(item => item.group === "Result"), ...history, ...schemas].filter(
|
|
94
|
+
(item) =>
|
|
95
|
+
(scope === "All" || scope === item.group) &&
|
|
96
|
+
`${item.label} ${item.context || ""} ${item.op?.path || ""} ${item.op?.method || ""}`
|
|
97
|
+
.toLowerCase()
|
|
98
|
+
.includes(query),
|
|
99
|
+
).slice(0, 250);
|
|
100
|
+
selected = Math.min(selected, Math.max(0, items.length - 1));
|
|
101
|
+
let group = "";
|
|
102
|
+
list.innerHTML =
|
|
103
|
+
items
|
|
104
|
+
.map((item, index) => {
|
|
105
|
+
const heading =
|
|
106
|
+
item.group === group
|
|
107
|
+
? ""
|
|
108
|
+
: `<div class="pop-label" role="presentation">${safe(item.group === "Actions" ? t("palette.group.actions") : t("palette.scope." + item.group))}</div>`;
|
|
109
|
+
group = item.group;
|
|
110
|
+
const glyph = item.op
|
|
111
|
+
? method(item.op.method)
|
|
112
|
+
: icon(item.icon || (item.model ? "box" : "history"), true);
|
|
113
|
+
return `${heading}<button class="opt-row pal-item" type="button" role="option" id="palette-item-${index}"
|
|
114
|
+
aria-selected="${index === selected}" tabindex="-1" data-palette-index="${index}" ${item.id ? shortcutAttrs(item.id) : ""}>
|
|
115
|
+
${glyph}
|
|
116
|
+
<span class="stack"><span class="main">${highlighted(item.label, query)}</span>
|
|
117
|
+
${item.context ? `<span class="sub">${highlighted(item.context, query)}</span>` : ""}</span>
|
|
118
|
+
${item.id && shortcutAttrs(item.id) ? `<span class="end">${shortcutHint(item.id)}</span>` : ""}
|
|
119
|
+
</button>`;
|
|
120
|
+
})
|
|
121
|
+
.join("") ||
|
|
122
|
+
`<div class="pop-label" role="option" aria-selected="false" aria-disabled="true">${safe(t("palette.none"))}</div>`;
|
|
123
|
+
if (items.length)
|
|
124
|
+
input.setAttribute("aria-activedescendant", `palette-item-${selected}`);
|
|
125
|
+
else input.removeAttribute("aria-activedescendant");
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const close = () => {
|
|
129
|
+
overlay.innerHTML = "";
|
|
130
|
+
if (app) app.inert = false;
|
|
131
|
+
opener?.focus();
|
|
132
|
+
};
|
|
133
|
+
const choose = async (index, preview = false) => {
|
|
134
|
+
const item = items[index];
|
|
135
|
+
if (!item) return;
|
|
136
|
+
close();
|
|
137
|
+
if (item.model) {
|
|
138
|
+
openModel(item.model);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (item.op) {
|
|
142
|
+
selectOperation(item.index);
|
|
143
|
+
if (preview) await run(true);
|
|
144
|
+
} else if (item.run) {
|
|
145
|
+
item.run();
|
|
146
|
+
} else if (item.entry) {
|
|
147
|
+
const entry = item.entry;
|
|
148
|
+
const opIndex = state.ops.findIndex(
|
|
149
|
+
(op) => op.method === entry.op.method && op.path === entry.op.path,
|
|
150
|
+
);
|
|
151
|
+
if (opIndex < 0) return;
|
|
152
|
+
selectOperation(opIndex);
|
|
153
|
+
state.body = structuredClone(entry.body);
|
|
154
|
+
state.paramValues = structuredClone(entry.params);
|
|
155
|
+
state.headers = structuredClone(entry.headers || []);
|
|
156
|
+
renderOperation();
|
|
157
|
+
if (preview) await run(true);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
input.addEventListener("input", () => {
|
|
162
|
+
selected = 0;
|
|
163
|
+
draw();
|
|
164
|
+
});
|
|
165
|
+
overlay.querySelector(".palette").addEventListener("keydown", (event) => {
|
|
166
|
+
if (matchesShortcut("escape", event)) {
|
|
167
|
+
event.preventDefault();
|
|
168
|
+
event.stopPropagation();
|
|
169
|
+
close();
|
|
170
|
+
} else if (
|
|
171
|
+
!input.hidden &&
|
|
172
|
+
(matchesShortcut("down", event) || matchesShortcut("up", event))
|
|
173
|
+
) {
|
|
174
|
+
event.preventDefault();
|
|
175
|
+
selected =
|
|
176
|
+
(selected + (event.key === "ArrowDown" ? 1 : -1) + items.length) %
|
|
177
|
+
Math.max(items.length, 1);
|
|
178
|
+
draw();
|
|
179
|
+
list.querySelector(`[id="palette-item-${selected}"]`)?.scrollIntoView?.({ block: "nearest" });
|
|
180
|
+
input.focus();
|
|
181
|
+
} else if (!input.hidden && (matchesShortcut("enter", event) || matchesShortcut("lookup-preview", event))) {
|
|
182
|
+
event.preventDefault();
|
|
183
|
+
event.stopPropagation();
|
|
184
|
+
const focused = event.target.closest("[data-palette-index]");
|
|
185
|
+
choose(
|
|
186
|
+
focused ? Number(focused.dataset.paletteIndex) : selected,
|
|
187
|
+
event.metaKey || event.ctrlKey,
|
|
188
|
+
);
|
|
189
|
+
} else if (event.key === "Tab") {
|
|
190
|
+
if (!historyOnly && !input.hidden) {
|
|
191
|
+
const scopes = ["All", "Actions", "Operations", "Result", "Models", "History"];
|
|
192
|
+
scope = scopes[(scopes.indexOf(scope) + (event.shiftKey ? -1 : 1) + scopes.length) % scopes.length];
|
|
193
|
+
overlay.querySelector(".scope").textContent = scopeLabel(scope);
|
|
194
|
+
selected = 0;
|
|
195
|
+
draw();
|
|
196
|
+
input.focus();
|
|
197
|
+
event.preventDefault();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const focusable = [
|
|
201
|
+
...overlay.querySelectorAll(
|
|
202
|
+
"input:not([hidden]), [data-palette-shortcuts]",
|
|
203
|
+
),
|
|
204
|
+
];
|
|
205
|
+
const index = focusable.indexOf(document.activeElement);
|
|
206
|
+
if (
|
|
207
|
+
(!event.shiftKey && index === focusable.length - 1) ||
|
|
208
|
+
(event.shiftKey && index <= 0)
|
|
209
|
+
) {
|
|
210
|
+
event.preventDefault();
|
|
211
|
+
focusable[event.shiftKey ? focusable.length - 1 : 0]?.focus();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
overlay.onclick = (event) => {
|
|
216
|
+
if (event.target.closest('.scrim')) {
|
|
217
|
+
event.stopPropagation();
|
|
218
|
+
close();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if (event.target.closest("[data-palette-shortcuts]")) {
|
|
222
|
+
close();
|
|
223
|
+
commands().find(command => command.id === "shortcuts")?.run();
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const item = event.target.closest("[data-palette-index]");
|
|
227
|
+
if (item) choose(Number(item.dataset.paletteIndex));
|
|
228
|
+
};
|
|
229
|
+
draw();
|
|
230
|
+
input.focus();
|
|
231
|
+
}
|
|
232
|
+
return { palette };
|
|
233
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// The operation header above the request tabs (mock: request.js "Operation header"): title and
|
|
2
|
+
// summary from the spec, requirement pills, a notice for deprecation or a missing sign-in, and the
|
|
3
|
+
// URL bar with its copy button. The breadcrumbs and the Try it ⇄ Docs switch live in the head row
|
|
4
|
+
// above it (context-bar.js).
|
|
5
|
+
import { copyButton, esc, icon, method, statusTone } from './components.js';
|
|
6
|
+
import { t, th } from './i18n.js';
|
|
7
|
+
import { isPublic, primarySchemes, scopeText } from './security.js';
|
|
8
|
+
import { shortcutAttrs } from './shortcuts.js';
|
|
9
|
+
import { deprecationNotice } from './deprecation.js';
|
|
10
|
+
import { inline, linkOperationIds } from './markdown.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Requirement pills: the permission or public access, idempotency and documented codes.
|
|
14
|
+
* Colour carries meaning only: the access pill is green when the signed-in user may call the
|
|
15
|
+
* operation, amber when it needs a sign-in, and accent for a public operation; each status code
|
|
16
|
+
* takes its class's hue (statusTone), with its documented description as the tooltip.
|
|
17
|
+
*/
|
|
18
|
+
export function metaPills(op, document, signedIn) {
|
|
19
|
+
const pills = [];
|
|
20
|
+
if (isPublic(op, document)) pills.push(`<span class="pill access is-public">${icon('globe', true)}${esc(t('op.public'))}</span>`);
|
|
21
|
+
else {
|
|
22
|
+
const scopes = scopeText(op, document);
|
|
23
|
+
const names = scopes ? [scopes] : primarySchemes(op, document).map(item => item.name);
|
|
24
|
+
const state = signedIn ? 'is-met' : 'is-locked';
|
|
25
|
+
for (const name of names) pills.push(`<span class="pill access ${state}">${icon(signedIn ? 'shield' : 'lock', true)}<span>${esc(name)}</span></span>`);
|
|
26
|
+
}
|
|
27
|
+
if (op.parameters?.some(p => p.in === 'header' && p.name.toLowerCase() === 'idempotency-key'))
|
|
28
|
+
pills.push(`<span class="pill">${esc(t('op.idempotent'))}</span>`);
|
|
29
|
+
const responses = op.operation.responses || {};
|
|
30
|
+
const codes = Object.keys(responses).filter(code => /^\d/.test(code)).sort();
|
|
31
|
+
if (codes.length) {
|
|
32
|
+
const items = codes.map(code => {
|
|
33
|
+
const description = firstSentence(responses[code]?.description || '');
|
|
34
|
+
return `<span class="sc sc-${statusTone(code)}"${description ? ` title="${esc(`${code} · ${description}`)}"` : ''}><i aria-hidden="true"></i>${esc(code)}</span>`;
|
|
35
|
+
}).join('');
|
|
36
|
+
pills.push(`<span class="pill codes" role="group" aria-label="${esc(t('docs.responses'))}"><bdi class="ltr">${items}</bdi></span>`);
|
|
37
|
+
}
|
|
38
|
+
return pills.length ? `<div class="op-meta">${pills.join('')}</div>` : '';
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A deprecation notice (mock 88) wins over the sign-in notice; both sit above the URL bar. */
|
|
42
|
+
export function operationNotice(op, document, signedIn, canSignIn, ops = []) {
|
|
43
|
+
const operation = op.operation;
|
|
44
|
+
if (operation.deprecated) return deprecationNotice(op, { ops, document, where: 'try' });
|
|
45
|
+
if (!signedIn && !isPublic(op, document)) {
|
|
46
|
+
return `<div class="op-notice auth" role="note">${icon('lock', true)}<span class="grow">${th('op.needsSignIn')}</span>${canSignIn ? `<button type="button" class="btn btn-sm" data-action="signin" ${shortcutAttrs('signin')}>${icon('log-in', true)}${esc(t('shell.signIn'))}</button>` : ''}</div>`;
|
|
47
|
+
}
|
|
48
|
+
return '';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The URL bar. Path parameters are buttons that jump to their field. Always left to right. */
|
|
52
|
+
export function urlBar(op, base) {
|
|
53
|
+
const path = op.path.split(/(\{[^{}]+\})/).map((part, index) => index % 2
|
|
54
|
+
? `<button type="button" class="token" data-path-param="${esc(part.slice(1, -1))}" aria-label="${esc(t('op.pathParameter', { name: part.slice(1, -1) }))}">${icon('link', true)}${esc(part.slice(1, -1))}</button>`
|
|
55
|
+
: esc(part)).join('');
|
|
56
|
+
return `<div class="urlbar">${method(op.method)}<span class="url"><span class="base">${esc(base)}</span>${path}</span>${copyButton(t('op.copyUrl'), { action: 'copy-url', size: 'btn-xs', shortcut: 'copy-url' })}</div>`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The whole header for the Try it view. */
|
|
60
|
+
export function operationHeader(op, { base = '', document, signedIn = false, canSignIn = false, ops = [] } = {}) {
|
|
61
|
+
return `<div class="op-head">
|
|
62
|
+
<div class="op-title-row"><h1 class="op-title" id="op-title" lang="en">${esc(op.title)}</h1></div>
|
|
63
|
+
${op.operation.description || op.operation.summary && op.operation.summary !== op.title
|
|
64
|
+
? `<p class="op-desc" lang="en">${linkOperationIds(inline(firstSentence(op.operation.description || op.operation.summary, true)), ops, 'op')}</p>` : ''}
|
|
65
|
+
${metaPills(op, document, signedIn)}
|
|
66
|
+
${operationNotice(op, document, signedIn, canSignIn, ops)}
|
|
67
|
+
${urlBar(op, base)}
|
|
68
|
+
</div>`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The first paragraph of a markdown description, without markup, for the one-line summary. */
|
|
72
|
+
export function firstSentence(markdown, keepCode = false) {
|
|
73
|
+
// The notice says "Deprecated" once, so the summary doesn't repeat it. Code spans may stay
|
|
74
|
+
// (as `sort` in mono) when the caller renders inline markdown.
|
|
75
|
+
return String(markdown).replace(/^\s*\**deprecated\.?\**\s*/i, '').split(/\n\s*\n/)[0].replace(keepCode ? /[*_]/g : /[*_`]/g, '').trim();
|
|
76
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Identifying operations in addresses: the operationId, or "METHOD /path" when there is none.
|
|
2
|
+
|
|
3
|
+
/** The key that names an operation in a route. */
|
|
4
|
+
export const opKey = op => op.operation.operationId || `${op.method} ${op.path}`;
|
|
5
|
+
|
|
6
|
+
/** The operation a route key names, or undefined. */
|
|
7
|
+
export const findOperation = (ops, key) => ops.find(op => opKey(op) === key);
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// The API overview (mocks/docs.js overview): the landing page on first visit, built from info,
|
|
2
|
+
// servers and tags. It explains Preview, Send and Autofill before anything runs and suggests
|
|
3
|
+
// where to start. Same two columns and bars as every other view.
|
|
4
|
+
import { copyButton, esc, icon, method } from './components.js';
|
|
5
|
+
import { contextBar } from './context-bar.js';
|
|
6
|
+
import { copyText } from './copy.js';
|
|
7
|
+
import { baseUrl } from './docs-view.js';
|
|
8
|
+
import { t, th } from './i18n.js';
|
|
9
|
+
import { markdown } from './markdown.js';
|
|
10
|
+
import { opKey } from './operations.js';
|
|
11
|
+
import { shortcutAttrs, shortcutHint } from './shortcuts.js';
|
|
12
|
+
|
|
13
|
+
const firstSentence = text => String(text ?? '').split(/\n\s*\n/)[0].replace(/\s*\n\s*/g, ' ').replace(/[*_`]/g, '').trim().replace(/^(.{0,160}?[.!?])\s.*$/, '$1');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Renders the overview into the two reading panes.
|
|
17
|
+
* @param {HTMLElement} main the `.doc-pane`
|
|
18
|
+
* @param {HTMLElement} aside the `.aside-pane`
|
|
19
|
+
* @param {{ state: object }} ctx
|
|
20
|
+
*/
|
|
21
|
+
export function renderOverview(main, aside, { state }) {
|
|
22
|
+
const { doc, ops, cap } = state;
|
|
23
|
+
const info = doc.info || {};
|
|
24
|
+
const signedIn = !!cap.identity?.name;
|
|
25
|
+
const models = Object.keys(doc.components?.schemas || {}).length;
|
|
26
|
+
const tags = new Map();
|
|
27
|
+
for (const op of ops) tags.set(op.group, [...(tags.get(op.group) || []), op]);
|
|
28
|
+
const tagInfo = name => (doc.tags || []).find(tag => tag.name === name)?.description;
|
|
29
|
+
const firstRead = ops.find(op => op.method === 'GET');
|
|
30
|
+
const firstPreview = ops.find(op => (cap.previewOperations || []).some(item => item.method.toUpperCase() === op.method && item.path === op.path));
|
|
31
|
+
const how = [
|
|
32
|
+
['flask', t('overview.preview'), 'preview', t('overview.previewBody')],
|
|
33
|
+
['send', t('overview.send'), 'send', t('overview.sendBody')],
|
|
34
|
+
['wand', t('overview.autofill'), 'autofill-required', t('overview.autofillBody')],
|
|
35
|
+
];
|
|
36
|
+
const start = (op, note) => `<a href="#op/${encodeURIComponent(opKey(op))}" data-op="${ops.indexOf(op)}">${method(op.method)}<span class="label-text" lang="en">${esc(op.title)}</span><span class="where">${esc(note)}</span></a>`;
|
|
37
|
+
main.innerHTML = `${contextBar({ kind: 'overview' })}
|
|
38
|
+
<div class="pane-scroll"><div class="doc-inner">
|
|
39
|
+
<h1 id="ov-title" tabindex="-1" lang="en">${esc(cap.apiName || info.title || 'API')}${info.version ? ` <span class="faint" style="font-weight:400">${esc(/^\d/.test(info.version) ? 'v' + info.version : info.version)}</span>` : ''}</h1>
|
|
40
|
+
<div class="doc-meta"><span class="tag tag-mono">${esc(baseUrl(doc))}</span>${doc.openapi ? `<span class="tag tag-mono">OpenAPI ${esc(doc.openapi)}</span>` : ''}<span class="tag">${esc(t('overview.operations', { n: ops.length }))}</span><span class="tag">${esc(t('overview.models', { n: models }))}</span></div>
|
|
41
|
+
${info.summary || info.description ? `<div class="prose" lang="en">${info.description ? markdown(info.description) : `<p>${esc(info.summary)}</p>`}</div>` : ''}
|
|
42
|
+
<h2>${esc(t('overview.how'))}</h2>
|
|
43
|
+
<div class="how">${how.map(([glyph, name, id, text]) => `<div class="how-card"><div class="how-h">${icon(glyph, true)}<strong>${esc(name)}</strong><span class="spacer"></span>${shortcutHint(id)}</div><p>${esc(text)}</p></div>`).join('')}</div>
|
|
44
|
+
<h2>${esc(t('overview.byTag'))}</h2>
|
|
45
|
+
<div class="card used-list">${[...tags].map(([name, items]) => `<a href="#op/${encodeURIComponent(opKey(items[0]))}" data-op="${ops.indexOf(items[0])}"><span class="label-text" lang="en"><strong style="font-weight:500">${esc(name)}</strong>${tagInfo(name) ? ` <span class="faint">· ${esc(firstSentence(tagInfo(name)))}</span>` : ''}</span><span class="where">${esc(t('overview.ops', { n: items.length }))}</span></a>`).join('')}</div>
|
|
46
|
+
</div></div>
|
|
47
|
+
<div class="pane-bar"><span class="bar-text">${icon('info', true)}<span>${esc(t('overview.bar'))}</span></span><button type="button" class="btn btn-sm" data-action="palette" ${shortcutAttrs('palette')}>${icon('search', true)}${esc(t('overview.find'))}${shortcutHint('palette')}</button></div>`;
|
|
48
|
+
aside.innerHTML = `<div class="aside-head">${esc(t('overview.start'))}</div>
|
|
49
|
+
<div class="pane-scroll"><div class="aside-inner">
|
|
50
|
+
${signedIn ? `<div class="card"><div class="card-head">${icon('check', true)}<strong>${esc(t('overview.signedIn', { name: cap.identity.name }))}</strong></div>
|
|
51
|
+
<p style="margin:0;padding:0.875rem;font-size:var(--fs-md);color:var(--text-2)">${th(cap.identity.role ? 'overview.runsAsRole' : 'overview.runsAs', { name: esc(cap.identity.name), role: `<code>${esc(cap.identity.role || '')}</code>` })}</p></div>` : ''}
|
|
52
|
+
<div class="card used-list">
|
|
53
|
+
${firstRead ? start(firstRead, t('overview.readOnly')) : ''}
|
|
54
|
+
${firstPreview ? start(firstPreview, t('overview.tryPreview')) : ''}
|
|
55
|
+
<a href="#overview" data-action="shortcuts">${icon('keyboard', true)}<span class="label-text">${esc(t('overview.shortcuts'))}</span><span class="where">${shortcutHint('shortcuts')}</span></a>
|
|
56
|
+
</div>
|
|
57
|
+
</div></div>
|
|
58
|
+
<div class="pane-bar"><span class="bar-text">${icon('server', true)}<span>${esc(t('docs.base'))} <code class="ltr">${esc(baseUrl(doc))}</code></span></span>${copyButton(t('docs.copyBase'), { action: 'copy-base', size: 'btn-xs' })}</div>`;
|
|
59
|
+
aside.onclick = event => {
|
|
60
|
+
const base = event.target.closest('[data-action="copy-base"]');
|
|
61
|
+
if (base) { event.stopPropagation(); copyText(baseUrl(doc), { button: base, what: t('copy.what.url') }); }
|
|
62
|
+
};
|
|
63
|
+
}
|