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.4.1 to 2.0.0

lib/url_builder.js

2

lib/default_actions.js

@@ -1,2 +0,2 @@

// Generated by CoffeeScript 1.9.1
// Generated by CoffeeScript 1.8.0
module.exports = {

@@ -3,0 +3,0 @@ 'get': {

@@ -1,4 +0,4 @@

// Generated by CoffeeScript 1.9.1
var UrlAssembler, _, defaultActions, request, resourceClient,
slice = [].slice;
// Generated by CoffeeScript 1.8.0
var defaultActions, request, resourceClient, urlBuilder, _,
__slice = [].slice;

@@ -9,13 +9,15 @@ request = require('request');

UrlAssembler = require('url-assembler');
urlBuilder = require('./url_builder');
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;
module.exports = resourceClient = function(resourceOptions) {
var Resource, actionConfig, actionName, handleResponse, resourceRequest;
if (resourceOptions.params == null) {
resourceOptions.params = {};
}
resourceRequest = request.defaults(options);
if (resourceOptions.json == null) {
resourceOptions.json = true;
}
resourceRequest = request.defaults(resourceOptions);
Resource = (function() {

@@ -26,36 +28,60 @@ function Resource(newObject) {

Resource.prototype.toObject = function() {
return JSON.parse(JSON.stringify(this));
};
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.action = function(actionName, actionOptions) {
var actionRequest, actionUrl, _ref;
actionOptions.params = {};
actionUrl = actionOptions.url || resourceOptions.url;
actionRequest = resourceRequest.defaults(actionOptions);
if (actionOptions.method === 'GET' && !actionOptions.isArray) {
/*
* get single w/ params (class method).
*
* call w/ `({params}, {requestOptions}, callback)`
* OR `({params}, callback)`
*/
return Resource[actionName] = function() {
var done, i, opts, params, queryParams, reqOptions;
params = arguments[0], opts = 3 <= arguments.length ? slice.call(arguments, 1, i = arguments.length - 1) : (i = 1, []), done = arguments[i++];
queryParams = opts.shift() || {};
reqOptions = opts.pop() || {};
reqOptions.url = UrlAssembler().template(url).param(params).query(queryParams).toString();
return actionRequest.get(reqOptions, function(err, response) {
var done, opts, requestOptions, requestParams, _i;
opts = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), done = arguments[_i++];
requestParams = opts.shift() || {};
requestOptions = opts.pop() || {};
requestOptions.url = (function() {
var mergedParams;
mergedParams = _.assign({}, resourceOptions.params, actionOptions.params, requestParams);
return urlBuilder.build(actionUrl, mergedParams);
})();
return actionRequest.get(requestOptions, function(err, response) {
return handleResponse(err, response, null, done);
});
};
} else if (options.method === 'GET' && options.isArray) {
} else if (actionOptions.method === 'GET' && actionOptions.isArray) {
/*
* get multi w/o params (class method).
*
* call w/ `({params}, {requestOptions}, callback)`
* OR `({params}, callback)`
* OR `(callback)`
*/
return Resource[actionName] = function() {
var done, i, opts, queryParams, reqOptions;
opts = 2 <= arguments.length ? slice.call(arguments, 0, i = arguments.length - 1) : (i = 0, []), done = arguments[i++];
queryParams = opts.shift() || {};
reqOptions = opts.pop() || {};
reqOptions.url = UrlAssembler().template(baseUrl).query(queryParams).toString();
return actionRequest.get(reqOptions, function(err, response) {
return handleResponse(err, response, null, done, options);
var done, opts, requestOptions, requestParams, _i;
opts = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), done = arguments[_i++];
requestParams = opts.shift() || {};
requestOptions = opts.pop() || {};
requestOptions.url = (function() {
var mergedParams;
mergedParams = _.assign({}, resourceOptions.params, actionOptions.params, requestParams);
return urlBuilder.build(actionUrl, mergedParams);
})();
return actionRequest.get(requestOptions, function(err, response) {
return handleResponse(err, response, null, done, actionOptions);
});
};
} else if ((ref1 = options.method) === 'PUT' || ref1 === 'POST' || ref1 === 'DELETE') {
actionUrl = options.method === 'POST' ? baseUrl : url;
} else if ((_ref = actionOptions.method) === 'PUT' || _ref === 'POST' || _ref === 'DELETE') {
return (function(methodFn) {

@@ -65,53 +91,60 @@ if (methodFn === 'delete') {

}
/*
* modify single (class method).
*
* call w/ `({params}, body, {requestOptions}, callback)`
* OR `({params}, body, callback)`
* OR `({params}, callback)`
*/
Resource[actionName] = function() {
var body, done, i, opts, queryParams, reqOptions;
body = arguments[0], opts = 3 <= arguments.length ? slice.call(arguments, 1, i = arguments.length - 1) : (i = 1, []), done = arguments[i++];
queryParams = opts.shift() || {};
reqOptions = opts.pop() || {};
reqOptions.body = body;
reqOptions.url = (function() {
var idField, j, len, requestUrl;
requestUrl = UrlAssembler().template(actionUrl);
for (j = 0, len = idFields.length; j < len; j++) {
idField = idFields[j];
requestUrl.param(idField, body[idField]);
}
return requestUrl.query(queryParams).toString();
var done, opts, requestBody, requestOptions, requestParams, _i;
opts = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), done = arguments[_i++];
requestParams = opts.shift() || {};
requestBody = opts.shift() || {};
requestOptions = opts.pop() || {};
requestOptions.body = requestBody;
requestOptions.url = (function() {
var mergedParams;
mergedParams = _.assign({}, resourceOptions.params, actionOptions.params, requestParams);
return urlBuilder.build(actionUrl, mergedParams, requestOptions.body);
})();
return actionRequest[methodFn](reqOptions, function(err, response) {
return actionRequest[methodFn](requestOptions, function(err, response) {
return handleResponse(err, response, null, done);
});
};
/*
* modify single (instance method).
*
* call w/ `({params}, {requestOptions}, callback)`
* OR `({params}, callback)`
* OR `(callback)`
*/
return Resource.prototype[actionName] = function() {
var done, i, opts, queryParams, reqOptions;
opts = 2 <= arguments.length ? slice.call(arguments, 0, i = arguments.length - 1) : (i = 0, []), done = arguments[i++];
queryParams = opts.shift() || {};
reqOptions = opts.pop() || {};
reqOptions.body = this;
reqOptions.url = (function(_this) {
return function() {
var idField, j, len, requestUrl;
requestUrl = UrlAssembler().template(actionUrl);
for (j = 0, len = idFields.length; j < len; j++) {
idField = idFields[j];
requestUrl.param(idField, _this[idField]);
}
return requestUrl.query(queryParams).toString();
};
})(this)();
return actionRequest[methodFn](reqOptions, function(err, response) {
var done, opts, requestOptions, requestParams, _i;
opts = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), done = arguments[_i++];
requestParams = opts.shift() || {};
requestOptions = opts.pop() || {};
requestOptions.body = this;
requestOptions.url = (function() {
var mergedParams;
mergedParams = _.assign({}, resourceOptions.params, actionOptions.params, requestParams);
return urlBuilder.build(actionUrl, mergedParams, requestOptions.body);
})();
return actionRequest[methodFn](requestOptions, function(err, response) {
return handleResponse(err, response, this, done);
});
};
})(options.method.toLowerCase());
})(actionOptions.method.toLowerCase());
}
};
handleResponse = function(err, response, originalObject, done, options) {
var errorMessage, ref, resource, resources;
if (options == null) {
options = {};
handleResponse = function(err, response, originalObject, done, actionOptions) {
var errorMessage, resource, resources, _ref;
if (actionOptions == null) {
actionOptions = {};
}
if (err) {
return done(err);
} else if ((200 <= (ref = response.statusCode) && ref < 300)) {
} else if ((200 <= (_ref = response.statusCode) && _ref < 300)) {
if (Array.isArray(response.body)) {

@@ -121,3 +154,3 @@ resources = response.body.map(function(resource) {

});
if (options.returnFirst) {
if (actionOptions.returnFirst) {
resources = resources[0];

@@ -137,7 +170,2 @@ }

};
getUrlParts = function(url) {
var urlParts;
urlParts = url.split('/:');
return [urlParts[0], urlParts.slice(1)];
};
for (actionName in defaultActions) {

@@ -144,0 +172,0 @@ actionConfig = defaultActions[actionName];

{
"name": "resource-client",
"version": "1.4.1",
"version": "2.0.0",
"description": "Easily create api clients for your server side resources.",

@@ -33,7 +33,4 @@ "author": "Good Eggs <open-source@goodeggs.com>",

"chai": "^2.1.0",
"express": "^4.11.1",
"body-parser": "^1.10.2",
"resource-schema": "^0.12.1",
"fibrous": "^0.3.3",
"mongoose": "3.8.22"
"nock": "^1.6.1"
},

@@ -40,0 +37,0 @@ "publishConfig": {

# Resource Client
Easily create api clients for your server side resources. Inspired by [Angular Resource](https://docs.angularjs.org/api/ngResource/service/$resource).
Easily create node API clients for your APIs. Inspired by [Angular Resource](https://docs.angularjs.org/api/ngResource/service/$resource).

@@ -36,4 +36,5 @@ [![NPM version](http://img.shields.io/npm/v/resource-client.svg?style=flat-square)](https://www.npmjs.org/package/resource-client)

- **options** - default request options for this resource. You can use any option from the [request][request] module. There are a few key differences:
- **options** - default request options for this resource. You can use any option from the [request][request] module, with a few additions:
- **url** - same as request url but can contain variables prefixed with a colon such as `products/:name`
- **params** - First used to populate url params, then any leftover are added as query params. Note, you can define a param using '@' to read the param value off the request body `{_id: '@_id'}`.
- **json** - set to true by default

@@ -47,5 +48,4 @@

url: 'http://www.mysite.com/api/products/:_id',
headers: {
'X-Secret-Token': 'ABCD1234'
}
params: {_id: '@_id'}
headers: {'X-Secret-Token': 'ABCD1234'}
})

@@ -59,4 +59,5 @@ ```

- **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:
- **options** - default request options for this action. Merges with defaults set for the resource. You can use any option from the [request](https://github.com/request/request) module, with a few additions:
- **url** - same as request url but can contain variables prefixed with a colon such as `products/:name`
- **params** - First used to populate url params, then any leftover are added as query params. Note, you can define a param using '@' to read the param value off the request body `{_id: '@_id'}`.
- **isArray** - resource is an array. It will not populate variables in the url.

@@ -75,5 +76,6 @@ - **returnFirst** - resource is an array. It will return the first result from the array

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

@@ -90,2 +92,4 @@

- `Class.method([params], [options], callback)`
```javascript

@@ -95,2 +99,3 @@ Product.action('get', {

});
// class method

@@ -102,2 +107,5 @@ Product.get({_id: 1234}, function (err, product) { ... })

- `Class.method([params], [body], [options], callback)`
- `instance.method([params], [options], callback)`
```javascript

@@ -118,3 +126,4 @@ Product.action('save', {

Every new resource will come with these methods by default
Every new resource will come with these methods by default. However, we recommend
you explicitly define an action for each endpoint exposed by your API.

@@ -137,15 +146,8 @@ - **get** - {method: 'GET'}

Product.save(
{}, // params
{name: 'apple'}, // body
{}, // query params
{ headers: {} }, // extra options
{ headers: {} }, // options
function (err, product) { ... }
);
product = new Product({name: 'apple'}); // body comes from instance
product.save(
{}, // query params
{ headers: {} }, // extra options
function(err) { ... }
);
Product.getById(urlParams, queryParams, otherOptions, callback);

@@ -152,0 +154,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