Upload files to ''

This commit is contained in:
nikkel 2020-03-20 02:37:55 +07:00
parent 91f10d9c2d
commit 1e08b68429
5 changed files with 185 additions and 0 deletions

43
Client.js Normal file
View File

@ -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;

14
Message.js Normal file
View File

@ -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;

74
SendMessage.js Normal file
View File

@ -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;

28
SendRawMessage.js Normal file
View File

@ -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;

26
SendResult.js Normal file
View File

@ -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;