如何在使用Fastify与OpenAPI时为单个API设置自定义的bodyLimit。

huangapple go评论53阅读模式
英文:

How to have a custom bodyLimit for a single api when using fastify with openapi

问题

我正在使用openapi和fastify模块从Node.js中使用fastify。我想要一个特定的路由具有非默认的请求体限制。这是否可能?我尝试在我的api.json定义中使用以下内容,但这不起作用。

"/api/myApi": {
  "post": {
    "x-fastify-config": {
      "bodyLimit": 10000000
    }
    ...
  }
}

谢谢。

英文:

I am using fastify with openapi from nodejs with the modules

openapiGlue & fastify

I'd like one specific route to have a non-default body limit. Is this possible ? I tried the following in my api.json definition

    "/api/myApi": {
      "post": {
        "x-fastify-config": {
          "bodyLimit":10000000
        }
     ...

but this does not work.

Thanks

答案1

得分: 1

无法从openapi JSON文件中找到方法来实现...但是...可以使用onRoute钩子来完成,该钩子在解析openapi JSON时运行。

fastify.addHook('onRoute', (routeOptions) => {
  if (routeOptions.path === 'mypath') {
    console.log(JSON.stringify(routeOptions));
    routeOptions.bodyLimit = 10000000;
  }
});
英文:

I could not find a way to do it from the openapi JSON file ... but ... it can be done with an onRoute hook which is run as openapi JSON is parsed.

fastify.addHook('onRoute', (routeOptions) => {
  if (routeOptions.path === 'mypath') {
    console.log(JSON.stringify(routeOptions));
    routeOptions.bodyLimit=10000000
  }
});

huangapple
  • 本文由 发表于 2023年6月25日 19:32:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定