import { createRequire } from 'node:module' import { readFileSync } from 'node:fs' import type { HttpContext } from '@adonisjs/core/http' import { buildOpenApiDocument } from '#start/openapi/generator' const require = createRequire(import.meta.url) /** * The self-contained Scalar browser bundle, read from the installed * `@scalar/api-reference` package and cached in memory. Served from this app * (not a CDN) so the API reference works on an offline appliance. */ let scalarBundle: string | null = null function getScalarBundle(): string { if (scalarBundle === null) { const entry = require.resolve('@scalar/api-reference') const marker = '/node_modules/@scalar/api-reference/' const root = entry.slice(0, entry.lastIndexOf(marker) + marker.length - 1) scalarBundle = readFileSync(`${root}/dist/browser/standalone.js`, 'utf-8') } return scalarBundle } /** * The reference page mounts Scalar against our locally served bundle and spec. * Scalar auto-detects the `#api-reference` element and reads `data-url`. */ const REFERENCE_HTML = `