From e49c09a129662daf97e7964257fac64a3906178d Mon Sep 17 00:00:00 2001 From: SomaticIT Date: Mon, 15 Sep 2014 00:09:11 +0200 Subject: [PATCH 1/7] Add definitions for sendgrid sdk for node --- sendgrid/sendgrid.d.ts | 122 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 sendgrid/sendgrid.d.ts diff --git a/sendgrid/sendgrid.d.ts b/sendgrid/sendgrid.d.ts new file mode 100644 index 000000000..96d52601e --- /dev/null +++ b/sendgrid/sendgrid.d.ts @@ -0,0 +1,122 @@ +// Type definitions for sendgrid 1.1.0 +// Project: https://github.com/sendgrid/sendgrid-nodejs +// Definitions by: Maxime LUCE +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +interface SendgridUriParts { + protocol: string; + host: string; + port: string; + endpoint: string; +} + +interface SendgridOptions { + protocol?: string; + host?: string; + port?: string; + endpoint?: string; + uri?: string; +} + +interface SendgridOptionsExport { + uriParts: SendgridUriParts; + uri: string; +} + +interface SendgridEmailOptions { + 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?: SendgridEmailFileOptions[]; + smtpapi?: any; +} + +declare class PrivateSendgridEmail { + 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: PrivateSendgridFile[]; + smtpapi: any; + + constructor(); + constructor(options: SendgridEmailOptions); + + 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: SendgridEmailFileOptions): 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; +} + +interface SendgridEmailFileOptions { + filename?: string; + contentType?: string; + cid?: string; + path?: string; + url?: string; + content?: any; +} + +declare class PrivateSendgridFile { + filename: string; + contentType: string; + cid: string; + + type: string; + content: string; + path: string; + url: string; + + loadContent(callback: (hasError: boolean, error: Error) => any): void; +} + +interface Sendgrid { + version: string; + api_user: string; + api_key: string; + options: SendgridOptionsExport; + Email: typeof PrivateSendgridEmail; + + send(email: PrivateSendgridEmail, callback: (err: Error, json: any) => any): void; +} + +declare module "sendgrid" { + interface SendgridConstructor { + (api_user: string, api_key: string, options?: SendgridOptions): Sendgrid; + new (api_user: string, api_key: string, options?: SendgridOptions): Sendgrid; + } + + export = SendgridConstructor; +} \ No newline at end of file From eb1d7786e2de5a11763e2360b25277540ebcf1f1 Mon Sep 17 00:00:00 2001 From: SomaticIT Date: Mon, 15 Sep 2014 18:13:02 +0200 Subject: [PATCH 2/7] Fix issue in sendgrid send method definition --- sendgrid/sendgrid.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/sendgrid/sendgrid.d.ts b/sendgrid/sendgrid.d.ts index 96d52601e..6d4974a03 100644 --- a/sendgrid/sendgrid.d.ts +++ b/sendgrid/sendgrid.d.ts @@ -109,6 +109,7 @@ interface Sendgrid { options: SendgridOptionsExport; Email: typeof PrivateSendgridEmail; + send(email: SendgridEmailOptions, callback: (err: Error, json: any) => any): void; send(email: PrivateSendgridEmail, callback: (err: Error, json: any) => any): void; } From c4a339b149a41136addf1f82a926e3ed0dd00c11 Mon Sep 17 00:00:00 2001 From: SomaticIT Date: Mon, 15 Sep 2014 18:13:16 +0200 Subject: [PATCH 3/7] Add tests suite for sendgrid --- sendgrid/sendgrid-tests.ts | 246 +++++++++++++++++++++++++++++++++++++ 1 file changed, 246 insertions(+) create mode 100644 sendgrid/sendgrid-tests.ts diff --git a/sendgrid/sendgrid-tests.ts b/sendgrid/sendgrid-tests.ts new file mode 100644 index 000000000..17a48dd99 --- /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: 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 +var https = require('https'); +var agent = new https.Agent(); +agent.maxSockets = 500; // Set Max Sockets to 500 +var sendgrid4 = sg('username', 'password', { web: { pool: agent } }); + + From 9953dfefde47178442e062ff890ee2959ef30998 Mon Sep 17 00:00:00 2001 From: SomaticIT Date: Mon, 15 Sep 2014 19:15:30 +0200 Subject: [PATCH 4/7] Add ghost module and fix module export issue --- sendgrid/sendgrid.d.ts | 248 ++++++++++++++++++++++++----------------- 1 file changed, 147 insertions(+), 101 deletions(-) diff --git a/sendgrid/sendgrid.d.ts b/sendgrid/sendgrid.d.ts index 6d4974a03..80b5d78ee 100644 --- a/sendgrid/sendgrid.d.ts +++ b/sendgrid/sendgrid.d.ts @@ -3,121 +3,167 @@ // Definitions by: Maxime LUCE // Definitions: https://github.com/borisyankov/DefinitelyTyped -interface SendgridUriParts { - protocol: string; - host: string; - port: string; - endpoint: string; -} +declare module Sendgrid { + //#region Options -interface SendgridOptions { - protocol?: string; - host?: string; - port?: string; - endpoint?: string; - uri?: string; -} + export interface UriParts { + protocol: string; + host: string; + port: string; + endpoint: string; + } -interface SendgridOptionsExport { - uriParts: SendgridUriParts; - uri: string; -} + export interface Options { + protocol?: string; + host?: string; + port?: string; + endpoint?: string; + uri?: string; + proxy?: string; + web?: { + pool?: any; + } + } -interface SendgridEmailOptions { - 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?: SendgridEmailFileOptions[]; - smtpapi?: any; -} + export interface OptionsExport { + uriParts: UriParts; + uri: string; -declare class PrivateSendgridEmail { - 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: PrivateSendgridFile[]; - smtpapi: any; + proxy?: string; + web?: { + pool?: any; + } + } - constructor(); - constructor(options: SendgridEmailOptions); + //#endregion - 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: SendgridEmailFileOptions): void; + //#region Email - 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; -} + 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?: SendgridEmailFileOptions[]; + smtpapi?: any; + } -interface SendgridEmailFileOptions { - filename?: string; - contentType?: string; - cid?: string; - path?: string; - url?: string; - content?: 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; -declare class PrivateSendgridFile { - filename: string; - contentType: string; - cid: string; + constructor(); + constructor(options: EmailOptions); - type: string; - content: string; - path: string; - url: string; + 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; - loadContent(callback: (hasError: boolean, error: Error) => any): 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; + } -interface Sendgrid { - version: string; - api_user: string; - api_key: string; - options: SendgridOptionsExport; - Email: typeof PrivateSendgridEmail; + //#endregion - send(email: SendgridEmailOptions, callback: (err: Error, json: any) => any): void; - send(email: PrivateSendgridEmail, callback: (err: Error, json: any) => any): void; + //#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" { - interface SendgridConstructor { - (api_user: string, api_key: string, options?: SendgridOptions): Sendgrid; - new (api_user: string, api_key: string, options?: SendgridOptions): Sendgrid; - } - - export = SendgridConstructor; + var ctor: Sendgrid.Constructor; + export = ctor; } \ No newline at end of file From 2f216035fd46070aa5828be445262ea6bef6f926 Mon Sep 17 00:00:00 2001 From: SomaticIT Date: Mon, 15 Sep 2014 19:16:13 +0200 Subject: [PATCH 5/7] Update tests to match definitions improvements --- sendgrid/sendgrid-tests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sendgrid/sendgrid-tests.ts b/sendgrid/sendgrid-tests.ts index 17a48dd99..e7d34feb0 100644 --- a/sendgrid/sendgrid-tests.ts +++ b/sendgrid/sendgrid-tests.ts @@ -8,7 +8,7 @@ /// import sg = require("sendgrid"); -var sendgrid: Sendgrid = sg("api_user", "api_key"); +var sendgrid = sg("api_user", "api_key"); /* * Simple Usage @@ -238,7 +238,7 @@ var sendgrid2 = sg('username', 'password', { "uri": "http://sendgrid.org:80/send */ var sendgrid3 = sg('username', 'password', { proxy: "http://localproxy:3128" }); // or -var https = require('https'); +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 } }); From e9e292d20d5c865da762c1e759313ff5607971ad Mon Sep 17 00:00:00 2001 From: SomaticIT Date: Mon, 15 Sep 2014 19:17:37 +0200 Subject: [PATCH 6/7] Fix issue in sendgrid EmailOptions --- sendgrid/sendgrid.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sendgrid/sendgrid.d.ts b/sendgrid/sendgrid.d.ts index 80b5d78ee..7f644b65b 100644 --- a/sendgrid/sendgrid.d.ts +++ b/sendgrid/sendgrid.d.ts @@ -51,7 +51,7 @@ declare module Sendgrid { replyto?: string; date?: Date; headers?: { [key: string]: string }; - files?: SendgridEmailFileOptions[]; + files?: FileHandlerOptions[]; smtpapi?: any; } From e032721081f8ebcf7b8b01899749bb480aad3623 Mon Sep 17 00:00:00 2001 From: SomaticIT Date: Tue, 16 Sep 2014 04:30:34 +0200 Subject: [PATCH 7/7] Add sendgrid to contributors.md --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index ef83ff093..b4441e234 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -324,6 +324,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))