From 55ce9e8a332eca6f19c07c8d350aa849bfe0ba7a Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Tue, 1 Mar 2022 20:29:06 +0100 Subject: [PATCH] Fix bug with default ports in api config JavaScript brilliance strikes again: new URL("https://acme.com:443").port == "" Signed-off-by: Alexis Sellier --- src/api.ts | 16 +++++++++------- src/base/seeds/Seed.ts | 16 ++++++++-------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/api.ts b/src/api.ts index c5df28ffe..b9673b626 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,6 +1,6 @@ export interface Host { host: string; - port: number; + port: number | null; } export async function get( @@ -23,24 +23,26 @@ export async function get( path = path.startsWith("/") ? path.slice(1) : path; const baseUrl = path - ? `${protocol}${base}:${port}/v1/${path}` - : `${protocol}${base}:${port}`; - const url = search ? `${baseUrl}?${search}` : baseUrl; + ? `${protocol}${base}/v1/${path}` + : `${protocol}${base}`; + const url = new URL(search ? `${baseUrl}?${search}` : baseUrl); + url.port = String(port); + const urlString = String(url); let response = null; try { - response = await fetch(url, { + response = await fetch(urlString, { method: 'GET', headers: { 'Accept': 'application/json', } }); } catch (err) { - throw new ApiError(url, "API request failed"); + throw new ApiError(urlString, "API request failed"); } if (! response.ok) { - throw new ApiError(url, "Not found"); + throw new ApiError(urlString, "Not found"); } return response.json(); } diff --git a/src/base/seeds/Seed.ts b/src/base/seeds/Seed.ts index c4dfa18d0..95438402c 100644 --- a/src/base/seeds/Seed.ts +++ b/src/base/seeds/Seed.ts @@ -19,8 +19,8 @@ export class InvalidSeed { export class Seed { valid: true = true; - api: { host: string; port: number }; - git: { host: string; port: number }; + api: { host: string; port: number | null }; + git: { host: string; port: number | null }; link: { host: string; id: string; port: number }; version?: string; @@ -38,14 +38,14 @@ export class Seed { let api = null; let git = null; - let apiPort = null; - let gitPort = null; + let apiPort: number | null = cfg.seed.api.port; + let gitPort: number | null = cfg.seed.git.port; if (seed.api) { try { const url = new URL(seed.api); api = url.hostname; - apiPort = url.port || null; + apiPort = url.port ? Number(url.port) : null; } catch { api = seed.api; } @@ -56,7 +56,7 @@ export class Seed { try { const url = new URL(seed.git); git = url.hostname; - gitPort = url.port || null; + gitPort = url.port ? Number(url.port) : null; } catch { git = seed.git; } @@ -75,8 +75,8 @@ export class Seed { api = api ?? seed.host; git = git ?? seed.host; - this.api = { host: api, port: Number(apiPort) || cfg.seed.api.port }; - this.git = { host: git, port: Number(gitPort) || cfg.seed.git.port }; + this.api = { host: api, port: apiPort }; + this.git = { host: git, port: gitPort }; this.link = { host: seed.host, id: seed.id, port: cfg.seed.link.port }; if (seed.version) {