Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

postageapp

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postageapp - npm Package Compare versions

Comparing version 1.1.5 to 2.0.0

bin/postageapp

236

lib/postageapp.js

@@ -1,178 +0,124 @@

var http = require('http');
var crypto = require('crypto');
// == Imports ===============================================================
var postageVersion = require('../package.json').version;
const http = require('http');
const https = require('https');
module.exports = function(apiKey) {
return {
version: function() {
return postageVersion;
},
const ApiCall = require('./api_call');
getApiKey: function() {
return apiKey;
},
const Config = require('./config');
const toCamelCase = require('./support').toCamelCase;
setApiKey: function(newKey) {
apiKey = newKey;
},
// == Constants =============================================================
sendMessage: function (options, success, error) {
var recipients = options.recipients;
var recipientOverride = options.recipient_override;
const methods = [
'get_method_list',
'get_message_receipt',
'send_message',
'get_metrics',
'get_suppression_list',
'get_message_transmissions',
'get_account_info',
'messages_history',
'get_project_info',
'test_error'
];
var subject = options.subject;
var from = options.from;
// == Exported Classes ======================================================
var attachments = options.attachments ? options.attachments : {};
var content = options.content;
class PostageApp {
constructor(config) {
this._config = new Config(config);
var template = options.template;
var variables = options.variables;
this.version = require('../package.json').version;
}
// no-op callback defaults
if (typeof(success) !== 'function') success = function() {};
if (typeof(error) !== 'function') error = function() {};
config() {
return this._config;
}
/*
Creates a string of numbers to be used for the UID, which has to be a unique identifier in order
to prevent duplicate sending through PostageApp.
*/
var str = JSON.stringify(options);
var hash = crypto.createHash('sha1').update(str).digest('hex');
call(method, args, uid) {
return new ApiCall(method, args, uid, this._config.apiKey);
}
/*
Payload is the aggregated data that will be passed to the API server including all of the parameters
required to send an email through PostageApp.
*/
var payload = {
api_key: apiKey,
uid: hash,
arguments: {
recipients: recipients,
recipient_override: recipientOverride,
headers: {
subject: subject,
from: from
},
content: content,
attachments: attachments,
template: template,
variables: variables
}
};
test() {
return this.send(this.call('get_project_info'));
}
payload = JSON.stringify(payload);
send(call) {
return new Promise((resolve, reject) => {
var payload = call.serialize();
var handler = this._config.secure ? https : http;
var request = http.request({
'port': 80,
'host': 'api.postageapp.com',
'method': 'POST',
'path': '/v.1.0/send_message.json',
'headers': {
'host': 'api.postageapp.com',
'content-type': 'application/json; charset=utf8',
'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.js ' + process.version + ')',
var options = {
host: this._config.host,
port: this._config.port,
method: 'POST',
path: `/v.1.0/${call.method}.json`,
headers: {
host: this._config.host,
'content-type': 'application/json',
'user-agent': 'PostageApp Node.js ' + this.version + ' (Node.js ' + process.version + ')',
'content-length': Buffer.byteLength(payload, 'utf8')
}
});
};
request.on('response', function (response) {
response.setEncoding('utf8');
var request = handler.request(options, function(response) {
var buffer = '';
var json;
response.on('data', function (data) {
if (response.headers['content-type'].indexOf("application/json") === 0) {
json = JSON.parse(data);
}
else {
json = {
response: {
status: "fail",
message: "Received an unexpected non-json response"
}
}
}
response.on('data', (data) => {
buffer += data;
});
if (response.statusCode >= 400 && response.statusCode <= 600) {
error(json.response.message, json);
response.on('end', () => {
var reply = JSON.parse(buffer);
switch (response.statusCode) {
case 200:
resolve(reply.data);
break;
default:
reject(reply.response);
break;
}
else {
success(response, json);
}
});
response.on('error', (err) => {
reject(err);
});
});
request.on('error', function (err) {
error(err, { });
request.on('error', (err) => {
reject(err);
});
request.end(payload, 'utf8');
},
});
}
};
accountInfo: function () {
var payload = {
api_key: apiKey
};
// Dynamically generates methods to handle all of the API endpoints.
methods.forEach((methodName) => {
var variants = [ methodName ];
payload = JSON.stringify(payload);
variants.push(toCamelCase(methodName));
var request = http.request({
'port': 80,
'host': 'api.postageapp.com',
'method': 'POST',
'path': '/v.1.0/get_account_info.json',
'headers': {
'host': 'api.postageapp.com',
'content-type': 'application/json',
'user-agent': 'PostageApp Node.js ' + postageVersion + ' (Node.js ' + process.version + ')',
'content-length': Buffer.byteLength(payload, 'utf8')
}
});
// Also aliases things like `get_a_thing` to `aThing` for convenience.
if (methodName.match(/^get_/)) {
var sansGet = methodName.replace(/^get_/, '');
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
variants.push(sansGet);
variants.push(toCamelCase(sansGet));
}
request.end(payload);
},
var proc = function(args, uid) {
return this.send(this.call(methodName, args, uid));
};
messageStatus: function (options) {
var desiredUID = options.desiredUID;
variants.forEach((name) => {
PostageApp.prototype[name] = proc;
});
})
var payload = {
api_key: apiKey,
uid: desiredUID,
};
// == Exports ===============================================================
payload = JSON.stringify(payload);
var request = http.request({
'port': 80,
'host': 'api.postageapp.com',
'method': 'POST',
'path': '/v.1.0/message_status.json',
'headers': {
'host': 'api.postageapp.com',
'content-type': 'application/json',
'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.js ' + process.version + ')',
'content-length': Buffer.byteLength(payload, 'utf8')
}
});
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
request.end(payload);
}
}
};
module.exports = PostageApp;
{
"name": "postageapp",
"version": "1.1.5",
"node": ">= 0.6.0",
"version": "2.0.0",
"node": ">= 6.0.0",
"homepage": "https://github.com/postageapp/postageapp-nodejs",

@@ -19,6 +19,2 @@ "repository": {

"url": "http://postageapp.com/"
},
{
"name": "Jon Lim",
"email": "jon@postageapp.com"
}

@@ -28,3 +24,3 @@ ],

"license": "MIT",
"main": "index",
"main": "lib/postageapp",
"scripts": {

@@ -36,3 +32,8 @@ "test": "mocha"

"mocha": "^3.1.2"
},
"dependencies": {
"bluebird": "^3.4.6",
"commander": "^2.9.0",
"uuid": "^2.0.3"
}
}

@@ -14,2 +14,7 @@ # PostageApp for Node.JS

This version of the library **requires Node v6** or more recent. For older
Node.js installations you can use a version from the 1.1.x branch. The
1.1.5 release should be current but uses Node.js callback methods instead of
promises.
### NPM

@@ -104,10 +109,8 @@

postageapp.sendMessage(options,
function (response, object) {
console.log('HTTP Status code: ', response.statusCode);
console.log('Message UID', object.response.uid);
}, function (err, object) {
console.log('Ack! An error has occurred: ', err);
}
);
postageapp.sendMessage(options).then((response) => {
console.log('HTTP Status code: ', response.statusCode);
console.log('Message UID', object.response.uid);
}).catch((error) => {
console.error(error);
});

@@ -119,3 +122,4 @@ ## Getting Account Info

var postageapp = require('postageapp')('ACCOUNT_API_KEY');
const PostageApp = require('postageapp');
var postageapp = new PostageApp('ACCOUNT_API_KEY');

@@ -135,18 +139,53 @@ postageapp.accountInfo();

var postageapp = require('postageapp')('ACCOUNT_API_KEY');
const PostageApp = require('postageapp');
var postageapp = new PostageApp('ACCOUNT_API_KEY');
postageapp.messageStatus(options);
postageapp.messageStatus({ uid: 'PREVIOUS_UID' }).then((status) => {
console.log(status);
});
The `options` parameter in the `messageStatus()` function is a hash that
contains one thing: the UID of the message you wish you retrieve:
You will receive a JSON string back from the API server that will look like:
var options = {
desiredUID: 'message UID here',
{"response":{"status":"ok","uid":"PREVIOUS_UID"},"data":{"message_status":{"completed":1}}}
For more information about formatting of recipients, templates and variables
please see the [PostageApp documentation](http://help.postageapp.com/kb/api/send_message).
## Configuration
### File Format
The configuration is read in from a JSON file that has this structure:
{
"host": "api.postageapp.com",
"port": "443",
"secure": secure,
"apiKey": "__PROJECT_API_KEY__"
}
You will receive a JSON string back from the API server that will look like:
The only one that's required is the `apiKey` value, the others are optional
and will default to what's shown here.
{"response":{"status":"ok","uid":"message UID here"},"data":{"message_status":{"completed":1}}}
### Environment Variables
For more information about formatting of recipients, templates and variables
please see the [PostageApp documentation](http://help.postageapp.com/kb/api/send_message).
You can specify the configuration for your PostageApp account via a series of
environment variables, in a config file, or in a config file path expressed as
an environment variable:
* `POSTAGEAPP_CONFIG` - Path to a JSON configuration file that will be loaded.
* `POSTAGEPPP_API_KEY` - Which API key to use by default.
* `POSTAGEAPP_HOST` - Which host to contact for the API (default `api.postageapp.com`)
* `POSTAGEAPP_PORT` - Which port to contact for the API (default 443)
* `POSTAGEAPP_SECURE` - Use HTTPS to connect to the API (default 1)
Any of these settings can be overruled by constructing a `PostageApp`
instance with different options. For example, if you're sending calls through
a custom HTTP proxy:
var postageapp = new PostageApp({
host: 'my.proxy',
port: 8080,
secure: false,
apiKey: '__PROJECT_API_KEY'
});

@@ -1,62 +0,65 @@

var assert = require('chai').assert;
// == Imports ===============================================================
var settings = require('./config/test.json');
var postageapp = require('../lib/postageapp')(settings.apikey);
const helpers = require('./helpers');
describe('postageapp', function () {
this.timeout(5000);
// == Tests =================================================================
describe('version', function() {
it('should return a version string', function() {
assert.equal('string', typeof(postageapp.version()));
});
});
// describe('postageapp', function () {
// var postageapp = require('../lib/postageapp')(apiKey);
describe('sendMessage', function () {
it('should get a successful response back from the server', function (done) {
postageapp.sendMessage({
content: "hello world",
recipients: 'test@null.postageapp.com'
}, function(throwaway, r) {
assert.equal('ok', r.response.status);
done();
});
});
// this.timeout(5000);
it('should invoke the error callback for a bogus request', function(done) {
postageapp.sendMessage({}, function() {}, function(err, r) {
assert.equal('precondition_failed', r.response.status);
done();
});
});
// describe('version', function() {
// it('should return a version string', function() {
// assert.equal('string', typeof(postageapp.version()));
// });
// });
it('should work with utf8-encoded content', function(done) {
postageapp.sendMessage({
content: "hello snowman ☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃",
recipients: "test@null.postageapp.com"
}, function(throwaway, r) {
assert.equal('ok', r.response.status);
done();
}, function(err) {
console.log(err);
assert(false, 'should not have received an error')
done();
});
});
// describe('sendMessage', function () {
// it('should get a successful response back from the server', function (done) {
// postageapp.sendMessage({
// content: "hello world",
// recipients: 'test@null.postageapp.com'
// }, function(throwaway, r) {
// assert.equal('ok', r.response.status);
// done();
// });
// });
it('should support recipient override', function(done) {
postageapp.sendMessage({
content: 'recipient override test',
recipients: 'test@null.postageapp.com',
recipient_override: 'test2@null.postageapp.com'
}, function (throwaway, r) {
assert.equal('ok', r.response.status);
done();
}, function (err) {
console.log(err);
assert(false, 'should not have received an error');
done();
});
});
});
});
// it('should invoke the error callback for a bogus request', function(done) {
// postageapp.sendMessage({}, function() {}, function(err, r) {
// assert.equal('precondition_failed', r.response.status);
// done();
// });
// });
// it('should work with utf8-encoded content', function(done) {
// postageapp.sendMessage({
// content: "hello snowman ☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃",
// recipients: "test@null.postageapp.com"
// }, function(throwaway, r) {
// assert.equal('ok', r.response.status);
// done();
// }, function(err) {
// console.log(err);
// assert(false, 'should not have received an error')
// done();
// });
// });
// it('should support recipient override', function(done) {
// postageapp.sendMessage({
// content: 'recipient override test',
// recipients: 'test@null.postageapp.com',
// recipient_override: 'test2@null.postageapp.com'
// }, function (throwaway, r) {
// assert.equal('ok', r.response.status);
// done();
// }, function (err) {
// console.log(err);
// assert(false, 'should not have received an error');
// done();
// });
// });
// });
// });

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc