首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何扩展快速请求接口

如何扩展快速请求接口
EN

Stack Overflow用户
提问于 2022-08-18 08:32:00
回答 1查看 78关注 0票数 0

我用的是特快专递打字本。我想扩展我的快速请求接口,因为我所做的事情如下:-

Middleware.ts

代码语言:javascript
复制
import { NextFunction, Request, Response } from 'express';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const configureI18n = require('../helpers/i18n');
const [_, i18nObj] = configureI18n(false);

export interface CRequest extends Request {
  i18nObj: any;
}

const langHeadersMiddleware = (
  request: CRequest,
  response: Response,
  next: NextFunction
): void => {
  try {
    const language = request.headers['accept-language'];

    i18nObj.setLocale(language ? language : 'en');
    request.i18nObj = i18nObj;
    next();
  } catch (error) {
    i18nObj.setLocale('en');
  }
};

export default langHeadersMiddleware;

route.ts

代码语言:javascript
复制
getUserProfile.get(
  '/:id',
  async (request: CRequest, response: express.Response) => {
    try {
           const id = request.params.id;
           response.json({
        err: 0,
        message: request.i18nObj.__('MESSAGES.USER_FETCH'),
        data
      });
    } catch (error: any) {
      response.json({ err: 1, message: error.message, error });
    }
  }
);

在这条路上,我遇到了一个错误:-

没有重载与此调用匹配。最后一次重载导致以下错误。类型的参数(请求: CRequest,响应: express.Response) =>允诺‘不能分配给'Application’的参数。类型‘(请求: CRequest,响应: Response>) =>允诺’在'Application‘:init,defaultConfiguration,engine,set等类型中缺少以下属性。

我看了那么多博客,文章,但每个人都在使用和我一样。

EN

回答 1

Stack Overflow用户

发布于 2022-08-18 08:53:02

您将不得不对整个快递包进行分叉和返工,这绝对不推荐:)

但你可以:

  • 将您的i18n添加到中间件中的请求中,就像您正在做的那样,只需将其与//@ts一起使用-忽略上面的
  • ,将您的i18n添加到中间件中的请求主体中,只需使用
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73399825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档