rachel v0.0.1
Install
- NPM:
npm install rachel --save Yarn:
yarn add rachelDependency: Rachel is built on top of
fetchyou will need to install it.
Features
- Under 4KB! (1.17KB gziped).
- 2 dependencies::
fetchandPromise. - Builds: CommonJS, ES and UMD.
- JSON by default!
Example
// 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/123Documentation
createApi
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,putanddel).prefix-String. The prefix to add to all API requests.
list
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
get
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
post
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
put
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
del
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
URI path pattern
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.
Request Options
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, iffalseall subsequent request will return the same promise.extract-String. Default:null. The field name of the value to extract from the JSON response.
Development
yarn build- Build production assets.
Tests
yarn test- Run all tests.yarn test -- --watch- Run all tests in watch mode.
8 years ago