From 3dc25a8447d779068901d56484f03201e424316f Mon Sep 17 00:00:00 2001 From: Rizky Date: Sat, 20 Jan 2024 18:53:06 +0700 Subject: [PATCH] wip fix --- Dockerfile | 1 - app/web/src/base/load/api/api-proxy.tsx | 161 ++++++++++++------------ app/web/src/nova/vi/render/script.tsx | 8 +- 3 files changed, 88 insertions(+), 82 deletions(-) diff --git a/Dockerfile b/Dockerfile index 81730988..675d78b7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,7 +15,6 @@ RUN echo $DATABASE_URL # RUN npm install -g node-gyp # RUN npm install -g node-gyp-build-optional-packages - # RUN curl -fsSL https://bun.sh/install | bash -s "bun-v1.0.18" # RUN ln -s $HOME/.bun/bin/bun /usr/local/bin/bun diff --git a/app/web/src/base/load/api/api-proxy.tsx b/app/web/src/base/load/api/api-proxy.tsx index e8ec16c6..90e2bfe9 100644 --- a/app/web/src/base/load/api/api-proxy.tsx +++ b/app/web/src/base/load/api/api-proxy.tsx @@ -11,96 +11,101 @@ export const apiProxy = (api_url: string) => { w.prasiApi = {}; } - const base = new URL(api_url); - const base_url = `${base.protocol}//${base.host}`; - if (!w.prasiApi[base_url]) { - if (!apiProxyLoaded[base_url]) { - apiProxyLoaded[base_url] = loadApiProxyDef(base_url, true); + try { + const base = new URL(api_url); + const base_url = `${base.protocol}//${base.host}`; + if (!w.prasiApi[base_url]) { + if (!apiProxyLoaded[base_url]) { + apiProxyLoaded[base_url] = loadApiProxyDef(base_url, true); + } } - } - return new Proxy( - {}, - { - get: (_, actionName: string) => { - const createFn = (actionName: string) => { - return function ( - this: { api_url: string } | undefined, - ...rest: any - ) { - return new Promise(async (resolve, reject) => { - try { - let api_ref = w.prasiApi[base_url]; + return new Proxy( + {}, + { + get: (_, actionName: string) => { + const createFn = (actionName: string) => { + return function ( + this: { api_url: string } | undefined, + ...rest: any + ) { + return new Promise(async (resolve, reject) => { + try { + let api_ref = w.prasiApi[base_url]; - if ( - !api_ref && - apiProxyLoaded && - typeof apiProxyLoaded[base_url] === "object" - ) { - await apiProxyLoaded[base_url]; - api_ref = w.prasiApi[base_url]; - } - - if (api_ref) { - if (!api_ref.apiEntry) api_ref.apiEntry = {}; - if (api_ref.apiEntry && !api_ref.apiEntry[actionName]) { - reject( - `API ${actionName.toString()} not found, existing API: \n - ${Object.keys( - api_ref || {} - ).join("\n - ")}` - ); - return; + if ( + !api_ref && + apiProxyLoaded && + typeof apiProxyLoaded[base_url] === "object" + ) { + await apiProxyLoaded[base_url]; + api_ref = w.prasiApi[base_url]; } - } - let actionUrl = api_ref.apiEntry[actionName].url; - const actionParams = api_ref.apiEntry[actionName].args; - if (actionUrl && actionParams) { - if (rest.length > 0 && actionParams.length > 0) { - for (const [idx, p] of Object.entries(rest)) { - const paramName = actionParams[parseInt(idx)]; - if (actionParams && actionParams.includes(paramName)) { - if ( - !!p && - typeof p !== "string" && - typeof p !== "number" - ) { - continue; - } - } - actionUrl = actionUrl.replace(`:${paramName}?`, p + ""); - actionUrl = actionUrl.replace(`:${paramName}`, p + ""); + if (api_ref) { + if (!api_ref.apiEntry) api_ref.apiEntry = {}; + if (api_ref.apiEntry && !api_ref.apiEntry[actionName]) { + reject( + `API ${actionName.toString()} not found, existing API: \n - ${Object.keys( + api_ref || {} + ).join("\n - ")}` + ); + return; } } - const url = `${base_url}${actionUrl}`; + let actionUrl = api_ref.apiEntry[actionName].url; + const actionParams = api_ref.apiEntry[actionName].args; + if (actionUrl && actionParams) { + if (rest.length > 0 && actionParams.length > 0) { + for (const [idx, p] of Object.entries(rest)) { + const paramName = actionParams[parseInt(idx)]; + if (actionParams && actionParams.includes(paramName)) { + if ( + !!p && + typeof p !== "string" && + typeof p !== "number" + ) { + continue; + } + } + actionUrl = actionUrl.replace(`:${paramName}?`, p + ""); + actionUrl = actionUrl.replace(`:${paramName}`, p + ""); + } + } - const result = await fetchSendApi(url, rest); - resolve(result); - } else { - console.error(`API Not Found: ${actionName.toString()}`); + const url = `${base_url}${actionUrl}`; + + const result = await fetchSendApi(url, rest); + resolve(result); + } else { + console.error(`API Not Found: ${actionName.toString()}`); + } + } catch (e) { + reject(e); } - } catch (e) { - reject(e); - } - }); + }); + }; }; - }; - if (actionName === "then") { - return new Proxy( - {}, - { - get: (_, actionName: string) => { - return createFn(actionName); - }, - } - ); - } + if (actionName === "then") { + return new Proxy( + {}, + { + get: (_, actionName: string) => { + return createFn(actionName); + }, + } + ); + } - return createFn(actionName); - }, - } - ); + return createFn(actionName); + }, + } + ); + } catch (e) { + console.warn("Init API failed for URL: ", JSON.stringify(api_url)); + return null; + } }; const fetchSendApi = async (url: string, params: any) => { diff --git a/app/web/src/nova/vi/render/script.tsx b/app/web/src/nova/vi/render/script.tsx index d554e548..bdd94131 100644 --- a/app/web/src/nova/vi/render/script.tsx +++ b/app/web/src/nova/vi/render/script.tsx @@ -1,4 +1,4 @@ -import { FC, ReactNode } from "react"; +import { FC, ReactNode, useEffect, useState } from "react"; import { useGlobal, useLocal } from "web-utils"; import { IMeta } from "../../ed/logic/ed-global"; import { ViGlobal } from "./global"; @@ -12,8 +12,10 @@ export const ViScript: FC<{ passprop?: any; }> = ({ meta, children, passprop }) => { const vi = useGlobal(ViGlobal, "VI"); - const local = useLocal({}); - meta.render = local.render; + const [_, _set] = useState({}); + meta.render = () => { + _set({}); + }; let _pass = passprop; if (meta.item.component?.id) {