forked from nazirov91/ra-strapi-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
265 lines (244 loc) · 9.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import {
fetchUtils,
GET_LIST,
GET_ONE,
GET_MANY,
GET_MANY_REFERENCE,
CREATE,
UPDATE,
UPDATE_MANY,
DELETE,
DELETE_MANY,
} from 'react-admin';
/**
* Maps react-admin queries to a simple REST API
* @example
* GET_LIST => GET http://my.api.url/posts?sort=['title','ASC']&range=[0, 24]
* GET_ONE => GET http://my.api.url/posts/123
* GET_MANY => GET http://my.api.url/posts?filter={ids:[123,456,789]}
* UPDATE => PUT http://my.api.url/posts/123
* CREATE => POST http://my.api.url/posts
* DELETE => DELETE http://my.api.url/posts/123
*/
export default (apiUrl, httpClient = fetchUtils.fetchJson, uploadFields = []) => {
/**
* @param {String} type One of the constants appearing at the top if this file, e.g. 'UPDATE'
* @param {String} resource Name of the resource to fetch, e.g. 'posts'
* @param {Object} params The data request params, depending on the type
* @returns {Object} { url, options } The HTTP request parameters
*/
const convertDataRequestToHTTP = (type, resource, params) => {
let url = '';
const options = {};
switch (type) {
case GET_LIST:
case GET_MANY_REFERENCE:
url = `${apiUrl}/${resource}?${adjustQueryForStrapi(params)}`;
break;
case GET_ONE:
url = `${apiUrl}/${resource}/${params.id}`;
break;
case UPDATE:
url = `${apiUrl}/${resource}/${params.id}`;
options.method = 'PUT';
// Omit created_at/updated_at(RDS) and createdAt/updatedAt(Mongo) in request body
const {created_at, updated_at, createdAt, updatedAt, ...data} = params.data;
options.body = JSON.stringify(data);
break;
case CREATE:
url = `${apiUrl}/${resource}`;
options.method = 'POST';
options.body = JSON.stringify(params.data);
break;
case DELETE:
url = `${apiUrl}/${resource}/${params.id}`;
options.method = 'DELETE';
break;
default:
throw new Error(`Unsupported fetch action type ${type}`);
}
return { url, options };
};
const adjustQueryForStrapi = (params) => {
/*
params = {
pagination: { page: {int} , perPage: {int} },
sort: { field: {string}, order: {string} },
filter: {Object},
target: {string}, (REFERENCE ONLY)
id: {mixed} (REFERENCE ONLY)
}
*/
// Handle SORTING
const s = params.sort;
const sort = s.field === "" ? "_sort=updated_at:DESC" : ("_sort=" + s.field + ":" + s.order);
// Handle FILTER
const f = params.filter;
let filter = "";
const keys = Object.keys(f);
for(let i = 0; i < keys.length; i++){
//react-admin uses q filter in several components and strapi use _q
if (keys[i] === "q" && f[keys[i]] !== '') {
filter += "_q=" + f[keys[i]] + (keys[i + 1] ? "&" : "")
} else {
filter += keys[i] + "=" + f[keys[i]] + (keys[i + 1] ? "&" : "");
}
}
if(params.id && params.target && params.target.indexOf('_id') !== -1){
const target = params.target.substring(0, params.target.length - 3);
filter += "&" + target + "=" + params.id;
}
// Handle PAGINATION
const { page, perPage } = params.pagination;
const start = (page - 1) * perPage;
const limit = perPage;//for strapi the _limit params indicate the amount of elements to return in the response
const range = "_start=" + start + "&_limit=" + limit;
return sort + "&" + range + "&" + filter;
}
// Determines if there are new files to upload
const determineUploadFieldNames = params => {
if (!params.data) return [];
// Check if the field names are mentioned in the uploadFields
// and verify there are new files being added
const requestUplaodFieldNames = [];
Object.keys(params.data).forEach(field => {
let fieldData = params.data[field];
if (uploadFields.includes(field)) {
fieldData = !Array.isArray(fieldData) ? [fieldData] : fieldData;
fieldData.filter(f => f && f.rawFile instanceof File).length > 0
&& requestUplaodFieldNames.push(field);
}
});
// Return an array of field names where new files are added
return requestUplaodFieldNames;
};
// Handles file uploading for CREATE and UPDATE types
const handleFileUpload = (type, resource, params, uploadFieldNames) => {
const { created_at, updated_at, createdAt, updatedAt, ...data } = params.data;
const id = type === UPDATE ? `/${params.id}` : "";
const url = `${apiUrl}/${resource}${id}`;
const requestMethod = type === UPDATE ? "PUT" : "POST";
const formData = new FormData();
for (let fieldName of uploadFieldNames) {
let fieldData = params.data[fieldName];
fieldData = !Array.isArray(fieldData) ? [fieldData] : fieldData;
const existingFileIds = [];
for (let item of fieldData) {
item.rawFile instanceof File
? formData.append(`files.${fieldName}`, item.rawFile)
: existingFileIds.push(item.id || item._id);
}
data[fieldName] = [...existingFileIds];
}
formData.append("data", JSON.stringify(data));
return httpClient(url, {
method: requestMethod,
body: formData
}).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()
* @param {String} type One of the constants appearing at the top if this file, e.g. 'UPDATE'
* @param {String} resource Name of the resource to fetch, e.g. 'posts'
* @param {Object} params The data request params, depending on the type
* @returns {Object} Data response
*/
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')) {
throw new Error(
'The Content-Range header is missing in the HTTP Response. The simple REST data provider expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?'
);
}
return {
data: json,
total: parseInt(headers.get('content-range').split('/').pop(), 10)
};
case CREATE:
return { data: { ...params.data, id: json.id } };
case DELETE:
return { data: { id: null } };
default:
return { data: json };
}
};
/**
* @param {string} type Request type, e.g GET_LIST
* @param {string} resource Resource name, e.g. "posts"
* @param {Object} payload Request parameters. Depends on the request type
* @returns {Promise} the Promise for a data response
*/
return (type, resource, params) => {
// Handle file uploading
const uploadFieldNames = determineUploadFieldNames(params);
if (uploadFieldNames.length > 0) {
return handleFileUpload(type, resource, params, uploadFieldNames);
}
// simple-rest doesn't handle filters on UPDATE route, so we fallback to calling UPDATE n times instead
if (type === UPDATE_MANY) {
return Promise.all(
params.ids.map(id => {
// Omit created_at/updated_at(RDS) and createdAt/updatedAt(Mongo) in request body
const {created_at, updated_at, createdAt, updatedAt, ...data} = params.data;
return httpClient(`${apiUrl}/${resource}/${id}`, {
method: 'PUT',
body: JSON.stringify(data),
})
})
).then(responses => ({
data: responses.map(response => response.json),
}));
}
// simple-rest doesn't handle filters on DELETE route, so we fallback to calling DELETE n times instead
if (type === DELETE_MANY) {
return Promise.all(
params.ids.map(id =>
httpClient(`${apiUrl}/${resource}/${id}`, {
method: 'DELETE',
})
)
).then(responses => ({
data: responses.map(response => response.json),
}));
}
//strapi doesn't handle filters in GET route
if (type === GET_MANY) {
return Promise.all(
params.ids.map(i =>
httpClient(`${apiUrl}/${resource}/${i.id || i._id || i}`, {
method: 'GET',
})
)
).then(responses => ({
data: responses.map(response => response.json),
}));
}
const { url, options } = convertDataRequestToHTTP(
type,
resource,
params
);
return httpClient(url, options).then(response =>
convertHTTPResponse(response, type, resource, params)
);
};
};