initial Endpoints progress

This commit is contained in:
Victor Holguin committed 2023-10-26 00:45:40 -07:00
1 parent 6f43fb8451
commit f6a247d9be
2 files changed
+73

No files matched your search

+47
View File
@@ -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;
+26
View File
@@ -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;