From 5ac71bfac18c41bfc9d69440da29a10717f049b6 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Tue, 4 Mar 2025 09:10:41 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B(service-worker)=20update=20sw=20to?= =?UTF-8?q?=20create=20a=20doc=20without=20body?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Offline creation of a doc was broken because we don't add a default title anymore when we create a doc, leading to POST requests without body. we need to adapt the service worker to handle this case. --- .../src/features/service-worker/ApiPlugin.ts | 17 ----------------- .../service-worker/__tests__/ApiPlugin.test.tsx | 14 +++----------- 2 files changed, 3 insertions(+), 28 deletions(-) diff --git a/src/frontend/apps/impress/src/features/service-worker/ApiPlugin.ts b/src/frontend/apps/impress/src/features/service-worker/ApiPlugin.ts index 875bb87c..ce40eba2 100644 --- a/src/frontend/apps/impress/src/features/service-worker/ApiPlugin.ts +++ b/src/frontend/apps/impress/src/features/service-worker/ApiPlugin.ts @@ -146,20 +146,11 @@ export class ApiPlugin implements WorkboxPlugin { await RequestSerializer.fromRequest(this.initialRequest) ).toObject(); - if (!requestData.body) { - return new Response('Body found', { status: 404 }); - } - - const jsonObject = RequestSerializer.arrayBufferToJson>( - requestData.body, - ); - // Add a new doc id to the create request const uuid = self.crypto.randomUUID(); const newRequestData = { ...requestData, body: RequestSerializer.objectToArrayBuffer({ - ...jsonObject, id: uuid, }), }; @@ -175,16 +166,8 @@ export class ApiPlugin implements WorkboxPlugin { 'doc-mutation', ); - /** - * Create new item in the cache - */ - const bodyMutate = (await this.initialRequest - .clone() - .json()) as Partial; - const newResponse: Doc = { title: '', - ...bodyMutate, id: uuid, content: '', created_at: new Date().toISOString(), diff --git a/src/frontend/apps/impress/src/features/service-worker/__tests__/ApiPlugin.test.tsx b/src/frontend/apps/impress/src/features/service-worker/__tests__/ApiPlugin.test.tsx index 4285e00e..f9fdfae0 100644 --- a/src/frontend/apps/impress/src/features/service-worker/__tests__/ApiPlugin.test.tsx +++ b/src/frontend/apps/impress/src/features/service-worker/__tests__/ApiPlugin.test.tsx @@ -346,13 +346,8 @@ describe('ApiPlugin', () => { headers: new Headers({ 'Content-Type': 'application/json', }), - arrayBuffer: () => - RequestSerializer.objectToArrayBuffer({ - title: 'my new doc', - }), - json: () => ({ - title: 'my new doc', - }), + arrayBuffer: () => RequestSerializer.objectToArrayBuffer({}), + json: () => ({}), } as unknown as Request, } as any; @@ -389,9 +384,7 @@ describe('ApiPlugin', () => { ); expect(mockedPut).toHaveBeenCalledWith( 'doc-item', - expect.objectContaining({ - title: 'my new doc', - }), + expect.objectContaining({}), 'http://test.jest/documents/444555/', ); expect(mockedPut).toHaveBeenCalledWith( @@ -400,7 +393,6 @@ describe('ApiPlugin', () => { results: expect.arrayContaining([ expect.objectContaining({ id: '444555', - title: 'my new doc', }), ]), }),