我想用TypeScript将express-boom用于express。它缺乏打字,所以我想写自己的。只是让它编译是微不足道的。
这个中间件使用属性res
(源自boom
)来装饰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
});
但是使用打字稿我需要施放它,因为http.ServerResponse
和Express.Response
都没有繁荣属性,当然:
return (<any>res).boom.badRequest('bla bla bla');
这是最干净的方法吗?哪些其他类型的中间件正在做类似的事情?
您可以使用其他一些Express中间件作为示例,例如Method-Override和它的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();
}