如何为将 属性 添加到响应的中间件编写 TypeScript 定义?

How to write TypeScript definition for a middleware that adds property to the response?

我想使用 express-boom 通过 TypeScript 进行表达。它缺少打字,所以我想自己写。让它编译是微不足道的。

此中间件使用 属性 boom(派生自 boom module)装饰 res 对象:

var express = require('express');
var boom = require('express-boom');

var app = express();

app.use(boom());

app.use(function (req, res) {
  res.boom.notFound(); // Responsds with a 404 status code
});

但是使用 typescript 我需要转换它,因为 http.ServerResponseExpress.Response 都没有繁荣 属性,当然:

return (<any>res).boom.badRequest('bla bla bla');

哪种方法最干净?还有哪些其他类型的中间件在做类似的事情?

还有很多其他 Express 中间件可以用作示例,例如Method-Override, and its type definitions.

作为一个更具体的例子,如果你想将这个 .boom 属性 添加到响应对象,你应该只需要创建一个类型定义 (express-boom.d.ts) 包含:

declare namespace Express {
    interface Boom {
        // Add boom's properties in here
    }

    export interface Response {
        boom: Boom
    }
}

这是一个示例用法。希望这对某人有所帮助。

包括 express-boom.d.ts 以及您的应用程序源文件。

请参考以下要点中的 sample-usage.ts 示例用法:

Gist: Type definitions for express-boom:

express-boom.d.ts

/**
 * Type definitions for express-boom
 * Definitions by: Sandeep K Nair <https://github.com/sandeepsuvit>
 * @author: Sandeep K Nair
 */
declare namespace Express {
    interface Boom {
        // Add boom's properties in here
        wrap: (error: Error, statusCode?: number, message?: string) => BoomError;
        create: (statusCode: number, message?: string, data?: any) => BoomError;

        // 4xx
        badRequest: (message?: string, data?: any) => BoomError;
        unauthorized: (message?: string, scheme?: any, attributes?: any) => BoomError;
        forbidden: (message?: string, data?: any) => BoomError;
        notFound: (message?: string, data?: any) => BoomError;
        methodNotAllowed: (message?: string, data?: any) => BoomError;
        notAcceptable: (message?: string, data?: any) => BoomError;
        proxyAuthRequired: (message?: string, data?: any) => BoomError;
        clientTimeout: (message?: string, data?: any) => BoomError;
        conflict: (message?: string, data?: any) => BoomError;
        resourceGone: (message?: string, data?: any) => BoomError;
        lengthRequired: (message?: string, data?: any) => BoomError;
        preconditionFailed: (message?: string, data?: any) => BoomError;
        entityTooLarge: (message?: string, data?: any) => BoomError;
        uriTooLong: (message?: string, data?: any) => BoomError;
        unsupportedMediaType: (message?: string, data?: any) => BoomError;
        rangeNotSatisfiable: (message?: string, data?: any) => BoomError;
        expectationFailed: (message?: string, data?: any) => BoomError;
        badData: (message?: string, data?: any) => BoomError;
        tooManyRequests: (message?: string, data?: any) => BoomError;

        // 5xx
        notImplemented: (message?: string, data?: any) => BoomError;
        badGateway: (message?: string, data?: any) => BoomError;
        serverTimeout: (message?: string, data?: any) => BoomError;
        gatewayTimeout: (message?: string, data?: any) => BoomError;
        badImplementation: (message?: string, data?: any) => BoomError;
    }

    export interface BoomError {
        data: any;
        reformat: () => void;
        isBoom: boolean;
        isServer: boolean;
        message: string;
        output: Output;
    }

    export interface Output {
        statusCode: number;
        headers: any;
        payload: any;
    }

    export interface Response {
        boom: Boom
    }
}

sample-usage.ts

export function someMiddleware(req: express.Request, res: express.Response, next: express.NextFunction) {
  // use it this way
  res.boom.forbidden("Failed to grant access to resource.");
  next();
}