From b815c9004bce9bddbafe5460b13310743515fa17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Thu, 11 May 2023 17:59:37 +0200 Subject: [PATCH] Fix httpd client root query MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rūdolfs Ošiņš --- httpd-client/index.ts | 1 - httpd-client/lib/fetcher.ts | 13 ++++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/httpd-client/index.ts b/httpd-client/index.ts index ae2e520ba..7dc19ccff 100644 --- a/httpd-client/index.ts +++ b/httpd-client/index.ts @@ -119,7 +119,6 @@ export class HttpdClient { return this.#fetcher.fetchOk( { method: "GET", - path: "", options, }, nodeInfoSchema, diff --git a/httpd-client/lib/fetcher.ts b/httpd-client/lib/fetcher.ts index acc4d6d05..dc6af1044 100644 --- a/httpd-client/lib/fetcher.ts +++ b/httpd-client/lib/fetcher.ts @@ -40,15 +40,15 @@ export class ResponseError extends Error { // body fails. export class ResponseParseError extends Error { public method: string; - public path: string; public body: unknown; public zodIssues: ZodIssue[]; + public path?: string; public constructor( method: string, - path: string, body: unknown, zodIssues: ZodIssue[], + path?: string, ) { super("Failed to parse response body"); this.method = method; @@ -65,7 +65,7 @@ export interface RequestOptions { export interface FetchParams { method: Method; // Path to append to the `Fetcher`s base URL to get the final URL. - path: string; + path?: string; // Object that is serialized into JSON and sent as the data. body?: unknown; // Query parameters to be serialized with URLSearchParams. @@ -106,9 +106,9 @@ export class Fetcher { } else { throw new ResponseParseError( params.method, - params.path, responseBody, result.error.errors, + params.path, ); } } @@ -142,9 +142,12 @@ export class Fetcher { headers["content-type"] = "application/json"; } + const pathSegment = path === undefined ? "" : `/${path}`; + let url = `${this.#baseUrl.scheme}://${this.#baseUrl.hostname}:${ this.#baseUrl.port - }/api/v1/${path}`; + }/api/v1${pathSegment}`; + if (query) { const searchparams = new URLSearchParams(query as Record); url = `${url}?${searchparams.toString()}`;