gpt4 book ai didi

node.js - typescript -创建自定义快速中间件定义

转载 作者:行者123 更新时间:2023-12-03 12:11:57 25 4
gpt4 key购买 nike

我一直很难为我们的应用程序定义自定义中间件。我正在使用typescript@3.6.3和express@4.17.1。

我见过的所有键入中间件的示例都暗示用户没有在reqres参数中添加任何内容,但是我知道这就是中间件的意义-改变那些对象之一,知道中间件已经以某种顺序加载(即cookie解析器或body解析器在早期是臭名昭著的,我想还是吗?)。无论如何,大多数示例都可以解决类似github问题的问题:https://github.com/DefinitelyTyped/DefinitelyTyped/issues/26146#issuecomment-393386416

我遇到的问题是,我们确实对传递给每个中间件的reqres对象进行了突变,所以我不得不相应地键入两个对象:

import {
Express as ExpressInterface,
Request as ExpressRequest,
Response as ExpressResponse,
NextFunction as ExpressNextFunction,
} from 'express';
import LoggerType from 'some-log-package';

// don't do anything different the `next()`
export type ExpressNextFunction = ExpressNextFunction;

export interface CustomServerApp extends ExpressInterface {
customHeader: string;
customLogger: LoggerType;
}

export interface CustomServerRequest extends ExpressRequest {
app: CustomServerApp,
id: string,
urlSearchParams: URLSearchParams,
// etc
}

export interface CustomServerResponse extends ExpressResponse { ... }

因此,我导出了中间件的基本参数:
  • req = CustomServerRequest
  • res = CustomServerResponse
  • next = ExpressNextFunction

  • 当然,我将这些内容相应地导入到我的中间件文件中:

    import {
    CustomServerRequest,
    CustomServerResponse,
    ExpressNextFunction
    } from './server-types'

    export const customMiddleware = (req: CustomServerRequest, res: CustomServerResponse, next: ExpressNextFunction): void {
    // do something here with either `req` or `res` or both!
    next()
    };


    我们有一个集中的 middleware.ts文件,我们在其中放入了所有全局中间件,因此它看起来像这样:

    import { CustomServerApp } from './server-types';
    import { customMiddleware } from './middlewares/customMiddleware';
    export const middlewares = (app: CustomServerApp): void => {
    app.use(
    customMiddleware,
    // maybe more middlewares, etc
    );
    }

    但是,然后运行类型检查器时,出现如下错误:
    No overload matches this call.
    The last overload gave the following error.
    Argument of type '(req: Request, res: Response, next: NextFunction) => void' is not assignable to parameter of type 'PathParams'.
    Type '(req: Request, res: Response, next: NextFunction) => void' is missing the following properties from type '(string | RegExp)[]': pop, push, concat, join, and 27 more.ts(2769)

    我一直很难理解为什么这会被最后一个重载定义所困扰。仅基于 @types/express,即使类型声明合并,似乎也很难向中间件处理程序添加额外的重载。

    即使当我遵循与核心表达类型相似的模式来定义中间件处理程序- https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express-serve-static-core/index.d.ts#L40-L43时,它也不起作用。从 https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express/index.d.ts#L99extend似乎也不容易,因为与路由器定义( IRouterHandlerIRouterMatcher)不同,您无法将泛型传递给核心RequestHandler。当我尝试以这种方式键入它时,会出现不同但相似的类型错误。

    有没有其他人经历过类似的事情?

    最佳答案

    您无法在req和res变量中添加必填字段,因为express无法满足您的约束。另外,在通过不同的中间件后,express不处理更改的req/res类型。因此,您的自定义类型可以包含与ExpressRequest/ExpressResponse中相同的必填字段,其余字段应该是可选的,并且在中间件内部应具有nullcheck。

    关于node.js - typescript -创建自定义快速中间件定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58278755/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com