mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-05 09:48:44 +00:00
initial Endpoints progress
This commit is contained in:
1 parent
6f43fb8451
commit
f6a247d9be
2 files changed
+73
No files matched your search
@@ -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;
|
||||
@@ -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;
|
||||
Reference in new issue
Block a user