Refactor to make it easier to set the api path.

This commit is contained in:
comfyanonymous
2023-07-14 00:46:25 -04:00
parent 30ea187160
commit 907c9fbf0d
4 changed files with 29 additions and 16 deletions

View File

@@ -3,6 +3,16 @@ class ComfyApi extends EventTarget {
constructor() {
super();
this.api_host = location.host;
this.api_base = location.pathname.split('/').slice(0, -1).join('/');
}
apiURL(route) {
return this.api_base + route;
}
fetchApi(route, options) {
return fetch(this.apiURL(route), options);
}
addEventListener(type, callback, options) {
@@ -16,7 +26,7 @@ class ComfyApi extends EventTarget {
#pollQueue() {
setInterval(async () => {
try {
const resp = await fetch("./prompt");
const resp = await this.fetchApi("/prompt");
const status = await resp.json();
this.dispatchEvent(new CustomEvent("status", { detail: status }));
} catch (error) {
@@ -40,7 +50,7 @@ class ComfyApi extends EventTarget {
existingSession = "?clientId=" + existingSession;
}
this.socket = new WebSocket(
`ws${window.location.protocol === "https:" ? "s" : ""}://${location.host}${location.pathname}ws${existingSession}`
`ws${window.location.protocol === "https:" ? "s" : ""}://${this.api_host}${this.api_base}/ws${existingSession}`
);
this.socket.binaryType = "arraybuffer";
@@ -149,7 +159,7 @@ class ComfyApi extends EventTarget {
* @returns An array of script urls to import
*/
async getExtensions() {
const resp = await fetch("./extensions", { cache: "no-store" });
const resp = await this.fetchApi("/extensions", { cache: "no-store" });
return await resp.json();
}
@@ -158,7 +168,7 @@ class ComfyApi extends EventTarget {
* @returns An array of script urls to import
*/
async getEmbeddings() {
const resp = await fetch("./embeddings", { cache: "no-store" });
const resp = await this.fetchApi("/embeddings", { cache: "no-store" });
return await resp.json();
}
@@ -167,7 +177,7 @@ class ComfyApi extends EventTarget {
* @returns The node definitions
*/
async getNodeDefs() {
const resp = await fetch("./object_info", { cache: "no-store" });
const resp = await this.fetchApi("/object_info", { cache: "no-store" });
return await resp.json();
}
@@ -189,7 +199,7 @@ class ComfyApi extends EventTarget {
body.number = number;
}
const res = await fetch("./prompt", {
const res = await this.fetchApi("/prompt", {
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -224,7 +234,7 @@ class ComfyApi extends EventTarget {
*/
async getQueue() {
try {
const res = await fetch("./queue");
const res = await this.fetchApi("/queue");
const data = await res.json();
return {
// Running action uses a different endpoint for cancelling
@@ -246,7 +256,7 @@ class ComfyApi extends EventTarget {
*/
async getHistory() {
try {
const res = await fetch("./history");
const res = await this.fetchApi("/history");
return { History: Object.values(await res.json()) };
} catch (error) {
console.error(error);
@@ -261,7 +271,7 @@ class ComfyApi extends EventTarget {
*/
async #postItem(type, body) {
try {
await fetch("./" + type, {
await this.fetchApi("/" + type, {
method: "POST",
headers: {
"Content-Type": "application/json",