Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ function replyAcceptMethod () {
function fastifyAccepts (fastify, options, done) {
fastify.decorateRequest('accepts', acceptsMethod)

methodNames.forEach(methodName => {
const methodNamesLength = methodNames.length
for (let i = 0; i < methodNamesLength; i += 1) {
const methodName = methodNames[i]
// Defining methods this way to ensure named functions show in stack traces
fastify.decorateRequest(methodName, {
[methodName]: function (arr) {
Expand All @@ -42,12 +44,13 @@ function fastifyAccepts (fastify, options, done) {
return acceptsObject[methodName](arr)
}
}[methodName])
})
}

if (options.decorateReply === true) {
fastify.decorateReply('requestAccepts', replyAcceptMethod)

methodNames.forEach(methodName => {
for (let i = 0; i < methodNamesLength; i += 1) {
const methodName = methodNames[i]
const capitalizedMethodName = methodName.replace(/(?:^|\s)\S/gu, a => a.toUpperCase())
const replyMethodName = 'request' + capitalizedMethodName
const acceptsMethodName = 'accepts' + capitalizedMethodName
Expand All @@ -59,7 +62,7 @@ function fastifyAccepts (fastify, options, done) {
return acceptsObject[methodName](arr)
}
}[acceptsMethodName])
})
}
}

done()
Expand Down