1
0
mirror of https://github.com/eleith/emailjs.git synced 2024-07-03 11:38:50 +00:00

strongly type functions

This commit is contained in:
Zack Schuster 2018-06-28 20:44:54 -07:00
parent f81c7152ca
commit 03b7e9a9d0
4 changed files with 27 additions and 31 deletions

View File

@ -16,11 +16,12 @@ class Client {
* @property {string[]} [authentication]
*
* @typedef {Object} MessageStack
* @property {Function} [callback]
* @property {Message} message
* @property {function(Error, Message): void} [callback]
* @property {Message} [message]
* @property {string} [returnPath]
* @property {string} [from]
* @property {Array} [to]
* @property {string} [subject]
* @property {string|Array} [to]
* @property {Array} [cc]
* @property {Array} [bcc]
* @property {string} [text]
@ -122,7 +123,7 @@ class Client {
/**
* @param {Message|MessageStack} msg msg
* @param {Function} callback callback
* @param {function(Error, MessageStack): void} callback callback
* @returns {void}
*/
send(msg, callback) {
@ -145,7 +146,8 @@ class Client {
msg instanceof Message ? msg : canMakeMessage(msg) ? create(msg) : null;
if (message == null) {
callback(new Error('message is not a valid Message instance'), msg);
// prettier-ignore
callback(new Error('message is not a valid Message instance'), /**@type {MessageStack}*/(msg));
return;
}
@ -178,7 +180,8 @@ class Client {
this.queue.push(stack);
this._poll();
} else {
callback(new Error(why), msg);
// prettier-ignore
callback(new Error(why), /**@type {MessageStack}*/(msg));
}
});
}
@ -211,8 +214,8 @@ class Client {
/**
* @param {MessageStack} stack stack
* @param {Function} next next
* @returns {Function} callback
* @param {function(MessageStack): void} next next
* @returns {function(Error): void} callback
*/
_sendsmtp(stack, next) {
/**
@ -245,7 +248,8 @@ class Client {
* @returns {void}
*/
_sendrcpt(stack) {
const to = stack.to.shift().address;
//prettier-ignore
const to = /** @type{Array} */(stack.to).shift().address;
this.smtp.rcpt(
this._sendsmtp(stack, stack.to.length ? this._sendrcpt : this._senddata),
'<' + to + '>'

View File

@ -208,7 +208,7 @@ class Message {
}
/**
* @param {Function} callback the function to call with the error and buffer
* @param {function(Error, string): void} callback the function to call with the error and buffer
* @returns {void}
*/
read(callback) {
@ -298,7 +298,7 @@ class MessageStream extends Stream {
* @param {string} boundary the boundary text between outputs
* @param {MessageAttachment[]} list the list of potential messages to output
* @param {number} index the index of the list item to output
* @param {Function} callback the function to call if index is greater than upper bound
* @param {function(): void} callback the function to call if index is greater than upper bound
* @returns {void}
*/
const output_message = (boundary, list, index, callback) => {
@ -353,15 +353,9 @@ class MessageStream extends Stream {
output(data.concat([CRLF]).join(''));
};
/**
* @callback NextFn
* @param {NodeJS.ErrnoException} err
* @returns {void}
*/
/**
* @param {MessageAttachment} attachment the metadata to use as headers
* @param {NextFn} callback the function to call after output is finished
* @param {function(): void} callback the function to call after output is finished
* @returns {void}
*/
const output_attachment = (attachment, callback) => {
@ -376,7 +370,7 @@ class MessageStream extends Stream {
/**
* @param {MessageAttachment} attachment the metadata to use as headers
* @param {Function} callback the function to call after output is finished
* @param {function(): void} callback the function to call after output is finished
* @returns {void}
*/
const output_data = (attachment, callback) => {
@ -390,7 +384,7 @@ class MessageStream extends Stream {
/**
* @param {MessageAttachment} attachment the metadata to use as headers
* @param {NextFn} next the function to call when the file is closed
* @param {function(NodeJS.ErrnoException): void} next the function to call when the file is closed
* @returns {void}
*/
const output_file = (attachment, next) => {
@ -399,7 +393,7 @@ class MessageStream extends Stream {
const closed = fd => fs.closeSync(fd);
/**
* @param {*} err the error to emit
* @param {Error} err the error to emit
* @param {number} fd the file descriptor
* @returns {void}
*/
@ -449,7 +443,7 @@ class MessageStream extends Stream {
/**
* @param {MessageAttachment} attachment the metadata to use as headers
* @param {Function} callback the function to call after output is finished
* @param {function(): void} callback the function to call after output is finished
* @returns {void}
*/
const output_stream = (attachment, callback) => {
@ -496,7 +490,7 @@ class MessageStream extends Stream {
/**
* @param {string} data the data to output as base64
* @param {Function} [callback] the function to call after output is finished
* @param {function(): void} [callback] the function to call after output is finished
* @returns {void}
*/
const output_base64 = (data, callback) => {
@ -533,7 +527,7 @@ class MessageStream extends Stream {
/**
* @param {Message} message the message to output
* @param {Function} callback the function to call after output is finished
* @param {function(): void} callback the function to call after output is finished
* @returns {void}
*/
const output_alternative = (message, callback) => {
@ -561,7 +555,7 @@ class MessageStream extends Stream {
/**
* @param {MessageAttachment} message the message to output
* @param {Function} callback the function to call after output is finished
* @param {function(): void} callback the function to call after output is finished
* @returns {void}
*/
const output_related = (message, callback) => {
@ -618,7 +612,7 @@ class MessageStream extends Stream {
/**
* @param {string} data the data to output
* @param {Function} [callback] the function
* @param {function(...args): void} [callback] the function
* @param {*[]} [args] array of arguments to pass to the callback
* @returns {void}
*/

View File

@ -6,7 +6,7 @@ class SMTPResponse {
* @constructor
* @param {NodeJS.Socket | TLSSocket} stream the open socket to stream a response from
* @param {number} timeout the time to wait (in milliseconds) before closing the socket
* @param {Function} onerror the function to call on error
* @param {function(Error): void} onerror the function to call on error
*/
constructor(stream, timeout, onerror) {
let buffer = '';
@ -132,7 +132,7 @@ exports.SMTPResponse = SMTPResponse;
/**
* @param {NodeJS.Socket | TLSSocket} stream the open socket to stream a response from
* @param {number} timeout the time to wait (in milliseconds) before closing the socket
* @param {Function} onerror the function to call on error
* @param {function(Error): void} onerror the function to call on error
* @returns {SMTPResponse} the smtp response
*/
exports.monitor = (stream, timeout, onerror) =>

View File

@ -3,18 +3,16 @@ const assert = require('assert');
describe('Connect to wrong email server', function() {
const emailModulePath = require.resolve(path.join(__dirname, '..', 'email'));
let email;
beforeEach(function() {
if (require.cache[emailModulePath]) {
delete require.cache[emailModulePath];
}
email = require('../email');
});
it('Should not call callback multiple times with wrong server configuration', function(done) {
this.timeout(5000);
const server = email.server.connect({ host: 'bar.baz' });
const server = require(emailModulePath).server.connect({ host: 'bar.baz' });
server.send(
{
from: 'foo@bar.baz',