Skip to content

Commit d90632e

Browse files
committed
Fix hydra client with prefixed API URL (#124)
* fix hydra client for prefixed api * fix tests
1 parent a71c8ee commit d90632e

File tree

3 files changed

+905
-34
lines changed

3 files changed

+905
-34
lines changed

src/hydra/hydraClient.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
UPDATE,
1010
} from 'react-admin';
1111
import isPlainObject from 'lodash.isplainobject';
12-
import qs from 'qs';
1312
import fetchHydra from './fetchHydra';
1413

1514
class ReactAdminDocument {
@@ -169,6 +168,9 @@ export default ({entrypoint, resources = []}, httpClient = fetchHydra) => {
169168
* @returns {Object}
170169
*/
171170
const convertReactAdminRequestToHydraRequest = (type, resource, params) => {
171+
const collectionUrl = new URL(`${entrypoint}/${resource}`);
172+
const itemUrl = new URL(params.id, entrypoint);
173+
172174
switch (type) {
173175
case CREATE:
174176
return transformReactAdminDataToRequestBody(resource, params.data).then(
@@ -177,7 +179,7 @@ export default ({entrypoint, resources = []}, httpClient = fetchHydra) => {
177179
body,
178180
method: 'POST',
179181
},
180-
url: `${entrypoint}/${resource}`,
182+
url: collectionUrl,
181183
}),
182184
);
183185

@@ -186,7 +188,7 @@ export default ({entrypoint, resources = []}, httpClient = fetchHydra) => {
186188
options: {
187189
method: 'DELETE',
188190
},
189-
url: entrypoint + params.id,
191+
url: itemUrl,
190192
});
191193

192194
case GET_LIST: {
@@ -195,31 +197,34 @@ export default ({entrypoint, resources = []}, httpClient = fetchHydra) => {
195197
sort: {field, order},
196198
} = params;
197199

200+
if (order) collectionUrl.searchParams.set(`order[${field}]`, order);
201+
if (page) collectionUrl.searchParams.set('page', page);
202+
if (perPage) collectionUrl.searchParams.set('perPage', perPage);
203+
if (params.filter) {
204+
Object.keys(params.filter).map(key =>
205+
collectionUrl.searchParams.set(key, params.filter[key]),
206+
);
207+
}
208+
198209
return Promise.resolve({
199210
options: {},
200-
url: `${entrypoint}/${resource}?${qs.stringify({
201-
...params.filter,
202-
order: {
203-
[field]: order,
204-
},
205-
page,
206-
perPage,
207-
})}`,
211+
url: collectionUrl,
208212
});
209213
}
210214

211215
case GET_MANY_REFERENCE:
216+
if (params.target) {
217+
collectionUrl.searchParams.set(params.target, params.id);
218+
}
212219
return Promise.resolve({
213220
options: {},
214-
url: `${entrypoint}/${resource}?${qs.stringify({
215-
[params.target]: params.id,
216-
})}`,
221+
url: collectionUrl,
217222
});
218223

219224
case GET_ONE:
220225
return Promise.resolve({
221226
options: {},
222-
url: entrypoint + params.id,
227+
url: itemUrl,
223228
});
224229

225230
case UPDATE:
@@ -229,7 +234,7 @@ export default ({entrypoint, resources = []}, httpClient = fetchHydra) => {
229234
body,
230235
method: 'PUT',
231236
},
232-
url: entrypoint + params.id,
237+
url: itemUrl,
233238
}),
234239
);
235240

src/hydra/hydraClient.test.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,16 @@ describe('fetch data from an hydra api', () => {
8686
}),
8787
);
8888

89-
await hydraClient({entrypoint: ''}, mockHttpClient)(GET_LIST, 'books', {
90-
pagination: {page: 2},
91-
sort: {field: 'id', order: 'ASC'},
92-
}).then(() => {
93-
expect(mockHttpClient.mock.calls[0][0]).toBe(
94-
'/books?order%5Bid%5D=ASC&page=2',
89+
await hydraClient({entrypoint: 'http://www.example.com'}, mockHttpClient)(
90+
GET_LIST,
91+
'books',
92+
{
93+
pagination: {page: 2},
94+
sort: {field: 'id', order: 'ASC'},
95+
},
96+
).then(() => {
97+
expect(mockHttpClient.mock.calls[0][0].href).toBe(
98+
'http://www.example.com/books?order%5Bid%5D=ASC&page=2',
9599
);
96100
});
97101
});
@@ -102,9 +106,13 @@ describe('fetch data from an hydra api', () => {
102106
.mockReturnValueOnce(Promise.resolve({json: {'@id': '/books/3'}}))
103107
.mockReturnValue(Promise.resolve({json: {'@id': '/books/5'}}));
104108

105-
await hydraClient({entrypoint: ''}, mockHttpClient)(GET_MANY, 'books', {
106-
ids: [3, 5],
107-
}).then(response => {
109+
await hydraClient({entrypoint: 'http://www.example.com'}, mockHttpClient)(
110+
GET_MANY,
111+
'books',
112+
{
113+
ids: [3, 5],
114+
},
115+
).then(response => {
108116
expect(response.data[0].id).toEqual('/books/3');
109117
expect(response.data[1].id).toEqual('/books/5');
110118
});
@@ -114,10 +122,16 @@ describe('fetch data from an hydra api', () => {
114122
const mockHttpClient = jest.fn();
115123
mockHttpClient.mockReturnValueOnce(Promise.resolve(''));
116124

117-
await hydraClient({entrypoint: ''}, mockHttpClient)(DELETE, 'books', {
118-
id: '/books/1',
119-
}).then(() => {
120-
expect(mockHttpClient.mock.calls[0][0]).toBe('/books/1');
125+
await hydraClient({entrypoint: 'http://www.example.com'}, mockHttpClient)(
126+
DELETE,
127+
'books',
128+
{
129+
id: '/books/1',
130+
},
131+
).then(() => {
132+
expect(mockHttpClient.mock.calls[0][0].href).toBe(
133+
'http://www.example.com/books/1',
134+
);
121135
expect(mockHttpClient.mock.calls[0][1].method).toBe('DELETE');
122136
});
123137
});
@@ -126,12 +140,20 @@ describe('fetch data from an hydra api', () => {
126140
const mockHttpClient = jest.fn();
127141
mockHttpClient.mockReturnValueOnce(Promise.resolve(''));
128142

129-
await hydraClient({entrypoint: ''}, mockHttpClient)(DELETE_MANY, 'books', {
130-
ids: ['/books/1', '/books/2'],
131-
}).then(() => {
132-
expect(mockHttpClient.mock.calls[0][0]).toBe('/books/1');
143+
await hydraClient({entrypoint: 'http://www.example.com'}, mockHttpClient)(
144+
DELETE_MANY,
145+
'books',
146+
{
147+
ids: ['/books/1', '/books/2'],
148+
},
149+
).then(() => {
150+
expect(mockHttpClient.mock.calls[0][0].href).toBe(
151+
'http://www.example.com/books/1',
152+
);
133153
expect(mockHttpClient.mock.calls[0][1].method).toBe('DELETE');
134-
expect(mockHttpClient.mock.calls[1][0]).toBe('/books/2');
154+
expect(mockHttpClient.mock.calls[1][0].href).toBe(
155+
'http://www.example.com/books/2',
156+
);
135157
expect(mockHttpClient.mock.calls[1][1].method).toBe('DELETE');
136158
});
137159
});

0 commit comments

Comments
 (0)