Skip to content

Commit

Permalink
feat(promise): use real promise
Browse files Browse the repository at this point in the history
  • Loading branch information
p-j committed Apr 26, 2016
1 parent 975d1b6 commit b62fcb6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 45 deletions.
31 changes: 17 additions & 14 deletions mailjet-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ const STRICT = false
* fs will simply be used to read files
*/

var qs = require('querystring')
var request = require('request')
var EventEmitter = require('events').EventEmitter
var _path = require('path')
const qs = require('querystring')
const request = require('request')
const Promise = require('bluebird')
const _path = require('path')

/*
* MailjetClient constructor.
Expand Down Expand Up @@ -139,8 +139,6 @@ MailjetClient.prototype.path = function (resource, sub, params) {
*/

MailjetClient.prototype.httpRequest = function (method, url, data, callback) {
var promise = new EventEmitter().on('error', function () {})

/**
* json: true converts the output from string to JSON
*/
Expand Down Expand Up @@ -170,6 +168,8 @@ MailjetClient.prototype.httpRequest = function (method, url, data, callback) {
if (method === 'delete') {
method = 'del'
}

return new Promise((resolve, reject) => {
/*
* request[method] returns either request.post, request.get etc
*
Expand All @@ -178,21 +178,24 @@ MailjetClient.prototype.httpRequest = function (method, url, data, callback) {
*/
request[method](options, function (err, response, body) {
if (err || (response.statusCode > 210)) {
if (callback) {
if (typeof callback === 'function') {
callback(err || body, response)
} else {
promise.emit('error', err || body, response)
}
reject({
error: err || body,
response: response
})
} else {
if (callback) {
if (typeof callback === 'function') {
callback(null, response, body)
} else {
promise.emit('success', response, body)
}
resolve({
response: response,
body: body
})
}
})

return promise
})
}

/*
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "node-mailjet",
"version": "1.1.0",
"version": "2.0.0",
"description": "Mailjet NodeJS API client",
"main": "index.js",
"directories": {
"test": "test"
},
"dependencies": {
"bluebird": "^3.3.5",
"chai": "^3.2.0",
"qs": "^4.0.0",
"request": "^2.61.0",
Expand All @@ -29,6 +30,11 @@
"mailjet"
],
"author": "Guillaume Badi <gbadi@mailjet.com> (https://github.com/GuillaumeBadi)",
"contributors": [
"Arnaud Breton <iam@arnaud.ninja> (https://github.com/arnaudbreton)",
"Nicholas Smith <nicksmith.biz@gmail.com> (https://github.com/safani)",
"Jérémie Parker <jeremie@vizeat.com> (https://github.com/p-j)"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/mailjet/mailjet-apiv3-nodejs/issues"
Expand Down
59 changes: 29 additions & 30 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ var Mailjet = require('../mailjet-client')
var chai = require('chai')
var expect = chai.expect
var should = chai.should() // eslint-disable-line no-unused-vars
var EventEmitter = require('events').EventEmitter
var Promise = require('bluebird')
if (typeof API_KEY === 'undefined' || typeof API_SECRET === 'undefined') {
throw new Error(`Mailjet API_KEY and API_SECRET are required, respectively "${API_KEY}" and "${API_SECRET}" given`)
}

describe('Basic Usage', function () {
var client = Mailjet.connect(API_KEY, API_SECRET)
Expand All @@ -39,38 +42,37 @@ describe('Basic Usage', function () {

it('calls the contact ressource instance whith no parameters', function (done) {
contact.request()
.on('success', function (response, body) {
body.should.be.a('object')
expect(response.statusCode).to.equal(200)
.then(function (result) {
result.body.should.be.a('object')
expect(result.response.statusCode).to.equal(200)
done()
})
.on('error', function (e) {
.catch(function (reason) {
// We want it to raise an error if it gets here
expect(e).to.equal(undefined)
expect(reason).to.equal(undefined)
done()
})
})

it('calls the contact ressource instance whith parameters', function (done) {
var ee = contact.request({Name: 'Guillaume Badi'})
.on('success', function (response, body) {
body.should.be.a('object')
expect(response.statusCode).to.be.within(200, 201)
var promise = contact.request({Name: 'Guillaume Badi'})
.then(function (result) {
result.body.should.be.a('object')
expect(result.response.statusCode).to.be.within(200, 201)
done()
})
.on('error', function (e) {
.catch(function (reason) {
// We want it to raise an error if it gets here
expect(e).to.equal(undefined)
expect(reason).to.equal(undefined)
done()
})
expect(EventEmitter.prototype.isPrototypeOf(ee)).to.equal(true)
expect(Promise.prototype.isPrototypeOf(promise)).to.equal(true)
})

it('calls the contact ressource instance with empty parameters', function (done) {
contact.request({})
.on('success', function (response, body) {
body.should.be.a('object')
expect(response.statusCode).to.be.within(200, 201)
contact.request({}).then(function (result) {
result.body.should.be.a('object')
expect(result.response.statusCode).to.be.within(200, 201)
done()
})
})
Expand All @@ -80,38 +82,35 @@ describe('Basic Usage', function () {
var sender = client.post('sender')

it('calls the sender ressource instance whith no parameters', function (done) {
sender.request()
.on('error', function (e, response) {
response.statusCode.should.equal(400)
sender.request().catch(function (reason) {
reason.response.statusCode.should.equal(400)
done()
})
})

it('calls the sender ressource instance whith invalid parameters', function (done) {
sender.request({Name: 'Guillaume Badi'})
.on('error', function (e, response) {
expect(response.statusCode).to.equal(400)
sender.request({Name: 'Guillaume Badi'}).catch(function (reason) {
expect(reason.response.statusCode).to.equal(400)
done()
})
})

it('calls the sender ressource instance whith valid parameters', function (done) {
sender.request({email: 'gbadi@mailjet.com'})
.on('success', function (response, body) {
expect(response.statusCode).to.equal(201)
.then(function (result) {
expect(result.response.statusCode).to.equal(201)
done()
})
.on('error', function (e, response) {
.catch(function (reason) {
// if it fails because the sender already exist. should be 400
expect(response.statusCode).to.equal(400)
expect(reason.response.statusCode).to.equal(400)
done()
})
})

it('calls the sender resource with empty parameters', function (done) {
sender.request({})
.on('error', function (e, response) {
expect(response.statusCode).to.equal(400)
sender.request({}).catch(function (reason) {
expect(reason.response.statusCode).to.equal(400)
done()
})
})
Expand Down

0 comments on commit b62fcb6

Please sign in to comment.