UNPKG

@artinet/sdk

Version:
33 lines 970 B
import { v4 as uuidv4 } from "uuid"; export class ServiceDispatcher { services; engine; constructor(options) { this.engine = options.engine; this.services = options.services; } static createExecutionContext(req) { return { id: req.id ?? uuidv4(), protocol: req.protocol, getRequestParams: () => req.params, isCancelled: () => false, requestContext: req, }; } async onRequest(req) { const service = this.services[req.protocol]; if (!service) { throw new Error(`Unknown service: ${req.protocol}`); } const executionContext = ServiceDispatcher.createExecutionContext(req); await service.execute({ executionContext, engine: this.engine, }); } addService(service) { this.services[service.protocol] = service; } } //# sourceMappingURL=dispatcher.js.map