From 1e08b68429243740434d147a606e8a873c9ff373 Mon Sep 17 00:00:00 2001 From: nikkel Date: Fri, 20 Mar 2020 02:37:55 +0700 Subject: [PATCH] Upload files to '' --- Client.js | 43 +++++++++++++++++++++++++++ Message.js | 14 +++++++++ SendMessage.js | 74 +++++++++++++++++++++++++++++++++++++++++++++++ SendRawMessage.js | 28 ++++++++++++++++++ SendResult.js | 26 +++++++++++++++++ 5 files changed, 185 insertions(+) create mode 100644 Client.js create mode 100644 Message.js create mode 100644 SendMessage.js create mode 100644 SendRawMessage.js create mode 100644 SendResult.js diff --git a/Client.js b/Client.js new file mode 100644 index 0000000..83dbb2d --- /dev/null +++ b/Client.js @@ -0,0 +1,43 @@ +var Promise = require('promise'); +var https = require('https'); +var concatStream = require('concat-stream'); + +function Client(host, serverKey) { + this.host = host; + this.serverKey = serverKey; +} + +Client.prototype.makeRequest = function makeRequest(controller, action, parameters) { + return new Promise(function (resolve, reject) { + var data = JSON.stringify(parameters); + + var request = https.request({ + headers: { + 'Content-Type': 'application/json', + 'X-Server-API-Key': this.serverKey + }, + host: this.host, + method: 'POST', + path: '/api/v1/' + controller + '/' + action + }, function (response) { + response.pipe(concatStream(function (content) { + var json = JSON.parse(content); + if (json.status === 'success') { + resolve(json.data); + } else { + reject(json.data); + } + })); + }); + + request.on('error', function (error) { + reject(error); + }); + + request.write(data); + + request.end(); + }.bind(this)); +}; + +module.exports = Client; diff --git a/Message.js b/Message.js new file mode 100644 index 0000000..bc0341e --- /dev/null +++ b/Message.js @@ -0,0 +1,14 @@ +function Message(client, attributes) { + this.client = client; + this.attributes = attributes; +} + +Message.prototype.id = function id() { + return this.attributes.id; +} + +Message.prototype.token = function token() { + return this.attributes.token; +} + +module.exports = Message; diff --git a/SendMessage.js b/SendMessage.js new file mode 100644 index 0000000..ccbe2ea --- /dev/null +++ b/SendMessage.js @@ -0,0 +1,74 @@ +var SendResult = require('./SendResult'); + +function SendMessage(client) { + this.attributes = { + to: [], + cc: [], + bcc: [], + headers: {}, + attachments: [] + }; + this.client = client; +} + +SendMessage.prototype.to = function to(address) { + this.attributes.to.push(address); +}; + +SendMessage.prototype.cc = function cc(address) { + this.attributes.cc.push(address); +}; + +SendMessage.prototype.bcc = function bcc(address) { + this.attributes.bcc.push(address); +}; + +SendMessage.prototype.from = function from(address) { + this.attributes.from = address; +}; + +SendMessage.prototype.sender = function sender(address) { + this.attributes.sender = address; +}; + +SendMessage.prototype.subject = function subject(_subject) { + this.attributes.subject = _subject; +}; + +SendMessage.prototype.tag = function tag(_tag) { + this.attributes.tag = _tag; +}; + +SendMessage.prototype.replyTo = function replyTo(_replyTo) { + this.attributes.reply_to = _replyTo; +}; + +SendMessage.prototype.plainBody = function plainBody(content) { + this.attributes.plain_body = content; +}; + +SendMessage.prototype.htmlBody = function htmlBody(content) { + this.attributes.html_body = content; +}; + +SendMessage.prototype.header = function header(key, value) { + this.attributes.headers[key] = value; +}; + +SendMessage.prototype.attach = function attach(filename, contentType, data) { + var attachment = { + content_type: contentType, + data: new Buffer(data).toString('base64'), + name: filename + }; + this.attributes.attachments.push(attachment); +}; + +SendMessage.prototype.send = function send() { + return this.client.makeRequest('send', 'message', this.attributes) + .then(function (result) { + return new SendResult(this.client, result); + }.bind(this)); +}; + +module.exports = SendMessage; diff --git a/SendRawMessage.js b/SendRawMessage.js new file mode 100644 index 0000000..79c796c --- /dev/null +++ b/SendRawMessage.js @@ -0,0 +1,28 @@ +var SendResult = require('./SendResult'); + +function SendRawMessage(client) { + this.attributes = {}; + this.client = client; +} + +SendRawMessage.prototype.mailFrom = function mailFrom(address) { + this.attributes.mail_from = address; +}; + +SendRawMessage.prototype.rcptTo = function rcptTo(address) { + this.attributes.rcpt_to = (this.attributes.rcpt_to || []); + this.attributes.rcpt_to.push(address); +}; + +SendRawMessage.prototype.data = function data(content) { + this.attributes.data = new Buffer(content).toString('base64'); +}; + +SendRawMessage.prototype.send = function send(callback) { + return this.client.makeRequest('send', 'raw', this.attributes) + .then(function (result) { + return new SendResult(this.client, result); + }.bind(this)); +}; + +module.exports = SendRawMessage; diff --git a/SendResult.js b/SendResult.js new file mode 100644 index 0000000..99e111a --- /dev/null +++ b/SendResult.js @@ -0,0 +1,26 @@ +var Message = require('./Message'); + +function SendResult(client, result) { + this.client = client; + this.result = result; +} + +SendResult.prototype.recipients = function recipients() { + var messages; + + if (!this._recipients) { + this._recipients = {}; + messages = this.result.messages; + for (var key in messages) { + this._recipients[key.toLowerCase()] = new Message(this.client, messages[key]); + } + } + + return this._recipients; +}; + +SendResult.prototype.size = function size() { + return this.recipients.length; +}; + +module.exports = SendResult;