-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathappInitializer.js
More file actions
104 lines (84 loc) · 2.5 KB
/
appInitializer.js
File metadata and controls
104 lines (84 loc) · 2.5 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict'
const fastify = require('fastify')
const { fastifyRequestContext } = require('../..')
function initAppGet (endpoint) {
const app = fastify({ logger: true })
app.register(fastifyRequestContext)
app.get('/', endpoint)
return app
}
function initAppPost (endpoint) {
const app = fastify({ logger: true })
app.register(fastifyRequestContext)
app.post('/', endpoint)
return app
}
function initAppPostWithPrevalidation (endpoint) {
const app = fastify({ logger: true })
app.register(fastifyRequestContext, { hook: 'preValidation' })
const preValidationFn = (req, _reply, done) => {
const requestId = Number.parseInt(req.body.requestId)
req.requestContext.set('testKey', `testValue${requestId}`)
done()
}
app.route({
url: '/',
method: ['GET', 'POST'],
preValidation: preValidationFn,
handler: endpoint,
})
return app
}
function initAppPostWithAllPlugins (endpoint, requestHook) {
const app = fastify({ logger: true })
app.register(fastifyRequestContext, { hook: requestHook })
app.addHook('onRequest', (req, _reply, done) => {
req.requestContext.set('onRequest', 'dummy')
done()
})
app.addHook('preParsing', (req, _reply, payload, done) => {
req.requestContext.set('preParsing', 'dummy')
done(null, payload)
})
app.addHook('preValidation', (req, _reply, done) => {
const requestId = Number.parseInt(req.body.requestId)
req.requestContext.set('preValidation', requestId)
req.requestContext.set('testKey', `testValue${requestId}`)
done()
})
app.addHook('preHandler', (req, _reply, done) => {
const requestId = Number.parseInt(req.body.requestId)
req.requestContext.set('preHandler', requestId)
done()
})
app.addHook('preSerialization', (req, _reply, payload, done) => {
const onRequestValue = req.requestContext.get('onRequest')
const preValidationValue = req.requestContext.get('preValidation')
done(null, {
...payload,
preSerialization1: onRequestValue,
preSerialization2: preValidationValue,
})
})
app.route({
url: '/',
method: ['GET', 'POST'],
handler: endpoint,
})
return app
}
function initAppGetWithDefaultStoreValues (endpoint, defaultStoreValues) {
const app = fastify({ logger: true })
app.register(fastifyRequestContext, {
defaultStoreValues,
})
app.get('/', endpoint)
return app
}
module.exports = {
initAppPostWithAllPlugins,
initAppPostWithPrevalidation,
initAppPost,
initAppGet,
initAppGetWithDefaultStoreValues,
}