mirror of
https://github.com/JoshiCodes/knock.git
synced 2026-12-04 14:59:41 +01:00
- 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
176 lines
8.6 KiB
Svelte
176 lines
8.6 KiB
Svelte
<script lang="ts">
|
|
import { toast } from '@joshicodes/cock';
|
|
import Icon from '$lib/ui/Icon.svelte';
|
|
import { ws, type Tab } from '$lib/state/workspace.svelte';
|
|
import type { AuthDebug, Exchange, Json } from '$lib/types';
|
|
import TokenInspector from './TokenInspector.svelte';
|
|
import { formatMs } from '$lib/request';
|
|
|
|
let { debug, tab }: { debug: AuthDebug; tab?: Tab } = $props();
|
|
|
|
let open = $state<Record<string, boolean>>({});
|
|
let firstError = $derived(debug.steps.findIndex((s) => s.status === 'error'));
|
|
|
|
$effect(() => {
|
|
if (firstError >= 0) open[`${firstError}`] = true;
|
|
});
|
|
|
|
const mark: Record<string, { icon: string; cls: string; label: string }> = {
|
|
ok: { icon: 'check', cls: 'text-ok', label: 'ok' },
|
|
error: { icon: 'x', cls: 'text-delete', label: 'failed' },
|
|
skipped: { icon: 'minus', cls: 'text-fg-subtle', label: 'skipped' },
|
|
pending: { icon: 'more', cls: 'text-fg-muted', label: 'waiting' },
|
|
warning: { icon: 'alert', cls: 'text-post', label: 'warning' }
|
|
};
|
|
|
|
function m(s: string) {
|
|
return ws.maskSecrets ? maskText(s) : s;
|
|
}
|
|
|
|
/** Masks token-looking values and known secret parameters. */
|
|
function maskText(s: string): string {
|
|
return s
|
|
.replace(/\b(eyJ[\w-]+\.[\w-]+\.[\w-]*)/g, '••••••')
|
|
.replace(/((?:access_token|refresh_token|id_token|client_secret|code|password|code_verifier)["']?\s*[:=]\s*["']?)([^"'&\s,}]+)/gi, '$1••••••')
|
|
.replace(/(Authorization:\s*\w+\s+)\S+/gi, '$1••••••');
|
|
}
|
|
|
|
function copy(text: string, what = 'Copied') {
|
|
navigator.clipboard.writeText(text);
|
|
toast.success(what);
|
|
}
|
|
|
|
function isTokenData(d: Json): d is { tokens: Record<string, string> } {
|
|
return !!d && typeof d === 'object' && !Array.isArray(d) && 'tokens' in d;
|
|
}
|
|
function isParams(d: Json): d is { params: [string, string][]; url?: string } {
|
|
return !!d && typeof d === 'object' && !Array.isArray(d) && 'params' in d && Array.isArray((d as { params: unknown }).params);
|
|
}
|
|
|
|
function exchangeText(x: Exchange, side: 'req' | 'res'): string {
|
|
if (side === 'req') {
|
|
return `${x.method} ${x.url}\n${x.request_headers.map(([k, v]) => `${k}: ${v}`).join('\n')}${x.request_body ? `\n\n${x.request_body}` : ''}`;
|
|
}
|
|
return `${x.status ?? '(no response)'}\n${x.response_headers.map(([k, v]) => `${k}: ${v}`).join('\n')}${x.response_body ? `\n\n${prettyJson(x.response_body)}` : ''}`;
|
|
}
|
|
|
|
function prettyJson(s: string): string {
|
|
try {
|
|
return JSON.stringify(JSON.parse(s), null, 2);
|
|
} catch {
|
|
return s;
|
|
}
|
|
}
|
|
|
|
function exportFlow(format: 'json' | 'md') {
|
|
const steps = debug.steps.map((s) => ({ ...s, summary: maskText(s.summary), exchange: s.exchange ? JSON.parse(maskText(JSON.stringify(s.exchange))) : null, data: JSON.parse(maskText(JSON.stringify(s.data ?? null))) }));
|
|
const text =
|
|
format === 'json'
|
|
? JSON.stringify({ kind: debug.kind, origin: debug.origin, steps, warnings: debug.warnings }, null, 2)
|
|
: [`# ${debug.kind} auth flow`, '', ...steps.map((s, i) => `${i + 1}. **${s.title}**: ${s.status} (${Math.round(s.duration_ms)} ms) ${s.summary}${s.hint ? `\n > ${s.hint}` : ''}${s.exchange ? `\n\n \`\`\`\n ${s.exchange.curl.replace(/\n/g, '\n ')}\n \`\`\`` : ''}`)].join('\n');
|
|
copy(text, `Flow copied as ${format === 'json' ? 'JSON' : 'Markdown'} (secrets masked)`);
|
|
}
|
|
</script>
|
|
|
|
<div class="k-scroll selectable h-full p-3 text-[12.5px]">
|
|
{#if !debug.kind || debug.kind === 'none'}
|
|
<p class="text-fg-subtle">No auth was used for this request.</p>
|
|
{:else}
|
|
<div class="mb-3 flex items-center gap-3">
|
|
<span class="text-fg"><span class="font-bold">{debug.kind}</span> <span class="text-fg-muted">from the {debug.origin}{debug.origin !== 'request' ? ` (${debug.source})` : ''}</span></span>
|
|
<span class="flex-1"></span>
|
|
{#if debug.steps.length > 1}
|
|
<button class="text-[12px] text-fg-muted hover:text-fg" onclick={() => exportFlow('md')}>Copy as Markdown</button>
|
|
<button class="text-[12px] text-fg-muted hover:text-fg" onclick={() => exportFlow('json')}>Copy as JSON</button>
|
|
{/if}
|
|
</div>
|
|
|
|
{#each debug.warnings as w}
|
|
<p class="mb-1 flex items-center gap-1.5 text-post"><Icon name="alert" size={12} />{w}</p>
|
|
{/each}
|
|
|
|
<ol class="relative mt-2">
|
|
{#each debug.steps as s, i (i)}
|
|
{@const mk = mark[s.status] ?? mark.ok}
|
|
{@const isFirstError = i === firstError}
|
|
<li class="relative pb-1 pl-7">
|
|
{#if i < debug.steps.length - 1}<span class="absolute bottom-0 left-[9px] top-6 w-px bg-line"></span>{/if}
|
|
<span class="absolute left-0 top-1 grid size-[19px] place-items-center rounded-full border {isFirstError ? 'border-delete bg-delete/15' : 'border-line bg-panel'}">
|
|
<Icon name={mk.icon} size={11} class={mk.cls} stroke={2} />
|
|
</span>
|
|
<button class="flex w-full items-baseline gap-2 py-1 text-left" onclick={() => (open[`${i}`] = !open[`${i}`])}>
|
|
<span class="{s.status === 'error' ? 'font-bold text-delete' : s.status === 'skipped' ? 'text-fg-muted' : 'text-fg'}">{s.title}</span>
|
|
<span class="min-w-0 flex-1 truncate text-fg-muted">{m(s.summary)}</span>
|
|
<span class="mono shrink-0 text-[11px] text-fg-subtle">{s.duration_ms ? formatMs(s.duration_ms) : ''}</span>
|
|
{#if s.exchange || (s.data !== null && s.data !== undefined)}
|
|
<Icon name={open[`${i}`] ? 'chevron-down' : 'chevron-right'} size={11} class="shrink-0 text-fg-subtle" />
|
|
{/if}
|
|
</button>
|
|
{#if isFirstError}
|
|
<div class="mb-2 rounded border border-delete/40 bg-delete/8 px-3 py-2">
|
|
<div class="font-bold text-delete">This is where it failed</div>
|
|
<div class="mt-0.5 text-fg">{m(s.summary)}</div>
|
|
{#if s.hint}<div class="mt-1.5 whitespace-pre-wrap text-fg-muted">{s.hint}</div>{/if}
|
|
</div>
|
|
{:else if s.hint}
|
|
<div class="mb-1 whitespace-pre-wrap text-[12px] text-fg-muted">{s.hint}</div>
|
|
{/if}
|
|
{#if open[`${i}`]}
|
|
<div class="mb-3 flex flex-col gap-2">
|
|
{#if isParams(s.data)}
|
|
<div class="rounded border border-line">
|
|
{#if s.data.url}
|
|
<div class="flex items-start gap-2 border-b border-line px-2 py-1.5">
|
|
<span class="mono min-w-0 flex-1 break-all text-[12px]">{m(s.data.url)}</span>
|
|
<button class="shrink-0 text-[11px] text-accent" onclick={() => copy(String((s.data as { url: string }).url))}>copy</button>
|
|
</div>
|
|
{/if}
|
|
{#each s.data.params as [k, v]}
|
|
<div class="group mono flex items-start gap-3 border-b border-line/50 px-2 py-1 text-[12px] last:border-0">
|
|
<span class="w-40 shrink-0 text-get">{k}</span>
|
|
<span class="min-w-0 flex-1 break-all">{m(v)}</span>
|
|
<button class="shrink-0 font-sans text-[11px] text-accent opacity-0 group-hover:opacity-100" onclick={() => copy(v)}>copy</button>
|
|
</div>
|
|
{/each}
|
|
</div>
|
|
{/if}
|
|
{#if isTokenData(s.data)}
|
|
{#each Object.entries(s.data.tokens) as [name, token]}
|
|
<TokenInspector {name} {token} />
|
|
{/each}
|
|
{/if}
|
|
{#if s.exchange}
|
|
{@const x = s.exchange}
|
|
<div class="grid grid-cols-1 gap-2 xl:grid-cols-2">
|
|
<div class="rounded border border-line">
|
|
<div class="flex items-center border-b border-line px-2 py-1"><span class="k-label flex-1">Request</span><button class="text-[11px] text-accent" onclick={() => copy(x.curl, 'cURL copied')}>Copy as cURL</button></div>
|
|
<pre class="mono max-h-72 overflow-auto whitespace-pre-wrap break-all p-2 text-[11.5px]">{m(exchangeText(x, 'req'))}</pre>
|
|
</div>
|
|
<div class="rounded border border-line">
|
|
<div class="flex items-center border-b border-line px-2 py-1"><span class="k-label flex-1">Response</span><button class="text-[11px] text-accent" onclick={() => copy(exchangeText(x, 'res'))}>copy</button></div>
|
|
<pre class="mono max-h-72 overflow-auto whitespace-pre-wrap break-all p-2 text-[11.5px]">{m(exchangeText(x, 'res'))}</pre>
|
|
</div>
|
|
</div>
|
|
{/if}
|
|
{#if s.data !== null && s.data !== undefined && !isParams(s.data) && !isTokenData(s.data)}
|
|
<pre class="mono max-h-72 overflow-auto whitespace-pre-wrap break-all rounded border border-line p-2 text-[11.5px]">{m(JSON.stringify(s.data, null, 2))}</pre>
|
|
{/if}
|
|
</div>
|
|
{/if}
|
|
</li>
|
|
{/each}
|
|
</ol>
|
|
|
|
{#if debug.sent.length}
|
|
<div class="k-label mb-1.5 mt-4">Attached to the request</div>
|
|
{#each debug.sent as x}
|
|
<div class="mono flex gap-3 border-b border-line/50 py-1">
|
|
<span class="w-16 shrink-0 text-fg-subtle">{x.location}</span>
|
|
<span class="w-40 shrink-0 text-get">{x.name}</span>
|
|
<span class="min-w-0 break-all">{ws.maskSecrets ? '••••••' : x.value}</span>
|
|
</div>
|
|
{/each}
|
|
{/if}
|
|
{/if}
|
|
</div>
|