-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
53 lines (45 loc) · 1.48 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import path from 'path'
import AutoLoad from '@fastify/autoload'
import fp from 'fastify-plugin'
import { TypeBoxValidatorCompiler } from '@fastify/type-provider-typebox'
import { errors } from '@vinejs/vine'
// Pass --options via CLI arguments in command to enable these options.
export const options = {}
/**
* @param {import('fastify').FastifyInstance} fastify
*/
export default fp(async function (fastify, opts) {
// Place here your custom code!
const api = fastify
.setValidatorCompiler(TypeBoxValidatorCompiler)
.withTypeProvider()
fastify.setErrorHandler(function (error, _request, reply) {
if (error instanceof errors.E_VALIDATION_ERROR) {
const errorDetail = {
status: 422,
title: 'Validation Error',
detail: 'Errors related to business logic such as uniqueness',
errors: error.messages,
}
reply.code(422).send(errorDetail)
}
else {
reply.send(error)
}
})
// Do not touch the following lines
// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
api.register(AutoLoad, {
dir: path.join(import.meta.dirname, 'plugins'),
options: Object.assign({}, opts),
})
// This loads all plugins defined in routes
// define your routes in one of these
api.register(AutoLoad, {
dir: path.join(import.meta.dirname, 'routes'),
options: Object.assign({}, opts),
})
// fastify.register(fastifySwagger, { openapi })
})