diff --git a/index.js b/index.js index 9db565b..0ad0a3f 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ -var Promise = require("bluebird"); -var methodNamesToPromisify = "get post put delete patch".split(" "); +var Promise = require('bluebird'); +var methodNamesToPromisify = 'get post put delete patch'.split(' '); var nodeRestClient = require('node-rest-client'); /** @@ -36,19 +36,42 @@ function EventEmitterPromisifier(originalMethod) { // listen to specific events leading to rejects emitter - .on("error", function (err) { + .on('error', function (err) { reject(err); }) - .on("requestTimeout", function () { + .on('requestTimeout', function () { reject(new Promise.TimeoutError()); }) - .on("responseTimeout", function () { + .on('responseTimeout', function () { reject(new Promise.TimeoutError()); }); }); }; }; +var registerPromiseMethod = function (name, url, method) { + // create method in method registry with preconfigured REST invocation + // method + + var self = this; + + var PromisifiedMethod = function (url, method) { + var httpMethod = self[method.toLowerCase()]; + + return function (args) { + var completeURL = url; + if (!args) { + args = {}; + } + + // eslint-disable-next-line new-cap + return EventEmitterPromisifier(httpMethod)(completeURL, args); + }; + }; + + this.methods[name] = new PromisifiedMethod(url, method); +}; + /** * A simple wrapper around `new Client(options)`, returning a promisified * client object. @@ -67,7 +90,11 @@ var client = function (options) { promisifier: EventEmitterPromisifier, suffix: 'Promise' }); + + promisifiedClient.registerMethodPromise = + registerPromiseMethod.bind(promisifiedClient); + return promisifiedClient; -} +}; exports.Client = client; diff --git a/package.json b/package.json index 052b529..eb22401 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-rest-client-promise", - "version": "3.0.0", + "version": "3.1.0", "description": "node-rest-client, but with promises", "main": "index.js", "scripts": { diff --git a/test/promiseTest.js b/test/promiseTest.js index aa061d7..5fe86a5 100644 --- a/test/promiseTest.js +++ b/test/promiseTest.js @@ -1,12 +1,14 @@ -var chai = require("chai"); +var chai = require('chai'); +var Promise = require('bluebird'); var nodeRestPromised = require('../index'); -var it = require("mocha").it; -var describe = require("mocha").describe; +var it = require('mocha').it; +var describe = require('mocha').describe; var should = chai.should(); describe('node-rest-client-promise', function () { describe('client', function () { it('should generate the promisified methods', function () { + // eslint-disable-next-line new-cap var client = nodeRestPromised.Client({}); client.should.hasOwnProperty( @@ -36,21 +38,14 @@ describe('node-rest-client-promise', function () { }); - it('should provide working promises', function (done) { + it('should provide working promises', function () { + // eslint-disable-next-line new-cap var client = nodeRestPromised.Client({}); - client.getPromise( + return client.getPromise( 'https://www.google.de' ) - .catch( - function (error) { - should.not.exist( - error, - 'Got error: ' + error.message - ); - } - ) .then( function (result) { result.response.statusCode @@ -65,4 +60,24 @@ describe('node-rest-client-promise', function () { }); }); + describe('client#registerMethod', function() { + it('should add a promise', function () { + // eslint-disable-next-line new-cap + var client = nodeRestPromised.Client({}); + + client.registerMethodPromise( + 'testMethod', 'https://somedomain', 'GET' + ); + + return client.methods.testMethod() + .catch( + function (e) { + return e.code === 'ENOTFOUND'; + }, + function () { + return Promise.resolve(); + } + ); + }); + }); });