Skip to content

Commit

Permalink
Support response schema update
Browse files Browse the repository at this point in the history
  • Loading branch information
greguz committed Aug 6, 2021
1 parent 561fef1 commit ce34e5b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ function plugin (fastify, options, callback) {
body: true,
headers: false,
params: false,
query: true
query: false,
response: false
},
options
options || {}
)

fastify.addHook('onRoute', route => {
Expand All @@ -47,7 +48,15 @@ function plugin (fastify, options, callback) {
route.schema.params = updateSchema(route.schema.params.valueOf())
}
if (route.schema.querystring && options.query) {
route.schema.querystring = updateSchema(route.schema.querystring.valueOf())
route.schema.querystring = updateSchema(
route.schema.querystring.valueOf()
)
}
if (route.schema.response) {
route.schema.response = mapValues(
route.schema.response,
schema => updateSchema(schema.valueOf())
)
}
}
})
Expand Down
20 changes: 17 additions & 3 deletions tests/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ function buildFastify () {
body: true,
headers: true,
params: true,
query: true
query: true,
response: true
})

app.route({
method: 'POST',
url: '/foo/:a/:b',
handler (request, reply) {
reply.send({
a: '0',
b: '1',
body: request.body,
headers: request.headers,
params: request.params,
Expand All @@ -32,15 +35,23 @@ function buildFastify () {
body: schema,
headers: schema,
params: schema,
querystring: schema
querystring: schema,
response: {
200: S.object()
.prop('body')
.prop('headers')
.prop('params')
.prop('query')
.extend(schema)
}
}
})

return app
}

test('default', t => {
t.plan(10)
t.plan(12)

const app = buildFastify()

Expand Down Expand Up @@ -74,5 +85,8 @@ test('default', t => {

t.strictEqual(data.query.a, 0)
t.strictEqual(data.query.b, undefined)

t.strictEqual(data.a, 0)
t.strictEqual(data.b, undefined)
})
})

0 comments on commit ce34e5b

Please sign in to comment.