Skip to content

Commit

Permalink
Merge pull request #22 from autotelic/feat/pass-options
Browse files Browse the repository at this point in the history
Pass options through to autoload, makeOperationResolver and makeSecurityHandlers
  • Loading branch information
eadmundo authored Mar 14, 2024
2 parents 8156ff3 + 1d79e5f commit 221143d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ async function openapiAutoload (fastify, options = {}) {
dir: handlersDir,
maxDepth: 1,
dirNameRoutePrefix: false,
encapsulate: false
encapsulate: false,
options
})

const openapiGlueOpts = {
Expand All @@ -36,11 +37,11 @@ async function openapiAutoload (fastify, options = {}) {

// Factory/creator functions for security handlers & operation resolver
if (makeSecurityHandlers) {
openapiGlueOpts.securityHandlers = makeSecurityHandlers(fastify)
openapiGlueOpts.securityHandlers = makeSecurityHandlers(fastify, options)
}

if (makeOperationResolver) {
openapiGlueOpts.operationResolver = makeOperationResolver(fastify)
openapiGlueOpts.operationResolver = makeOperationResolver(fastify, options)
}

// Resolve multi-file OpenAPI specification
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/handlers/getBar/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default async (fastify, { operationId }) => {
export default async (fastify, { operationId, foo }) => {
fastify.decorate(operationId, async (_req, reply) => {
reply.code(200).send('bar')
reply.code(200).send(foo)
})
}

Expand Down
17 changes: 10 additions & 7 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ function buildApp ({
handlersDir: join(fixturesDir, 'handlers'),
openapiOpts: {
specification: join(fixturesDir, 'test-spec.yaml')
}
},
foo: 'bar'
}

if (operationResolver) {
Expand Down Expand Up @@ -103,10 +104,11 @@ test('should use a custom operation resolver if provided', async ({ equal, teard
test('should use a custom operation resolver factory if provided', async ({ equal, teardown }) => {
teardown(async () => fastify.close())
const fastify = buildApp({
makeOperationResolver: (fastify) => (operationId) => {
makeOperationResolver: (fastify, options) => (operationId) => {
if (operationId === 'getFoo') {
const { foo } = options
return async (_req, reply) => {
reply.code(200).send('this is a test')
reply.code(200).send(`this is a test ${foo}`)
}
}
return fastify[operationId]
Expand All @@ -121,7 +123,7 @@ test('should use a custom operation resolver factory if provided', async ({ equa
})

equal(fooResponse.statusCode, 200)
equal(fooResponse.body, 'this is a test')
equal(fooResponse.body, 'this is a test bar')

const barResponse = await fastify.inject({
method: 'GET',
Expand All @@ -135,8 +137,9 @@ test('should use a custom operation resolver factory if provided', async ({ equa
test('should use a custom security handlers factory if provided', async ({ equal, same, teardown }) => {
teardown(async () => fastify.close())
const fastify = buildApp({
makeSecurityHandlers: (fastify) => {
fastify.decorateRequest('authenticate', async () => 123)
makeSecurityHandlers: (fastify, options) => {
const { foo } = options
fastify.decorateRequest('authenticate', async () => `${foo}123`)
return {
async bearerAuth (request, reply, params) {
try {
Expand All @@ -158,5 +161,5 @@ test('should use a custom security handlers factory if provided', async ({ equal
})

equal(fooResponse.statusCode, 200)
same(fooResponse.json(), { userId: 123 })
same(fooResponse.json(), { userId: 'bar123' })
})

0 comments on commit 221143d

Please sign in to comment.