-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
index.ts
153 lines (148 loc) · 5.43 KB
/
index.ts
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
import FakeRest from 'fakerest';
import { DataProvider } from 'ra-core';
/* eslint-disable no-console */
function log(type, resource, params, response) {
if (console.group) {
// Better logging in Chrome
console.groupCollapsed(type, resource, JSON.stringify(params));
console.log(response);
console.groupEnd();
} else {
console.log('FakeRest request ', type, resource, params);
console.log('FakeRest response', response);
}
}
/**
* Respond to react-admin data queries using a local JavaScript object
*
* Useful for debugging and testing - do not use in production.
*
* @example
*
* import fakeDataProvider from 'ra-data-fakerest';
* const dataProvider = fakeDataProvider({
* posts: [
* { id: 0, title: 'Hello, world!' },
* { id: 1, title: 'FooBar' },
* ],
* comments: [
* { id: 0, post_id: 0, author: 'John Doe', body: 'Sensational!' },
* { id: 1, post_id: 0, author: 'Jane Doe', body: 'I agree' },
* ],
* })
*/
export default (data, loggingEnabled = false): DataProvider => {
const restServer = new FakeRest.Server();
restServer.init(data);
if (window) {
// give way to update data in the console
(window as any).restServer = restServer;
}
function getResponse(type, resource, params) {
switch (type) {
case 'getList': {
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
const query = {
sort: [field, order],
range: [(page - 1) * perPage, page * perPage - 1],
filter: params.filter,
};
return {
data: restServer.getAll(resource, query),
total: restServer.getCount(resource, {
filter: params.filter,
}),
};
}
case 'getOne':
return {
data: restServer.getOne(resource, params.id, { ...params }),
};
case 'getMany':
return {
data: restServer.getAll(resource, {
filter: { id: params.ids },
}),
};
case 'getManyReference': {
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
const query = {
sort: [field, order],
range: [(page - 1) * perPage, page * perPage - 1],
filter: { ...params.filter, [params.target]: params.id },
};
return {
data: restServer.getAll(resource, query),
total: restServer.getCount(resource, {
filter: query.filter,
}),
};
}
case 'update':
return {
data: restServer.updateOne(resource, params.id, {
...params.data,
}),
};
case 'updateMany':
params.ids.forEach(id =>
restServer.updateOne(resource, id, {
...params.data,
})
);
return { data: params.ids };
case 'create':
return {
data: restServer.addOne(resource, { ...params.data }),
};
case 'delete':
return { data: restServer.removeOne(resource, params.id) };
case 'deleteMany':
params.ids.forEach(id => restServer.removeOne(resource, id));
return { data: params.ids };
default:
return false;
}
}
/**
* @param {String} type One of the data Provider methods, e.g. 'getList'
* @param {String} resource Name of the resource to fetch, e.g. 'posts'
* @param {Object} params The data request params, depending on the type
* @returns {Promise} The response
*/
const handle = (type, resource, params): Promise<any> => {
const collection = restServer.getCollection(resource);
if (!collection) {
return Promise.reject(
new Error(`Undefined collection "${resource}"`)
);
}
let response;
try {
response = getResponse(type, resource, params);
} catch (error) {
console.error(error);
return Promise.reject(error);
}
if (loggingEnabled) {
log(type, resource, params, response);
}
return Promise.resolve(response);
};
return {
getList: (resource, params) => handle('getList', resource, params),
getOne: (resource, params) => handle('getOne', resource, params),
getMany: (resource, params) => handle('getMany', resource, params),
getManyReference: (resource, params) =>
handle('getManyReference', resource, params),
update: (resource, params) => handle('update', resource, params),
updateMany: (resource, params) =>
handle('updateMany', resource, params),
create: (resource, params) => handle('create', resource, params),
delete: (resource, params) => handle('delete', resource, params),
deleteMany: (resource, params) =>
handle('deleteMany', resource, params),
};
};