feat(web): add api.{get,post,del} typed facade over apiFetch
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { afterEach, describe, expect, test, vi } from 'vitest';
|
||||
import { apiFetch, type ApiError } from './client';
|
||||
import { api, apiFetch, type ApiError } from './client';
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
@@ -55,3 +55,27 @@ describe('apiFetch', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('api.get/post/del', () => {
|
||||
test('api.get returns typed JSON on 200', async () => {
|
||||
stubFetch(200, { id: 'abc', name: 'A' });
|
||||
type T = { id: string; name: string };
|
||||
const result = await api.get<T>('/api/artists/abc');
|
||||
expect(result).toEqual({ id: 'abc', name: 'A' });
|
||||
});
|
||||
|
||||
test('api.post serializes body and sets method', async () => {
|
||||
const spy = stubFetch(200, { ok: true });
|
||||
await api.post('/api/auth/login', { username: 'u', password: 'p' });
|
||||
const init = spy.mock.calls[0][1] as RequestInit;
|
||||
expect(init.method).toBe('POST');
|
||||
expect(init.body).toBe(JSON.stringify({ username: 'u', password: 'p' }));
|
||||
});
|
||||
|
||||
test('api.del sends DELETE and resolves to null on 204', async () => {
|
||||
const spy = stubFetch(204, null);
|
||||
const result = await api.del('/api/sessions/xyz');
|
||||
expect(spy.mock.calls[0][1]).toMatchObject({ method: 'DELETE' });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user