Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

resource-client

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

resource-client

Easily create api clients for your server side resources.

  • 1.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
33
increased by153.85%
Maintainers
1
Weekly downloads
 
Created
Source

Resource Client

Easily create api clients for your server side resources. Inspired by Angular Resource.

NPM version Build Status MIT License

Usage

npm install resource-client --save
var resourceClient = require('resource-client');

var Product = resourceClient({
  url: 'http://www.mysite.com/api/products/:_id',
  headers: {
    'X-Secret-Token': 'ABCD1234'
  }
});

Product.query({isActive: true}, function(err, products) {
  product = products[0]
  product.name = 'apple'
  product.save()
});

Creating a Resource

resourceClient(options)

  • options - default request options for this resource. You can use any option from the request module. There are a few key differences:
    • url - same as request url but can contain variables prefixed with a colon such as products/:name
    • json - set to true by default
var resourceClient = require('resource-client');

var Product = resourceClient({
  url: 'http://www.mysite.com/api/products/:_id',
  headers: {
    'X-Secret-Token': 'ABCD1234'
  }
})

Defining Resource Actions

Resource.action(name, options)

  • name - name of action
  • options - default request options for this action. Overrides defaults set for the resource. You can use any option from the request module. There are a couple extra options available:
    • url - same as request url but can contain variables prefixed with a colon such as products/:name
    • isArray - resource is an array. It will not populate variables in the url.
var resourceClient = require('resource-client');

var Product = resourceClient({
  url: 'http://www.mysite.com/api/products/:_id',
  headers: {
    'X-Secret-Token': 'ABCD1234'
  }
});

Product.action('query', {
  method: 'GET'
  isArray: true
});

If the method is GET, you can use it as a class method:

Product.action('get', {
  method: 'GET'
});
// class method
Product.get({_id: 1234}, function (err, product) { ... })

If the method is PUT, POST, DELETE, you can use it as an instance method:

Product.action('save', {
  method: 'POST'
});
// class method
Product.save({name: 'apple'}, function (err, product) { ... });

// instance method
product = new Product({name: 'apple'});
product.save()

Default Actions

Every new resource will come with these methods by default

  • get - {method: 'GET'}
  • query - {method: 'GET', isArray: true}
  • update - {method: 'PUT'}
  • save - {method: 'POST'}
  • remove - {method: 'DELETE'}

Contributing

Please follow our Code of Conduct when contributing to this project.

$ git clone https://github.com/goodeggs/resource-client && cd resource-client
$ npm install
$ npm test

Module scaffold generated by generator-goodeggs-npm.

Keywords

FAQs

Package last updated on 30 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc