Skip to content

Commit 5ac71bf

Browse files
committed
🐛(service-worker) update sw to create a doc without body
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.
1 parent cb4e148 commit 5ac71bf

File tree

2 files changed

+3
-28
lines changed

2 files changed

+3
-28
lines changed

src/frontend/apps/impress/src/features/service-worker/ApiPlugin.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,11 @@ export class ApiPlugin implements WorkboxPlugin {
146146
await RequestSerializer.fromRequest(this.initialRequest)
147147
).toObject();
148148

149-
if (!requestData.body) {
150-
return new Response('Body found', { status: 404 });
151-
}
152-
153-
const jsonObject = RequestSerializer.arrayBufferToJson<Partial<Doc>>(
154-
requestData.body,
155-
);
156-
157149
// Add a new doc id to the create request
158150
const uuid = self.crypto.randomUUID();
159151
const newRequestData = {
160152
...requestData,
161153
body: RequestSerializer.objectToArrayBuffer({
162-
...jsonObject,
163154
id: uuid,
164155
}),
165156
};
@@ -175,16 +166,8 @@ export class ApiPlugin implements WorkboxPlugin {
175166
'doc-mutation',
176167
);
177168

178-
/**
179-
* Create new item in the cache
180-
*/
181-
const bodyMutate = (await this.initialRequest
182-
.clone()
183-
.json()) as Partial<Doc>;
184-
185169
const newResponse: Doc = {
186170
title: '',
187-
...bodyMutate,
188171
id: uuid,
189172
content: '',
190173
created_at: new Date().toISOString(),

src/frontend/apps/impress/src/features/service-worker/__tests__/ApiPlugin.test.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,8 @@ describe('ApiPlugin', () => {
346346
headers: new Headers({
347347
'Content-Type': 'application/json',
348348
}),
349-
arrayBuffer: () =>
350-
RequestSerializer.objectToArrayBuffer({
351-
title: 'my new doc',
352-
}),
353-
json: () => ({
354-
title: 'my new doc',
355-
}),
349+
arrayBuffer: () => RequestSerializer.objectToArrayBuffer({}),
350+
json: () => ({}),
356351
} as unknown as Request,
357352
} as any;
358353

@@ -389,9 +384,7 @@ describe('ApiPlugin', () => {
389384
);
390385
expect(mockedPut).toHaveBeenCalledWith(
391386
'doc-item',
392-
expect.objectContaining({
393-
title: 'my new doc',
394-
}),
387+
expect.objectContaining({}),
395388
'http://test.jest/documents/444555/',
396389
);
397390
expect(mockedPut).toHaveBeenCalledWith(
@@ -400,7 +393,6 @@ describe('ApiPlugin', () => {
400393
results: expect.arrayContaining([
401394
expect.objectContaining({
402395
id: '444555',
403-
title: 'my new doc',
404396
}),
405397
]),
406398
}),

0 commit comments

Comments
 (0)