From 38878281eecdb1341bc58c888cf761e1a61c3b3c Mon Sep 17 00:00:00 2001 From: Victor Holguin Date: Tue, 31 Oct 2023 13:29:44 -0700 Subject: [PATCH] using Promise.reject for request error handling, content-type for post/patch requests, GetApiHosts returns a config object vs array with useSsl inverted from disableSsl, plus additonal semantic function renaming --- packages/components/src/api/GetApiHost.ts | 32 +++++++++++-------- .../api/{GrabEndpoint.ts => MakeRequest.ts} | 18 ++++++----- .../src/api/voice_designer/CreateVoice.ts | 6 ++-- .../src/api/voice_designer/DeleteVoice.ts | 6 ++-- .../src/api/voice_designer/ListVoices.ts | 6 ++-- .../src/api/voice_designer/UpdateVoice.ts | 6 ++-- 6 files changed, 40 insertions(+), 34 deletions(-) rename packages/components/src/api/{GrabEndpoint.ts => MakeRequest.ts} (55%) diff --git a/packages/components/src/api/GetApiHost.ts b/packages/components/src/api/GetApiHost.ts index 4d1245c690..cee251418f 100644 --- a/packages/components/src/api/GetApiHost.ts +++ b/packages/components/src/api/GetApiHost.ts @@ -1,26 +1,30 @@ -const GetApiHost = () => { - switch (document.location.host.split(":")[0]) { // strip ports +// interface ApiDetails { hostname: string, useSsl: boolean, } + +const domainWithoutPort = document.location.host.split(":")[0]; + +const hostConfig = (host: string, useSsl = true) => ({ host, useSsl }); + +export default function GetApiHost() { + switch (domainWithoutPort) { case "fakeyou.com": case "staging.fakeyou.com": - return ["api.fakeyou.com"]; + return hostConfig("api.fakeyou.com"); case "storyteller.ai": case "staging.storyteller.ai": - return ["api.storyteller.ai"]; + return hostConfig("api.storyteller.ai"); case "storyteller.stream": // Storyteller.stream is deprecated and will be decommissioned in the future. - return ["api.storyteller.stream"]; + return hostConfig("api.storyteller.stream"); case "devproxy.fakeyou.com": - return ["api.fakeyou.com"]; + return hostConfig("api.fakeyou.com"); case "devproxy.storyteller.ai": - return ["api.storyteller.ai"]; + return hostConfig("api.storyteller.ai"); case "dev.fakeyou.com": - return ["api.dev.fakeyou.com"]; // NB: for dev machines with nginx proxies + return hostConfig("api.dev.fakeyou.com"); // NB: for dev machines with nginx proxies case "dev.fakeyou.com": - return ["api.dev.fakeyou.com:12345",true]; // true = disableSSL + return hostConfig("api.dev.fakeyou.com:12345",false); // true = disableSSL default: return document.location.host.includes("localhost") ? - ["localhost:12345",document.location.protocol !== "https:"] : - ["api.fakeyou.com"]; // default + hostConfig("localhost:12345",document.location.protocol === "https:") : + hostConfig("api.fakeyou.com"); // default } -}; - -export default GetApiHost; \ No newline at end of file +}; \ No newline at end of file diff --git a/packages/components/src/api/GrabEndpoint.ts b/packages/components/src/api/MakeRequest.ts similarity index 55% rename from packages/components/src/api/GrabEndpoint.ts rename to packages/components/src/api/MakeRequest.ts index b5adcc26d7..f03fe306a0 100644 --- a/packages/components/src/api/GrabEndpoint.ts +++ b/packages/components/src/api/MakeRequest.ts @@ -1,6 +1,6 @@ import GetApiHost from "./GetApiHost"; -const [ apiHost, disableSSL = false ] = GetApiHost(); +const { host = "", useSsl = true } = GetApiHost(); type UrlRoutingFunction = (urlRouteArgs: UrlRouteArgs) => string; @@ -13,29 +13,31 @@ interface RouteSetup { routingFunction: UrlRoutingFunction; } -const GrabPath = (endpoint = "") => `${ disableSSL ? "http" : "https" }://${ apiHost }/${ endpoint }`; +const formatUrl = (endpoint = "") => `${ useSsl ? "https" : "http" }://${ host + endpoint }`; -const GrabEndpoint = (routeSetup: RouteSetup) : (urlRouteArgs: UrlRouteArgs, request: Request) => Promise => { +const MakeRequest = (routeSetup: RouteSetup) : (urlRouteArgs: UrlRouteArgs, request: Request) => Promise => { return async function(urlRouteArgs: UrlRouteArgs, request: Request) : Promise { const endpoint = routeSetup.routingFunction(urlRouteArgs); const method = routeSetup.method; + const isGet = method === "GET"; - return fetch(GrabPath(endpoint), { + return fetch(formatUrl(endpoint), { method, headers: { "Accept": "application/json", + ...isGet ? {} : { "Content-Type": "application/json" } }, credentials: 'include', - ...method === "GET" ? {} : { body: JSON.stringify(request) }, + ...isGet ? {} : { body: JSON.stringify(request) }, }) .then(res => res.json()) .then(res => { - if (res && 'success' in res) { + if (res && "success" in res) { return res; - } else throw new Error; + } else Promise.reject(); }) .catch(e => ({ success : false })); } }; -export { GrabEndpoint, GrabPath }; \ No newline at end of file +export default MakeRequest; \ No newline at end of file diff --git a/packages/components/src/api/voice_designer/CreateVoice.ts b/packages/components/src/api/voice_designer/CreateVoice.ts index e281caafa0..690feaf491 100644 --- a/packages/components/src/api/voice_designer/CreateVoice.ts +++ b/packages/components/src/api/voice_designer/CreateVoice.ts @@ -1,4 +1,4 @@ -import { GrabEndpoint } from "../GrabEndpoint" +import MakeRequest from "../MakeRequest"; interface CreateVoiceRequest { uuid_idempotency_token: string, @@ -10,9 +10,9 @@ interface CreateVoiceResponse { inference_job_token: string, } -const CreateVoice = GrabEndpoint({ +const CreateVoice = MakeRequest({ method: "POST", - routingFunction: () => "voice_designer/create", + routingFunction: () => "/voice_designer/create", }); diff --git a/packages/components/src/api/voice_designer/DeleteVoice.ts b/packages/components/src/api/voice_designer/DeleteVoice.ts index 14940744d8..19e49c7be2 100644 --- a/packages/components/src/api/voice_designer/DeleteVoice.ts +++ b/packages/components/src/api/voice_designer/DeleteVoice.ts @@ -1,4 +1,4 @@ -import { GrabEndpoint } from "../GrabEndpoint" +import MakeRequest from "../MakeRequest"; interface DreateVoiceRequest { set_delete: boolean, @@ -9,9 +9,9 @@ interface DreateVoiceResponse { success: boolean } -const DeleteVoice = GrabEndpoint({ +const DeleteVoice = MakeRequest({ method: "POST", - routingFunction: (voiceToken: string) => `${ voiceToken }/delete`, + routingFunction: (voiceToken: string) => `/${ voiceToken }/delete`, }); diff --git a/packages/components/src/api/voice_designer/ListVoices.ts b/packages/components/src/api/voice_designer/ListVoices.ts index ca06321806..f5ba8888f9 100644 --- a/packages/components/src/api/voice_designer/ListVoices.ts +++ b/packages/components/src/api/voice_designer/ListVoices.ts @@ -1,4 +1,4 @@ -import { GrabEndpoint } from "../GrabEndpoint" +import MakeRequest from "../MakeRequest"; interface ListVoiceRequest {} @@ -14,9 +14,9 @@ interface ListVoiceResponse { updated_at: Date, } -const List = GrabEndpoint({ +const List = MakeRequest({ method: "GET", - routingFunction: (userToken: string) => `user/${ userToken }/list`, + routingFunction: (userToken: string) => `/user/${ userToken }/list`, }); diff --git a/packages/components/src/api/voice_designer/UpdateVoice.ts b/packages/components/src/api/voice_designer/UpdateVoice.ts index 8d0f0d6ff3..2ebc865fdc 100644 --- a/packages/components/src/api/voice_designer/UpdateVoice.ts +++ b/packages/components/src/api/voice_designer/UpdateVoice.ts @@ -1,4 +1,4 @@ -import { GrabEndpoint } from "../GrabEndpoint" +import MakeRequest from "../MakeRequest"; interface UpdateVoiceRequest { title: string, @@ -10,9 +10,9 @@ interface UpdateVoiceResponse { success: boolean, } -const UpdateVoice = GrabEndpoint({ +const UpdateVoice = MakeRequest({ method: "PATCH", - routingFunction: (voiceToken: string) => `${ voiceToken }/update`, + routingFunction: (voiceToken: string) => `/${ voiceToken }/update`, });