Skip to content

Instantly share code, notes, and snippets.

@pablohpsilva
Created April 15, 2018 13:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablohpsilva/b16a2dde44499e2973fb71a46fdab5f7 to your computer and use it in GitHub Desktop.
Save pablohpsilva/b16a2dde44499e2973fb71a46fdab5f7 to your computer and use it in GitHub Desktop.
An example using x-resources
//
// Create a resource file, for example: 'resources/user.js'.
// Create your resource-actions and export your custom resource
//
// File: resources/user.js
import { resources } from 'x-resources'
const baseURL = 'https://api.github.com'
const actions = {
find: { method: 'GET', url: '/users' },
findOne: { method: 'GET', url: '/users/:id' },
filter: { method: 'GET', url: '/users/filterBy=:filters' },
create: { method: 'POST', url: '/users' },
append: { method: 'POST', url: '/users/:id' },
update: { method: 'PUT', url: '/users/:id' },
remove: { method: 'DELETE', url: '/users/:id' }
}
export default resources(baseURL, actions)
// -------
// -------
//
// Now import this resource on your project and use it how you like it.
// You can import the whole resource and use it like so:
//
import userResource from 'resources/user'
userResource.fetch() // GET on https://api.github.com/users
.then( ... )
.catch( ... )
userResource.findOne({ id: 1 }) // GET on https://api.github.com/users/1
.then( ... )
.catch( ... )
userResource.update({ id: 1, name: 'john' }) // PUT on https://api.github.com/users/1
.then( ... )
.catch( ... )
//
// Or you could import just what you want from it and use it like so:
//
import { create as userCreate, remove as userRemove } from 'resources/user'
userCreate({ name: 'john' }) // POST on https://api.github.com/users
.then( ... )
.catch( ... )
userRemove({ id: 1 }) // DELETE on https://api.github.com/users/1
.then( ... )
.catch( ... )
userRemove({ id: 1 }, { headers: { 'Content-Type': 'application/pdf' } }) // DELETE on https://api.github.com/users/1
.then( ... )
.catch( ... )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment