-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
29 lines (29 loc) · 1 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
/**
* create vue-resource's resource like object
* taken from : https://github.com/mzabriskie/axios/issues/894
*
* You can add to the default actions by passing an object on creation, eg...
*
* ```
* const post = resource('/posts', axiosInstance, {
* paging: (params) => axiosInstance.get('/posts', {
* params
* })
* })
* ```
*
* @param string path the resource path
* @param object http axios instance
* @param object actions custom actions
* @return object the resource object
*/
export default function resource(path, http, actions = {}) {
let obj = {
query: (params = {}, conf = {}) => http.get(path, Object.assign(conf, { params })),
get: (id, params = {}, conf = {}) => http.get(path + '/' + id, Object.assign(conf, { params })),
create: (obj, conf = {}) => http.post(path, obj, conf),
update: (id, obj, conf = {}) => http.put(path + '/' + id, obj, conf),
delete: (id, conf = {}) => http.delete(path + '/' + id, conf)
}
return Object.assign(obj, actions)
}