mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
refactor(ui): build the kind query through one encoding helper
resources.ts and audit.ts appended ?kind= unencoded while backups.ts encoded it. Every call now uses kindQuery in http.ts, which encodes. Today's kind values are fixed words, so requests do not change; the helper keeps a future value from breaking the query string.
This commit is contained in:
+4
-4
@@ -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<AuditAllResponse>(`/audit${kind ? '?kind=' + kind : ''}`),
|
||||
apiFetch<AuditAllResponse>(`/audit${kindQuery(kind)}`),
|
||||
auditSkill: (name: string, kind?: 'skill' | 'agent') =>
|
||||
apiFetch<AuditSkillResponse>(`/audit/${encodeURIComponent(name)}${kind === 'agent' ? '?kind=agent' : ''}`),
|
||||
apiFetch<AuditSkillResponse>(`/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,
|
||||
|
||||
@@ -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<TrashListResponse>('/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' },
|
||||
),
|
||||
};
|
||||
|
||||
@@ -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<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
let res: Response;
|
||||
try {
|
||||
|
||||
@@ -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<typeof import('./http')>(), 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']);
|
||||
});
|
||||
});
|
||||
@@ -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>('/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' }),
|
||||
|
||||
Reference in New Issue
Block a user