我想使用express-boom用于表达TypeScript。它缺乏打字,所以我想写自己的。只是让它编译是微不足道的。
此中间件使用属性res
(源自boom module)来装饰boom
对象:
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');
哪种方法最干净?哪些其他类型的中间件正在做类似的事情?
答案 0 :(得分:3)
您可以使用其他一些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
}
}
答案 1 :(得分:1)
这是示例用法。希望这对某人有帮助。
包括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();
}