🐛(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.
This commit is contained in:
@@ -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<Partial<Doc>>(
|
||||
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<Doc>;
|
||||
|
||||
const newResponse: Doc = {
|
||||
title: '',
|
||||
...bodyMutate,
|
||||
id: uuid,
|
||||
content: '',
|
||||
created_at: new Date().toISOString(),
|
||||
|
||||
@@ -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',
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user