Refactor VPN proxy components and update related behavior
This commit is contained in:
@@ -5,18 +5,33 @@ import type { ConnectionService } from '../../features/connection/index.js';
|
||||
interface ServerApplyRouteDependencies {
|
||||
connection: Pick<ConnectionService, 'apply'>;
|
||||
readBody(req: IncomingMessage): Promise<Record<string, unknown>>;
|
||||
withOperation<T>(kind: string, operation: () => Promise<T>): Promise<T>;
|
||||
sendState(res: ServerResponse, extra: { serverId: string; selectedTag: string }): Promise<void>;
|
||||
withOperation<T>(
|
||||
kind: string,
|
||||
operation: (operationRevision: number) => Promise<T>,
|
||||
options?: { expectedRevision?: unknown; profileId?: unknown; serverId?: unknown },
|
||||
): Promise<T>;
|
||||
sendState(res: ServerResponse, extra: { profileId: string; serverId: string; selectedTag: string }): Promise<void>;
|
||||
}
|
||||
|
||||
export function createServerApplyRoute(dependencies: ServerApplyRouteDependencies) {
|
||||
return {
|
||||
async handle(req: IncomingMessage, res: ServerResponse) {
|
||||
if (req.method !== 'POST' || req.url !== '/api/apply') return false;
|
||||
const { serverId = '', selectedTag = '' } = await dependencies.readBody(req);
|
||||
const {
|
||||
profileId = '',
|
||||
serverId = '',
|
||||
selectedTag = '',
|
||||
expectedRevision,
|
||||
} = await dependencies.readBody(req);
|
||||
const result = await dependencies.withOperation(
|
||||
'apply-server',
|
||||
() => dependencies.connection.apply(serverId, selectedTag),
|
||||
(operationRevision) => dependencies.connection.apply(
|
||||
profileId,
|
||||
serverId,
|
||||
selectedTag,
|
||||
operationRevision,
|
||||
),
|
||||
{ expectedRevision, profileId, serverId },
|
||||
);
|
||||
await dependencies.sendState(res, result);
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user