Fix content type and offset detection

This commit is contained in:
Jack Kingsman
2026-02-23 21:01:48 -08:00
parent a22224980e
commit 4b84f609b7
2 changed files with 7 additions and 8 deletions

View File

@@ -21,10 +21,11 @@ import type {
const API_BASE = '/api';
async function fetchJson<T>(url: string, options?: RequestInit): Promise<T> {
const hasBody = options?.body !== undefined;
const res = await fetch(`${API_BASE}${url}`, {
...options,
headers: {
'Content-Type': 'application/json',
...(hasBody && { 'Content-Type': 'application/json' }),
...options?.headers,
},
});
@@ -148,8 +149,8 @@ export const api = {
signal?: AbortSignal
) => {
const searchParams = new URLSearchParams();
if (params?.limit) searchParams.set('limit', params.limit.toString());
if (params?.offset) searchParams.set('offset', params.offset.toString());
if (params?.limit !== undefined) searchParams.set('limit', params.limit.toString());
if (params?.offset !== undefined) searchParams.set('offset', params.offset.toString());
if (params?.type) searchParams.set('type', params.type);
if (params?.conversation_key) searchParams.set('conversation_key', params.conversation_key);
if (params?.before !== undefined) searchParams.set('before', params.before.toString());

View File

@@ -159,7 +159,7 @@ describe('fetchJson (via api methods)', () => {
});
describe('Content-Type header', () => {
it('always sends Content-Type: application/json on GET requests', async () => {
it('omits Content-Type on GET requests (no body)', async () => {
installMockFetch();
mockFetch.mockResolvedValueOnce({
ok: true,
@@ -169,12 +169,10 @@ describe('fetchJson (via api methods)', () => {
await api.getHealth();
const [, options] = mockFetch.mock.calls[0];
expect(options.headers).toEqual(
expect.objectContaining({ 'Content-Type': 'application/json' })
);
expect(options.headers).not.toHaveProperty('Content-Type');
});
it('always sends Content-Type: application/json on POST requests', async () => {
it('sends Content-Type: application/json on POST requests with body', async () => {
installMockFetch();
mockFetch.mockResolvedValueOnce({
ok: true,