wip fix api db

This commit is contained in:
Rizky 2023-10-28 10:00:34 +07:00
parent 07ca4afb0c
commit 511e37268a
5 changed files with 57 additions and 15 deletions

View File

@ -0,0 +1,3 @@
export const EdApiDB = ({ db }: { db: string }) => {
return <div>DB</div>;
};

View File

@ -2,6 +2,7 @@ import { forwardRef } from "react";
import { useGlobal, useLocal } from "web-utils"; import { useGlobal, useLocal } from "web-utils";
import { EDGlobal } from "../../../logic/ed-global"; import { EDGlobal } from "../../../logic/ed-global";
import { apiUrl, checkAPI, dev } from "./api-utils"; import { apiUrl, checkAPI, dev } from "./api-utils";
import { EdApiDB } from "./api-db";
export const EdApiServer = forwardRef< export const EdApiServer = forwardRef<
HTMLDivElement, HTMLDivElement,
@ -13,8 +14,9 @@ export const EdApiServer = forwardRef<
const local = useLocal( const local = useLocal(
{ {
api_url: p.site.config.api_url, api_url: p.site.config.api_url,
status: "offline" as "online" | "offline" | "checking", status: "checking" as "online" | "error" | "offline" | "checking",
deployable: false, deployable: false,
db: "",
hasDB: false, hasDB: false,
}, },
() => { () => {
@ -33,18 +35,19 @@ export const EdApiServer = forwardRef<
const url = apiUrl(p); const url = apiUrl(p);
const check = () => { const check = async () => {
local.status = "checking";
local.render(); local.render();
const res = checkAPI(p); const res = await checkAPI(p);
if (res) { if (typeof res === "object") {
local.db = res.db;
local.hasDB = res.hasDB; local.hasDB = res.hasDB;
local.status = "online"; local.status = "online";
local.deployable = res.deployable; local.deployable = res.deployable;
local.render(); local.render();
} else { } else {
local.db = "";
local.hasDB = false; local.hasDB = false;
local.status = "offline"; local.status = res;
local.deployable = false; local.deployable = false;
local.render(); local.render();
} }
@ -73,6 +76,9 @@ export const EdApiServer = forwardRef<
{local.status === "offline" && ( {local.status === "offline" && (
<div className="text-white px-2 bg-slate-500">OFFLINE</div> <div className="text-white px-2 bg-slate-500">OFFLINE</div>
)} )}
{local.status === "error" && (
<div className="text-white px-2 bg-red-500">SERVER ERROR</div>
)}
{local.status === "checking" && ( {local.status === "checking" && (
<div className="text-blue-500">Checking...</div> <div className="text-blue-500">Checking...</div>
)} )}
@ -160,7 +166,7 @@ export const EdApiServer = forwardRef<
</div> </div>
{local.status === "online" && ( {local.status === "online" && (
<> <>
{!local.deployable && !local.hasDB && ( {!local.deployable && !local.db && (
<div className="h-[50px] flex items-center justify-center text-slate-400 text-center"> <div className="h-[50px] flex items-center justify-center text-slate-400 text-center">
This server is not deployable <br /> This server is not deployable <br />
and do not have DB and do not have DB
@ -168,6 +174,7 @@ export const EdApiServer = forwardRef<
)} )}
</> </>
)} )}
{local.hasDB && <EdApiDB db={local.db} />}
</div> </div>
); );
}); });

View File

@ -1,3 +1,4 @@
import { createAPI, initApi } from "../../../../../utils/script/init-api";
import { PG } from "../../../logic/ed-global"; import { PG } from "../../../logic/ed-global";
export const dev = JSON.parse(localStorage.getItem("prasi-dev") || "{}") as { export const dev = JSON.parse(localStorage.getItem("prasi-dev") || "{}") as {
@ -33,9 +34,33 @@ export const apiUrl = function (p: PG): string {
return ""; return "";
}; };
export const checkAPI = (p: PG) => { const apiDef = {} as any;
const url = apiUrl(p);
if (!url) return null;
return { deployable: false, hasDB: false }; export const checkAPI = async (p: PG) => {
const url = apiUrl(p);
if (!url) return "offline";
try {
if (!apiDef[url]) {
await initApi({ api_url: url }, "dev");
apiDef[url] = createAPI(url);
}
const capi = apiDef[url];
let res = await capi._deploy({
type: "check",
id_site: p.site.id,
});
if (!res) {
return { deployable: false, db: "", hasDB: false };
} else {
if (res.db && res.now) {
return { deployable: false, db: res.db, hasDB: true };
}
}
} catch (e) {
console.error(e);
return "error";
}
return { deployable: false, db: "", hasDB: false };
}; };

View File

@ -22,6 +22,7 @@ export const createAPI = (url: string) => {
if (!url) { if (!url) {
throw new Error("No URL provided"); throw new Error("No URL provided");
} }
return w.apiClient(w.prasiApi[url]?.apiEntry, url); return w.apiClient(w.prasiApi[url]?.apiEntry, url);
}; };

View File

@ -17,11 +17,17 @@ export const apiClient = (
_apiURL = this.apiUrl; _apiURL = this.apiUrl;
} }
if (!api || !api[actionName]) { if (!api) {
resolve(null); reject(
console.error( new Error(`API Definition for ${_apiURL} is not loaded.`)
);
return;
}
if (api && !api[actionName]) {
reject(
`API ${actionName.toString()} not found, existing API: \n - ${Object.keys( `API ${actionName.toString()} not found, existing API: \n - ${Object.keys(
api api || {}
).join("\n - ")}` ).join("\n - ")}`
); );
return; return;