【问题标题】:How to use the Sendgrid.d.ts to use sendgrid?如何使用 Sendgrid.d.ts 来使用 sendgrid?
【发布时间】:2017-01-22 01:29:14
【问题描述】:

我遇到了与Chris P with his question 相同的问题,我更了解为什么my issue here happened 的原因。

我已经通过 npm 安装了 sendgrid,然后是 typings,我的 node_modules 有一个 sendgrid 目录,而我的 typings 有一个 global/sendgrid 目录。

当我使用 sendgrid(通过 'npm install 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'。为什么有两个模块?

//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。

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

但是,当我使用 ts 引入 sendgrid 时,它并没有像我认为的那样工作。

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

当我使用 VSCode 时,智能感知会显示 sendgrid/index.d.ts 对象,但是当我调用编译为 js 文件时,这些对象是空的。

如何读取/使用 sendgrid/index.d.ts 文件对 sendgrid 进行写入,并让智能感知(现在可以使用)和编译的 js 文件(现在无法使用)都可以使用。

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

d.ts 文件是不是错了?我怎么知道这是在未来?除了智能感知,d.ts 文件没有有意义地连接到底层的 sendgrid 库。

【问题讨论】:

  • 我认为 d.ts 文件要么不同步,要么与我认为应该相关的 sendgrid 无关。我将从头开始构建我自己的 d.ts 文件。

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


【解决方案1】:

我看到了两个模块,Sendgrid 和“sendgrid”。为什么有两个模块

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多