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.
Files changed (229) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +55 -0
  4. data/lib/sendproof/assets.rb +70 -0
  5. data/lib/sendproof/attribution.rb +98 -0
  6. data/lib/sendproof/capture.rb +66 -0
  7. data/lib/sendproof/clock.rb +54 -0
  8. data/lib/sendproof/configuration.rb +241 -0
  9. data/lib/sendproof/data_store.rb +34 -0
  10. data/lib/sendproof/dynamodb/store.rb +183 -0
  11. data/lib/sendproof/dynamodb.rb +17 -0
  12. data/lib/sendproof/elasticsearch/store.rb +24 -0
  13. data/lib/sendproof/elasticsearch.rb +17 -0
  14. data/lib/sendproof/firestore/interceptor.rb +38 -0
  15. data/lib/sendproof/firestore/store.rb +150 -0
  16. data/lib/sendproof/firestore.rb +99 -0
  17. data/lib/sendproof/handler.rb +23 -0
  18. data/lib/sendproof/identity.rb +37 -0
  19. data/lib/sendproof/limits.rb +113 -0
  20. data/lib/sendproof/middleware.rb +68 -0
  21. data/lib/sendproof/mongo/engine.rb +692 -0
  22. data/lib/sendproof/mongo/guarded.rb +100 -0
  23. data/lib/sendproof/mongo/plain.rb +45 -0
  24. data/lib/sendproof/mongo/store.rb +54 -0
  25. data/lib/sendproof/mongo/subscriber.rb +21 -0
  26. data/lib/sendproof/mongo.rb +41 -0
  27. data/lib/sendproof/mongoid/store.rb +58 -0
  28. data/lib/sendproof/mongoid.rb +65 -0
  29. data/lib/sendproof/observed_request.rb +108 -0
  30. data/lib/sendproof/opensearch/store.rb +21 -0
  31. data/lib/sendproof/opensearch.rb +17 -0
  32. data/lib/sendproof/operation_matcher.rb +28 -0
  33. data/lib/sendproof/pool_hooks.rb +80 -0
  34. data/lib/sendproof/preview_request.rb +44 -0
  35. data/lib/sendproof/preview_transport.rb +331 -0
  36. data/lib/sendproof/protocol.rb +81 -0
  37. data/lib/sendproof/provider.rb +83 -0
  38. data/lib/sendproof/recorder.rb +298 -0
  39. data/lib/sendproof/redaction.rb +98 -0
  40. data/lib/sendproof/redis/commands.rb +76 -0
  41. data/lib/sendproof/redis/store.rb +205 -0
  42. data/lib/sendproof/redis.rb +18 -0
  43. data/lib/sendproof/search/middleware.rb +39 -0
  44. data/lib/sendproof/search/store.rb +213 -0
  45. data/lib/sendproof/search.rb +82 -0
  46. data/lib/sendproof/stores/classify.rb +346 -0
  47. data/lib/sendproof/stores/guard.rb +127 -0
  48. data/lib/sendproof/stores/tables.rb +1328 -0
  49. data/lib/sendproof/stores.rb +12 -0
  50. data/lib/sendproof/trace_store.rb +110 -0
  51. data/lib/sendproof/transaction_control.rb +187 -0
  52. data/lib/sendproof/ui/action-bar.js +91 -0
  53. data/lib/sendproof/ui/api.js +239 -0
  54. data/lib/sendproof/ui/app.css +848 -0
  55. data/lib/sendproof/ui/app.js +742 -0
  56. data/lib/sendproof/ui/autofill.js +137 -0
  57. data/lib/sendproof/ui/code-copy.js +71 -0
  58. data/lib/sendproof/ui/code-picker.js +149 -0
  59. data/lib/sendproof/ui/commands.js +197 -0
  60. data/lib/sendproof/ui/components.js +77 -0
  61. data/lib/sendproof/ui/context-bar.js +82 -0
  62. data/lib/sendproof/ui/copy.js +92 -0
  63. data/lib/sendproof/ui/coverage-popover.js +35 -0
  64. data/lib/sendproof/ui/crumb-fit.js +80 -0
  65. data/lib/sendproof/ui/deprecation.js +164 -0
  66. data/lib/sendproof/ui/display-settings.js +175 -0
  67. data/lib/sendproof/ui/doc-diff.js +209 -0
  68. data/lib/sendproof/ui/docs-view.js +347 -0
  69. data/lib/sendproof/ui/favicon.png +0 -0
  70. data/lib/sendproof/ui/favicon.svg +1 -0
  71. data/lib/sendproof/ui/field-help.js +52 -0
  72. data/lib/sendproof/ui/field-modules.js +23 -0
  73. data/lib/sendproof/ui/fields/controls.js +179 -0
  74. data/lib/sendproof/ui/fields/dom.js +36 -0
  75. data/lib/sendproof/ui/fields/infer.js +71 -0
  76. data/lib/sendproof/ui/fields/lookup-source.js +63 -0
  77. data/lib/sendproof/ui/fields/lookup.js +285 -0
  78. data/lib/sendproof/ui/fields/render.js +496 -0
  79. data/lib/sendproof/ui/fields/select.js +175 -0
  80. data/lib/sendproof/ui/fields/validate.js +363 -0
  81. data/lib/sendproof/ui/fields/validation-formats.js +164 -0
  82. data/lib/sendproof/ui/fields/validation-references.js +68 -0
  83. data/lib/sendproof/ui/fields/validation-values.js +137 -0
  84. data/lib/sendproof/ui/generate-menu.js +44 -0
  85. data/lib/sendproof/ui/generate.js +184 -0
  86. data/lib/sendproof/ui/header-editor.js +777 -0
  87. data/lib/sendproof/ui/headers.js +188 -0
  88. data/lib/sendproof/ui/highlight.js +257 -0
  89. data/lib/sendproof/ui/history-popover.js +24 -0
  90. data/lib/sendproof/ui/i18n.js +253 -0
  91. data/lib/sendproof/ui/icons.js +87 -0
  92. data/lib/sendproof/ui/index.html +70 -0
  93. data/lib/sendproof/ui/issues.js +187 -0
  94. data/lib/sendproof/ui/json-view.js +166 -0
  95. data/lib/sendproof/ui/lang-icons.js +62 -0
  96. data/lib/sendproof/ui/layout.js +262 -0
  97. data/lib/sendproof/ui/listbox.js +334 -0
  98. data/lib/sendproof/ui/locales/ar-capture.js +366 -0
  99. data/lib/sendproof/ui/locales/ar-stores.js +116 -0
  100. data/lib/sendproof/ui/locales/ar.js +561 -0
  101. data/lib/sendproof/ui/locales/cs-capture.js +442 -0
  102. data/lib/sendproof/ui/locales/cs-stores.js +116 -0
  103. data/lib/sendproof/ui/locales/cs.js +689 -0
  104. data/lib/sendproof/ui/locales/da-capture.js +347 -0
  105. data/lib/sendproof/ui/locales/da-stores.js +116 -0
  106. data/lib/sendproof/ui/locales/da.js +522 -0
  107. data/lib/sendproof/ui/locales/de-capture.js +347 -0
  108. data/lib/sendproof/ui/locales/de-stores.js +116 -0
  109. data/lib/sendproof/ui/locales/de.js +519 -0
  110. data/lib/sendproof/ui/locales/el-capture.js +347 -0
  111. data/lib/sendproof/ui/locales/el-stores.js +116 -0
  112. data/lib/sendproof/ui/locales/el.js +520 -0
  113. data/lib/sendproof/ui/locales/en-capture.js +349 -0
  114. data/lib/sendproof/ui/locales/en-stores.js +95 -0
  115. data/lib/sendproof/ui/locales/en.js +522 -0
  116. data/lib/sendproof/ui/locales/es-capture.js +353 -0
  117. data/lib/sendproof/ui/locales/es-stores.js +116 -0
  118. data/lib/sendproof/ui/locales/es.js +525 -0
  119. data/lib/sendproof/ui/locales/fa-capture.js +348 -0
  120. data/lib/sendproof/ui/locales/fa-stores.js +116 -0
  121. data/lib/sendproof/ui/locales/fa.js +521 -0
  122. data/lib/sendproof/ui/locales/fi-capture.js +347 -0
  123. data/lib/sendproof/ui/locales/fi-stores.js +116 -0
  124. data/lib/sendproof/ui/locales/fi.js +523 -0
  125. data/lib/sendproof/ui/locales/fr-capture.js +353 -0
  126. data/lib/sendproof/ui/locales/fr-stores.js +116 -0
  127. data/lib/sendproof/ui/locales/fr.js +525 -0
  128. data/lib/sendproof/ui/locales/he-capture.js +358 -0
  129. data/lib/sendproof/ui/locales/he-stores.js +116 -0
  130. data/lib/sendproof/ui/locales/he.js +529 -0
  131. data/lib/sendproof/ui/locales/hi-capture.js +348 -0
  132. data/lib/sendproof/ui/locales/hi-stores.js +116 -0
  133. data/lib/sendproof/ui/locales/hi.js +523 -0
  134. data/lib/sendproof/ui/locales/id-capture.js +347 -0
  135. data/lib/sendproof/ui/locales/id-stores.js +116 -0
  136. data/lib/sendproof/ui/locales/id.js +521 -0
  137. data/lib/sendproof/ui/locales/it-capture.js +361 -0
  138. data/lib/sendproof/ui/locales/it-stores.js +116 -0
  139. data/lib/sendproof/ui/locales/it.js +523 -0
  140. data/lib/sendproof/ui/locales/ja-capture.js +346 -0
  141. data/lib/sendproof/ui/locales/ja-stores.js +116 -0
  142. data/lib/sendproof/ui/locales/ja.js +521 -0
  143. data/lib/sendproof/ui/locales/ko-capture.js +346 -0
  144. data/lib/sendproof/ui/locales/ko-stores.js +116 -0
  145. data/lib/sendproof/ui/locales/ko.js +522 -0
  146. data/lib/sendproof/ui/locales/nb-capture.js +349 -0
  147. data/lib/sendproof/ui/locales/nb-stores.js +116 -0
  148. data/lib/sendproof/ui/locales/nb.js +521 -0
  149. data/lib/sendproof/ui/locales/nl-capture.js +355 -0
  150. data/lib/sendproof/ui/locales/nl-stores.js +116 -0
  151. data/lib/sendproof/ui/locales/nl.js +520 -0
  152. data/lib/sendproof/ui/locales/pl-capture.js +432 -0
  153. data/lib/sendproof/ui/locales/pl-stores.js +116 -0
  154. data/lib/sendproof/ui/locales/pl.js +528 -0
  155. data/lib/sendproof/ui/locales/pt-BR-capture.js +350 -0
  156. data/lib/sendproof/ui/locales/pt-BR-stores.js +116 -0
  157. data/lib/sendproof/ui/locales/pt-BR.js +523 -0
  158. data/lib/sendproof/ui/locales/pt-PT-capture.js +350 -0
  159. data/lib/sendproof/ui/locales/pt-PT-stores.js +116 -0
  160. data/lib/sendproof/ui/locales/pt-PT.js +523 -0
  161. data/lib/sendproof/ui/locales/ru-capture.js +437 -0
  162. data/lib/sendproof/ui/locales/ru-stores.js +116 -0
  163. data/lib/sendproof/ui/locales/ru.js +624 -0
  164. data/lib/sendproof/ui/locales/sv-capture.js +349 -0
  165. data/lib/sendproof/ui/locales/sv-stores.js +116 -0
  166. data/lib/sendproof/ui/locales/sv.js +518 -0
  167. data/lib/sendproof/ui/locales/th-capture.js +347 -0
  168. data/lib/sendproof/ui/locales/th-stores.js +116 -0
  169. data/lib/sendproof/ui/locales/th.js +521 -0
  170. data/lib/sendproof/ui/locales/tr-capture.js +348 -0
  171. data/lib/sendproof/ui/locales/tr-stores.js +116 -0
  172. data/lib/sendproof/ui/locales/tr.js +521 -0
  173. data/lib/sendproof/ui/locales/uk-capture.js +442 -0
  174. data/lib/sendproof/ui/locales/uk-stores.js +116 -0
  175. data/lib/sendproof/ui/locales/uk.js +630 -0
  176. data/lib/sendproof/ui/locales/vi-capture.js +346 -0
  177. data/lib/sendproof/ui/locales/vi-stores.js +116 -0
  178. data/lib/sendproof/ui/locales/vi.js +523 -0
  179. data/lib/sendproof/ui/locales/zh-Hans-capture.js +347 -0
  180. data/lib/sendproof/ui/locales/zh-Hans-stores.js +116 -0
  181. data/lib/sendproof/ui/locales/zh-Hans.js +523 -0
  182. data/lib/sendproof/ui/locales/zh-Hant-capture.js +346 -0
  183. data/lib/sendproof/ui/locales/zh-Hant-stores.js +116 -0
  184. data/lib/sendproof/ui/locales/zh-Hant.js +522 -0
  185. data/lib/sendproof/ui/manifest.json +866 -0
  186. data/lib/sendproof/ui/markdown.js +39 -0
  187. data/lib/sendproof/ui/model-view.js +182 -0
  188. data/lib/sendproof/ui/navigation.js +233 -0
  189. data/lib/sendproof/ui/operation-header.js +76 -0
  190. data/lib/sendproof/ui/operations.js +7 -0
  191. data/lib/sendproof/ui/overview-view.js +63 -0
  192. data/lib/sendproof/ui/personas.js +59 -0
  193. data/lib/sendproof/ui/popover.js +71 -0
  194. data/lib/sendproof/ui/preview-false.js +108 -0
  195. data/lib/sendproof/ui/request-editor.js +496 -0
  196. data/lib/sendproof/ui/response-metadata.js +354 -0
  197. data/lib/sendproof/ui/response-viewer.js +618 -0
  198. data/lib/sendproof/ui/result-blocked.js +155 -0
  199. data/lib/sendproof/ui/result-capture.js +25 -0
  200. data/lib/sendproof/ui/result-database.js +185 -0
  201. data/lib/sendproof/ui/result-effects.js +176 -0
  202. data/lib/sendproof/ui/result-headers.js +106 -0
  203. data/lib/sendproof/ui/result-logs.js +408 -0
  204. data/lib/sendproof/ui/result-states.js +213 -0
  205. data/lib/sendproof/ui/results.js +371 -0
  206. data/lib/sendproof/ui/router.js +118 -0
  207. data/lib/sendproof/ui/schema-model.js +336 -0
  208. data/lib/sendproof/ui/schema-tree.js +344 -0
  209. data/lib/sendproof/ui/schema.js +397 -0
  210. data/lib/sendproof/ui/security.js +31 -0
  211. data/lib/sendproof/ui/seeded-menu.js +69 -0
  212. data/lib/sendproof/ui/settings.js +93 -0
  213. data/lib/sendproof/ui/shortcuts-sheet.js +73 -0
  214. data/lib/sendproof/ui/shortcuts.js +190 -0
  215. data/lib/sendproof/ui/sidebar.js +289 -0
  216. data/lib/sendproof/ui/signin.js +259 -0
  217. data/lib/sendproof/ui/snippets.js +530 -0
  218. data/lib/sendproof/ui/store-marks.js +47 -0
  219. data/lib/sendproof/ui/stores.js +131 -0
  220. data/lib/sendproof/ui/styles.css +971 -0
  221. data/lib/sendproof/ui/toggletip.js +53 -0
  222. data/lib/sendproof/ui/uuid.js +10 -0
  223. data/lib/sendproof/ui/view.js +122 -0
  224. data/lib/sendproof/ui/views.css +653 -0
  225. data/lib/sendproof/version.rb +9 -0
  226. data/lib/sendproof/workbench.rb +102 -0
  227. data/lib/sendproof-core.rb +3 -0
  228. data/lib/sendproof.rb +121 -0
  229. metadata +292 -0
