diff --git a/packages/components/src/api/Endpoints.ts b/packages/components/src/api/Endpoints.ts new file mode 100644 index 0000000000..6cb281bc7a --- /dev/null +++ b/packages/components/src/api/Endpoints.ts @@ -0,0 +1,47 @@ +import GetApiHost from "./GetApiHost"; + +interface StrObj { + [key: string]: any; +} + +const n = (x: any) => ""; +const [ apiHost, disableSSL = false ] = GetApiHost(); + +const endpoints: { [key: string]: any } = { + voice_designer: { + create: () => `create`, + update: ({ voiceToken = "" }) => `${ voiceToken }/update`, + delete: ({ voiceToken = "" }) => `${ voiceToken }/delete`, + list: ({ userToken = "" }) => `user/${ userToken }/list`, + } +}; + +const getPath = (category = "", method = "") => + `${ disableSSL ? "http" : "https" }://${ apiHost }/${ ((endpoints[category] || {})[method] || n)() }`; + +const consumeEndpoint = async(category = "", method = "", request = {}) => { + return await fetch(getPath(category,method), { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify(request), + }).then(res => { + console.log("response got",res); + let resObj = res.json(); + if (resObj && "success" in res) { + console.log("a good one"); + return res + } else { + console.log("a bad one"); + throw new Error; + } + }).catch(e => { + console.log("🚛"); + return { success: false }; + }); +}; + +export default consumeEndpoint; \ No newline at end of file diff --git a/packages/components/src/api/GetApiHost.ts b/packages/components/src/api/GetApiHost.ts new file mode 100644 index 0000000000..4d1245c690 --- /dev/null +++ b/packages/components/src/api/GetApiHost.ts @@ -0,0 +1,26 @@ +const GetApiHost = () => { + switch (document.location.host.split(":")[0]) { // strip ports + case "fakeyou.com": + case "staging.fakeyou.com": + return ["api.fakeyou.com"]; + case "storyteller.ai": + case "staging.storyteller.ai": + return ["api.storyteller.ai"]; + case "storyteller.stream": // Storyteller.stream is deprecated and will be decommissioned in the future. + return ["api.storyteller.stream"]; + case "devproxy.fakeyou.com": + return ["api.fakeyou.com"]; + case "devproxy.storyteller.ai": + return ["api.storyteller.ai"]; + case "dev.fakeyou.com": + return ["api.dev.fakeyou.com"]; // NB: for dev machines with nginx proxies + case "dev.fakeyou.com": + return ["api.dev.fakeyou.com:12345",true]; // true = disableSSL + default: + return document.location.host.includes("localhost") ? + ["localhost:12345",document.location.protocol !== "https:"] : + ["api.fakeyou.com"]; // default + } +}; + +export default GetApiHost; \ No newline at end of file