import { config, listenURL } from "."; export interface CallParams { method: string; path: string; headers: Record; body: ReadableStream | null; } export async function apiCall( service: string, endpoint: string, params: CallParams, ): Promise { // TODO handle service auth let loc: { baseUrl: string } | undefined = config().deployment?.serviceDiscovery?.services?.[service]; if (loc === undefined) { const listen = listenURL(); if (listen === undefined) { throw new Error( `cannot determine local address, serve() has not been called`, ); } loc = { baseUrl: listen.toString() }; } const req = new Request(`${loc!.baseUrl}${params.path}`, { method: params.method, headers: params.headers, body: params.body, }); return fetch(req); }