Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ const methodNames = [
'type', 'types'
]

/** @this {import('fastify').FastifyRequest} */
function acceptsMethod () {
if (!this.raw[acceptsObjectSymbol]) {
this.raw[acceptsObjectSymbol] = accepts(this.raw)
}
return this.raw[acceptsObjectSymbol]
}

/** @this {import('fastify').FastifyReply} */
function replyAcceptMethod () {
if (!this.request[acceptsObjectSymbol]) {
this.request[acceptsObjectSymbol] = accepts(this.request.raw)
}
return this.request[acceptsObjectSymbol]
}

/** @type {typeof import('./types/index').fastifyAccepts} */
function fastifyAccepts (fastify, options, done) {
fastify.decorateRequest('accepts', acceptsMethod)

Expand Down
8 changes: 5 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const fastifyAccepts = require('..')

const Fastify = require('fastify')

/** @typedef {import('node:test').TestContext} TestContext */

const testCases = [
{
name: 'request - no header',
Expand Down Expand Up @@ -88,7 +90,7 @@ const testCases = [
}
]

test('accept header', async t => {
test('accept header', async (/** @type {TestContext} */ t) => {
t.plan(testCases.length)

const fastify = Fastify()
Expand Down Expand Up @@ -120,7 +122,7 @@ test('accept header', async t => {
const BASE_URL = `http://localhost:${fastify.server.address().port}`

for (const testCase of testCases) {
await t.test(testCase.name, async (t) => {
await t.test(testCase.name, async (/** @type {TestContext} */ t) => {
t.plan(1)

const result = await fetch(`${BASE_URL}${testCase.url}`, {
Expand All @@ -133,7 +135,7 @@ test('accept header', async t => {
}
})

test('no reply decorator', async function (t) {
test('no reply decorator', async function (/** @type {TestContext} */ t) {
const fastify = Fastify()
fastify.register(fastifyAccepts, { decorateReply: false })
await fastify.ready()
Expand Down
Loading