- NPM:
npm install rachel --save
- Yarn:
yarn add rachel
Dependency: Rachel is built on top of fetch
you will need to install it.
- Under 4KB! (1.17KB gziped).
- 2 dependencies::
fetch
andPromise
. - Builds: CommonJS, ES and UMD.
- JSON by default!
// api.js
import Rachel from 'rachel'
const api = Rachel.createApi(baseUrl, options)
export default {
users: {
list: api.list('/users'),
get: api.get('/users/:id'),
post: api.post('/users'),
put: api.put('/users/:id'),
del: api.del('/users/:id'),
}
}
// main.js
import api from './api'
api.users.list() // GET: /users
api.users.get(123) // GET: /users/123
api.users.post(user) // POST: /users { "name": "foo" }
api.users.put(123, user) // PUT: /users/123 { "name": "bar" }
api.users.del(123) // DELETE: /users/123
Create an API wrapper with options.
rachel.createApi(baseUrl, options)
baseUrl
-String
. Required. The base URL.options
-Object
. Optional. Request options that will be transfered to all methods (list
,get
,post
,put
anddel
).prefix
-String
. The prefix to add to all API requests.
Prepare a GET
request function that will list all items of a resource.
rachel.list(path, options)
path
-String
. Required. URI path pattern.options
-Object
. Optional. Request options.
Returns: Function
. A function that accept one argument:
options
-Object
. Optional. Request options
Prepare a GET
request function that will obtain a single resource by its identifier.
rachel.get(path, options)
path
-String
. Required. URI path pattern.options
-Object
. Optional. Request options.
Returns: Function
. A function that accept two arguments:
id
-Object
. Required. The resource identifier.options
-Object
. Optional. Request options
Prepare a POST
request function that will create a new resource.
rachel.post(path, options)
path
-String
. Required. URI path pattern.options
-Object
. Optional. Request options.
Returns: Function
. A function that accept two arguments:
data
-Object
. Required. The data of the resource to create.options
-Object
. Optional. Request options
Prepare a PUT
request function that will update an existing resource.
rachel.put(path, options)
path
-String
. Required. URI path pattern.options
-Object
. Optional. Request options.
Returns: Function
. A function that accept three arguments:
id
-Object
. Required. The resource identifier.data
-Object
. Required. The resource to be updated.options
-Object
. Optional. Request options
Prepare a DELETE
request function that will delete an existing resource
rachel.del(path, options)
path
-String
. Required. URI path pattern.options
-Object
. Optional. Request options.
Returns: Function
. A function that accept two arguments:
id
-Object
. Required. The resource identifier.options
-Object
. Optional. Request options
An URI path pattern is a String
that contains placeholders. For example: '/users/:id' is an URI pattern that contain a placeholder (:id
). Rachel interpret strings that starts with :
as placeholders that will be replaced with an identifier or a data object.
URI path pattern | id |
data |
Result |
---|---|---|---|
/users/:id |
123 |
/users/123 |
|
/roles/:name |
{ name: 'foo' } |
/roles/foo |
|
/users/:id |
123 |
{ id: 456 } |
/users/123 |
π Rachel prioritize identifiers over data objects.
baseUrl
-String
. Default:null
. The base URL.prefix
-String
. Default:null
. The prefix to add to all API requests.cache
-Boolean
. Default:false
. Indicate if the request should be cached.multiple
-Boolean
. Default:true
. Indicate if the request can be issued multiple times, iffalse
all subsequent request will return the same promise.extract
-String
. Default:null
. The field name of the value to extract from the JSON response.
yarn build
- Build production assets.
yarn test
- Run all tests.yarn test -- --watch
- Run all tests in watch mode.
MIT License
Created with β€οΈ by Rubens Mariuzzo
Amelie Rachel β my first born daughter