英文:
Nuxt 3 - after response middleware
问题
I have a use-case where I would like to call a middleware after the response went through the route handler. The docs describe that the standard server middleware only runs BEFORE the request is handled (https://nuxt.com/docs/guide/directory-structure/server).
What I'd like to accomplish is:
// file: server/api/test/index.ts
export default defineEventHandler(async (event) => {
return { "test": true }
})
When I call the endpoint via GET /api/test I would like the response to be:
{ "result": { "test": true } }
So basically mapping all APIs response in an object with the key "result".
This is quite easy to do with express middleware and other frameworks as you can usually await the route handler's result and then just wrap the result in the object.
How can this be accomplished with Nuxt 3 Middleware?
英文:
I have a use-case where I would like to call a middleware after the response went through the route handler. The docs describe that the standard server middleware only runs BEFORE the request is handled (https://nuxt.com/docs/guide/directory-structure/server).
What I'd like to accomplish is:
// file: server/api/test/index.ts
export default defineEventHandler(async (event) => {
return { "test": true }
})
When I call the endpoint via GET /api/test I would like the response to be:
{ "result": { "test": true } }
So basically mapping all APIs response in a object with key "result".
This is quite easy to do with express middleware and other frameworks as you can usually await the route handler's result and then just wrap the result in the object.
How can this be accomplished with Nuxt 3 Middleware?
答案1
得分: 1
中间件的作用是在执行端点之前检查逻辑。我不认为使用中间件会完成您的任务。
以下是一种不涉及任何中间件的解决方案:
您可以创建一个自定义函数来映射您的响应。
function mapResponse(data){
return {
result: data
}
}
然后在您的 API 端点内使用它,像这样:
export default defineEventHandler((event) => {
return mapResponse({ "test": true })
})
然后您可以根据需要自定义该函数。
英文:
Middlewares purpose are for checking logic before the the endpoint gets executed. I don't think using a middleware will accomplish your task.
Here is one solution that does not involve any middleware:
you can create a custom function that maps your responses.
function mapResponse(data){
return {
result: data
}
}
and use it inside you api endpoints like this:
export default defineEventHandler((event) => {
return mapResponse({ "test": true })
})
then you can also customize the function however you want.
答案2
得分: 1
h3 的 GitHub 页面上了解到了一种可行的解决方案。(https://github.com/unjs/h3/issues/397)
// ~/server/utils/handler.ts
导入类型 { H3Event } 从 'h3'
导出 const 定义我的处理程序 = (handler: (event: H3Event) => any) => {
const _handler = async (event: H3Event) => {
const response = await handler(event)
const responseNew = 做一些(response )
return responseNew
}
return 定义事件处理程序(_handler)
}
英文:
The awesome people over at the h3's GitHub page showed me a working solution.(https://github.com/unjs/h3/issues/397)
// ~/server/utils/handler.ts
import type { H3Event } from 'h3'
export const defineMyHandler = (handler: (event: H3Event) => any) => {
const _handler = async (event: H3Event) => {
const response = await handler(event)
const responseNew = doSome(response )
return responseNew
}
return defineEventHandler(_handler)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论