UNPKG

3.47 kBMarkdownView Raw
1# Resource Client
2
3Easily create api clients for your server side resources. Inspired by [Angular Resource](https://docs.angularjs.org/api/ngResource/service/$resource).
4
5[![NPM version](http://img.shields.io/npm/v/resource-client.svg?style=flat-square)](https://www.npmjs.org/package/resource-client)
6[![Build Status](http://img.shields.io/travis/goodeggs/resource-client.svg?style=flat-square)](https://travis-ci.org/goodeggs/resource-client)
7[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/goodeggs/resource-client/blob/master/LICENSE.md)
8
9## Usage
10
11```
12npm install resource-client --save
13```
14
15```javascript
16var resourceClient = require('resource-client');
17
18var Product = resourceClient({
19 url: 'http://www.mysite.com/api/products/:_id',
20 headers: {
21 'X-Secret-Token': 'ABCD1234'
22 }
23});
24
25Product.query({isActive: true}, function(err, products) {
26 product = products[0]
27 product.name = 'apple'
28 product.save()
29});
30```
31
32## Creating a Resource
33
34### resourceClient(options)
35
36- **options** - default request options for this resource. You can use any option from the [request](https://github.com/request/request) module. There are a few key differences:
37 - **url** - same as request url but can contain variables prefixed with a colon such as `products/:name`
38 - **json** - set to true by default
39
40
41```javascript
42var resourceClient = require('resource-client');
43
44var Product = resourceClient({
45 url: 'http://www.mysite.com/api/products/:_id',
46 headers: {
47 'X-Secret-Token': 'ABCD1234'
48 }
49})
50```
51
52## Defining Resource Actions
53
54### Resource.action(name, options)
55
56- **name** - name of action
57- **options** - default request options for this action. Overrides defaults set for the resource. You can use any option from the [request](https://github.com/request/request) module. There are a couple extra options available:
58 - **url** - same as request url but can contain variables prefixed with a colon such as `products/:name`
59 - **isArray** - resource is an array. It will not populate variables in the url.
60
61```javascript
62var resourceClient = require('resource-client');
63
64var Product = resourceClient({
65 url: 'http://www.mysite.com/api/products/:_id',
66 headers: {
67 'X-Secret-Token': 'ABCD1234'
68 }
69});
70
71Product.action('query', {
72 method: 'GET'
73 isArray: true
74});
75```
76
77If the method is GET, you can use it as a class method:
78```javascript
79Product.action('get', {
80 method: 'GET'
81});
82// class method
83Product.get({_id: 1234}, function (err, product) { ... })
84```
85If the method is PUT, POST, DELETE, you can use it as an instance method:
86```javascript
87Product.action('save', {
88 method: 'POST'
89});
90// class method
91Product.save({name: 'apple'}, function (err, product) { ... });
92
93// instance method
94product = new Product({name: 'apple'});
95product.save()
96```
97
98## Default Actions
99
100Every new resource will come with these methods by default
101
102- **get** - {method: 'GET'}
103- **query** - {method: 'GET', isArray: true}
104- **update** - {method: 'PUT'}
105- **save** - {method: 'POST'}
106- **remove** - {method: 'DELETE'}
107
108## Contributing
109
110Please follow our [Code of Conduct](https://github.com/goodeggs/mongoose-webdriver/blob/master/CODE_OF_CONDUCT.md)
111when contributing to this project.
112
113```
114$ git clone https://github.com/goodeggs/resource-client && cd resource-client
115$ npm install
116$ npm test
117```
118
119_Module scaffold generated by [generator-goodeggs-npm](https://github.com/goodeggs/generator-goodeggs-npm)._