Skip to content

Commit 5897309

Browse files
committed
registerMethodPromise support. Fixes #2
1 parent 034b420 commit 5897309

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

index.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var Promise = require("bluebird");
2-
var methodNamesToPromisify = "get post put delete patch".split(" ");
1+
var Promise = require('bluebird');
2+
var methodNamesToPromisify = 'get post put delete patch'.split(' ');
33
var nodeRestClient = require('node-rest-client');
44

55
/**
@@ -36,19 +36,42 @@ function EventEmitterPromisifier(originalMethod) {
3636
// listen to specific events leading to rejects
3737

3838
emitter
39-
.on("error", function (err) {
39+
.on('error', function (err) {
4040
reject(err);
4141
})
42-
.on("requestTimeout", function () {
42+
.on('requestTimeout', function () {
4343
reject(new Promise.TimeoutError());
4444
})
45-
.on("responseTimeout", function () {
45+
.on('responseTimeout', function () {
4646
reject(new Promise.TimeoutError());
4747
});
4848
});
4949
};
5050
};
5151

52+
var registerPromiseMethod = function (name, url, method) {
53+
// create method in method registry with preconfigured REST invocation
54+
// method
55+
56+
var self = this;
57+
58+
var PromisifiedMethod = function (url, method) {
59+
var httpMethod = self[method.toLowerCase()];
60+
61+
return function (args) {
62+
var completeURL = url;
63+
if (!args) {
64+
args = {};
65+
}
66+
67+
// eslint-disable-next-line new-cap
68+
return EventEmitterPromisifier(httpMethod)(completeURL, args);
69+
};
70+
};
71+
72+
this.methods[name] = new PromisifiedMethod(url, method);
73+
};
74+
5275
/**
5376
* A simple wrapper around `new Client(options)`, returning a promisified
5477
* client object.
@@ -67,7 +90,11 @@ var client = function (options) {
6790
promisifier: EventEmitterPromisifier,
6891
suffix: 'Promise'
6992
});
93+
94+
promisifiedClient.registerMethodPromise =
95+
registerPromiseMethod.bind(promisifiedClient);
96+
7097
return promisifiedClient;
71-
}
98+
};
7299

73100
exports.Client = client;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-rest-client-promise",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"description": "node-rest-client, but with promises",
55
"main": "index.js",
66
"scripts": {

test/promiseTest.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
var chai = require("chai");
1+
var chai = require('chai');
2+
var Promise = require('bluebird');
23
var nodeRestPromised = require('../index');
3-
var it = require("mocha").it;
4-
var describe = require("mocha").describe;
4+
var it = require('mocha').it;
5+
var describe = require('mocha').describe;
56

67
var should = chai.should();
78
describe('node-rest-client-promise', function () {
89
describe('client', function () {
910
it('should generate the promisified methods', function () {
11+
// eslint-disable-next-line new-cap
1012
var client = nodeRestPromised.Client({});
1113

1214
client.should.hasOwnProperty(
@@ -36,21 +38,14 @@ describe('node-rest-client-promise', function () {
3638

3739
});
3840

39-
it('should provide working promises', function (done) {
41+
it('should provide working promises', function () {
4042

43+
// eslint-disable-next-line new-cap
4144
var client = nodeRestPromised.Client({});
4245

43-
client.getPromise(
46+
return client.getPromise(
4447
'https://www.google.de'
4548
)
46-
.catch(
47-
function (error) {
48-
should.not.exist(
49-
error,
50-
'Got error: ' + error.message
51-
);
52-
}
53-
)
5449
.then(
5550
function (result) {
5651
result.response.statusCode
@@ -65,4 +60,24 @@ describe('node-rest-client-promise', function () {
6560

6661
});
6762
});
63+
describe('client#registerMethod', function() {
64+
it('should add a promise', function () {
65+
// eslint-disable-next-line new-cap
66+
var client = nodeRestPromised.Client({});
67+
68+
client.registerMethodPromise(
69+
'testMethod', 'https://somedomain', 'GET'
70+
);
71+
72+
return client.methods.testMethod()
73+
.catch(
74+
function (e) {
75+
return e.code === 'ENOTFOUND';
76+
},
77+
function () {
78+
return Promise.resolve();
79+
}
80+
);
81+
});
82+
});
6883
});

0 commit comments

Comments
 (0)