resource-client
Advanced tools
Comparing version 1.1.0 to 1.2.0
141
lib/index.js
// Generated by CoffeeScript 1.8.0 | ||
var UrlAssembler, defaultActions, request, resourceClient, _; | ||
request = require('request'); | ||
_ = require('lodash'); | ||
UrlAssembler = require('url-assembler'); | ||
defaultActions = require('./default_actions'); | ||
module.exports = resourceClient = function(options) { | ||
var Resource, actionConfig, actionName, getUrlParts, handleResponse, resourceRequest, url; | ||
url = options.url; | ||
if (options.json == null) { | ||
options.json = true; | ||
} | ||
resourceRequest = request.defaults(options); | ||
module.exports = Resource = (function() { | ||
function Resource(newObject) { | ||
_.assign(this, newObject); | ||
} | ||
return Resource; | ||
})(); | ||
Resource.action = function(actionName, options) { | ||
var actionRequest, actionUrl, baseUrl, idFields, _ref, _ref1; | ||
if (options.url) { | ||
url = options.url; | ||
} | ||
_ref = getUrlParts(url), baseUrl = _ref[0], idFields = _ref[1]; | ||
actionRequest = resourceRequest.defaults(options); | ||
if (options.method === 'GET' && !options.isArray) { | ||
Resource[actionName] = function(paramConfig, queryParams, done) { | ||
var requestUrl; | ||
if (paramConfig == null) { | ||
paramConfig = {}; | ||
} | ||
if (queryParams == null) { | ||
queryParams = {}; | ||
} | ||
done = arguments[arguments.length - 1]; | ||
requestUrl = UrlAssembler().template(url).param(paramConfig).query(queryParams).toString(); | ||
return actionRequest({ | ||
url: requestUrl | ||
}, function(err, response) { | ||
return handleResponse(err, response, null, done); | ||
}); | ||
}; | ||
} | ||
if (options.method === 'GET' && options.isArray) { | ||
Resource[actionName] = function(queryParams, done) { | ||
var requestUrl; | ||
if (queryParams == null) { | ||
queryParams = {}; | ||
} | ||
done = arguments[arguments.length - 1]; | ||
requestUrl = UrlAssembler().template(baseUrl).query(queryParams).toString(); | ||
return actionRequest({ | ||
url: requestUrl | ||
}, function(err, response) { | ||
return handleResponse(err, response, null, done); | ||
}); | ||
}; | ||
} | ||
if ((_ref1 = options.method) === 'PUT' || _ref1 === 'POST' || _ref1 === 'DELETE') { | ||
actionUrl = options.method === 'POST' ? baseUrl : url; | ||
Resource[actionName] = function(body, queryParams, done) { | ||
var requestUrl; | ||
if (queryParams == null) { | ||
queryParams = {}; | ||
} | ||
done = arguments[arguments.length - 1]; | ||
requestUrl = (function() { | ||
var idField, _i, _len; | ||
requestUrl = UrlAssembler().template(actionUrl); | ||
for (_i = 0, _len = idFields.length; _i < _len; _i++) { | ||
idField = idFields[_i]; | ||
requestUrl.param(idField, body[idField]); | ||
} | ||
return requestUrl.query(queryParams).toString(); | ||
})(); | ||
return actionRequest({ | ||
url: requestUrl, | ||
body: body | ||
}, function(err, response) { | ||
return handleResponse(err, response, null, done); | ||
}); | ||
}; | ||
return Resource.prototype[actionName] = function(queryParams, done) { | ||
var requestUrl; | ||
if (queryParams == null) { | ||
queryParams = {}; | ||
} | ||
done = arguments[arguments.length - 1]; | ||
requestUrl = (function(_this) { | ||
return function() { | ||
var idField, _i, _len; | ||
requestUrl = UrlAssembler().template(actionUrl); | ||
for (_i = 0, _len = idFields.length; _i < _len; _i++) { | ||
idField = idFields[_i]; | ||
requestUrl.param(idField, _this[idField]); | ||
} | ||
return requestUrl.query(queryParams).toString(); | ||
}; | ||
})(this)(); | ||
return actionRequest({ | ||
url: requestUrl, | ||
body: this | ||
}, function(err, response) { | ||
return handleResponse(err, response, this, done); | ||
}); | ||
}; | ||
} | ||
}; | ||
handleResponse = function(err, response, originalObject, done) { | ||
var resource, resources, _ref; | ||
if ((200 <= (_ref = response.statusCode) && _ref < 300)) { | ||
if (Array.isArray(response.body)) { | ||
resources = response.body.map(function(resource) { | ||
return new Resource(resource); | ||
}); | ||
return done(null, resources); | ||
} else { | ||
resource = originalObject ? _.assign(originalObject, response.body) : new Resource(response.body); | ||
return done(null, resource); | ||
} | ||
} else { | ||
return done(err); | ||
} | ||
}; | ||
getUrlParts = function(url) { | ||
var urlParts; | ||
urlParts = url.split('/:'); | ||
return [urlParts[0], urlParts.slice(1)]; | ||
}; | ||
for (actionName in defaultActions) { | ||
actionConfig = defaultActions[actionName]; | ||
Resource.action(actionName, actionConfig); | ||
} | ||
return Resource; | ||
}; |
{ | ||
"name": "resource-client", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Easily create api clients for your server side resources.", | ||
@@ -11,3 +11,7 @@ "author": "Good Eggs <open-source@goodeggs.com>", | ||
"keywords": [ | ||
"resource" | ||
"resource", | ||
"request", | ||
"ngResource", | ||
"client", | ||
"api" | ||
], | ||
@@ -21,6 +25,15 @@ "main": "lib/index.js", | ||
"bugs": "https://github.com/goodeggs/resource-client/issues", | ||
"dependencies": {}, | ||
"dependencies": { | ||
"request": "^2.51.0", | ||
"lodash": "^3.0.0", | ||
"url-assembler": "0.0.3" | ||
}, | ||
"devDependencies": { | ||
"coffee-script": ">=1.7.x", | ||
"mocha": "~1.x.x" | ||
"mocha": "~1.x.x", | ||
"express": "^4.11.1", | ||
"body-parser": "^1.10.2", | ||
"resource-schema": "^0.12.1", | ||
"fibrous": "^0.3.3", | ||
"mongoose": "^3.8.22" | ||
}, | ||
@@ -27,0 +40,0 @@ "publishConfig": { |
# Resource Client | ||
Easily create api clients for your server side resources. | ||
Easily create api clients for your server side resources. Inspired by [Angular Resource](https://docs.angularjs.org/api/ngResource/service/$resourcehttps://docs.angularjs.org/api/ngResource/service/$resource). | ||
@@ -12,3 +12,3 @@ [![NPM version](http://img.shields.io/npm/v/resource-client.svg?style=flat-square)](https://www.npmjs.org/package/resource-client) | ||
``` | ||
npm install resource-client | ||
npm install resource-client --save | ||
``` | ||
@@ -18,4 +18,93 @@ | ||
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](https://github.com/request/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 | ||
```javascript | ||
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](https://github.com/request/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. | ||
```javascript | ||
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: | ||
```javascript | ||
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: | ||
```javascript | ||
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 | ||
@@ -22,0 +111,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
17790
9
155
120
0
3
7
+ Addedlodash@^3.0.0
+ Addedrequest@^2.51.0
+ Addedurl-assembler@0.0.3
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@2.0.23.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedlodash@3.10.1(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.14.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@2.4.26.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addedurl-assembler@0.0.3(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)