diff --git a/ui/src/api/audit.ts b/ui/src/api/audit.ts index adc97da1d..960a6d965 100644 --- a/ui/src/api/audit.ts +++ b/ui/src/api/audit.ts @@ -1,11 +1,11 @@ -import { apiFetch, BASE, createSSEStream } from './http'; +import { apiFetch, BASE, createSSEStream, kindQuery } from './http'; import type { AuditAllResponse, AuditPolicy, AuditRulesResponse, AuditSkillResponse, CompiledRulesResponse } from './types/audit'; export const auditApi = { auditAll: (kind?: 'skills' | 'agents') => - apiFetch(`/audit${kind ? '?kind=' + kind : ''}`), + apiFetch(`/audit${kindQuery(kind)}`), auditSkill: (name: string, kind?: 'skill' | 'agent') => - apiFetch(`/audit/${encodeURIComponent(name)}${kind === 'agent' ? '?kind=agent' : ''}`), + apiFetch(`/audit/${encodeURIComponent(name)}${kindQuery(kind === 'agent' ? kind : undefined)}`), auditAllStream: ( onStart: (total: number) => void, onProgress: (scanned: number) => void, @@ -13,7 +13,7 @@ export const auditApi = { onError: (err: Error) => void, kind?: 'skills' | 'agents', ): EventSource => - createSSEStream(BASE + `/audit/stream${kind ? '?kind=' + kind : ''}`, { + createSSEStream(BASE + `/audit/stream${kindQuery(kind)}`, { start: (d) => onStart(d.total), progress: (d) => onProgress(d.scanned), done: onDone, diff --git a/ui/src/api/backups.ts b/ui/src/api/backups.ts index 0348d8ba9..4218a2da5 100644 --- a/ui/src/api/backups.ts +++ b/ui/src/api/backups.ts @@ -1,4 +1,4 @@ -import { apiFetch } from './http'; +import { apiFetch, kindQuery } from './http'; import type { BackupListResponse, FileBackup, FileBackupVersions, RestoreValidateResponse, TrashListResponse } from './types/backups'; export const backupsApi = { @@ -36,17 +36,17 @@ export const backupsApi = { listTrash: () => apiFetch('/trash'), restoreTrash: (name: string, kind?: 'skill' | 'agent') => apiFetch<{ success: boolean }>( - `/trash/${encodeURIComponent(name)}/restore${kind ? `?kind=${encodeURIComponent(kind)}` : ''}`, + `/trash/${encodeURIComponent(name)}/restore${kindQuery(kind)}`, { method: 'POST' }, ), deleteTrash: (name: string, kind?: 'skill' | 'agent') => apiFetch<{ success: boolean }>( - `/trash/${encodeURIComponent(name)}${kind ? `?kind=${encodeURIComponent(kind)}` : ''}`, + `/trash/${encodeURIComponent(name)}${kindQuery(kind)}`, { method: 'DELETE' }, ), emptyTrash: (kind: 'skill' | 'agent' | 'all' = 'all') => apiFetch<{ success: boolean; removed: number }>( - `/trash/empty${kind ? `?kind=${encodeURIComponent(kind)}` : ''}`, + `/trash/empty${kindQuery(kind)}`, { method: 'POST' }, ), }; diff --git a/ui/src/api/http.ts b/ui/src/api/http.ts index f11f26447..c40782508 100644 --- a/ui/src/api/http.ts +++ b/ui/src/api/http.ts @@ -70,6 +70,11 @@ export function parseApiErrorPayload(data: any, status: number, statusText: stri return { code, message, params }; } +// kindQuery returns the `?kind=` query for a request, or '' when no kind is given. +export function kindQuery(kind?: string): string { + return kind ? `?kind=${encodeURIComponent(kind)}` : ''; +} + export async function apiFetch(path: string, init?: RequestInit): Promise { let res: Response; try { diff --git a/ui/src/api/kindQuery.test.ts b/ui/src/api/kindQuery.test.ts new file mode 100644 index 000000000..0d966ce76 --- /dev/null +++ b/ui/src/api/kindQuery.test.ts @@ -0,0 +1,32 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { apiFetch } from './http'; +import { auditApi } from './audit'; +import { resourcesApi } from './resources'; + +vi.mock('./http', async (load) => ({ ...await load(), apiFetch: vi.fn() })); + +// The kind unions are closed today; a cast stands in for a value that needs escaping. +const odd = 'a&b' as 'skill'; + +describe('?kind= query', () => { + beforeEach(() => vi.mocked(apiFetch).mockReset()); + + it('is encoded on resource requests', async () => { + await resourcesApi.getResource('demo', odd); + + expect(vi.mocked(apiFetch).mock.calls[0][0]).toBe('/resources/demo?kind=a%26b'); + }); + + it('is encoded on audit requests', async () => { + await auditApi.auditAll(odd as unknown as 'skills'); + + expect(vi.mocked(apiFetch).mock.calls[0][0]).toBe('/audit?kind=a%26b'); + }); + + it('is left off without a kind', async () => { + await resourcesApi.listSkills(); + await auditApi.auditAll(); + + expect(vi.mocked(apiFetch).mock.calls.map(([path]) => path)).toEqual(['/resources', '/audit']); + }); +}); diff --git a/ui/src/api/resources.ts b/ui/src/api/resources.ts index 026fe1add..8fe83ce2b 100644 --- a/ui/src/api/resources.ts +++ b/ui/src/api/resources.ts @@ -1,27 +1,27 @@ -import { apiFetch } from './http'; +import { apiFetch, kindQuery } from './http'; import type { BatchToggleResult, BatchUninstallRequest, BatchUninstallResult, CollectResult, CollectScanResult, CreateSkillRequest, CreateSkillResponse, Overview, Skill, SkillFileContent, TemplatesResponse } from './types/resources'; export const resourcesApi = { getOverview: () => apiFetch('/overview'), listSkills: (kind?: 'skill' | 'agent') => - apiFetch<{ resources: Skill[] }>(kind ? `/resources?kind=${kind}` : '/resources'), + apiFetch<{ resources: Skill[] }>(`/resources${kindQuery(kind)}`), getResource: (name: string, kind?: 'skill' | 'agent') => apiFetch<{ resource: Skill; skillMdContent: string; files: string[] }>( - `/resources/${encodeURIComponent(name)}${kind ? `?kind=${kind}` : ''}` + `/resources/${encodeURIComponent(name)}${kindQuery(kind)}` ), deleteResource: (name: string, kind?: 'skill' | 'agent') => apiFetch<{ success: boolean }>( - `/resources/${encodeURIComponent(name)}${kind ? `?kind=${kind}` : ''}`, + `/resources/${encodeURIComponent(name)}${kindQuery(kind)}`, { method: 'DELETE' } ), disableResource: (name: string, kind?: 'skill' | 'agent') => apiFetch<{ success: boolean; name: string; disabled: boolean }>( - `/resources/${encodeURIComponent(name)}/disable${kind ? `?kind=${kind}` : ''}`, + `/resources/${encodeURIComponent(name)}/disable${kindQuery(kind)}`, { method: 'POST' } ), enableResource: (name: string, kind?: 'skill' | 'agent') => apiFetch<{ success: boolean; name: string; disabled: boolean }>( - `/resources/${encodeURIComponent(name)}/enable${kind ? `?kind=${kind}` : ''}`, + `/resources/${encodeURIComponent(name)}/enable${kindQuery(kind)}`, { method: 'POST' } ), batchUninstall: (opts: BatchUninstallRequest) => @@ -74,14 +74,14 @@ export const resourcesApi = { path: string; contentType: string; savedAt: string; - }>(`/resources/${encodeURIComponent(name)}/content${kind ? `?kind=${kind}` : ''}`, { + }>(`/resources/${encodeURIComponent(name)}/content${kindQuery(kind)}`, { method: 'PUT', body: JSON.stringify({ content }), }), // Update source URL for a tracked skill or agent. updateSkillSource: (name: string, source: string, kind?: 'skill' | 'agent') => apiFetch<{ success: boolean; source: string; repoUrl: string }>( - `/resources/${encodeURIComponent(name)}/source${kind ? `?kind=${kind}` : ''}`, + `/resources/${encodeURIComponent(name)}/source${kindQuery(kind)}`, { method: 'PATCH', body: JSON.stringify({ source }), @@ -93,7 +93,7 @@ export const resourcesApi = { opts?: { editor?: string; kind?: 'skill' | 'agent' } ) => apiFetch<{ editor: string; path: string; pid: number }>( - `/resources/${encodeURIComponent(name)}/open-in-editor${opts?.kind ? `?kind=${opts.kind}` : ''}`, + `/resources/${encodeURIComponent(name)}/open-in-editor${kindQuery(opts?.kind)}`, { method: 'POST', body: JSON.stringify({ editor: opts?.editor ?? 'auto' }),