(registryOrServices: HostApiRegistry | HostServiceRegistry)
| 86 | } |
| 87 | |
| 88 | export function createHostInvokeDispatcher(registryOrServices: HostApiRegistry | HostServiceRegistry) { |
| 89 | const registry = toHostApiRegistry(registryOrServices); |
| 90 | return async function dispatchHostRequest(request: unknown): Promise<HostResponse> { |
| 91 | const requestId = request && typeof request === 'object' |
| 92 | ? String((request as Record<string, unknown>).id ?? '') |
| 93 | : undefined; |
| 94 | |
| 95 | if (!isHostRequest(request)) { |
| 96 | return { |
| 97 | id: requestId, |
| 98 | ok: false, |
| 99 | error: { code: 'VALIDATION', message: 'Invalid host request format' }, |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | const action = registry.resolve(request.module, request.action); |
| 104 | if (typeof action !== 'function') { |
| 105 | return { |
| 106 | id: request.id, |
| 107 | ok: false, |
| 108 | error: { |
| 109 | code: 'UNSUPPORTED', |
| 110 | message: `Unsupported host request: ${request.module}.${request.action}`, |
| 111 | }, |
| 112 | }; |
| 113 | } |
| 114 | |
| 115 | try { |
| 116 | const data = await action(request.payload); |
| 117 | return { id: request.id, ok: true, data }; |
| 118 | } catch (error) { |
| 119 | return { |
| 120 | id: request.id, |
| 121 | ok: false, |
| 122 | error: { |
| 123 | code: 'INTERNAL', |
| 124 | message: error instanceof Error ? error.message : String(error), |
| 125 | }, |
| 126 | }; |
| 127 | } |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | export function registerHostInvokeHandler(registry: HostApiRegistry): void { |
| 132 | const dispatch = createHostInvokeDispatcher(registry); |
no test coverage detected