1
0
mirror of https://github.com/eleith/emailjs.git synced 2024-07-05 20:10:37 +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] * @property {string[]} [authentication]
* *
* @typedef {Object} MessageStack * @typedef {Object} MessageStack
* @property {Function} [callback] * @property {function(Error, Message): void} [callback]
* @property {Message} message * @property {Message} [message]
* @property {string} [returnPath] * @property {string} [returnPath]
* @property {string} [from] * @property {string} [from]
* @property {Array} [to] * @property {string} [subject]
* @property {string|Array} [to]
* @property {Array} [cc] * @property {Array} [cc]
* @property {Array} [bcc] * @property {Array} [bcc]
* @property {string} [text] * @property {string} [text]
@ -122,7 +123,7 @@ class Client {
/** /**
* @param {Message|MessageStack} msg msg * @param {Message|MessageStack} msg msg
* @param {Function} callback callback * @param {function(Error, MessageStack): void} callback callback
* @returns {void} * @returns {void}
*/ */
send(msg, callback) { send(msg, callback) {
@ -145,7 +146,8 @@ class Client {
msg instanceof Message ? msg : canMakeMessage(msg) ? create(msg) : null; msg instanceof Message ? msg : canMakeMessage(msg) ? create(msg) : null;
if (message == 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; return;
} }
@ -178,7 +180,8 @@ class Client {
this.queue.push(stack); this.queue.push(stack);
this._poll(); this._poll();
} else { } 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 {MessageStack} stack stack
* @param {Function} next next * @param {function(MessageStack): void} next next
* @returns {Function} callback * @returns {function(Error): void} callback
*/ */
_sendsmtp(stack, next) { _sendsmtp(stack, next) {
/** /**
@ -245,7 +248,8 @@ class Client {
* @returns {void} * @returns {void}
*/ */
_sendrcpt(stack) { _sendrcpt(stack) {
const to = stack.to.shift().address; //prettier-ignore
const to = /** @type{Array} */(stack.to).shift().address;
this.smtp.rcpt( this.smtp.rcpt(
this._sendsmtp(stack, stack.to.length ? this._sendrcpt : this._senddata), this._sendsmtp(stack, stack.to.length ? this._sendrcpt : this._senddata),
'<' + to + '>' '<' + 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} * @returns {void}
*/ */
read(callback) { read(callback) {
@ -298,7 +298,7 @@ class MessageStream extends Stream {
* @param {string} boundary the boundary text between outputs * @param {string} boundary the boundary text between outputs
* @param {MessageAttachment[]} list the list of potential messages to output * @param {MessageAttachment[]} list the list of potential messages to output
* @param {number} index the index of the list item 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} * @returns {void}
*/ */
const output_message = (boundary, list, index, callback) => { const output_message = (boundary, list, index, callback) => {
@ -353,15 +353,9 @@ class MessageStream extends Stream {
output(data.concat([CRLF]).join('')); output(data.concat([CRLF]).join(''));
}; };
/**
* @callback NextFn
* @param {NodeJS.ErrnoException} err
* @returns {void}
*/
/** /**
* @param {MessageAttachment} attachment the metadata to use as headers * @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} * @returns {void}
*/ */
const output_attachment = (attachment, callback) => { const output_attachment = (attachment, callback) => {
@ -376,7 +370,7 @@ class MessageStream extends Stream {
/** /**
* @param {MessageAttachment} attachment the metadata to use as headers * @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} * @returns {void}
*/ */
const output_data = (attachment, callback) => { const output_data = (attachment, callback) => {
@ -390,7 +384,7 @@ class MessageStream extends Stream {
/** /**
* @param {MessageAttachment} attachment the metadata to use as headers * @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} * @returns {void}
*/ */
const output_file = (attachment, next) => { const output_file = (attachment, next) => {
@ -399,7 +393,7 @@ class MessageStream extends Stream {
const closed = fd => fs.closeSync(fd); 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 * @param {number} fd the file descriptor
* @returns {void} * @returns {void}
*/ */
@ -449,7 +443,7 @@ class MessageStream extends Stream {
/** /**
* @param {MessageAttachment} attachment the metadata to use as headers * @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} * @returns {void}
*/ */
const output_stream = (attachment, callback) => { const output_stream = (attachment, callback) => {
@ -496,7 +490,7 @@ class MessageStream extends Stream {
/** /**
* @param {string} data the data to output as base64 * @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} * @returns {void}
*/ */
const output_base64 = (data, callback) => { const output_base64 = (data, callback) => {
@ -533,7 +527,7 @@ class MessageStream extends Stream {
/** /**
* @param {Message} message the message to output * @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} * @returns {void}
*/ */
const output_alternative = (message, callback) => { const output_alternative = (message, callback) => {
@ -561,7 +555,7 @@ class MessageStream extends Stream {
/** /**
* @param {MessageAttachment} message the message to output * @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} * @returns {void}
*/ */
const output_related = (message, callback) => { const output_related = (message, callback) => {
@ -618,7 +612,7 @@ class MessageStream extends Stream {
/** /**
* @param {string} data the data to output * @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 * @param {*[]} [args] array of arguments to pass to the callback
* @returns {void} * @returns {void}
*/ */

View File

@ -6,7 +6,7 @@ class SMTPResponse {
* @constructor * @constructor
* @param {NodeJS.Socket | TLSSocket} stream the open socket to stream a response from * @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 {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) { constructor(stream, timeout, onerror) {
let buffer = ''; let buffer = '';
@ -132,7 +132,7 @@ exports.SMTPResponse = SMTPResponse;
/** /**
* @param {NodeJS.Socket | TLSSocket} stream the open socket to stream a response from * @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 {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 * @returns {SMTPResponse} the smtp response
*/ */
exports.monitor = (stream, timeout, onerror) => exports.monitor = (stream, timeout, onerror) =>

View File

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