@@ -0,0 +1,344 @@
1
+ // The attribute list used by model pages, Docs (parameters, request bodies, responses) and the
2
+ // response Schema view: Stripe's reading style on our tokens (mocks 91–99, 102, 103, m21).
3
+ //
4
+ // name type Required|Optional [path|query|header|cookie] [Deprecated]
5
+ // The description, flush with the name, at a readable measure.
6
+ // Format uuid Max length 42 Pattern ^…$ Default 25 (quiet text, tinted values)
7
+ // Possible enum values: each with its description, or a compact list of code chips
8
+ // [Show child attributes] → a bordered, inset child list headed "Hide child attributes"
9
+ //
10
+ // Rows are flat on hairlines; leaf rows have no toggle column and no indent. Child lists start
11
+ // open three levels deep (deeper with Expand all); what the user closes is remembered per API
12
+ // and list in local storage. $ref types are links to the model page; $ref cycles stop at
13
+ // "recursive". A oneOf child list starts with "Chosen by `scheme`" and a tab per variant. In
14
+ // compact mode (the response Schema view) rows keep only their head line, with a presence mark
15
+ // at the line end and the schema check's reason on the rows that failed it.
16
+ import { esc, icon, tab } from './components.js';
17
+ import { t } from './i18n.js';
18
+ import { inline } from './markdown.js';
19
+ import {
20
+ discriminatorOf, enumOf, isExpandable, propertiesOf, refName, resolve, rowSchema, typeLabel, typeOf, unwrapNull, variantsOf,
21
+ } from './schema-model.js';
22
+
23
+ const OPEN_DEPTH = 3;
24
+ const MAX_DEPTH = 8;
25
+ const ENUM_SHOWN = 6;
26
+ const STORAGE_KEY = 'sendproof-attrs';
27
+ const trees = new Map();
28
+ const bound = new WeakSet();
29
+ let treeCounter = 0;
30
+
31
+ /**
32
+ * The state a list keeps while the user opens and closes child lists. `mode` is 'default'
33
+ * (open three levels deep), 'all' (Expand all) or 'none' (Collapse all). Callers keep it across
34
+ * renders; `persist` names it in local storage so closed child lists stay closed after a reload.
35
+ */
36
+ export function newTreeState(persist) {
37
+ const state = { expanded: new Set(), collapsed: new Set(), mode: 'default', variant: {}, allValues: new Set(), persist };
38
+ if (persist) {
39
+ const saved = readStore()[persist];
40
+ if (saved) { state.collapsed = new Set(saved.collapsed || []); state.mode = saved.mode === 'none' || saved.mode === 'all' ? saved.mode : 'default'; }
41
+ }
42
+ return state;
43
+ }
44
+
45
+ function readStore() {
46
+ try { return JSON.parse(globalThis.localStorage?.getItem(STORAGE_KEY) || '{}') || {}; } catch { return {}; }
47
+ }
48
+ function save(state) {
49
+ if (!state.persist) return;
50
+ const store = readStore();
51
+ if (!state.collapsed.size && state.mode === 'default') delete store[state.persist];
52
+ else store[state.persist] = { collapsed: [...state.collapsed], mode: state.mode };
53
+ try { globalThis.localStorage?.setItem(STORAGE_KEY, JSON.stringify(store)); } catch { /* Private mode can refuse storage. */ }
54
+ }
55
+
56
+ /** The colour family of a type, for its quiet tint: string, number, boolean, object or enum. */
57
+ function family(schema, document) {
58
+ if (enumOf(schema, document)) return 'enum';
59
+ const type = typeOf(schema, document);
60
+ if (type === 'integer' || type === 'number') return 'number';
61
+ if (type === 'boolean') return 'boolean';
62
+ if (type === 'string') return 'string';
63
+ return 'object';
64
+ }
65
+
66
+ /** The type after a name: "string", "nullable string", "array of AccountReference" (a link). */
67
+ function typeText(schema, nullable, document) {
68
+ const label = typeLabel(schema, document);
69
+ const resolved = resolve(schema, document);
70
+ const link = name => `<a class="attr-type is-ref" href="#model/${encodeURIComponent(name)}" data-model="${esc(name)}" title="${esc(t('schema.openModel', { name }))}">${esc(name)}</a>`;
71
+ const plain = (text, kind) => `<span class="attr-type t-${kind}">${esc(text)}</span>`;
72
+ let html;
73
+ if (label.array) {
74
+ const inner = resolve(resolved.items || {}, document);
75
+ html = `<span class="attr-type">${esc(t('attr.arrayOf'))}</span> ${label.ref ? link(label.ref) : plain(label.text.replace(/\[\]$/, ''), family(inner, document))}`;
76
+ } else if (label.ref) html = link(label.ref);
77
+ else if (resolved.oneOf || resolved.anyOf) html = plain(t('attr.oneOfN', { n: variantsOf(resolved, document).length }), 'object');
78
+ else html = plain(label.text === 'string' && enumOf(schema, document) ? 'enum' : label.text, family(schema, document));
79
+ return nullable ? `<span class="attr-type">${esc(t('attr.nullable'))}</span> ${html}` : html;
80
+ }
81
+
82
+ /** Constraints as quiet inline text: "Format uuid Max length 42 Pattern ^…$". */
83
+ function facts(raw, extra = []) {
84
+ const schema = { ...resolve(raw), ...Object.fromEntries(Object.entries(raw || {}).filter(([key]) => key !== '$ref')) };
85
+ const out = [...extra];
86
+ const fact = (key, value) => out.push(value === undefined ? `<span>${esc(key)}</span>` : `<span>${esc(key)} <code>${esc(value)}</code></span>`);
87
+ const type = typeOf(schema);
88
+ if (schema.format && type !== 'array' && schema.format !== 'decimal') fact(t('attr.f.format'), schema.format);
89
+ // Platform limits such as .NET decimal's maximum say nothing useful, so they're left out.
90
+ const max = schema.maximum !== undefined && Math.abs(schema.maximum) < 1e15 ? schema.maximum : undefined;
91
+ if (schema.minimum !== undefined && max !== undefined) fact(t('attr.f.range'), `${schema.minimum}–${max}`);
92
+ else if (schema.minimum !== undefined) fact(t('attr.f.min'), schema.minimum);
93
+ else if (max !== undefined) fact(t('attr.f.max'), max);
94
+ if (typeof schema.exclusiveMinimum === 'number') fact(t('attr.f.gt'), schema.exclusiveMinimum);
95
+ if (typeof schema.exclusiveMaximum === 'number') fact(t('attr.f.lt'), schema.exclusiveMaximum);
96
+ if (schema.multipleOf !== undefined) fact(t('attr.f.step'), schema.multipleOf);
97
+ if (schema.minLength !== undefined && schema.maxLength !== undefined) fact(t('attr.f.length'), schema.minLength === schema.maxLength ? schema.minLength : `${schema.minLength}–${schema.maxLength}`);
98
+ else if (schema.maxLength !== undefined) fact(t('attr.f.maxLength'), schema.maxLength);
99
+ else if (schema.minLength !== undefined) fact(t('attr.f.minLength'), schema.minLength);
100
+ if (schema.pattern) out.push(`<span>${esc(t('attr.f.pattern'))} <code class="pat">${esc(schema.pattern)}</code></span>`);
101
+ if (schema.minItems !== undefined || schema.maxItems !== undefined) fact(t('attr.f.items'), schema.minItems !== undefined && schema.maxItems !== undefined ? `${schema.minItems}–${schema.maxItems}` : schema.maxItems !== undefined ? `≤ ${schema.maxItems}` : `≥ ${schema.minItems}`);
102
+ if (schema.uniqueItems) fact(t('attr.f.unique'));
103
+ if (schema.default !== undefined) fact(t('attr.f.default'), typeof schema.default === 'string' ? schema.default : JSON.stringify(schema.default));
104
+ if (schema.readOnly) fact(t('attr.f.readOnly'));
105
+ if (schema.writeOnly) fact(t('attr.f.writeOnly'));
106
+ return out;
107
+ }
108
+
109
+ /** "Possible enum values": described values as a list, others as compact code chips. */
110
+ function enumBlock(en, ctx, path) {
111
+ const all = ctx.state.allValues.has(path);
112
+ const long = en.values.length > ENUM_SHOWN + 1;
113
+ const shown = long && !all ? en.values.slice(0, ENUM_SHOWN - 1) : en.values;
114
+ const more = en.values.length - shown.length;
115
+ const list = en.described
116
+ ? `<dl class="enum-dl">${shown.map(value => `<div><dt><code>${esc(value)}</code></dt>${en.desc[String(value)] ? `<dd lang="en">${inline(en.desc[String(value)])}</dd>` : '<dd></dd>'}</div>`).join('')}</dl>`
117
+ : `<ul class="enum-inline">${shown.map(value => `<li><code>${esc(value)}</code></li>`).join('')}</ul>`;
118
+ const toggle = more ? `<button type="button" class="link-btn" data-attr-values="${esc(path)}" aria-expanded="false">${esc(t('attr.moreValues', { n: more }))}</button>`
119
+ : long && all ? `<button type="button" class="link-btn" data-attr-values="${esc(path)}" aria-expanded="true">${esc(t('attr.fewerValues'))}</button>` : '';
120
+ return `<div class="attr-enum"><p class="attr-enum-h">${esc(t('attr.enumValues'))}</p>${list}${toggle}</div>`;
121
+ }
122
+
123
+ /** The presence mark at the end of a row in the Schema view. */
124
+ const mark = kind => `<span class="attr-mark is-${kind}" role="img" aria-label="${esc(t('attr.mark.' + kind))}"></span>`;
125
+
126
+ function isOpen(ctx, path, depth) {
127
+ const { state } = ctx;
128
+ if (state.collapsed.has(path)) return false;
129
+ if (state.expanded.has(path)) return true;
130
+ if (state.mode === 'none') return false;
131
+ return state.mode === 'all' || depth < OPEN_DEPTH;
132
+ }
133
+
134
+ /** One attribute row and, when open, its child list. `path` is the dotted path used for state. */
135
+ function attrRow(name, raw, required, ctx, depth, path, seen) {
136
+ const { document, options } = ctx;
137
+ const { schema, nullable } = unwrapNull(raw);
138
+ const resolved = resolve(schema, document);
139
+ const target = typeLabel(schema, document).ref;
140
+ const recursive = !!target && seen.has(target);
141
+ const canOpen = isExpandable(schema, document) && !recursive && depth < MAX_DEPTH;
142
+ const open = canOpen && isOpen(ctx, path, depth);
143
+ const description = raw.description || schema.description || (target ? undefined : resolved.description);
144
+ const en = enumOf(schema, document);
145
+ const extra = [];
146
+ // A one-value enum is a constant (a discriminator inside its variant): say it once, as a fact.
147
+ if (en?.values.length === 1 && !String(description || '').includes(String(en.values[0]))) extra.push(`<span>${esc(t('attr.always', { value: '\u0000' })).replace('\u0000', `<code>${esc(en.values[0])}</code>`)}</span>`);
148
+ const example = (() => { const merged = { ...resolved, ...raw }; const value = merged.example ?? merged.examples?.[0]; return value !== undefined && typeof value !== 'object' ? value : undefined; })();
149
+ const factList = options.compact ? [] : facts(schema, extra);
150
+ if (!options.compact && example !== undefined && !en) factList.push(`<span>${esc(t('attr.f.example'))} <code>${esc(String(example).length > 48 ? String(example).slice(0, 47) + '…' : example)}</code></span>`);
151
+ const id = `${ctx.id}-${path.replace(/[^\w]/g, '-')}`;
152
+ const present = options.present ? options.present.has(path) : undefined;
153
+ const issue = options.issues?.[path];
154
+ const deprecated = raw.deprecated || resolved.deprecated;
155
+ const where = options.locations?.[name];
156
+
157
+ let kids = '';
158
+ if (canOpen) {
159
+ const toggle = `<button type="button" class="attr-toggle" data-attr-toggle="${esc(path)}" aria-expanded="${open}"${open ? ` aria-controls="${id}-kids"` : ''}>${icon(open ? 'minus' : 'plus', true)}<span>${esc(t(open ? 'attr.hideKids' : 'attr.showKids'))}</span></button>`;
160
+ if (!open) kids = toggle;
161
+ else {
162
+ const next = new Set(seen);
163
+ if (target) next.add(target);
164
+ const array = typeOf(resolved, document) === 'array';
165
+ const innerRaw = array ? resolved.items : schema;
166
+ const inner = resolve(innerRaw, document);
167
+ const childPath = array ? path + '[]' : path;
168
+ const body = inner.oneOf || inner.anyOf ? variants(innerRaw, ctx, depth + 1, childPath, next) : attrs(innerRaw, ctx, depth + 1, childPath, next);
169
+ kids = `<div class="attr-kids" id="${id}-kids">${toggle}${body}</div>`;
170
+ }
171
+ }
172
+ const state = issue ? 'issue' : present === false ? 'off' : present ? 'on' : '';
173
+ return `<li class="attr${state ? ' is-' + state : ''}${options.highlight === path ? ' is-hl' : ''}" id="${id}" data-path="${esc(path)}">
174
+ <div class="attr-head">
175
+ <code class="attr-name">${esc(name)}</code>${typeText(schema, nullable, document)}
176
+ ${recursive ? `<span class="attr-note">${esc(t('schema.recursive'))}</span>` : ''}
177
+ ${where ? `<span class="attr-in in-${esc(where)}" lang="en" title="in: ${esc(where)}">${esc(where)}</span>` : ''}
178
+ <span class="attr-req${required ? ' is-req' : ''}">${esc(t(required ? 'attr.required' : 'attr.optional'))}</span>
179
+ ${deprecated ? `<span class="attr-dep">${icon('warn', true)}${esc(t('attr.deprecated'))}</span>` : ''}
180
+ ${present !== undefined || issue ? `<span class="spacer"></span>${mark(state || 'off')}` : ''}
181
+ </div>
182
+ ${issue ? `<p class="attr-issue">${icon('warn', true)}<span>${esc(issue)}</span></p>` : ''}
183
+ ${options.compact ? '' : `${description ? `<p class="attr-desc" lang="en">${inline(String(description).replace(/\s*\n\s*/g, ' '))}</p>` : target ? '' : `<p class="attr-desc is-quiet">${esc(t('schema.noDesc'))}</p>`}
184
+ ${factList.length ? `<p class="attr-facts">${factList.join('')}</p>` : ''}
185
+ ${en && en.values.length > 1 ? enumBlock(en, ctx, path) : ''}`}
186
+ ${kids}
187
+ </li>`;
188
+ }
189
+
190
+ /** The rows of an object schema, plus (at the top of the Schema view) undocumented properties. */
191
+ function attrs(raw, ctx, depth, path, seen) {
192
+ const { props, required } = propertiesOf(raw, ctx.document);
193
+ const rows = Object.entries(props).map(([name, child]) => attrRow(name, child, required.has(name), ctx, depth, path ? `${path}.${name}` : name, seen));
194
+ if (depth === 0 && ctx.options.extra?.length) {
195
+ rows.push(...ctx.options.extra.map(item => `<li class="attr is-extra" data-path="${esc(item.name)}"><div class="attr-head"><code class="attr-name">${esc(item.name)}</code><span class="attr-type">${esc(Array.isArray(item.value) ? 'array' : item.value === null ? 'null' : typeof item.value)}</span><span class="attr-req is-undoc">${esc(t('attr.undocumented'))}</span><span class="spacer"></span>${mark('issue')}</div>${item.issue ? `<p class="attr-issue">${icon('warn', true)}<span>${esc(item.issue)}</span></p>` : ''}</li>`));
196
+ }
197
+ return `<ul class="attrs${ctx.options.compact ? ' is-compact' : ''}" role="list">${rows.join('')}</ul>`;
198
+ }
199
+
200
+ /** A oneOf inside a list: how the variant is chosen, a tab per variant, then its rows. */
201
+ function variants(raw, ctx, depth, path, seen) {
202
+ const { document, state } = ctx;
203
+ const list = variantsOf(raw, document);
204
+ const discriminator = discriminatorOf(raw, document);
205
+ const key = path || '';
206
+ const chosen = state.variant[key] || refName(list[0]) || '0';
207
+ const pick = list.find(variant => refName(variant) === chosen) || list[0];
208
+ const chosenName = refName(pick) || chosen;
209
+ return `<div class="attr-variants">
210
+ <span class="k">${discriminator ? esc(t('attr.chosenBy', { prop: '\u0000' })).replace('\u0000', `<code>${esc(discriminator.prop)}</code>`) : esc(t('attr.exactlyOne'))}</span>
211
+ <div class="tabs v-tabs" role="tablist" aria-label="${esc(t('schema.variant'))}">${list.map((variant, index) => {
212
+ const name = refName(variant) || String(index);
213
+ const value = discriminator?.map.find(([, target]) => target === name)?.[0];
214
+ return tab(value || name, { selected: name === chosenName, data: `data-variant="${esc(name)}" data-variant-path="${esc(key)}"`, lang: 'en' });
215
+ }).join('')}</div>
216
+ ${refName(pick) ? `<a class="attr-type is-ref" href="#model/${encodeURIComponent(chosenName)}" data-model="${esc(chosenName)}">${esc(chosenName)}</a>` : ''}
217
+ </div>${attrs(pick, ctx, depth, path, seen)}`;
218
+ }
219
+
220
+ function renderList(model) {
221
+ const { raw, document, options } = model;
222
+ const ctx = { document, state: model.state, options, id: model.prefix };
223
+ const schema = resolve(raw, document);
224
+ const seen = new Set([refName(raw)]);
225
+ if (schema.oneOf || schema.anyOf) return `<div class="attr-top">${variants(raw, ctx, 0, '', seen)}</div>`;
226
+ if (typeOf(schema, document) === 'array') return attrs(schema.items, ctx, 0, '[]', new Set([refName(schema.items)]));
227
+ return attrs(raw, ctx, 0, '', seen);
228
+ }
229
+
230
+ /**
231
+ * The attribute list of a schema (a `$ref`, an inline object, an array of objects or a union).
232
+ * Options: `id` (unique prefix for DOM ids), `state` (a {@link newTreeState} the caller keeps),
233
+ * `compact` (head lines only), `present` (a Set of paths a response returned), `issues`
234
+ * ({ path: reason }), `extra` ([{ name, value, issue }] returned but undocumented),
235
+ * `highlight` (a path), `label` (accessible name), `locations` ({ name: 'query' | … }).
236
+ * Call {@link bindTrees} on the container once the markup is in the document.
237
+ */
238
+ export function propertyTree(raw, document, options = {}) {
239
+ const id = options.id || `tree${++treeCounter}`;
240
+ const model = { raw, document, options, state: options.state || newTreeState(), prefix: id };
241
+ const key = `${id}:${++treeCounter}`;
242
+ trees.set(key, model);
243
+ const label = options.label || refName(raw) || t('model.schema');
244
+ return `<div class="stree" role="region" aria-label="${esc(t('schema.treeLabel', { name: label }))}" data-tree="${esc(key)}">${renderList(model)}</div>`;
245
+ }
246
+
247
+ /** Parameters (path, query, header, cookie) in the same list, each with its location pill. */
248
+ export function parameterTree(parameters, document, options = {}) {
249
+ const properties = Object.fromEntries(parameters.map(parameter => [parameter.name, {
250
+ ...(parameter.schema || Object.values(parameter.content || {})[0]?.schema || {}),
251
+ ...(parameter.description ? { description: parameter.description } : {}),
252
+ ...(parameter.example !== undefined ? { example: parameter.example } : {}),
253
+ ...(parameter.deprecated ? { deprecated: true } : {}),
254
+ }]));
255
+ const schema = { type: 'object', properties, required: parameters.filter(parameter => parameter.required).map(parameter => parameter.name) };
256
+ const locations = Object.fromEntries(parameters.map(parameter => [parameter.name, parameter.in]));
257
+ return propertyTree(schema, document, { ...options, locations, label: options.label || t('docs.parameters') });
258
+ }
259
+
260
+ /** Whether a list has any child list, for showing Collapse all. */
261
+ export function hasNested(raw, document) {
262
+ const { props } = propertiesOf(rowSchema(raw, document), document);
263
+ return Object.values(props).some(child => isExpandable(child, document));
264
+ }
265
+
266
+ /** Re-renders one list after its state changed and keeps the scroll position and focus. */
267
+ function redraw(tree, focus) {
268
+ const model = tree.treeModel;
269
+ if (!model) return;
270
+ const scroller = tree.closest('.pane-scroll');
271
+ const top = scroller?.scrollTop;
272
+ tree.innerHTML = renderList(model);
273
+ if (focus) tree.querySelector(focus)?.focus({ preventScroll: true });
274
+ if (scroller && top !== undefined) scroller.scrollTop = top;
275
+ }
276
+
277
+ /** Opens (`all` true) or closes every child list. */
278
+ export function setAllOpen(tree, all) {
279
+ const model = tree.treeModel;
280
+ if (!model) return;
281
+ model.state.expanded.clear();
282
+ model.state.collapsed.clear();
283
+ model.state.mode = all ? 'all' : 'none';
284
+ save(model.state);
285
+ redraw(tree);
286
+ }
287
+
288
+ /** Whether a list shows its child lists (so its head offers Collapse all). */
289
+ export const isAllOpen = tree => !!tree.treeModel && tree.treeModel.state.mode !== 'none' && !tree.treeModel.state.collapsed.size;
290
+
291
+ /**
292
+ * Wires every list under a container: the child-list toggles, "Show N more values" and the
293
+ * variant tabs. Safe to call again after the container's markup changes.
294
+ */
295
+ export function bindTrees(container) {
296
+ for (const tree of container.querySelectorAll('[data-tree]')) {
297
+ const model = trees.get(tree.dataset.tree);
298
+ if (!model) continue;
299
+ trees.delete(tree.dataset.tree);
300
+ tree.treeModel = model;
301
+ }
302
+ if (bound.has(container)) return;
303
+ bound.add(container);
304
+ container.addEventListener('click', event => {
305
+ const tree = event.target.closest?.('.stree');
306
+ const model = tree?.treeModel;
307
+ if (!model || !container.contains(tree)) return;
308
+ const toggle = event.target.closest('[data-attr-toggle]');
309
+ if (toggle) {
310
+ const path = toggle.dataset.attrToggle;
311
+ const open = toggle.getAttribute('aria-expanded') === 'true';
312
+ model.state.expanded.delete(path);
313
+ model.state.collapsed.delete(path);
314
+ (open ? model.state.collapsed : model.state.expanded).add(path);
315
+ save(model.state);
316
+ redraw(tree, `[data-attr-toggle="${CSS.escape(path)}"]`);
317
+ tree.dispatchEvent(new CustomEvent('tree-change', { bubbles: true }));
318
+ return;
319
+ }
320
+ const values = event.target.closest('[data-attr-values]');
321
+ if (values) {
322
+ const path = values.dataset.attrValues;
323
+ if (model.state.allValues.has(path)) model.state.allValues.delete(path); else model.state.allValues.add(path);
324
+ redraw(tree, `[data-attr-values="${CSS.escape(path)}"]`);
325
+ return;
326
+ }
327
+ const pick = event.target.closest('[data-variant]');
328
+ if (pick) {
329
+ model.state.variant[pick.dataset.variantPath] = pick.dataset.variant;
330
+ redraw(tree, `[data-variant="${CSS.escape(pick.dataset.variant)}"][data-variant-path="${CSS.escape(pick.dataset.variantPath)}"]`);
331
+ }
332
+ });
333
+ // Variant tabs: one tab stop per tab list, arrows move between variants.
334
+ container.addEventListener('keydown', event => {
335
+ const current = event.target.closest?.('.v-tabs [role="tab"]');
336
+ if (!current || !['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) return;
337
+ const tabs = [...current.parentElement.querySelectorAll('[role="tab"]')];
338
+ const rtl = getComputedStyle(current).direction === 'rtl';
339
+ const at = tabs.indexOf(current);
340
+ const step = event.key === 'Home' ? -at : event.key === 'End' ? tabs.length - 1 - at : (event.key === 'ArrowRight') !== rtl ? 1 : -1;
341
+ event.preventDefault();
342
+ tabs[(at + step + tabs.length) % tabs.length].click();
343
+ });
344
+ }