node-mailjet
Advanced tools
Comparing version 3.3.13 to 3.4.0
module.exports = { | ||
'env': { | ||
'commonjs': true, | ||
'es6': true, | ||
'node': true | ||
'es2020': true, | ||
'node': true, | ||
'mocha': true | ||
}, | ||
'extends': 'eslint:recommended', | ||
'ignorePatterns': ['examples/**'], | ||
'rules': { | ||
@@ -24,5 +26,5 @@ 'no-console': 0, | ||
'error', | ||
'never' | ||
'always' | ||
] | ||
} | ||
} | ||
}; |
@@ -1,3 +0,3 @@ | ||
var mailjet = require('./mailjet-client') | ||
var mailjet = require('./mailjet-client'); | ||
module.exports = mailjet | ||
module.exports = mailjet.MailjetClient; |
@@ -26,6 +26,6 @@ /* | ||
const DEBUG_MODE = false | ||
const RESOURCE = 0 | ||
const ID = 1 | ||
const ACTION = 2 | ||
const DEBUG_MODE = false; | ||
const RESOURCE = 0; | ||
const ID = 1; | ||
const ACTION = 2; | ||
@@ -41,17 +41,12 @@ /* | ||
const qs = require('querystring') | ||
const request = require('superagent') | ||
const _path = require('path') | ||
const JSONb = require('json-bigint')({ storeAsString: true }) | ||
const version = require('./package.json').version | ||
const qs = require('querystring'); | ||
const request = require('superagent'); | ||
const _path = require('path'); | ||
const JSONb = require('json-bigint')({ storeAsString: true }); | ||
const version = require('./package.json').version; | ||
const setValueIfExist = require('./lib/utils/setValueIfExist'); | ||
/* Extend superagent request with proxy method */ | ||
require('superagent-proxy')(request) | ||
require('superagent-proxy')(request); | ||
function setValueIfExist(targetObject, path, value) { | ||
if(value) { | ||
targetObject[path] = value; | ||
} | ||
} | ||
/* | ||
@@ -68,3 +63,3 @@ * MailjetClient constructor. | ||
function MailjetClient (api_key, api_secret, options, perform_api_call) { | ||
return this.authStrategy(api_key, api_secret, options, perform_api_call) | ||
return this.authStrategy(api_key, api_secret, options, perform_api_call); | ||
} | ||
@@ -80,4 +75,4 @@ | ||
var isTokenRequired = this.isTokenRequired(api_key, api_secret, options, perform_api_call) | ||
var self = this | ||
var isTokenRequired = this.isTokenRequired(api_key, api_secret, options, perform_api_call); | ||
var self = this; | ||
// Check if api version requires toekn authentication | ||
@@ -90,6 +85,6 @@ // This is one of the approaches, maybe there is better | ||
// options becomes perform_api_call | ||
return tokenAuthentication(api_key, api_secret, options) | ||
return tokenAuthentication(api_key, api_secret, options); | ||
} else { | ||
// params are in correct order | ||
return basicAuthentication(api_key, api_secret, options, perform_api_call) | ||
return basicAuthentication(api_key, api_secret, options, perform_api_call); | ||
} | ||
@@ -106,11 +101,11 @@ | ||
self.config = self.setConfig(options) | ||
self.perform_api_call = perform_api_call || false | ||
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 | ||
self.version = version; | ||
if (api_key && api_secret) { | ||
self.connect(api_key, api_secret, options) | ||
self.connect(api_key, api_secret, options); | ||
} | ||
return self | ||
return self; | ||
} | ||
@@ -125,33 +120,24 @@ | ||
function tokenAuthentication(api_token, options, perform_api_call) { | ||
self.perform_api_call = perform_api_call || false | ||
self.perform_api_call = perform_api_call || false; | ||
// To be updated according to the npm repo version | ||
self.version = version | ||
self.version = version; | ||
if (api_token) { | ||
self.connect(api_token, options) | ||
self.connect(api_token, options); | ||
} | ||
return self | ||
return self; | ||
} | ||
} | ||
}; | ||
MailjetClient.prototype.isTokenRequired = function () { | ||
var args = [].slice.call(arguments) | ||
var vals = args.filter(a => a !== undefined) | ||
var args = [].slice.call(arguments); | ||
var vals = args.filter(a => a !== undefined); | ||
if (DEBUG_MODE) { | ||
console.log('Defined arguments: ' + JSON.stringify(vals)) | ||
console.log('Defined arguments: ' + JSON.stringify(vals)); | ||
} | ||
return (vals.length === 1 || (vals.length >= 2 && typeof vals[1] === 'object')) | ||
} | ||
return (vals.length === 1 || (vals.length >= 2 && typeof vals[1] === 'object')); | ||
}; | ||
MailjetClient.prototype.typeJson = function (body) { | ||
var keys = Object.keys(body) | ||
for (var i in keys) { | ||
var key = keys[i] | ||
body[key] = parseInt(body[key]) || body[key] | ||
} | ||
return body | ||
} | ||
/* | ||
@@ -168,4 +154,4 @@ * [Static] connect. | ||
MailjetClient.connect = function (k, s, o) { | ||
return new MailjetClient().connect(k, s, o) | ||
} | ||
return new MailjetClient().connect(k, s, o); | ||
}; | ||
@@ -183,4 +169,4 @@ /* | ||
MailjetClient.prototype.connect = function (apiKey, apiSecret, options) { | ||
return this.connectStrategy(apiKey, apiSecret, options) | ||
} | ||
return this.connectStrategy(apiKey, apiSecret, options); | ||
}; | ||
@@ -194,9 +180,9 @@ /** | ||
var self = this | ||
var isTokenRequired = this.isTokenRequired(apiKey, apiSecret, options) | ||
var self = this; | ||
var isTokenRequired = this.isTokenRequired(apiKey, apiSecret, options); | ||
if (isTokenRequired) { | ||
return tokenConnectStrategy(apiKey, apiSecret) | ||
return tokenConnectStrategy(apiKey, apiSecret); | ||
} else { | ||
return basicConnectStrategy(apiKey, apiSecret, options) | ||
return basicConnectStrategy(apiKey, apiSecret, options); | ||
} | ||
@@ -212,10 +198,10 @@ | ||
setOptions(options) | ||
self.apiKey = apiKey | ||
self.apiSecret = apiSecret | ||
return self | ||
setOptions(options); | ||
self.apiKey = apiKey; | ||
self.apiSecret = apiSecret; | ||
return self; | ||
} | ||
function tokenConnectStrategy(apiToken, options) { | ||
setOptions(options) | ||
if (!apiToken) { | ||
@@ -225,26 +211,28 @@ throw new Error('Mailjet API_TOKEN is required'); | ||
self.apiToken = apiToken | ||
return self | ||
setOptions(options); | ||
self.apiToken = apiToken; | ||
return self; | ||
} | ||
function setOptions(options) { | ||
self.options = options || {} | ||
self.options = options || {}; | ||
if (self.options) { | ||
self.config = self.setConfig(options) | ||
self.config = self.setConfig(options); | ||
} | ||
} | ||
} | ||
}; | ||
MailjetClient.prototype.setConfig = function (options) { | ||
const config = require('./config.json') | ||
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.perform_api_call) config.perform_api_call = options.perform_api_call | ||
} else if (options != null) { | ||
throw new Error('warning, your options variable is not a valid object.') | ||
const config = { ...require('./config.json') }; | ||
if (typeof options === 'object' && options !== null) { | ||
if (options.url) config.url = options.url; | ||
if (options.version) config.version = options.version; | ||
if ('perform_api_call' in options) config.perform_api_call = options.perform_api_call; | ||
} else if (options != null) { // TODO: Bug -> must be strict equal | ||
throw new Error('warning, your options variable is not a valid object.'); | ||
} | ||
return config | ||
} | ||
return config; | ||
}; | ||
@@ -264,18 +252,20 @@ /* | ||
if (DEBUG_MODE) { | ||
console.log('resource =', resource) | ||
console.log('subPath =', sub) | ||
console.log('filters =', params) | ||
console.log('resource =', resource); | ||
console.log('subPath =', sub); | ||
console.log('filters =', params); | ||
} | ||
const url = (options && 'url' in options) ? options.url : this.config.url | ||
const api_version = (options && 'version' in options) ? options.version : this.config.version | ||
const url = (options && 'url' in options) ? options.url : this.config.url; | ||
const api_version = (options && 'version' in options) ? options.version : this.config.version; | ||
const base = _path.join(api_version, sub) | ||
const base = _path.join(api_version, sub); | ||
const path = _path.join(url, base + '/' + resource); | ||
if (Object.keys(params).length === 0) { | ||
return _path.join(url, base + '/' + resource) | ||
return path; | ||
} | ||
const querystring = qs.stringify(params); | ||
return _path.join(url, base + '/' + resource + '?' + querystring) | ||
} | ||
return `${path}?${querystring}`; | ||
}; | ||
@@ -300,30 +290,30 @@ /* | ||
? 'text/plain' | ||
: 'application/json') | ||
: 'application/json'); | ||
if (this.apiToken) { | ||
req.set('Authorization', 'Bearer ' + this.apiToken) | ||
req.set('Authorization', 'Bearer ' + this.apiToken); | ||
} else { | ||
req.auth(this.apiKey, this.apiSecret) | ||
req.auth(this.apiKey, this.apiSecret); | ||
} | ||
if (this.options.proxyUrl) { | ||
req = req.proxy(this.options.proxyUrl) | ||
req = req.proxy(this.options.proxyUrl); | ||
} | ||
if (this.options.timeout) { | ||
req = req.timeout(this.options.timeout) | ||
req = req.timeout(this.options.timeout); | ||
} | ||
const payload = method === 'post' || method === 'put' ? data : {} | ||
const payload = method === 'post' || method === 'put' ? data : {}; | ||
if (DEBUG_MODE) { | ||
console.log('Final url: ' + url) | ||
console.log('body: ' + JSON.stringify(payload)) | ||
console.log('Final url: ' + url); | ||
console.log('body: ' + JSON.stringify(payload)); | ||
} | ||
if (perform_api_call === false || this.perform_api_call) { | ||
return Promise.resolve({body: payload, url: url}) | ||
return Promise.resolve({body: payload, url: url}); | ||
} | ||
if (method === 'delete') { method = 'del' } | ||
if (method === 'post' || method === 'put') { req = req.send(data) } | ||
if (method === 'delete') { method = 'del'; } | ||
if (method === 'post' || method === 'put') { req = req.send(data); } | ||
@@ -337,27 +327,27 @@ return new Promise(function (resolve, reject) { | ||
? reject(err) | ||
: resolve(result) | ||
} | ||
: resolve(result); | ||
}; | ||
req.end(function (err, result) { | ||
var body | ||
var body; | ||
try { | ||
body = JSONb.parse(result.text) | ||
body = JSONb.parse(result.text); | ||
} catch (e) { | ||
body = {} | ||
body = {}; | ||
} | ||
if (err) { | ||
const error = new Error() | ||
error.ErrorMessage = body.ErrorMessage || err.message | ||
const error = new Error(); | ||
error.ErrorMessage = body.ErrorMessage || err.message; | ||
error.statusCode = err.status || null | ||
error.response = result || null | ||
error.message = `Unsuccessful: Status Code: "${error.statusCode}" Message: "${error.ErrorMessage}"` | ||
error.statusCode = err.status || null; | ||
error.response = result || null; | ||
error.message = `Unsuccessful: Status Code: "${error.statusCode}" Message: "${error.ErrorMessage}"`; | ||
setValueIfExist(error, 'ErrorIdentifier', body.ErrorIdentifier) | ||
setValueIfExist(error, 'ErrorCode', body.ErrorCode) | ||
setValueIfExist(error, 'ErrorRelatedTo', body.ErrorRelatedTo) | ||
setValueIfExist(error, 'ErrorIdentifier', body.ErrorIdentifier); | ||
setValueIfExist(error, 'ErrorCode', body.ErrorCode); | ||
setValueIfExist(error, 'ErrorRelatedTo', body.ErrorRelatedTo); | ||
return ret(error) | ||
return ret(error); | ||
} | ||
@@ -368,6 +358,6 @@ | ||
body: body | ||
}) | ||
}) | ||
}) | ||
} | ||
}); | ||
}); | ||
}); | ||
}; | ||
@@ -385,10 +375,10 @@ /* | ||
function MailjetResource (method, func, options, context) { | ||
this.base = func | ||
this.callUrl = func | ||
this.options = options || context.options | ||
this.base = func; | ||
this.callUrl = func; | ||
this.options = options || context.options; | ||
this.resource = func.toLowerCase() | ||
this.resource = func.toLowerCase(); | ||
this.lastAdded = RESOURCE | ||
var self = context | ||
this.lastAdded = RESOURCE; | ||
var self = context; | ||
@@ -400,6 +390,6 @@ /* | ||
if (func.toLowerCase() !== 'send' && func.indexOf('sms') === -1) { | ||
return 'REST' | ||
return 'REST'; | ||
} | ||
return '' | ||
})() | ||
return ''; | ||
})(); | ||
@@ -413,8 +403,8 @@ /** | ||
*/ | ||
var that = this | ||
var that = this; | ||
this.result = function (params, callback) { | ||
params = params || {} | ||
params = params || {}; | ||
if (typeof params === 'function') { | ||
callback = params | ||
params = {} | ||
callback = params; | ||
params = {}; | ||
} | ||
@@ -428,23 +418,23 @@ | ||
if (params.filters) { | ||
var ret = params.filters | ||
delete params.filters | ||
return ret | ||
var ret = params.filters; | ||
delete params.filters; | ||
return ret; | ||
} else if (method === 'get') { | ||
return params | ||
return params; | ||
} else { | ||
return {} | ||
return {}; | ||
} | ||
})(), that.options) | ||
})(), that.options); | ||
var perform_api_call = null | ||
var perform_api_call = null; | ||
if (that.options && 'perform_api_call' in that.options) { | ||
perform_api_call = that.options.perform_api_call | ||
perform_api_call = that.options.perform_api_call; | ||
} else { | ||
perform_api_call = self.config.perform_api_call | ||
perform_api_call = self.config.perform_api_call; | ||
} | ||
that.callUrl = that.base | ||
self.lastAdded = RESOURCE | ||
return self.httpRequest(method, 'https://' + path, params, callback, perform_api_call) | ||
} | ||
that.callUrl = that.base; | ||
self.lastAdded = RESOURCE; | ||
return self.httpRequest(method, 'https://' + path, params, callback, perform_api_call); | ||
}; | ||
} | ||
@@ -464,9 +454,9 @@ | ||
if (this.lastAdded === ID && DEBUG_MODE) { | ||
console.warn('[WARNING] your request may fail due to invalid id chaining') | ||
console.warn('[WARNING] your request may fail due to invalid id chaining'); | ||
} | ||
this.callUrl = _path.join(this.callUrl, value.toString()) | ||
this.lastAdded = ID | ||
return this | ||
} | ||
this.callUrl = _path.join(this.callUrl, value.toString()); | ||
this.lastAdded = ID; | ||
return this; | ||
}; | ||
@@ -485,28 +475,26 @@ /** | ||
if (this.lastAdded === ACTION && DEBUG_MODE) { | ||
console.warn('[WARNING] your request may fail due to invalid action chaining') | ||
console.warn('[WARNING] your request may fail due to invalid action chaining'); | ||
} | ||
this.callUrl = _path.join(this.callUrl, name) | ||
this.action = name.toLowerCase() | ||
this.action = name.toLowerCase(); | ||
this.lastAdded = ACTION; | ||
this.lastAdded = ACTION | ||
if (this.action.toLowerCase() === 'csvdata') { | ||
this.action = 'csvdata/text:plain' | ||
this.action = 'csvdata/text:plain'; | ||
} else if (this.action.toLowerCase() === 'csverror') { | ||
this.action = 'csverror/text:csv' | ||
this.action = 'csverror/text:csv'; | ||
} | ||
var self = this | ||
this.callUrl = _path.join(this.callUrl, this.action); | ||
var self = this; | ||
this.subPath = (function () { | ||
if (self.resource === 'contactslist' && self.action === 'csvdata/text:plain' || | ||
self.resource === 'batchjob' && self.action === 'csverror/text:csv') { | ||
return 'DATA' | ||
} else { | ||
return self.subPath | ||
} | ||
})() | ||
return self | ||
} | ||
const isContactListWithCSV = self.resource === 'contactslist' && self.action === 'csvdata/text:plain'; | ||
const isBatchJobWithCSV = self.resource === 'batchjob' && self.action === 'csverror/text:csv'; | ||
return (isContactListWithCSV || isBatchJobWithCSV) ? 'DATA' : self.subPath; | ||
})(); | ||
return self; | ||
}; | ||
/** | ||
@@ -528,6 +516,6 @@ * | ||
if (typeof fullMessage === "string") { | ||
err.message = err.message + ";\n" + fullMessage; | ||
throw err; | ||
if (typeof fullMessage === 'string') { | ||
err.message = err.message + ';\n' + fullMessage; | ||
} | ||
throw err; | ||
@@ -548,4 +536,4 @@ } catch { | ||
MailjetClient.prototype.post = function (func, options) { | ||
return new MailjetResource('post', func, options, this) | ||
} | ||
return new MailjetResource('post', func, options, this); | ||
}; | ||
@@ -560,4 +548,4 @@ /* | ||
MailjetClient.prototype.get = function (func, options) { | ||
return new MailjetResource('get', func, options, this) | ||
} | ||
return new MailjetResource('get', func, options, this); | ||
}; | ||
@@ -572,4 +560,4 @@ /* | ||
MailjetClient.prototype.delete = function (func, options) { | ||
return new MailjetResource('delete', func, options, this) | ||
} | ||
return new MailjetResource('delete', func, options, this); | ||
}; | ||
@@ -584,4 +572,4 @@ /* | ||
MailjetClient.prototype.put = function (func, options) { | ||
return new MailjetResource('put', func, options, this) | ||
} | ||
return new MailjetResource('put', func, options, this); | ||
}; | ||
@@ -592,7 +580,8 @@ /* | ||
* you can require it like so: | ||
* var mj = require ('./mailjet-client') | ||
* var mj = require('./mailjet-client').MailjetClient | ||
* | ||
* or for the bleeding edge developpers out there: | ||
* import mj from './mailjet-client' | ||
* import { MailjetClient } from './mailjet-client' | ||
*/ | ||
module.exports = MailjetClient | ||
module.exports.MailjetClient = MailjetClient; | ||
module.exports.MailjetResource = MailjetResource; |
{ | ||
"name": "node-mailjet", | ||
"version": "3.3.13", | ||
"version": "3.4.0", | ||
"description": "Mailjet NodeJS API client", | ||
@@ -9,2 +9,13 @@ "main": "index.js", | ||
}, | ||
"scripts": { | ||
"test": "mocha --recursive", | ||
"test:int": "mocha test/integration/*", | ||
"test:unit": "mocha test/unit/*", | ||
"cover": "npm run cover:expandable npm run test", | ||
"cover:int": "npm run cover:expandable npm run test:int", | ||
"cover:unit": "npm run cover:expandable npm run test:unit", | ||
"cover:expandable": "nyc --reporter=text-summary --reporter=lcov --report-dir=.coverage --temp-dir=./.coverage/.nyc_output", | ||
"lint": "eslint .", | ||
"lint:fix": "eslint . --fix" | ||
}, | ||
"dependencies": { | ||
@@ -21,8 +32,5 @@ "json-bigint": "^1.0.0", | ||
"mocha": "^9.0.0", | ||
"nock": "^13.2.4" | ||
"nock": "^13.2.4", | ||
"nyc": "^15.1.0" | ||
}, | ||
"scripts": { | ||
"test": "mocha", | ||
"lint": "eslint ." | ||
}, | ||
"repository": { | ||
@@ -29,0 +37,0 @@ "type": "git", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 6 instances in 1 package
2
35007
6
16
544
1