Skip to content

Commit

Permalink
Merge pull request #21 from mailjet/v3.1
Browse files Browse the repository at this point in the history
V3.1
  • Loading branch information
eboisgon authored May 22, 2017
2 parents 709c47c + 8e32ec1 commit 8bbbb49
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 37 deletions.
52 changes: 45 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,19 @@ sendEmail
``` javascript
var emailData = {
'FromEmail': '[email protected]',
'FromName': 'Guillaume badi',
'FromEmail': '[email protected]',
'FromName': 'Pilot',
'Subject': 'Coucou Mailjet2',
'Text-part': 'Hello World2',
'Recipients': [{'Email': 'gbadi@mailjet.com'}],
'Recipients': [{'Email': 'passenger@mailjet.com'}],
};
var emailData2 = {
'FromEmail': '[email protected]',
'FromName': 'Guillaume badi',
'FromEmail': '[email protected]',
'FromName': 'Pilot',
'Subject': 'Coucou Mailjet2',
'Text-part': 'This is another Email',
'Recipients': [{'Email': 'gbadi@mailjet.com'}],
'Recipients': [{'Email': 'passenger@mailjet.com'}],
};
sendEmail
Expand Down Expand Up @@ -241,7 +241,7 @@ function newContact (email) {
function testEmail (text) {
email = {};
email['FromName'] = 'Your Name';
email['FromEmail'] = 'Your Sender Adress';
email['FromEmail'] = 'Your Sender Address';
email['Subject'] = 'Test Email';
email['Recipients'] = [{Email: 'Your email'}];
email['Text-Part'] = text;
Expand All @@ -261,6 +261,44 @@ testEmail('Hello World!');
npm test
```

## New !! Version 3.1.0 of the Nodejs wrapper !

This version modifies the way to construct the Client or the calls. We add the possibility to add an array with parameters on both Client creation and API call (please, note that each of these parameters are preset and are not mandatory in the creation or the call) :

Properties of the $settings (Client constructor) and $options (API call function)

url (Default: api.mailjet.com) : domain name of the API
version (Default: v3) : API version (only working for Mailjet API V3 +)
perform_api_call (Default: true) : turns on(true) / off the call to the API
secured (Default: true) : turns on(true) / off the use of 'https'



``` javascript

// The third argument (the object) is not mandatory, as each of its 4 keys.
const mailjet = require ('apiv3')
.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE, {
'url': 'api.mailjet.com', // default is the API url
'version': 'v3', // default is '/v3'
'secured': true, // default is a boolean true
'perform_api_call': true // used for tests. default is true
})

// the second argument (the object) is not mandatory, as each of its 4 keys
const request = mailjet
.post("send", {
'url': 'api.mailjet.com', 'version': 'v3', 'secured': 'https', 'perform_api_call': false
})
.request({
'FromEmail': '[email protected]',
'FromName': 'Pilot',
'Subject': 'Coucou Mailjet2',
'Text-part': 'Hello World2',
'Recipients': [{'Email': '[email protected]'}]})

```

## Contribute

