@contractkit/explorer-ui 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +130 -0
- package/dist/assets/style.css +859 -0
- package/dist/constraints.d.ts +4 -0
- package/dist/constraints.d.ts.map +1 -0
- package/dist/html.d.ts +28 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1359 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown.d.ts +10 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/render-code-samples.d.ts +20 -0
- package/dist/render-code-samples.d.ts.map +1 -0
- package/dist/render-item.d.ts +44 -0
- package/dist/render-item.d.ts.map +1 -0
- package/dist/render-model.d.ts +9 -0
- package/dist/render-model.d.ts.map +1 -0
- package/dist/render-operation.d.ts +17 -0
- package/dist/render-operation.d.ts.map +1 -0
- package/dist/render-schema.d.ts +18 -0
- package/dist/render-schema.d.ts.map +1 -0
- package/dist/render-tryit.d.ts +12 -0
- package/dist/render-tryit.d.ts.map +1 -0
- package/dist/render-type.d.ts +11 -0
- package/dist/render-type.d.ts.map +1 -0
- package/dist/render.d.ts +7 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/types.d.ts +65 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# @contractkit/explorer-ui
|
|
2
|
+
|
|
3
|
+
Pure HTML renderer for a ContractKit API explorer. Takes a normalized `PreviewData` snapshot and produces themable HTML strings — no DOM, no framework. The output runs anywhere an HTML string can be inserted: a VS Code webview, a static site, an Electron window, an iframe in a docs site.
|
|
4
|
+
|
|
5
|
+
`@contractkit/vscode-extension` is the first consumer and ships an inline preview panel built on top of this package. A future `@contractkit/plugin-explorer` will generate a self-contained static API explorer from the same renderer.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @contractkit/explorer-ui
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { renderApp, renderItemPage, type PreviewData } from '@contractkit/explorer-ui';
|
|
17
|
+
|
|
18
|
+
const data: PreviewData = {
|
|
19
|
+
configMeta: { title: 'Payments API', version: '1.0.0' },
|
|
20
|
+
operations: [
|
|
21
|
+
{
|
|
22
|
+
filePath: '/contracts/payments.ck',
|
|
23
|
+
fileGroup: 'payments',
|
|
24
|
+
routePath: '/payments/{id}',
|
|
25
|
+
method: 'get',
|
|
26
|
+
op: { /* OpOperationNode from @contractkit/core */ },
|
|
27
|
+
effectiveModifiers: [],
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
models: [
|
|
31
|
+
{
|
|
32
|
+
filePath: '/contracts/payments.ck',
|
|
33
|
+
model: { /* ModelNode from @contractkit/core */ },
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
warnings: [],
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Full sidebar + detail layout (workspace-wide view)
|
|
40
|
+
document.body.innerHTML = renderApp(data);
|
|
41
|
+
|
|
42
|
+
// Or just one item — paired with a host-supplied navigation tree
|
|
43
|
+
document.body.innerHTML = renderItemPage(data, { kind: 'operation', id: 'op-get-payments-id-' });
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Pair the HTML with the bundled stylesheet:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import '@contractkit/explorer-ui/style.css';
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
…or copy `dist/assets/style.css` into your project and link it.
|
|
53
|
+
|
|
54
|
+
## Theming
|
|
55
|
+
|
|
56
|
+
All colors and spacing reference CSS custom properties on `:root`. Override any to retheme:
|
|
57
|
+
|
|
58
|
+
```css
|
|
59
|
+
:root {
|
|
60
|
+
--ce-fg: var(--vscode-foreground);
|
|
61
|
+
--ce-bg: var(--vscode-editor-background);
|
|
62
|
+
--ce-sidebar-bg: var(--vscode-sideBar-background);
|
|
63
|
+
--ce-link: var(--vscode-textLink-foreground);
|
|
64
|
+
--ce-border: var(--vscode-panel-border);
|
|
65
|
+
--ce-code-bg: var(--vscode-textCodeBlock-background);
|
|
66
|
+
/* …method/status colors, badges, warning palette */
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The VS Code extension overrides these to match the active editor theme.
|
|
71
|
+
|
|
72
|
+
## Data contract
|
|
73
|
+
|
|
74
|
+
The renderer expects **pre-resolved** data — the consumer is responsible for running ContractKit's normalization passes (`applyOptionsDefaults`, `applyVariableSubstitution`, `decomposeCk`, `resolveModifiers`, `resolveSecurity`) and producing the shape in [`src/types.ts`](src/types.ts):
|
|
75
|
+
|
|
76
|
+
| Type | Role |
|
|
77
|
+
| --- | --- |
|
|
78
|
+
| `PreviewData` | Top-level snapshot — `configMeta`, `operations`, `models`, `warnings` |
|
|
79
|
+
| `PreviewConfigMeta` | `title`, `version`, optional `description` and `servers[]` |
|
|
80
|
+
| `ResolvedOperation` | Operation node + file path + effective modifiers/security + grouping key |
|
|
81
|
+
| `ResolvedModel` | Model node + file path |
|
|
82
|
+
| `PreviewWarning` | Non-fatal diagnostics (e.g. unresolved `{{var}}` references) |
|
|
83
|
+
|
|
84
|
+
`@contractkit/core` is a workspace dependency for types only — no runtime imports. An eslint rule (`no-restricted-imports`) enforces this so the bundle stays small in webview consumers.
|
|
85
|
+
|
|
86
|
+
## Public API
|
|
87
|
+
|
|
88
|
+
| Export | Returns | Purpose |
|
|
89
|
+
| --- | --- | --- |
|
|
90
|
+
| `renderApp(data)` | HTML string | Full layout: sidebar nav + detail pane |
|
|
91
|
+
| `renderItemPage(data, selection, options?)` | HTML string | Single-item detail page (operation / model / overview) |
|
|
92
|
+
| `renderOperation(op, options?)` | HTML string | Operation card with header, parameters, request/response schemas, and an optional Try-it form. `options.ctx` enables inline-ref expansion |
|
|
93
|
+
| `renderModel(resolvedModel, ctx?)` | HTML string | Model card with badges and field table |
|
|
94
|
+
| `renderFieldRows(fields, ctx?)` | HTML string | Just the field table — exported for reuse in inline-object rendering |
|
|
95
|
+
| `renderType(type, ctx?)` | HTML string | Recursive type rendering; `ctx.models` enables inline ref expansion |
|
|
96
|
+
| `renderSchemaTree(type, ctx?, options?)` | HTML string | Indented schema field tree with constraints and defaults; supports `exclude` to drop readonly/writeonly fields |
|
|
97
|
+
| `renderCodeSamples(op, baseUrl, ctx?)` | HTML string | Curl request + synthesized JSON response example for the right rail; deterministically seeded per operation |
|
|
98
|
+
| `renderTryIt(op, baseUrl, ctx?)` | HTML string | Standalone Try-it form, pre-filled with deterministic faker-generated samples (already included by `renderOperation` when configured) |
|
|
99
|
+
| `renderMarkdown(input)` | HTML string | Tiny safe Markdown renderer (paragraphs, headings, lists, code, bold/italic, http links) |
|
|
100
|
+
| `operationId(op)` / `modelId(name)` | string | Stable anchor ids used by `ItemSelection` |
|
|
101
|
+
| `listSelections(data)` | array | Flat list of every selectable item (useful for building a picker) |
|
|
102
|
+
| `escapeHtml`, `html`, `raw` | helpers | Tagged-template helpers used internally |
|
|
103
|
+
|
|
104
|
+
## Inline-expanding model refs
|
|
105
|
+
|
|
106
|
+
When `RenderContext.models` is provided, `ref` types in operations and models render as collapsible `<details>` blocks containing the model's fields recursively. Cycles are detected via a `visited` set and render as a `↺` indicator. Past `maxDepth` (default 4) refs collapse back to plain links carrying `data-jump-file` / `data-jump-line` attributes so the host can jump to source.
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
const ctx = { models: new Map(data.models.map(m => [m.model.name, m])) };
|
|
110
|
+
renderType(someTypeWithRefs, ctx);
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Host integration
|
|
114
|
+
|
|
115
|
+
The webview script (or static-site bootstrapper) attaches event delegation to handle:
|
|
116
|
+
|
|
117
|
+
| Selector | Behavior |
|
|
118
|
+
| --- | --- |
|
|
119
|
+
| `[data-tryit-action="send"]` | Read the form, post `{type:'sendRequest', request}` to the host, render response |
|
|
120
|
+
| `[data-open-model]` | Navigate the detail view to that model's dedicated page |
|
|
121
|
+
| `[data-jump-file] / [data-jump-line]` | Post `{type:'reveal', file, line}` to the host (in VS Code, opens the source) |
|
|
122
|
+
| `a.ce-ref` | Fallback for unresolved refs — host navigates to the dedicated page |
|
|
123
|
+
|
|
124
|
+
See [`apps/vscode-extension/src/webview/main.ts`](../../apps/vscode-extension/src/webview/main.ts) for a complete example.
|
|
125
|
+
|
|
126
|
+
## Why HTML strings, not DOM/JSX?
|
|
127
|
+
|
|
128
|
+
- **Isomorphic.** Same output in a webview, a static-site generator, a server response, or a test snapshot.
|
|
129
|
+
- **No runtime dependencies in consumers.** The webview bundle stays at ~20 KB; a static-site generator emits `index.html` and is done.
|
|
130
|
+
- **Easy to snapshot-test.** All 96 tests in this package work on plain strings.
|