import { getLatestVersion } from '../version.js' export default async function (app, opts) { // Add a hook to set the version headers app.addHook('preHandler', async (req, reply) => { const latestVersion = await getLatestVersion() const version = req.headers.version || latestVersion reply.header('version', version) reply.header('latest-version', latestVersion) reply.version = version }) // Add a hook to log errors app.addHook('onError', (req, reply, { message, stack, statusCode }, done) => { const error = { statusCode, stack } /* c8 ignore next */ reply.log.error(error, message || 'request errored') done() }) // Logs the request with the 'debug' level and also logs headers with the 'trace' level app.addHook('onResponse', (req, reply, done) => { const loggingOptions = { url: req.raw.url, res: reply, responseTime: reply.getResponseTime() } /* c8 ignore next */ if (process.env.LOG_LEVEL === 'trace') loggingOptions.headers = req.headers reply.log.debug(loggingOptions, 'request completed') done() }) }