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

I would like to use express boom for express with TypeScript. There is no typing in it, so I would like to write my own. Just making it compile is trivial.

This middleware decorates the object reswith a property boom(obtained from the boom module ):

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
});

But with typewriting, I need to bring it, because http.ServerResponseneither of Express.Responsecourse have the boom property:

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

What is the cleanest way to do this? What other typed middleware do similar things?

+5
4

Express-, . Method-Override .

, .boom , (express-boom.d.ts), :

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

    export interface Response {
        boom: Boom
    }
}
+3

. , - .

express-boom.d.ts .

sample-usage.ts sample-usage.ts , :

: -:

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 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();
}
+1

express express-serve-static-core.

, Express: https://github.com/DefiniteTyped/DefiniteTyped/blob/8fb0e959c2c7529b5fa4793a44b41b797ae671b9/types/express/index.d.ts#L19

, :

declare module 'express-serve-static-core' {
  interface Request {
    myField?: string
  }
  interface Response {
    myField?: string
  }
}
0

express-boom.d.ts :

import * as Boom from "boom";

declare namespace Express {
    export interface Response {
        boom: typeof Boom;
    }
}

, Express.Response boom. Boom, , , , Boom.

-1

All Articles