Voice Designer endpoints alongside GrabEndpoints shared fetch promise

This commit is contained in:
Victor Holguin committed 2023-10-30 13:41:57 -07:00
1 parent f6a247d9be
commit bff249a4fa
6 files changed
+117 -47

No files matched your search

-47
View File
@@ -1,47 +0,0 @@
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,41 @@
import GetApiHost from "./GetApiHost";
const [ apiHost, disableSSL = false ] = GetApiHost();
type UrlRoutingFunction<UrlRouteArgs> = (urlRouteArgs: UrlRouteArgs) => string;
interface RequestHeaders {
[name: string]: string,
}
interface RouteSetup<UrlRouteArgs> {
method: string;
routingFunction: UrlRoutingFunction<UrlRouteArgs>;
}
const GrabPath = (endpoint = "") => `${ disableSSL ? "http" : "https" }://${ apiHost }/${ endpoint }`;
const GrabEndpoint = <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;
return fetch(GrabPath(endpoint), {
method,
headers: {
"Accept": "application/json",
},
credentials: 'include',
...method === "GET" ? {} : { body: JSON.stringify(request) },
})
.then(res => res.json())
.then(res => {
if (res && 'success' in res) {
return res;
} else throw new Error;
})
.catch(e => ({ success : false }));
}
};
export { GrabEndpoint, GrabPath };
@@ -0,0 +1,19 @@
import { GrabEndpoint } from "../GrabEndpoint"
interface CreateVoiceRequest {
// args
example_token: String;
}
interface CreateVoiceResponse {
// args
name: string,
}
const CreateVoice = GrabEndpoint<string, CreateVoiceRequest, CreateVoiceResponse>({
method: "POST",
routingFunction: () => "voice_designer/create",
});
export default CreateVoice;
@@ -0,0 +1,19 @@
import { GrabEndpoint } from "../GrabEndpoint"
interface CreateVoiceRequest {
// args
example_token: String;
}
interface CreateVoiceResponse {
// args
name: string,
}
const DeleteVoice = GrabEndpoint<string, CreateVoiceRequest, CreateVoiceResponse>({
method: "POST",
routingFunction: (voiceToken: string) => `${ voiceToken }/delete`,
});
export default DeleteVoice;
@@ -0,0 +1,19 @@
import { GrabEndpoint } from "../GrabEndpoint"
interface CreateVoiceRequest {
// args
example_token: String;
}
interface CreateVoiceResponse {
// args
name: string,
}
const List = GrabEndpoint<string, CreateVoiceRequest, CreateVoiceResponse>({
method: "GET",
routingFunction: (userToken: string) => `user/${ userToken }/list`,
});
export default List;
@@ -0,0 +1,19 @@
import { GrabEndpoint } from "../GrabEndpoint"
interface CreateVoiceRequest {
// args
example_token: String;
}
interface CreateVoiceResponse {
// args
name: string,
}
const UpdateVoice = GrabEndpoint<string, CreateVoiceRequest, CreateVoiceResponse>({
method: "PATCH",
routingFunction: (voiceToken: string) => `${ voiceToken }/update`,
});
export default UpdateVoice;