Mailjet loves developers. You can be part of this project!
Expand Down
8 changes: 5 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"url": "api.mailjet.com",
"version": "/v3/",
"output": "json"
"url": "api.mailjet.com",
"version": "v3",
"output": "json",
"perform_api_call": true,
"secured": true
}
85 changes: 59 additions & 26 deletions mailjet-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ require('superagent-proxy')(request);
* If you don't know what this is about, sign up to Mailjet at:
* https://www.mailjet.com/
*/
function MailjetClient (api_key, api_secret, options, testMode) {
this.config = require('./config')
this.testMode = testMode || false
function MailjetClient (api_key, api_secret, options, perform_api_call) {
this.config = this.setConfig(options);
this.perform_api_call = perform_api_call || false
// To be updated according to the npm repo version
this.version = version
if (api_key && api_secret) {
Expand Down Expand Up @@ -106,9 +106,26 @@ MailjetClient.prototype.connect = function (apiKey, apiSecret, options) {
this.apiKey = apiKey
this.apiSecret = apiSecret
this.options = options || {}
if (options) {
this.config = this.setConfig(options);
}
return this
}

MailjetClient.prototype.setConfig = function (options) {
config = require('./config')
if (typeof options === 'object' && options != null && options.length != 0) {
if (options.url) config.url = options.url
if (options.version) config.version = options.version
if (options.secured) config.secured = options.secured
if (options.perform_api_call) config.perform_api_call = options.perform_api_call
} else if (options != undefined) {
throw "warning, your options variable is not a valid object."
}

return config
};

/*
* path.
*
Expand All @@ -120,19 +137,23 @@ MailjetClient.prototype.connect = function (apiKey, apiSecret, options) {
* @params {Object literal} {name: value}
*
*/
MailjetClient.prototype.path = function (resource, sub, params) {
MailjetClient.prototype.path = function (resource, sub, params, options) {
if (DEBUG_MODE) {
console.log('resource =', resource)
console.log('subPath =', sub)
console.log('filters =', params)
}
var base = _path.join(this.config.version, sub)

url = (options && 'url' in options ? options.url : this.config.url)
api_version = (options && 'version' in options ? options.version : this.config.version)

var base = _path.join(api_version, sub)
if (Object.keys(params).length === 0) {
return base + '/' + resource
return _path.join(url, base + '/' + resource)
}

var q = qs.stringify(params).replace(/%2B/g, '+')
return base + '/' + resource + '/?' + q
return _path.join(url, base + '/' + resource + '/?' + q)
}

/*
Expand All @@ -147,8 +168,7 @@ MailjetClient.prototype.path = function (resource, sub, params) {
* and error on error
*/

MailjetClient.prototype.httpRequest = function (method, url, data, callback) {

MailjetClient.prototype.httpRequest = function (method, url, data, callback, perform_api_call){
var req = request[method](url)
.set('user-agent', 'mailjet-api-v3-nodejs/' + this.version)

Expand All @@ -171,11 +191,11 @@ MailjetClient.prototype.httpRequest = function (method, url, data, callback) {
console.log('Final url: ' + url)
console.log('body: ' + payload)
}

if (this.testMode) {
if (perform_api_call === false || this.perform_api_call) {
return [url, payload]
}

if (method === 'delete') { method = 'del' }
if (method === 'post' || method === 'put') { req = req.send(data) }

Expand Down Expand Up @@ -224,9 +244,10 @@ MailjetClient.prototype.httpRequest = function (method, url, data, callback) {
* @func {String} resource/path to be sent
* @context {MailjetClient[instance]} parent client
*/
function MailjetResource (method, func, context) {
function MailjetResource (method, func, options, context) {
this.base = func
this.callUrl = func
this.options = options

this.resource = func.toLowerCase()

Expand Down Expand Up @@ -263,20 +284,32 @@ function MailjetResource (method, func, context) {
a filters property, we pass it to the url
*/
var path = self.path(that.callUrl, that.subPath, (function () {
if (params['filters']) {
var ret = params['filters']
delete params['filters']
if (params.filters) {
var ret = params.filters
delete params.filters
return ret
} else if (method === 'get') {
return params
} else {
return {}
}
})())
})(), that.options)

if (that.options && 'secured' in that.options) {
secured = that.options.secured;
} else {
secured = self.config.secured;
}

if (that.options && 'perform_api_call' in that.options) {
perform_api_call = that.options.perform_api_call;
} else {
perform_api_call = self.config.perform_api_call;
}

that.callUrl = that.base
self.lastAdded = RESOURCE
return self.httpRequest(method, 'https://' + _path.join(self.config.url, path), params, callback)
return self.httpRequest(method, (secured ? 'https' : 'http') + '://' + path, params, callback, perform_api_call)
}
}

Expand Down Expand Up @@ -359,8 +392,8 @@ MailjetResource.prototype.request = function (params, callback) {
*
* @returns a function that make an httpRequest for each call
*/
MailjetClient.prototype.post = function (func) {
return new MailjetResource('post', func, this)
MailjetClient.prototype.post = function (func, options) {
return new MailjetResource('post', func, options, this)
}

/*
Expand All @@ -370,8 +403,8 @@ MailjetClient.prototype.post = function (func) {
*
* @returns a function that make an httpRequest for each call
*/
MailjetClient.prototype.get = function (func) {
return new MailjetResource('get', func, this)
MailjetClient.prototype.get = function (func, options) {
return new MailjetResource('get', func, options, this)
}

/*
Expand All @@ -381,8 +414,8 @@ MailjetClient.prototype.get = function (func) {
*
* @returns a function that make an httpRequest for each call
*/
MailjetClient.prototype.delete = function (func) {
return new MailjetResource('delete', func, this)
MailjetClient.prototype.delete = function (func, options) {
return new MailjetResource('delete', func, options, this)
}

/*
Expand All @@ -392,8 +425,8 @@ MailjetClient.prototype.delete = function (func) {
*
* @returns a function that make an httpRequest for each call
*/
MailjetClient.prototype.put = function (func) {
return new MailjetResource('put', func, this)
MailjetClient.prototype.put = function (func, options) {
return new MailjetResource('put', func, options, this)
}

/*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-mailjet",
"version": "3.0.6",
"version": "3.1.0",
"description": "Mailjet NodeJS API client",
"main": "index.js",
"directories": {
Expand Down

0 comments on commit 8bbbb49

Please sign in to comment.