- add typedef for sendmail package

This commit is contained in:
Ostad 2019-03-05 13:04:15 -05:00
parent e618f3e1ef
commit a07c515fbd
4 changed files with 117 additions and 0 deletions

70
types/sendmail/index.d.ts vendored Normal file
View File

@ -0,0 +1,70 @@
// Type definitions for sendmail 1.4
// Project: https://github.com/guileen/node-sendmail
// Definitions by: My Self <https://github.com/me>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/*~ If this module is a UMD module that exposes a global variable 'myLib' when
*~ loaded outside a module loader environment, declare that global here.
*~ Otherwise, delete this declaration.
*/
declare namespace sendMailConstructor {
export interface IOptions {
logger?: {
debug?: () => void;
info?: () => void;
warn?: () => void;
error?: () => void;
};
silent?: boolean;
/** Default: False */
dkim?:
| boolean
| {
privateKey: string;
keySelector: string;
};
/** Default: False */
devPort?: number | boolean;
/** Default: localhost */
devHost?: string;
/** Default: 25 */
smtpPort?: number;
/** Default: -1 - extra smtp host after resolveMX */
smtpHost?: string | number;
}
export interface IMailInput {
from: string;
to: string;
cc?: string;
bcc?: string;
replyTo?: string;
returnTo?: string;
subject: string;
type?: string;
charset?: string;
encoding?: string;
id?: string;
headers?: object;
content?: string;
html?: string;
attachments?: {
type: string;
filename: string;
content: any;
}[];
}
}
type CallbackFn = (err: Error, domain: string) => void;
type SendMailFn = (
mail: sendMailConstructor.IMailInput,
callback: CallbackFn
) => void;
declare function sendMailConstructor(
options: sendMailConstructor.IOptions
): SendMailFn;
export = sendMailConstructor;

View File

@ -0,0 +1,24 @@
import sendmail = require("sendmail");
const emailSender = sendmail({
silent: false
});
const sendEmail = (options: sendmail.IMailInput): Promise<boolean> =>
new Promise((resolve, reject) => {
emailSender(options, (err, reply) => {
// if error happened or returned code is now started with 2**
if (err || !reply.startsWith("2")) {
reject(err);
} else {
resolve(true);
}
});
});
sendEmail({
from: "Test Mail <noreply@mydomain.com>",
to: "test@mydomain.com",
subject: "First Test",
html: "This is a Test message!"
});

View File

@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"sendmail-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }