Skeleton, REST core, variables and JSON editor (M0–M2)

- knock-core: YAML collection format with unknown-field round trip,
  variable resolution across scopes with secrets and dynamic values,
  HTTP engine with manual redirects, cookie jar and large-body spill,
  basic/bearer/API key auth with debug output, assertions and extracts
- Tauri app: collection commands, file watcher, SQLite history,
  keyring-backed secrets
- Svelte UI: sidebar tree, tabs, request editor, response viewer,
  JSON form/code editor with schema hints, environments, settings
This commit is contained in:
2026-11-27 16:42:31 +01:00
commit f6484f4dc8
127 changed files with 22909 additions and 0 deletions
@@ -0,0 +1,166 @@
<script lang="ts">
import { plain } from '$lib/plain.svelte';
import { toast, Switch } from '@joshicodes/cock';
import { open as openDialog } from '@tauri-apps/plugin-dialog';
import KeyValueTable from '$lib/ui/KeyValueTable.svelte';
import AuthPanel from '$lib/views/auth/AuthPanel.svelte';
import Icon from '$lib/ui/Icon.svelte';
import { api, errorText } from '$lib/api';
import { ws } from '$lib/state/workspace.svelte';
import { HEADER_NAMES, headerValues } from '$lib/headers';
import type { KeyValue, Manifest } from '$lib/types';
let { col }: { col: string } = $props();
let m = $state<Manifest | null>(null);
let saved = $state('');
let varRows = $state<KeyValue[]>([]);
let root = $derived(ws.collection(col)?.root ?? '');
async function load() {
const x = await api.manifestGet(col);
x.headers ??= [];
x.auth ??= 'none';
x.settings ??= {};
x.settings.client_certs ??= [];
m = x;
varRows = Object.entries(x.vars ?? {}).map(([name, value]) => ({ name, value }));
saved = JSON.stringify(x);
}
$effect(() => {
load();
});
let dirty = $derived(m ? JSON.stringify(m) !== saved : false);
async function save() {
if (!m) return;
m.vars = Object.fromEntries(varRows.filter((r) => r.name).map((r) => [r.name, r.value]));
const snap = plain(m);
if (snap.settings && !snap.settings.client_certs?.length) delete snap.settings.client_certs;
if (snap.settings?.proxy?.mode === 'system' && !snap.settings.proxy.url) delete snap.settings.proxy;
try {
ws.upsert(await api.manifestSave(col, snap));
saved = JSON.stringify(m);
toast.success('Collection saved');
} catch (e) {
toast.error('Could not save', { description: errorText(e) });
}
}
async function pick(filters?: { name: string; extensions: string[] }[]) {
const p = await openDialog({ defaultPath: root, filters });
if (typeof p !== 'string') return null;
return p.startsWith(root + '/') ? p.slice(root.length + 1) : p;
}
function onKey(e: KeyboardEvent) {
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
e.preventDefault();
e.stopPropagation();
save();
}
}
</script>
<svelte:window onkeydowncapture={onKey} />
{#if m}
{@const s = m.settings!}
<div class="k-scroll h-full">
<div class="mx-auto flex max-w-3xl flex-col gap-8 px-6 py-6">
<div class="flex items-center gap-3">
<div class="flex-1">
<div class="k-label">Collection</div>
<input class="-ml-1 w-full rounded bg-transparent px-1 text-[20px] font-bold text-strong outline-none hover:bg-input focus:bg-input" bind:value={m.name} />
<div class="mono text-[12px] text-fg-subtle">{root}/knock.yaml</div>
</div>
<button class="rounded bg-accent px-4 py-1.5 font-bold text-accent-fg disabled:opacity-40" disabled={!dirty} onclick={save}>Save</button>
</div>
<section>
<h2 class="k-label mb-2">Auth</h2>
<p class="mb-2 text-[12px] text-fg-subtle">Default for every request that inherits.</p>
<div class="rounded border border-line"><AuthPanel bind:auth={m.auth!} level="collection" /></div>
</section>
<section>
<h2 class="k-label mb-2">Headers</h2>
<KeyValueTable bind:rows={m.headers!} nameSuggestions={HEADER_NAMES} valueSuggestions={headerValues} />
</section>
<section>
<h2 class="k-label mb-2">Variables</h2>
<p class="mb-2 text-[12px] text-fg-subtle">The widest scope inside the collection. Environments override these.</p>
<KeyValueTable bind:rows={varRows} onchange={() => (m!.vars = Object.fromEntries(varRows.filter((r) => r.name).map((r) => [r.name, r.value])))} />
</section>
<section class="flex flex-col gap-4">
<h2 class="k-label">Network</h2>
<div class="flex items-center gap-4">
<div class="flex-1">
<div>Proxy</div>
<div class="text-[12px] text-fg-subtle">System honours HTTP_PROXY, HTTPS_PROXY and NO_PROXY</div>
</div>
<select
class="k-field !w-40 !border-line !bg-input !font-sans"
value={s.proxy?.mode ?? 'system'}
onchange={(e) => (s.proxy = { ...(s.proxy ?? {}), mode: (e.currentTarget as HTMLSelectElement).value as 'system' | 'none' | 'custom' })}
>
<option value="system">System</option>
<option value="none">No proxy</option>
<option value="custom">Custom</option>
</select>
</div>
{#if s.proxy?.mode === 'custom'}
<div class="flex gap-2 pl-4">
<input class="k-field mono flex-1 !border-line !bg-input" placeholder="http://proxy:3128" bind:value={() => s.proxy!.url ?? '', (v) => (s.proxy!.url = v || undefined)} />
<input class="k-field mono flex-1 !border-line !bg-input" placeholder="No proxy for: localhost,.internal" bind:value={() => s.proxy!.no_proxy ?? '', (v) => (s.proxy!.no_proxy = v || undefined)} />
</div>
{/if}
<div class="flex items-center gap-4">
<div class="flex-1">
<div>Extra CA certificates</div>
<div class="text-[12px] text-fg-subtle">PEM bundle trusted in addition to the system store</div>
</div>
<button class="mono max-w-[50%] truncate rounded border border-line bg-input px-2.5 py-1 text-[12px] hover:border-border-strong" onclick={async () => (s.ca_bundle = (await pick([{ name: 'PEM', extensions: ['pem', 'crt', 'cer'] }])) ?? s.ca_bundle)}>
{s.ca_bundle ?? 'Choose file…'}
</button>
{#if s.ca_bundle}<button class="text-fg-subtle hover:text-delete" onclick={() => (s.ca_bundle = undefined)}><Icon name="x" size={12} /></button>{/if}
</div>
<div class="flex items-center gap-4">
<div class="flex-1">
<div>Cookie jar</div>
<div class="text-[12px] text-fg-subtle">Store cookies from responses and send them back</div>
</div>
<Switch checked={s.cookies !== false} onchange={(v) => (s.cookies = v ? undefined : false)} aria-label="Cookie jar" />
</div>
</section>
<section>
<div class="mb-2 flex items-center">
<h2 class="k-label flex-1">Client certificates (mTLS)</h2>
<button class="text-[12px] text-accent hover:underline" onclick={() => s.client_certs!.push({ host: '' })}>Add certificate</button>
</div>
{#each s.client_certs! as c, i (i)}
<div class="mb-2 flex flex-col gap-2 rounded border border-line p-3">
<div class="flex items-center gap-2">
<input class="k-field mono flex-1 !border-line !bg-input" placeholder="api.example.com or api.example.com:8443" bind:value={c.host} />
<button class="text-fg-subtle hover:text-delete" onclick={() => s.client_certs!.splice(i, 1)} title="Remove"><Icon name="trash" size={13} /></button>
</div>
<div class="grid grid-cols-[100px_1fr] items-center gap-2 text-[12px]">
<span class="text-fg-muted">Certificate</span>
<button class="mono truncate rounded border border-line bg-input px-2 py-1 text-left" onclick={async () => (c.cert = (await pick()) ?? c.cert)}>{c.cert ?? 'PEM file…'}</button>
<span class="text-fg-muted">Private key</span>
<button class="mono truncate rounded border border-line bg-input px-2 py-1 text-left" onclick={async () => (c.key = (await pick()) ?? c.key)}>{c.key ?? 'PEM file…'}</button>
<span class="text-fg-muted">or PKCS#12</span>
<button class="mono truncate rounded border border-line bg-input px-2 py-1 text-left" onclick={async () => (c.pkcs12 = (await pick([{ name: 'PKCS#12', extensions: ['p12', 'pfx'] }])) ?? c.pkcs12)}>{c.pkcs12 ?? '.p12 / .pfx file…'}</button>
<span class="text-fg-muted">Passphrase</span>
<input class="k-field mono !border-line !bg-input" placeholder={'{{secret.certPassphrase}}'} bind:value={() => c.passphrase ?? '', (v) => (c.passphrase = v || undefined)} />
</div>
</div>
{/each}
</section>
</div>
</div>
{/if}