Skip to content

πŸš€ Quickly create a RESTful client!

License

Notifications You must be signed in to change notification settings

rmariuzzo/Rachel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Rachel

RESTful API Client helper

πŸš€ Quickly create a RESTful client!

Install

  • NPM: npm install rachel --save
  • Yarn: yarn add rachel

Dependency: Rachel is built on top of fetch you will need to install it.

Features

  • Under 4KB! (1.17KB gziped).
  • 2 dependencies:: fetch and Promise.
  • 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/123

Documentation

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, put and del).
    • 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)

Returns: Function. A function that accept one argument:

get

Prepare a GET request function that will obtain a single resource by its identifier.

rachel.get(path, 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)

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)

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)

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, if false all 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.

MIT License

Created with ❀️ by Rubens Mariuzzo

Amelie Rachel – my first born daughter

About

πŸš€ Quickly create a RESTful client!

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published