From 06c0cd159b195702d69fc8e6b72153e2d27c084c Mon Sep 17 00:00:00 2001 From: Riku Rouvila Date: Mon, 8 Jul 2024 00:55:48 +0300 Subject: [PATCH] implement response type, accept service baseUrl to be passed in --- packages/commons/src/http.ts | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/packages/commons/src/http.ts b/packages/commons/src/http.ts index 74018fb098..3218749338 100644 --- a/packages/commons/src/http.ts +++ b/packages/commons/src/http.ts @@ -59,18 +59,6 @@ export async function fetchJSON( type Services = AllRoutes -const SERVICE_URLS = { - 'user-mgnt': process.env.USER_MANAGEMENT_URL, - auth: process.env.AUTH_URL, - config: process.env.CONFIG_URL, - documents: process.env.DOCUMENTS_URL, - metrics: process.env.METRICS_URL, - notification: process.env.NOTIFICATION_SERVICE_URL, - search: process.env.SEARCH_URL, - webhooks: process.env.WEBHOOKS_URL, - workflow: process.env.WORKFLOW_URL -} - function parsePathParams(path: string, params: Record) { return path.replace(/{([^}]+)}/g, (_, key) => { return params[key] @@ -78,16 +66,9 @@ function parsePathParams(path: string, params: Record) { } export function createServiceClient( - service: ServiceName + service: ServiceName, + baseUrl: string ) { - const url = SERVICE_URLS[service] - - if (!url) { - throw new Error( - `Missing URL for service ${service}. Make sure you have set the corresponding environment variable` - ) - } - type Service = Services[ServiceName] type IsNever = [T] extends [never] ? true : false @@ -109,12 +90,6 @@ export function createServiceClient( Method extends keyof Service, Path extends keyof Service[Method] >(method: Method, path: Path, options: RequestOptions) { - if (!url) { - throw new Error( - `Missing URL for service ${service}. Make sure you have set the corresponding environment variable` - ) - } - const urlPathWithParams: string = 'params' in options ? parsePathParams( @@ -123,7 +98,9 @@ export function createServiceClient( ) : (path as string) - return fetchJSON(joinURL(url, urlPathWithParams).href, { + return fetchJSON< + Service[Method][Path] extends { response: infer R } ? R : unknown + >(joinURL(baseUrl, urlPathWithParams).href, { method: (method as string).toUpperCase(), headers: { 'Content-Type': 'application/json'