如何使用Sendgrid.d.ts来使用sendgrid?

时间:2016-09-05 20:12:15

标签: node.js typescript compilation sendgrid typescript-typings

我遇到与Chris P with his question相同的问题,我对my issue here happened的原因了解得多。

我通过npm然后打字安装了sendgrid,我的node_modules有一个sendgrid目录,我的打包有一个global / sendgrid目录。

当我使用sendgrid时(通过' npm安装sendgrid' - 直接js,而不是ts)我的工作代码如下:

var helper = require('sendgrid').mail;
var from_email = new helper.Email('dina@dfberry.io');
var to_email = new helper.Email('berry.dina@gmail.com');
var subject = 'Hello World from the SendGrid Node.js Library!';
var content = new helper.Content('text/plain', 'Hello, Email!');
var mail = new helper.Mail(from_email, subject, to_email, content);

var sg = require('sendgrid')("SG.TJKAFkv..THIS..IS..THE...KEY");
var request = sg.emptyRequest({
  method: 'POST',
  path: '/v3/mail/send',
  body: mail.toJSON(),
});

sg.API(request, function(error, response) {
  console.log(response.statusCode);
  console.log(response.body);
  console.log(response.headers);
});

当我查看sendgrid / index.d.ts(通过' typings install dt~sendgrid --global --save')时,我看到两个模块,Sendgrid& ' sendgrid&#39 ;.为什么有两个模块?

//sendgrid/index.d.ts
// Generated by typings
// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/ac94ea73311e9d5607f919b5c174d66d72c78a29/sendgrid/sendgrid-1.1.0.d.ts
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;
}

我认为我想要使用的导出内容是类Sendgrid.Email和" sendgrid" .ctor。

虽然我意识到我可以使用我的js并且完成它,但我想了解如何使用sendgrid / index.d.ts或者我是否应该使用它。该文件说它是从typings生成的,所以我假设typings遍历node_modules sendgrid文件,并决定如何将其构建为d.ts文件。

然而,当我使用ts来引入sendgrid时,我认为它应该工作的方式并不起作用。

我想我需要创建构造函数(api_user,api_key,options)并获取返回的实例。然后使用Instance.send()发送电子邮件。

当我使用VSCode时,intellisense显示sendgrid / index.d.ts对象,但是当我调用编译为js文件时,对象为空。

如何读取/使用sendgrid / index.d.ts文件来对sendgrid进行编写,并同时具有intellisense(现在可以工作)和编译的js文件(现在不能工作)。

正如您可以从Chris's SO读取的那样,ts代码会编译,但是使用d.ts文件的第一个对象是未定义的。

d.ts文件错了吗?我怎么知道这是将来的?除了intellisense之外,d.ts文件没有有意义地连接到底层的sendgrid库。

1 个答案:

答案 0 :(得分:1)

  

我看到两个模块,Sendgrid& ' sendgrid&#39 ;.为什么有两个模块

Sendgrid在全局命名空间中定义Sendgrid。这允许其他开发人员轻松添加/修复定义导出的功能。

'sendgrid'模块指向您可以import/require的NPM模块。执行import Sendgrid = require('sendgrid')时,这为您提供了安全保障。这是推荐用法(这里是模块的忠实粉丝)。