diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 71f35228b2..88fb5266b1 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -335,6 +335,7 @@ All definitions files include a header with the author and editors, so at some p * [Selenium WebDriverJS](https://code.google.com/p/selenium/) (by [Bill Armstrong](https://github.com/BillArmstrong)) * [Semver](https://github.com/isaacs/node-semver) (by [Bart van der Schoor](https://github.com/Bartvds)) * [Sencha Touch](http://www.sencha.com/products/touch/) (by [Brian Kotek](https://github.com/brian428)) +* [sendgrid](https://github.com/sendgrid/sendgrid-nodejs) (by [Maxime LUCE](https://github.com/SomaticIT)) * [SharePoint](http://sptypescript.codeplex.com) (by [Stanislav Vyshchepan](http://gandjustas.blogspot.ru) and [Andrey Markeev](http://markeev.com)) * [ShellJS](http://shelljs.org) (by [Niklas Mollenhauer](https://github.com/nikeee)) * [Showdown](https://github.com/coreyti/showdown) (by [Chris Bowdon](https://github.com/cbowdon)) diff --git a/sendgrid/sendgrid-tests.ts b/sendgrid/sendgrid-tests.ts new file mode 100644 index 0000000000..e7d34feb08 --- /dev/null +++ b/sendgrid/sendgrid-tests.ts @@ -0,0 +1,246 @@ +/** + * Test suite created by Maxime LUCE + * + * Created by using code samples from https://github.com/sendgrid/sendgrid-nodejs#usage + */ + +/// +/// + +import sg = require("sendgrid"); +var sendgrid = sg("api_user", "api_key"); + +/* + * Simple Usage + */ +var payload = { + to: 'to@example.com', + from: 'from@other.com', + subject: 'Saying Hi', + text: 'This is my first email through SendGrid' +}; +sendgrid.send(payload, function (err, json) { + if (err) { + console.error(err); + } + + console.log(json); +}); + +/** + * Email: + * https://github.com/sendgrid/sendgrid-nodejs#email + */ +var email = new sendgrid.Email({ + to: 'foo@bar.com', + from: 'you@yourself.com', + subject: 'Subject goes here', + text: 'Hello world' +}); + +sendgrid.send(email, function (err, json) { + if (err) { + return console.error(err); + } + console.log(json); +}); + +/** + * Setting Params + * https://github.com/sendgrid/sendgrid-nodejs#setting-params + */ +var email = new sendgrid.Email({ to: 'person@email.com' }); +email.to = "different@email.com"; +email.replyto = "reply-here@email.com"; +email.subject = "This is a subject"; + +/** + * addTo + * https://github.com/sendgrid/sendgrid-nodejs#addto + */ +var email = new sendgrid.Email(); +email.addTo('foo@bar.com'); +email.addTo('another@another.com'); +sendgrid.send(email, function (err, json) { }); + + +/** + * setFrom + * https://github.com/sendgrid/sendgrid-nodejs#setfrom + */ +var email = new sendgrid.Email(); +email.setFrom('foo@bar.com'); +sendgrid.send(email, function (err, json) { }); + +/** + * setSubject + * https://github.com/sendgrid/sendgrid-nodejs#setsubject + */ +var email = new sendgrid.Email(); +email.setSubject('Some subject'); +sendgrid.send(email, function (err, json) { }); + +/** + * setText + * https://github.com/sendgrid/sendgrid-nodejs#settext + */ +var email = new sendgrid.Email(); +email.setText('Some text'); +sendgrid.send(email, function (err, json) { }); + +/** + * setHtml + * https://github.com/sendgrid/sendgrid-nodejs#sethtml + */ +var email = new sendgrid.Email(); +email.setHtml('

Some html

'); +sendgrid.send(email, function (err, json) { }); + +/** + * addHeader + * https://github.com/sendgrid/sendgrid-nodejs#addheader + */ +var email = new sendgrid.Email(); +email.setHeaders({ full: 'hearts' }); // headers = {full: 'hearts'} +email.addHeader('spin', 'attack'); // headers = {full: 'hearts', spin: 'attack'} +email.addHeader('mask', 'salesman'); // headers = {full: 'hearts', spin: 'attack', mask: 'salesman'} +sendgrid.send(email, function (err, json) { }); + +/** + * setHeaders + * https://github.com/sendgrid/sendgrid-nodejs#setheaders + */ +var email = new sendgrid.Email(); +email.setHeaders({ full: 'hearts' }); // headers = {full: 'hearts'} +email.setHeaders({ mask: 'salesman' }); // headers = {mask: 'salesman'} +sendgrid.send(email, function (err, json) { }); + +/** + * addSubstitution + * https://github.com/sendgrid/sendgrid-nodejs#addsubstitution + */ +var email = new sendgrid.Email(); +email.addSubstitution('keep', 'secret'); // sub = {keep: ['secret']} +email.addSubstitution('other', ['one', 'two']); // sub = {keep: ['secret'], other: ['one', 'two']} + +/** + * setSubstitutions + * https://github.com/sendgrid/sendgrid-nodejs#setsubstitutions + */ +var email = new sendgrid.Email(); +email.setSubstitutions({ keep: ['secret'], other: ['one', 'two'] }); // sub = {keep: ['secret'], other: ['one', 'two']}); + +/** + * addSection + * https://github.com/sendgrid/sendgrid-nodejs#addsection + */ +var email = new sendgrid.Email(); +email.addSection({ '-charge-': 'This ship is useless.' }); // section = {'-charge-': 'This ship is useless.'} + +/** + * setSections + * https://github.com/sendgrid/sendgrid-nodejs#setsections + */ +var email = new sendgrid.Email(); +email.setSections({ '-charge-': 'This ship is useless.' }); // section = {'-charge-': 'This ship is useless.'} + +/** + * addUniqueArg + * https://github.com/sendgrid/sendgrid-nodejs#adduniquearg + */ +var email = new sendgrid.Email(); +email.setUniqueArgs({ cow: 'chicken' }); // unique_args = {cow: 'chicken'} +email.addUniqueArg({ cat: 'dog' }); // unique_args = {cow: 'chicken', cat: 'dog'} + +/** + * setUniqueArgs + * https://github.com/sendgrid/sendgrid-nodejs#setuniqueargs + */ +var email = new sendgrid.Email(); +email.setUniqueArgs({ cow: 'chicken' }); // unique_args = {cow: 'chicken'} +email.setUniqueArgs({ dad: 'proud' }); // unique_args = {dad: 'proud'} + + +/** + * addCategory + * https://github.com/sendgrid/sendgrid-nodejs#addcategory + */ +var email = new sendgrid.Email(); +email.addCategory('tactics'); // category = ['tactics'] +email.addCategory('advanced'); // category = ['tactics', 'advanced'] + +/** + * setCategories + * https://github.com/sendgrid/sendgrid-nodejs#setcategories + */ +var email = new sendgrid.Email(); +email.setCategories(['tactics']); // category = ['tactics'] +email.setCategories(['snowball-fight']); // category = ['snowball-fight'] + +/** + * addFilter + * https://github.com/sendgrid/sendgrid-nodejs#addfilter + */ +var email = new sendgrid.Email(); +email.addFilter('footer', 'enable', 1); +email.addFilter('footer', 'text/html', 'boo'); + +/** + * setFilters + * https://github.com/sendgrid/sendgrid-nodejs#setfilters + */ +var email = new sendgrid.Email(); +var email = new sendgrid.Email(); +email.setFilters({ + 'footer': { + 'setting': { + 'enable': 1, + 'text/plain': 'You can haz footers!' + } + } +}); + +/** + * addFile + * https://github.com/sendgrid/sendgrid-nodejs#addfile + */ +var email = new sendgrid.Email(); +email.addFile({ + filename: 'secret.txt', + content: new Buffer('You will never know....') +}); +// or +email.addFile({ + filename: 'icon.jpg', + url: 'http://i.imgur.com/2fDh8.jpg' +}); +// or +email.addFile({ + path: '../files/resume.txt' +}); +// or +email.addFile({ + cid: 'the_logo', // should match cid value in html + path: '../files/logo.png' +}); +email.setHtml('
Our logo:
'); + +/** + * Options - Changing URL + * https://github.com/sendgrid/sendgrid-nodejs#changing-url + */ +var sendgrid1 = sg('username', 'password', { "protocol": "http", "host": "sendgrid.org", "endpoint": "/send", "port": "80" }); +var sendgrid2 = sg('username', 'password', { "uri": "http://sendgrid.org:80/send" }); + +/** + * Options - Request + * https://github.com/sendgrid/sendgrid-nodejs#request + */ +var sendgrid3 = sg('username', 'password', { proxy: "http://localproxy:3128" }); +// or +import https = require('https'); +var agent = new https.Agent(); +agent.maxSockets = 500; // Set Max Sockets to 500 +var sendgrid4 = sg('username', 'password', { web: { pool: agent } }); + + diff --git a/sendgrid/sendgrid.d.ts b/sendgrid/sendgrid.d.ts new file mode 100644 index 0000000000..7f644b65b0 --- /dev/null +++ b/sendgrid/sendgrid.d.ts @@ -0,0 +1,169 @@ +// Type definitions for sendgrid 1.1.0 +// Project: https://github.com/sendgrid/sendgrid-nodejs +// Definitions by: Maxime LUCE +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module Sendgrid { + //#region Options + + export interface UriParts { + protocol: string; + host: string; + port: string; + endpoint: string; + } + + export interface Options { + protocol?: string; + host?: string; + port?: string; + endpoint?: string; + uri?: string; + proxy?: string; + web?: { + pool?: any; + } + } + + export interface OptionsExport { + uriParts: UriParts; + uri: string; + + proxy?: string; + web?: { + pool?: any; + } + } + + //#endregion + + //#region Email + + export interface EmailOptions { + to?: any; + toname?: string; + from?: string; + fromname?: string; + subject?: string; + text?: string; + html?: string; + bcc?: any; + replyto?: string; + date?: Date; + headers?: { [key: string]: string }; + files?: FileHandlerOptions[]; + smtpapi?: any; + } + + export class Email { + to: any; + toname: string; + from: string; + fromname: string; + subject: string; + text: string; + html: string; + bcc: any; + replyto: string; + date: Date; + headers: { [key: string]: string }; + files: FileHandler[]; + smtpapi: any; + + constructor(); + constructor(options: EmailOptions); + + addTo(address: string): void; + addHeader(type: string, value: string): void; + addSubstitution(type: string, value: string): void; + addSubstitution(type: string, value: string[]): void; + addSection(section: { [key: string]: string }): void; + addUniqueArg(uarg: { [key: string]: string }): void; + addCategory(category: string): void; + addFilter(filter: string, command: string, value: number): void; + addFilter(filter: string, command: string, value: string): void; + addFile(file: FileHandlerOptions): void; + + setFrom(address: string): void; + setSubject(subject: string): void; + setText(text: string): void; + setHtml(html: string): void; + setHeaders(headers: { [key: string]: string }): void; + setSubstitutions(substitutions: { [key: string]: string[] }): void; + setSections(sections: { [key: string]: string }): void; + setUniqueArgs(uargs: { [key: string]: string }): void; + setCategories(categories: string[]): void; + setFilters(filters: any): void; + } + + //#endregion + + //#region FileHandler + + export interface FileHandlerOptions { + filename?: string; + contentType?: string; + cid?: string; + path?: string; + url?: string; + content?: any; + } + + export class FileHandler { + filename: string; + contentType: string; + cid: string; + + type: string; + content: string; + path: string; + url: string; + + constructor(options: FileHandlerOptions); + + loadContent(callback: HandlerCallback): void; + + static handlers: { + content: Handler; + path: Handler; + url: Handler; + none: Handler; + }; + } + + export interface Handler { + (file: FileHandler, callback: HandlerCallback): void; + } + + export interface HandlerCallback { + (hasError: boolean, error: Error): void; + (hasError: boolean, error: string): void; + } + + //#endregion + + //#region Sendgrid Class + + interface Constructor { + (api_user: string, api_key: string, options?: Options): Instance; + new (api_user: string, api_key: string, options?: Options): Instance; + } + + export interface Instance { + version: string; + api_user: string; + api_key: string; + options: OptionsExport; + Email: typeof Email; + + send(email: EmailOptions, callback: (err: Error, json: any) => any): void; + send(email: Email, callback: (err: Error, json: any) => any): void; + } + + //#endregion +} + +declare module "sendgrid" { + var ctor: Sendgrid.Constructor; + export = ctor; +} \ No newline at end of file