Skip to content

Commit

Permalink
Added referenceArrayInput support
Browse files Browse the repository at this point in the history
  • Loading branch information
nazirov91 committed Mar 30, 2020
2 parents 7a9d61e + 8107d1b commit ebf56d6
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import {

/**
* Maps react-admin queries to a simple REST API
*
* The REST dialect is similar to the one of FakeRest
* @see https://github.com/marmelab/FakeRest
* @example
* GET_LIST => GET http://my.api.url/posts?sort=['title','ASC']&range=[0, 24]
* GET_ONE => GET http://my.api.url/posts/123
Expand Down Expand Up @@ -152,8 +149,23 @@ export default (apiUrl, httpClient = fetchUtils.fetchJson, uploadFields = []) =>
return httpClient(url, {
method: requestMethod,
body: formData
}).then(response => ({ data: response.json }));
}).then(response => ({ data: replaceRefObjectsWithIds(response.json) }));
};

// Replace reference objects with reference object IDs
const replaceRefObjectsWithIds = json => {
Object.keys(json).forEach(key => {
const fd = json[key]; // field data
const referenceKeys = [];
if (fd && (fd.id || fd._id) && !fd.mime) {
json[key] = fd.id || fd._id;
} else if (Array.isArray(fd) && fd.length > 0 && !fd[0].mime) {
fd.map(item => referenceKeys.push(item.id || item._id));
json[key] = referenceKeys;
}
});
return json;
}

/**
* @param {Object} response HTTP response from fetch()
Expand All @@ -165,6 +177,8 @@ export default (apiUrl, httpClient = fetchUtils.fetchJson, uploadFields = []) =>
const convertHTTPResponse = (response, type, resource, params) => {
const { headers, json } = response;
switch (type) {
case GET_ONE:
return { data: replaceRefObjectsWithIds(json) };
case GET_LIST:
case GET_MANY_REFERENCE:
if (!headers.has('content-range')) {
Expand Down Expand Up @@ -229,8 +243,8 @@ export default (apiUrl, httpClient = fetchUtils.fetchJson, uploadFields = []) =>
//strapi doesn't handle filters in GET route
if (type === GET_MANY) {
return Promise.all(
params.ids.map(id =>
httpClient(`${apiUrl}/${resource}/${id}`, {
params.ids.map(i =>
httpClient(`${apiUrl}/${resource}/${i.id || i._id || i}`, {
method: 'GET',
})
)
Expand Down

0 comments on commit ebf56d6

Please sign in to comment.