-
Notifications
You must be signed in to change notification settings - Fork 22
/
dataProvider.ts
74 lines (61 loc) · 2.07 KB
/
dataProvider.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
import { DataProvider } from '../admiral'
import _ from './request'
/**
*
* @example
*
* getList => GET http://my.api.url/posts?sort=['title','ASC']&range=[0, 24]
* getOne => GET http://my.api.url/posts/123
* getCreateFormData => GET http://my.api.url/posts/create
* create => POST http://my.api.url/posts
* getUpdateFormData => GET http://my.api.url/posts/123/update
* update => POST http://my.api.url/posts/123
* delete => DELETE http://my.api.url/posts/123
*
*/
const dataProvider = (apiUrl: string): DataProvider => ({
getList: (resource, params) => {
const { page, perPage } = params.pagination || { page: 1, perPage: 10 }
const query = {
page,
perPage,
sort: params.sort,
filter: params.filter,
}
const url = `${apiUrl}/${resource}`
return _.get(url)({ params: query })
},
reorderList: (resource, params) => {
const url = `${apiUrl}/${resource}/reorder`
return _.postFD(url)({ data: params.data })
},
getOne: (resource, params) => {
const url = `${apiUrl}/${resource}/${params.id}`
return _.get(url)({ params })
},
getCreateFormData: (resource) => {
const url = `${apiUrl}/${resource}/create`
return _.get(url)({})
},
getFiltersFormData: (resource, urlState) => {
const url = `${apiUrl}/${resource}/filters`
return _.get(url)({ params: urlState })
},
create: (resource, params) => {
const url = `${apiUrl}/${resource}`
return _.postFD(url)({ data: params.data })
},
getUpdateFormData: (resource, params) => {
const url = `${apiUrl}/${resource}/${params.id}/update`
return _.get(url)({ params })
},
update: (resource, params) => {
const url = `${apiUrl}/${resource}/${params.id}`
return _.postFD(url)({ data: params.data })
},
deleteOne: (resource, params) => {
const url = `${apiUrl}/${resource}/${params.id}`
return _.delete(url)()
},
})
export default dataProvider