| 7 | import openapi from './openapi.js'; |
| 8 | |
| 9 | export default async function registerPlugins(fastify: FastifyInstance): Promise<void> { |
| 10 | await fastify.register(swaggerPlugin, { |
| 11 | openapi, |
| 12 | routePrefix: '/docs/api', |
| 13 | exposeRoute: true, |
| 14 | }); |
| 15 | |
| 16 | if (config.server.apiKey !== null) { |
| 17 | await fastify.register(bearerAuthPlugin, { |
| 18 | addHook: false, |
| 19 | keys: new Set([config.server.apiKey]), |
| 20 | errorResponse: (error: Error): Schemas.Errors.ApiKeyError => { |
| 21 | switch (error.message) { |
| 22 | case 'missing authorization header': { |
| 23 | const {statusCode, code, message} = new MissingApiKey(); |
| 24 | |
| 25 | const built: Schemas.Errors.MissingApiKey = { |
| 26 | statusCode, |
| 27 | code, |
| 28 | message, |
| 29 | error: 'Unauthorized', |
| 30 | }; |
| 31 | |
| 32 | return built; |
| 33 | } |
| 34 | |
| 35 | default: { |
| 36 | const {statusCode, code, message} = new IncorrectApiKey(); |
| 37 | |
| 38 | const built: Schemas.Errors.IncorrectApiKey = { |
| 39 | statusCode, |
| 40 | code, |
| 41 | message, |
| 42 | error: 'Unauthorized', |
| 43 | }; |
| 44 | |
| 45 | return built; |
| 46 | } |
| 47 | } |
| 48 | }, |
| 49 | }); |
| 50 | } |
| 51 | } |