mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-06 10:11:31 +00:00
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
This commit is contained in:
1 parent
8d26a6a0d6
commit
38878281ee
6 files changed
+40
-34
No files matched your search
@@ -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;
|
||||
};
|
||||
+10
-8
@@ -1,6 +1,6 @@
|
||||
import GetApiHost from "./GetApiHost";
|
||||
|
||||
const [ apiHost, disableSSL = false ] = GetApiHost();
|
||||
const { host = "", useSsl = true } = GetApiHost();
|
||||
|
||||
type UrlRoutingFunction<UrlRouteArgs> = (urlRouteArgs: UrlRouteArgs) => string;
|
||||
|
||||
@@ -13,29 +13,31 @@ interface RouteSetup<UrlRouteArgs> {
|
||||
routingFunction: UrlRoutingFunction<UrlRouteArgs>;
|
||||
}
|
||||
|
||||
const GrabPath = (endpoint = "") => `${ disableSSL ? "http" : "https" }://${ apiHost }/${ endpoint }`;
|
||||
const formatUrl = (endpoint = "") => `${ useSsl ? "https" : "http" }://${ host + endpoint }`;
|
||||
|
||||
const GrabEndpoint = <UrlRouteArgs, Request, Response>(routeSetup: RouteSetup<UrlRouteArgs>) : (urlRouteArgs: UrlRouteArgs, request: Request) => Promise<Response> => {
|
||||
const MakeRequest = <UrlRouteArgs, Request, Response>(routeSetup: RouteSetup<UrlRouteArgs>) : (urlRouteArgs: UrlRouteArgs, request: Request) => Promise<Response> => {
|
||||
return async function(urlRouteArgs: UrlRouteArgs, request: Request) : Promise<Response> {
|
||||
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 };
|
||||
export default MakeRequest;
|
||||
@@ -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<string, CreateVoiceRequest, CreateVoiceResponse>({
|
||||
const CreateVoice = MakeRequest<string, CreateVoiceRequest, CreateVoiceResponse>({
|
||||
method: "POST",
|
||||
routingFunction: () => "voice_designer/create",
|
||||
routingFunction: () => "/voice_designer/create",
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -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<string, DreateVoiceRequest, DreateVoiceResponse>({
|
||||
const DeleteVoice = MakeRequest<string, DreateVoiceRequest, DreateVoiceResponse>({
|
||||
method: "POST",
|
||||
routingFunction: (voiceToken: string) => `${ voiceToken }/delete`,
|
||||
routingFunction: (voiceToken: string) => `/${ voiceToken }/delete`,
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -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<string, ListVoiceRequest, ListVoiceResponse>({
|
||||
const List = MakeRequest<string, ListVoiceRequest, ListVoiceResponse>({
|
||||
method: "GET",
|
||||
routingFunction: (userToken: string) => `user/${ userToken }/list`,
|
||||
routingFunction: (userToken: string) => `/user/${ userToken }/list`,
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -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<string, UpdateVoiceRequest, UpdateVoiceResponse>({
|
||||
const UpdateVoice = MakeRequest<string, UpdateVoiceRequest, UpdateVoiceResponse>({
|
||||
method: "PATCH",
|
||||
routingFunction: (voiceToken: string) => `${ voiceToken }/update`,
|
||||
routingFunction: (voiceToken: string) => `/${ voiceToken }/update`,
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user