-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
71 lines (60 loc) · 1.8 KB
/
app.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import fastify from 'fastify';
import fastswagger from 'fastify-swagger';
import Ajv from "ajv";
import { container, Container } from './src/container';
import { cartsRoutes } from './src/interfaces/http/routes/cart';
import { productsRoutes } from './src/interfaces/http/routes/product';
import { servicesRoutes } from './src/interfaces/http/routes/service';
import { vouchersRoutes } from './src/interfaces/http/routes/voucher';
import { swaggerOptions } from './src/config/swagger';
// this declaration must be in scope of the typescript interpreter to work
declare module 'fastify' {
interface FastifyRequest { // you must reference the interface and not the type
container: Container;
}
}
const app = fastify()
//validation configs for fastify requests
const schemaCompilers : { [key: string]: any} = {
body: new Ajv({
removeAdditional: true,
coerceTypes: false,
allErrors: false,
}),
params: new Ajv({
removeAdditional: true,
coerceTypes: true,
allErrors: false,
}),
querystring: new Ajv({
removeAdditional: true,
coerceTypes: true,
allErrors: false,
}),
headers: new Ajv({
removeAdditional: true,
coerceTypes: true,
allErrors: false,
}),
};
app.setValidatorCompiler((req) => {
if (!req.httpPart) {
throw new Error("Missing httpPart");
}
const compiler = schemaCompilers[req.httpPart];
if (!compiler) {
throw new Error(`Missing compiler for ${req.httpPart}`);
}
return compiler.compile(req.schema);
});
//Aplying middleware in app request
app.decorateRequest("container", null);
app.addHook("onRequest", async (req, reply) => {
req.container = container;
});
app.register(fastswagger, swaggerOptions);
app.register(cartsRoutes);
app.register(productsRoutes);
app.register(servicesRoutes);
app.register(vouchersRoutes);
export {app};