import type { IncomingMessage, ServerResponse } from 'node:http'; import type { ConnectionService } from '../../features/connection/index.js'; interface ServerApplyRouteDependencies { connection: Pick; readBody(req: IncomingMessage): Promise>; withOperation( kind: string, operation: (operationRevision: number) => Promise, options?: { expectedRevision?: unknown; profileId?: unknown; serverId?: unknown }, ): Promise; sendState(res: ServerResponse, extra: { profileId: string; serverId: string; selectedTag: string }): Promise; } export function createServerApplyRoute(dependencies: ServerApplyRouteDependencies) { return { async handle(req: IncomingMessage, res: ServerResponse) { if (req.method !== 'POST' || req.url !== '/api/apply') return false; const { profileId = '', serverId = '', selectedTag = '', expectedRevision, } = await dependencies.readBody(req); const result = await dependencies.withOperation( 'apply-server', (operationRevision) => dependencies.connection.apply( profileId, serverId, selectedTag, operationRevision, ), { expectedRevision, profileId, serverId }, ); await dependencies.sendState(res, result); return true; }, }; }