🐛(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)
|
await RequestSerializer.fromRequest(this.initialRequest)
|
||||||
).toObject();
|
).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
|
// Add a new doc id to the create request
|
||||||
const uuid = self.crypto.randomUUID();
|
const uuid = self.crypto.randomUUID();
|
||||||
const newRequestData = {
|
const newRequestData = {
|
||||||
...requestData,
|
...requestData,
|
||||||
body: RequestSerializer.objectToArrayBuffer({
|
body: RequestSerializer.objectToArrayBuffer({
|
||||||
...jsonObject,
|
|
||||||
id: uuid,
|
id: uuid,
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
@@ -175,16 +166,8 @@ export class ApiPlugin implements WorkboxPlugin {
|
|||||||
'doc-mutation',
|
'doc-mutation',
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
|
||||||
* Create new item in the cache
|
|
||||||
*/
|
|
||||||
const bodyMutate = (await this.initialRequest
|
|
||||||
.clone()
|
|
||||||
.json()) as Partial<Doc>;
|
|
||||||
|
|
||||||
const newResponse: Doc = {
|
const newResponse: Doc = {
|
||||||
title: '',
|
title: '',
|
||||||
...bodyMutate,
|
|
||||||
id: uuid,
|
id: uuid,
|
||||||
content: '',
|
content: '',
|
||||||
created_at: new Date().toISOString(),
|
created_at: new Date().toISOString(),
|
||||||
|
|||||||
@@ -346,13 +346,8 @@ describe('ApiPlugin', () => {
|
|||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
}),
|
}),
|
||||||
arrayBuffer: () =>
|
arrayBuffer: () => RequestSerializer.objectToArrayBuffer({}),
|
||||||
RequestSerializer.objectToArrayBuffer({
|
json: () => ({}),
|
||||||
title: 'my new doc',
|
|
||||||
}),
|
|
||||||
json: () => ({
|
|
||||||
title: 'my new doc',
|
|
||||||
}),
|
|
||||||
} as unknown as Request,
|
} as unknown as Request,
|
||||||
} as any;
|
} as any;
|
||||||
|
|
||||||
@@ -389,9 +384,7 @@ describe('ApiPlugin', () => {
|
|||||||
);
|
);
|
||||||
expect(mockedPut).toHaveBeenCalledWith(
|
expect(mockedPut).toHaveBeenCalledWith(
|
||||||
'doc-item',
|
'doc-item',
|
||||||
expect.objectContaining({
|
expect.objectContaining({}),
|
||||||
title: 'my new doc',
|
|
||||||
}),
|
|
||||||
'http://test.jest/documents/444555/',
|
'http://test.jest/documents/444555/',
|
||||||
);
|
);
|
||||||
expect(mockedPut).toHaveBeenCalledWith(
|
expect(mockedPut).toHaveBeenCalledWith(
|
||||||
@@ -400,7 +393,6 @@ describe('ApiPlugin', () => {
|
|||||||
results: expect.arrayContaining([
|
results: expect.arrayContaining([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: '444555',
|
id: '444555',
|
||||||
title: 'my new doc',
|
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in New Issue
Block a user