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 - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

lib/default_actions.js

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;
};

21

package.json
{
"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

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