-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
38 lines (36 loc) · 1.63 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
const rewritePath = require('./rewrite')
const { fetch } = global
module.exports = fresource
/**
* fresource gives access to rest apis in an idiomatic way.
* @param {String} path URL or path to the resource.
* @param {Object} options Options to be passed to fetch.
* @returns {Object} resource
* @example
* var freseource = require('fresource')
* var Users = fresource('/api/users/:id')
* Users.get() // will initiate request: `GET /api/users`
* Users.get({ id: 1 }) // will initiate request: `GET /api/users/1`
* Users.save({ name: 'Jason' }) // will initiate request: `POST { name: 'Jason' } /api/users/`
* Users.update({ id: 1, name: 'Tyrell' }) // will initiate request: `PUT { name: 'Tyrell' } /api/users/1`
* Users.delete({ id: 1 }) // will initiate request: `DELETE /api/users/1`
*/
function fresource (path, options) {
return {
get (params, opts) {
return fetch(rewritePath(path, params), Object.assign({}, options, opts))
},
save (params, opts) {
return fetch(rewritePath(path, params), Object.assign({}, options, opts, { method: 'POST', body: JSON.stringify(params) }))
},
update (params, opts) {
return fetch(rewritePath(path, params), Object.assign({}, options, opts, { method: 'PUT', body: JSON.stringify(params) }))
},
patch (params, opts) {
return fetch(rewritePath(path, params), Object.assign({}, options, opts, { method: 'PATCH', body: JSON.stringify(params) }))
},
delete (params, opts) {
return fetch(rewritePath(path, params), Object.assign({}, options, opts, { method: 'DELETE' }))
}
}
}