docs: include example for server utilities (#21337)

This commit is contained in:
Markus 2023-06-07 11:28:27 +02:00 committed by GitHub
parent 6bb0a849fe
commit 2fccfa2d5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -102,6 +102,27 @@ Server routes are powered by [unjs/h3](https://github.com/unjs/h3) which comes w
You can add more helpers yourself inside the `~/server/utils` directory.
For example, you can define a custom handler utility that wraps the original handler and performs additional operations before returning the final response.
**Example:**
```ts [server/utils/handler.ts]
import type { EventHandler } from 'h3'
export const defineWrappedResponseHandler = (handler: EventHandler) =>
defineEventHandler(async (event) => {
try {
// do something before the route handler
const response = await handler(event)
// do something after the route handler
return { response }
} catch (err) {
// Error handling
return { err }
}
})
```
## Server Types
::alert{type="info"}