This commit is contained in:
Rizky 2024-07-12 18:14:55 +07:00
parent 2d42d9e482
commit a2fe29da7a
3 changed files with 53 additions and 78 deletions

File diff suppressed because one or more lines are too long

View File

@ -4,59 +4,34 @@ import { fetchViaProxy } from "../proxy";
export const loadApiProxyDef = async (_url: string, with_types: boolean) => { export const loadApiProxyDef = async (_url: string, with_types: boolean) => {
const url = trim(_url, "/"); const url = trim(_url, "/");
const raw = await fetchViaProxy(urlPath(url, "/_prasi/_"));
let ver = "";
if (raw && (raw as any).prasi) {
ver = (raw as any).prasi;
}
const base = baseUrl(url); const base = baseUrl(url);
if (ver === "v2") { await new Promise<void>((done) => {
await new Promise<void>((done) => { const d = document;
const d = document; const script = d.createElement("script");
const script = d.createElement("script"); script.onload = async () => {
script.onload = async () => { done();
done();
};
if (!localStorage.getItem("api-ts-" + url)) {
localStorage.setItem("api-ts-" + url, Date.now().toString());
}
const ts = localStorage.getItem("api-ts-" + url);
if (with_types) {
script.src = `${base}/_prasi/load.js?url=${url}&v3&dev=1&ts=${ts}`;
} else {
script.src = `${base}/_prasi/load.js?url=${url}&v3&ts=${ts}`;
}
if (!document.querySelector(`script[src="${script.src}"]`)) {
d.body.appendChild(script);
} else {
done();
}
});
} else {
const apiEntry = await fetch(base + "/_prasi/api-entry");
w.prasiApi[url] = {
apiEntry: (await apiEntry.json()).srv,
}; };
if (with_types) { if (!localStorage.getItem("api-ts-" + url)) {
const apiTypes = await fetch(base + "/_prasi/api-types"); localStorage.setItem("api-ts-" + url, Date.now().toString());
w.prasiApi[url].apiTypes = await apiTypes.text();
w.prasiApi[url].prismaTypes = {
"prisma.d.ts": await loadText(`${base}/_prasi/prisma/index.d.ts`),
"runtime/index.d.ts": await loadText(
`${base}/_prasi/prisma/runtime/index.d.ts`
),
"runtime/library.d.ts": await loadText(
`${base}/_prasi/prisma/runtime/library.d.ts`
),
};
} }
}
const ts = localStorage.getItem("api-ts-" + url);
if (with_types) {
script.src = `${base}/_prasi/load.js?url=${url}&v3&dev=1&ts=${ts}`;
} else {
script.src = `${base}/_prasi/load.js?url=${url}&v3&ts=${ts}`;
}
if (!document.querySelector(`script[src="${script.src}"]`)) {
d.body.appendChild(script);
} else {
done();
}
});
}; };
const baseUrl = (url: string) => { const baseUrl = (url: string) => {

View File

@ -9,7 +9,7 @@ export const startDevWatcher = async () => {
await dirAsync(dir.path(`app/srv/api`)); await dirAsync(dir.path(`app/srv/api`));
watch(dir.path(`app/srv/api`), async (event, filename) => { watch(dir.path(`app/srv/api`), async (event, filename) => {
const s = file(dir.path(`app/srv/api/${filename}`)); const s = file(dir.path(`app/srv/api/${filename}`));
if (s.size === 0) { if (s.size === 0 && filename?.endsWith(".ts") && (await s.exists())) {
await Bun.write( await Bun.write(
`app/srv/api/${filename}`, `app/srv/api/${filename}`,
`\ `\