From 497fdd91866001ec86f10aa9bb83862d5c1c3ef9 Mon Sep 17 00:00:00 2001 From: Gildas Garcia <1122076+djhi@users.noreply.github.com> Date: Fri, 20 Nov 2020 16:15:08 +0100 Subject: [PATCH] Fix ra-data-simple-rest delete headers --- .../ra-data-simple-rest/src/index.spec.ts | 53 +++++++++++++++++++ packages/ra-data-simple-rest/src/index.ts | 10 +++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/packages/ra-data-simple-rest/src/index.spec.ts b/packages/ra-data-simple-rest/src/index.spec.ts index 27dc38c6ed6..49e0175f832 100644 --- a/packages/ra-data-simple-rest/src/index.spec.ts +++ b/packages/ra-data-simple-rest/src/index.spec.ts @@ -68,4 +68,57 @@ describe('Data Simple REST Client', () => { expect(result.total).toEqual(42); }); }); + describe('delete', () => { + it('should set the `Content-Type` header to `text/plain`', async () => { + const httpClient = jest.fn().mockResolvedValue({ json: { id: 1 } }); + + const client = simpleClient('http://localhost:3000', httpClient); + + await client.delete('posts', { + id: 1, + previousData: { id: 1 }, + }); + + expect(httpClient).toHaveBeenCalledWith( + 'http://localhost:3000/posts/1', + { + method: 'DELETE', + headers: { + 'Content-Type': 'text/plain', + }, + } + ); + }); + }); + describe('deleteMany', () => { + it('should set the `Content-Type` header to `text/plain`', async () => { + const httpClient = jest.fn().mockResolvedValue({ json: { id: 1 } }); + + const client = simpleClient('http://localhost:3000', httpClient); + + await client.deleteMany('posts', { + ids: [1, 2], + }); + + expect(httpClient).toHaveBeenCalledWith( + 'http://localhost:3000/posts/1', + { + method: 'DELETE', + headers: { + 'Content-Type': 'text/plain', + }, + } + ); + + expect(httpClient).toHaveBeenCalledWith( + 'http://localhost:3000/posts/2', + { + method: 'DELETE', + headers: { + 'Content-Type': 'text/plain', + }, + } + ); + }); + }); }); diff --git a/packages/ra-data-simple-rest/src/index.ts b/packages/ra-data-simple-rest/src/index.ts index 91964a48078..8157be55ec4 100644 --- a/packages/ra-data-simple-rest/src/index.ts +++ b/packages/ra-data-simple-rest/src/index.ts @@ -166,6 +166,9 @@ export default ( delete: (resource, params) => httpClient(`${apiUrl}/${resource}/${params.id}`, { method: 'DELETE', + headers: { + 'Content-Type': 'text/plain', + }, }).then(({ json }) => ({ data: json })), // simple-rest doesn't handle filters on DELETE route, so we fallback to calling DELETE n times instead @@ -174,7 +177,12 @@ export default ( params.ids.map(id => httpClient(`${apiUrl}/${resource}/${id}`, { method: 'DELETE', + headers: { + 'Content-Type': 'text/plain', + }, }) ) - ).then(responses => ({ data: responses.map(({ json }) => json.id) })), + ).then(responses => ({ + data: responses.map(({ json }) => json.id), + })), });