steam-backlog-enforcer/web/mock/plugin.ts
Krzysztof kuhy Rudnicki cef9fccab8
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Has been cancelled
Tests / test (push) Has been cancelled
Tests / web-coverage (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
Migrate steam-backlog-enforcer/web to Astro (no R…
Todo: 9b9342ba
2026-11-09 08:36:38 +01:00

32 lines
1.1 KiB
TypeScript

// Dev-only Vite plugin that serves the whole web-control API contract from
// memory (`pnpm dev:mock`). `apply: 'serve'` plus the mode gate in
// astro.config.mjs keep it out of every production build.
import { randomBytes } from 'node:crypto'
import type { AstroUserConfig } from 'astro'
import { handle } from './routes.ts'
// Vite is Astro's, not a direct dependency: take its Plugin type from there.
type PluginOption = NonNullable<NonNullable<AstroUserConfig['vite']>['plugins']>[number]
type Plugin = Extract<PluginOption, { name: string }>
export function mockApi(): Plugin {
// Per-launch token, like the real server's web-<port>.token.
const token = randomBytes(32).toString('hex')
return {
name: 'sbe-mock-api',
apply: 'serve',
// src/components/DevToken.astro renders this into every page in dev.
config() {
process.env.SBE_DEV_TOKEN = token
},
configureServer(server) {
server.middlewares.use((req, res, next) => {
handle(req, res, token).then((handled) => {
if (!handled) next()
}, next)
})
},
}
}