node-mailjet
Advanced tools
Comparing version 3.2.1 to 3.3.0
@@ -60,12 +60,78 @@ /* | ||
*/ | ||
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) { | ||
this.connect(api_key, api_secret, options) | ||
function MailjetClient (api_key, api_secret, options, perform_api_call) { | ||
return this.authStrategy(api_key, api_secret, options, perform_api_call) | ||
} | ||
/** | ||
* @param (optional){String} api_key || api_token | ||
* @param (optional){String} api_secret | ||
* @param (optional){Object} options | ||
* @param (optional){any} perform_api_call | ||
*/ | ||
MailjetClient.prototype.authStrategy = function(api_key, api_secret, options, perform_api_call) { | ||
var isTokenRequired = this.isTokenRequired(api_key, api_secret, options, perform_api_call) | ||
var self = this | ||
// Check if api version requires toekn authentication | ||
// This is one of the approaches, maybe there is better | ||
if (isTokenRequired) { | ||
// params are shifted one position to left as we don't have api secret any more | ||
// api_key becomes api_token | ||
// api_secret becomes options | ||
// options becomes perform_api_call | ||
return tokenAuthentication(api_key, api_secret, options) | ||
} else { | ||
// params are in correct order | ||
return basicAuthentication(api_key, api_secret, options, perform_api_call) | ||
} | ||
/** | ||
* | ||
* @param (optional){String} api_key mailjet api key | ||
* @param (optional){String} api_secret mailjet api secret | ||
* @param (optional){Object} options additional connection options | ||
* @param (optional){boolean} perform_api_call | ||
*/ | ||
function basicAuthentication(api_key, api_secret, options, perform_api_call) { | ||
self.config = self.setConfig(options) | ||
self.perform_api_call = perform_api_call || false | ||
// To be updated according to the npm repo version | ||
self.version = version | ||
if (api_key && api_secret) { | ||
self.connect(api_key, api_secret, options) | ||
} | ||
return self | ||
} | ||
/** | ||
* | ||
* @param (optional){String} api_token mailjet api token | ||
* @param (optional){Object} options additional connection options | ||
* @param (optional){boolean} perform_api_call | ||
*/ | ||
function tokenAuthentication(api_token, options, perform_api_call) { | ||
self.perform_api_call = perform_api_call || false | ||
// To be updated according to the npm repo version | ||
self.version = version | ||
if (api_token) { | ||
self.connect(api_token, options) | ||
} | ||
return self | ||
} | ||
} | ||
MailjetClient.prototype.isTokenRequired = function () { | ||
var args = [].slice.call(arguments) | ||
var vals = args.filter(a => a !== undefined) | ||
if (DEBUG_MODE) { | ||
console.log('Defined arguments: ' + JSON.stringify(vals)) | ||
} | ||
return (vals.length === 1 || (vals.length >= 2 && typeof vals[1] === 'object')) | ||
} | ||
MailjetClient.prototype.typeJson = function (body) { | ||
@@ -99,3 +165,3 @@ var keys = Object.keys(body) | ||
* | ||
* @apiKey {String} | ||
* @apiKey || @apiToken {String} | ||
* @apiSecret {String} | ||
@@ -106,9 +172,40 @@ * @options {Object} | ||
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.connectStrategy(apiKey, apiSecret, options) | ||
} | ||
/** | ||
* @param (optional){String} apiKey || apiToken | ||
* @param (optional){String} apiSecret | ||
* @param (optional){Object} options | ||
*/ | ||
MailjetClient.prototype.connectStrategy = function (apiKey, apiSecret, options) { | ||
var self = this | ||
var isTokenRequired = this.isTokenRequired(apiKey, apiSecret, options) | ||
if (isTokenRequired) { | ||
return tokenConnectStrategy(apiKey, apiSecret) | ||
} else { | ||
return basicConnectStrategy(apiKey, apiSecret, options) | ||
} | ||
return this | ||
function basicConnectStrategy(apiKey, apiSecret, options) { | ||
setOptions(options) | ||
self.apiKey = apiKey | ||
self.apiSecret = apiSecret | ||
return self | ||
} | ||
function tokenConnectStrategy(apiToken, options) { | ||
setOptions(options) | ||
self.apiToken = apiToken | ||
return self | ||
} | ||
function setOptions(options) { | ||
self.options = options || {} | ||
if (self.options) { | ||
self.config = self.setConfig(options) | ||
} | ||
} | ||
} | ||
@@ -174,9 +271,13 @@ | ||
var req = request[method](url) | ||
.set('user-agent', 'mailjet-api-v3-nodejs/' + this.version) | ||
.set('user-agent', 'mailjet-api-v3-nodejs/' + this.version) | ||
.set('Content-type', url.indexOf('text:plain') > -1 | ||
? 'text/plain' | ||
: 'application/json') | ||
.set('Content-type', url.indexOf('text:plain') > -1 | ||
? 'text/plain' | ||
: 'application/json') | ||
.auth(this.apiKey, this.apiSecret) | ||
if (this.apiToken) { | ||
req.set('Authorization', 'Bearer ' + this.apiToken) | ||
} else { | ||
req.auth(this.apiKey, this.apiSecret) | ||
} | ||
@@ -194,3 +295,3 @@ if (this.options.proxyUrl) { | ||
console.log('Final url: ' + url) | ||
console.log('body: ' + payload) | ||
console.log('body: ' + JSON.stringify(payload)) | ||
} | ||
@@ -211,4 +312,4 @@ | ||
: err | ||
? reject(err) | ||
: resolve(result) | ||
? reject(err) | ||
: resolve(result) | ||
} | ||
@@ -254,3 +355,3 @@ | ||
this.callUrl = func | ||
this.options = options | ||
this.options = options || context.options | ||
@@ -266,3 +367,3 @@ this.resource = func.toLowerCase() | ||
this.subPath = (function () { | ||
if (func.toLowerCase() !== 'send') { | ||
if (func.toLowerCase() !== 'send' && func.indexOf('sms') === -1) { | ||
return 'REST' | ||
@@ -269,0 +370,0 @@ } |
{ | ||
"name": "node-mailjet", | ||
"version": "3.2.1", | ||
"version": "3.3.0", | ||
"description": "Mailjet NodeJS API client", | ||
@@ -32,2 +32,3 @@ "main": "index.js", | ||
"email", | ||
"sms", | ||
"node", | ||
@@ -48,2 +49,2 @@ "mail", | ||
"homepage": "https://github.com/mailjet/mailjet-apiv3-nodejs#readme" | ||
} | ||
} |
[mailjet]: http://www.mailjet.com | ||
[api_credential]: https://app.mailjet.com/account/api_keys | ||
[api_token]: https://app.mailjet.com/sms | ||
[eventemitter]: https://nodejs.org/api/events.html | ||
@@ -12,3 +13,3 @@ [doc]: http://dev.mailjet.com/guides/?javascript# | ||
[![Build Status](https://travis-ci.org/mailjet/mailjet-apiv3-nodejs.svg?branch=master)](https://travis-ci.org/mailjet/mailjet-apiv3-nodejs) | ||
![Current Version](https://img.shields.io/badge/version-3.2-green.svg) | ||
![Current Version](https://img.shields.io/badge/version-3.2.1-green.svg) | ||
@@ -60,3 +61,3 @@ # Mailjet NodeJs Wrapper | ||
// The third argument (the object) is not mandatory. Each configuration key is also optional | ||
const mailjet = require ('apiv3') | ||
const mailjet = require ('node-mailjet') | ||
.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE, { | ||
@@ -277,4 +278,78 @@ url: 'api.mailjet.com', // default is the API url | ||
``` | ||
## SMS API | ||
##### `IMPORTANT` | ||
In mailjet-client v4 we have introduced new **_token_** authentication method for the sms api. | ||
You can generate token from [here][api_token] | ||
```javascript | ||
var Mailjet = require('node-mailjet').connect('api token'); | ||
``` | ||
Additional connection options may be passed as a **_second_** argument. The supported values are: | ||
- `proxyUrl`: HTTP proxy URL to send the API requests through | ||
- `timeout`: API request timeout in milliseconds | ||
- `url` (default: `api.mailjet.com`): Base Mailjet API URL | ||
- `version` (default: v3): API version to use in the URL | ||
- `perform_api_call` (default: true): controls if the must call must be performed to Mailjet API or not (dry run) | ||
``` javascript | ||
// The second argument (the object) is not mandatory. Each configuration key is also optional | ||
const mailjet = require ('node-mailjet') | ||
.connect(process.env.MJ_API_TOKEN, { | ||
url: 'api.mailjet.com', // default is the API url | ||
version: 'v4', // default is '/v3' | ||
perform_api_call: true // used for tests. default is true | ||
}) | ||
``` | ||
**_We kept all other functionality unchanged_** | ||
### Get cosy with Mailjet SMS | ||
#### Save your `API_TOKEN`: | ||
`echo 'export MJ_API_TOKEN=MY_API_TOKEN' >> ~/.zshrc` | ||
`source ~/.zshrc` | ||
replace `zshrc` with `bash_profile` if you are simply using bash | ||
#### And use it in your projects | ||
``` javascript | ||
var apiToken = process.env.MJ_API_TOKEN; | ||
``` | ||
### Store SMS resource | ||
``` javascript | ||
// GET resource | ||
var sms = Mailjet.get('sms'); | ||
// POST resource | ||
var sendSms = Mailjet.post('sms-send'); | ||
``` | ||
### Send SMS | ||
``` javascript | ||
var smsSend = Mailjet.post('sms-send'); | ||
var smsData = { | ||
'Text': 'Have a nice SMS flight with Mailjet !', | ||
'To': '+33600000000', | ||
'From': 'MJPilot' | ||
} | ||
smsSend | ||
.request(smsData) | ||
.then(handlePostResponse) | ||
.catch(handleError); | ||
``` | ||
## Run Test | ||
@@ -281,0 +356,0 @@ |
@@ -27,4 +27,8 @@ /* global describe, it */ | ||
var emailOptions = { | ||
version: 'v3' | ||
} | ||
describe('Basic Usage', function () { | ||
var client = Mailjet.connect(API_KEY, API_SECRET) | ||
var client = Mailjet.connect(API_KEY, API_SECRET, emailOptions) | ||
@@ -161,3 +165,3 @@ describe('connection', function () { | ||
var res = this.fn.request(this.payload) | ||
var ret = res[0] + ' ' + this.format(res[1]) | ||
var ret = res[0].replace(/\\/g, '/') + ' ' + this.format(res[1]) | ||
return ret | ||
@@ -167,3 +171,3 @@ } | ||
var client2 = new Mailjet(API_KEY, API_SECRET, null, true) | ||
var client2 = new Mailjet(API_KEY, API_SECRET, emailOptions, true) | ||
@@ -220,3 +224,3 @@ const EXAMPLES_SET = [ | ||
/* Set a very short timeout */ | ||
var client = Mailjet.connect(API_KEY, API_SECRET, { timeout: 10 }) | ||
var client = Mailjet.connect(API_KEY, API_SECRET, { timeout: 10, version: 'v3' }) | ||
@@ -223,0 +227,0 @@ describe('method request', function () { |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
533020
35
9127
382
24