diff --git a/app/srv/ws/sync/actions/code_edit.ts b/app/srv/ws/sync/actions/code_edit.ts index 39d64631..1390b5b7 100644 --- a/app/srv/ws/sync/actions/code_edit.ts +++ b/app/srv/ws/sync/actions/code_edit.ts @@ -75,7 +75,7 @@ export const code_edit: SAction["code"]["edit"] = async function ( } } } else { - const { comp_id, prop_kind, prop_name, value } = arg; + const { comp_id, prop_kind, prop_name } = arg; if (comp_id) { const ref = docs.comp[comp_id]; if (ref) { diff --git a/app/web/src/base/load/api/api-proxy.tsx b/app/web/src/base/load/api/api-proxy.tsx index 22612af4..8985260b 100644 --- a/app/web/src/base/load/api/api-proxy.tsx +++ b/app/web/src/base/load/api/api-proxy.tsx @@ -15,7 +15,7 @@ export const apiProxy = (api_url: string) => { const base_url = `${base.protocol}//${base.host}`; if (!w.prasiApi[base_url]) { if (!apiProxyPending[base_url]) { - apiProxyPending[base_url] = loadApiProxyDef(base_url, false); + throw new Error(`API Definition not found: ${base_url}`); } } diff --git a/app/web/src/base/load/proxy.ts b/app/web/src/base/load/proxy.ts index b25d5be9..0c85f0d1 100644 --- a/app/web/src/base/load/proxy.ts +++ b/app/web/src/base/load/proxy.ts @@ -66,11 +66,13 @@ export const fetchViaProxy = async ( } else { const res = await fetch(`/_proxy`, { method: "POST", - body: JSON.stringify({ - url, - body, - headers, - }), + body: JSON.stringify([ + { + url, + body, + headers, + }, + ]), headers: { "content-type": "application/json" }, }); return res.json(); diff --git a/app/web/src/index.tsx b/app/web/src/index.tsx index a330ff61..209317c5 100644 --- a/app/web/src/index.tsx +++ b/app/web/src/index.tsx @@ -1,6 +1,7 @@ import { Root as ReactRoot, createRoot } from "react-dom/client"; import { defineReact, defineWindow } from "web-utils"; import { apiProxy } from "./base/load/api/api-proxy"; +import { loadApiProxyDef } from "./base/load/api/api-proxy-def"; import { dbProxy } from "./base/load/db/db-proxy"; import { Root } from "./base/root"; import "./index.css"; @@ -18,6 +19,8 @@ const start = async () => { const cur = new URL(location.href); const base_url = `${cur.protocol}//${cur.host}`; w.db = dbProxy(base_url); + + await loadApiProxyDef(base_url, false); w.api = apiProxy(base_url); w.serverurl = base; diff --git a/app/web/src/nova/ed/logic/ed-global.ts b/app/web/src/nova/ed/logic/ed-global.ts index e470f18b..bdb99da3 100644 --- a/app/web/src/nova/ed/logic/ed-global.ts +++ b/app/web/src/nova/ed/logic/ed-global.ts @@ -21,7 +21,11 @@ export const EmptySite = { config: { api_url: "" }, js: "", js_compiled: "", - layout: { id: "", meta: {} as Record, entry: [] as string[] }, + layout: { + id: "--", + meta: {} as Record, + entry: [] as string[], + }, }; export type ESite = typeof EmptySite; diff --git a/app/web/src/nova/ed/logic/ed-route.ts b/app/web/src/nova/ed/logic/ed-route.ts index 49a73751..8f5beaa3 100644 --- a/app/web/src/nova/ed/logic/ed-route.ts +++ b/app/web/src/nova/ed/logic/ed-route.ts @@ -15,7 +15,7 @@ export const edRoute = async (p: PG) => { return; } - await loadSite(p, site); + await loadSite(p, site, "from-route"); } if ( diff --git a/app/web/src/nova/ed/logic/ed-site.ts b/app/web/src/nova/ed/logic/ed-site.ts index 35efd6aa..6cdb555d 100644 --- a/app/web/src/nova/ed/logic/ed-site.ts +++ b/app/web/src/nova/ed/logic/ed-site.ts @@ -2,10 +2,12 @@ import { viLoadLegacy } from "../../vi/load/load-legacy"; import { ESite, PG } from "./ed-global"; import { reloadPage } from "./ed-route"; -export const loadSite = async (p: PG, site: ESite) => { +export const loadSite = async (p: PG, site: ESite, note: string) => { + console.log("note", note); const old_layout_id = p.site.layout.id; const layout_changed = p.site.layout.id !== site.layout.id; p.site = site; + if (layout_changed) { const old_layout = p.page.list[old_layout_id]; diff --git a/app/web/src/nova/ed/logic/ed-sync.tsx b/app/web/src/nova/ed/logic/ed-sync.tsx index a9093c73..9551b860 100644 --- a/app/web/src/nova/ed/logic/ed-sync.tsx +++ b/app/web/src/nova/ed/logic/ed-sync.tsx @@ -31,12 +31,13 @@ export const edInitSync = (p: PG) => { p.ui.popup.code.init = false; p.sync.site.load(params.site_id).then(async (site) => { if (site) { - await loadSite(p, site); + await loadSite(p, site, "from-sync"); p.render(); } else { alert("Site not found. redirecting..."); location.href = `/ed/`; } + return; }); return false; } diff --git a/app/web/src/nova/ed/panel/popup/script/scope/extract-exim.tsx b/app/web/src/nova/ed/panel/popup/script/scope/extract-exim.tsx new file mode 100644 index 00000000..6391024b --- /dev/null +++ b/app/web/src/nova/ed/panel/popup/script/scope/extract-exim.tsx @@ -0,0 +1,118 @@ +import { IMeta, PG } from "../../../../logic/ed-global"; + +export const extractExportImport = (p: PG, m: IMeta, imports: string[]) => { + let _export = {}; + + const def = m.scope.def; + if (def) { + if (def.local) { + const local = extractLocal(p, m, def, imports); + if (local) { + for (const [k, v] of Object.entries(local)) { + v.names.forEach((n) => + imports.push(`import { ${n} } from "./${k}";`) + ); + } + } + } + } + + return _export; +}; + +const extractLocal = ( + p: PG, + m: IMeta, + def: IMeta["scope"]["def"], + imports: string[] +) => { + if (def?.local) { + let loc = { + item_id: m.item.id, + comp_id: m.parent?.comp_id, + type: "item", + }; + let filename = `ts:scope~${JSON.stringify(loc)}.d.ts`; + return { + [filename]: { + names: [def.local.name], + src: `\ +${imports.join("\n")} +type _local = ${def.local.value}; +export const ${def.local.name}: _local & { render: () =>void }; + `, + }, + }; + } +}; + +const extractPassProp = ( + p: PG, + m: IMeta, + def: IMeta["scope"]["def"], + imports: string[] +) => { + if (def?.passprop) { + let loc = { + item_id: m.item.id, + comp_id: m.parent?.comp_id, + type: "item", + }; + let filename = `ts:scope~${JSON.stringify(loc)}.d.ts`; + + const result = { + names: [] as string[], + src: "", + }; + + const exports: string[] = []; + for (const [e, v] of Object.entries(def.passprop)) { + if (e !== "idx" && e !== "key") { + result.names.push(e); + exports.push(`export const ${e} = ${v.value};`); + } + } + result.src = `\ +${imports.join("\n")} +${exports.join("\n")} +`; + + return { [filename]: result }; + } +}; + +const extractProps = ( + p: PG, + m: IMeta, + def: IMeta["scope"]["def"], + imports: string[] +) => { + if (def?.passprop) { + let loc = { + item_id: m.item.id, + comp_id: m.parent?.comp_id, + type: "item", + }; + let filename = `ts:scope~${JSON.stringify(loc)}.d.ts`; + + const result = { + names: [] as string[], + src: "", + }; + + const exports: string[] = []; + for (const [e, v] of Object.entries(def.passprop)) { + if (e !== "idx" && e !== "key") { + result.names.push(e); + exports.push(`export const ${e} = ${v.value};`); + } + } + + result.src = `\ +${imports.join("\n")} +${exports.join("\n")} +`; + + return { [filename]: result }; + } +}; diff --git a/app/web/src/nova/ed/panel/popup/script/scope/gen-import.tsx b/app/web/src/nova/ed/panel/popup/script/scope/gen-import.tsx deleted file mode 100644 index abd3da24..00000000 --- a/app/web/src/nova/ed/panel/popup/script/scope/gen-import.tsx +++ /dev/null @@ -1,3 +0,0 @@ -export const genImport = () => { - return ``; -}; diff --git a/app/web/src/nova/ed/panel/popup/script/scope/scope-parent.tsx b/app/web/src/nova/ed/panel/popup/script/scope/scope-parent.tsx index 8d4b5638..f6641843 100644 --- a/app/web/src/nova/ed/panel/popup/script/scope/scope-parent.tsx +++ b/app/web/src/nova/ed/panel/popup/script/scope/scope-parent.tsx @@ -26,36 +26,12 @@ export const defineScopeParent = (p: PG, meta: IMeta, monaco: Monaco) => { m.scope.def.props = meta.scope?.def?.props; } } + const def = m.scope.def; if (def) { if (def.local) { - addScope(p, "local", { - monaco, - loc: { - item_id: m.item.id, - comp_id: m.parent?.comp_id, - type: "item", - }, - source: `\ -type _local = ${def.local.value}; -export const ${def.local.name}: _local & { render: () =>void }; -`, - }); } else if (def.passprop) { - Object.entries(def.passprop).map(([e, v]) => { - if (e !== "idx" && e !== "key") { - addScope(p, "passprop", { - monaco, - loc: { - item_id: m.item.id, - comp_id: m.parent?.comp_id, - type: "item", - }, - source: `\ -export const ${e} = ${v.value};`, - }); - } - }); + } else if (def.props) { } } } diff --git a/app/web/src/nova/vi/load/load-legacy.tsx b/app/web/src/nova/vi/load/load-legacy.tsx index 74a276c3..8afacef8 100644 --- a/app/web/src/nova/vi/load/load-legacy.tsx +++ b/app/web/src/nova/vi/load/load-legacy.tsx @@ -1,3 +1,4 @@ +import { loadApiProxyDef } from "../../../base/load/api/api-proxy-def"; import importModule from "../../../render/editor/tools/dynamic-import"; import { viScriptArg } from "../render/script/arg"; @@ -38,7 +39,7 @@ export const viLoadLegacy = async (vi: { let api_url = vi.site.api_url; if (!api_url) api_url = ((site.config as any) || {}).api_url || ""; - // await initApi(site.config); + await loadApiProxyDef(api_url, true); const path = `/npm/site/${vi.site.id}/site.js`; await importModule(path); diff --git a/pkgs/core/api/_proxy.ts b/pkgs/core/api/_proxy.ts index 6ae3541a..168f539e 100644 --- a/pkgs/core/api/_proxy.ts +++ b/pkgs/core/api/_proxy.ts @@ -8,11 +8,18 @@ export const _ = { headers: any; body: any; }) { - const res = await fetch(arg.url, { - method: arg.method, - headers: arg.headers, - body: arg.body, - }); + const res = await fetch( + arg.url, + arg.body + ? { + method: arg.method || "POST", + headers: arg.headers, + body: arg.body, + } + : { + headers: arg.headers, + } + ); return res as any; }, }; diff --git a/pkgs/core/server/serve-api.ts b/pkgs/core/server/serve-api.ts index f5d46e3f..2a32daa4 100644 --- a/pkgs/core/server/serve-api.ts +++ b/pkgs/core/server/serve-api.ts @@ -11,22 +11,31 @@ export const serveAPI = { }, serve: async (url: URL, req: Request) => { let found = g.router.lookup(url.pathname); + let found_not_match = false; if (!found?.url) { - if (!url.pathname.endsWith("/")) { - found = g.router.lookup(url.pathname + "/"); + let pathname = url.pathname; + if (!pathname.endsWith("/")) { + pathname = pathname + "/"; + found = g.router.lookup(pathname); + found_not_match = true; + } + if (!pathname.endsWith("_")) { + found = g.router.lookup(pathname + "_"); + found_not_match = true; } if (!found?.url) { found = null; } } - if (found) { const params = { ...found.params }; - let args = found.args.map((e) => { - return params[e]; - }); + let args = found_not_match + ? [] + : found.args.map((e) => { + return params[e]; + }); if (req.method !== "GET") { if (