diff --git a/src/core/client.test.ts b/src/core/client.test.ts new file mode 100644 index 0000000..c6b21e8 --- /dev/null +++ b/src/core/client.test.ts @@ -0,0 +1,23 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { PixelmuseClient } from './client.js' + +describe('PixelmuseClient', () => { + afterEach(() => { + vi.restoreAllMocks() + }) + + it('handles 204 delete responses without trying to parse JSON', async () => { + const fetchSpy = vi + .spyOn(globalThis, 'fetch') + .mockResolvedValue(new Response(null, { status: 204 })) + + const client = new PixelmuseClient(`pm_live_${'a'.repeat(32)}`) + + await expect(client.deleteGeneration('img_123')).resolves.toBeUndefined() + expect(fetchSpy).toHaveBeenCalledTimes(1) + expect(fetchSpy).toHaveBeenCalledWith( + 'https://www.pixelmuse.studio/api/v1/images/img_123', + expect.objectContaining({ method: 'DELETE' }), + ) + }) +}) diff --git a/src/core/client.ts b/src/core/client.ts index f53afb6..a66b730 100644 --- a/src/core/client.ts +++ b/src/core/client.ts @@ -79,6 +79,10 @@ export class PixelmuseClient { ) } + if (res.status === 204 || res.status === 205) { + return undefined as T + } + return (await res.json()) as T }