This commit is contained in:
Rizky 2024-07-12 19:38:40 +07:00
parent 58ce3642bb
commit 6b5b8526ee
3 changed files with 27 additions and 6 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,4 @@
import trim from "lodash.trim"; import trim from "lodash.trim";
import { w } from "../../../utils/types/general";
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, "/");
@ -20,10 +18,18 @@ export const loadApiProxyDef = async (_url: string, with_types: boolean) => {
const ts = localStorage.getItem("api-ts-" + url); const ts = localStorage.getItem("api-ts-" + url);
const url_target = new URL(url);
const url_cur = new URL(location.href);
let is_remote = "";
if (url_target.host !== url_cur.host) {
is_remote = "&remote=1";
}
if (with_types) { if (with_types) {
script.src = `${base}/_prasi/load.js?url=${url}&v3&dev=1&ts=${ts}`; script.src = `/_prasi/load.js?url=${url}&v3&dev=1&ts=${ts}${is_remote}`;
} else { } else {
script.src = `${base}/_prasi/load.js?url=${url}&v3&ts=${ts}`; script.src = `/_prasi/load.js?url=${url}&v3&ts=${ts}${is_remote}`;
} }
script.onerror = () => { script.onerror = () => {
done(); done();

View File

@ -10,6 +10,7 @@ const cache = {
export const _ = { export const _ = {
url: "/_prasi/*", url: "/_prasi/*",
raw: true,
async api() { async api() {
const { req, res } = apiContext(this); const { req, res } = apiContext(this);
res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Origin", "*");
@ -25,6 +26,20 @@ export const _ = {
? JSON.stringify(req.query_parameters["url"]) ? JSON.stringify(req.query_parameters["url"])
: "undefined"; : "undefined";
const is_remote = req.query_parameters["remote"];
if (is_remote) {
const cur_url = new URL(req.url);
const remote_url = new URL(req.query_parameters["url"]);
cur_url.hostname = remote_url.hostname;
cur_url.port = remote_url.port;
cur_url.protocol = remote_url.protocol;
const res = await (await fetch(cur_url.toString())).text();
return new Response(res, {
headers: { "content-type": "text/javascript; charset=UTF-8" },
});
}
const mode = req.query_parameters["dev"] ? "dev" : "prod"; const mode = req.query_parameters["dev"] ? "dev" : "prod";
if (!cache[mode]) { if (!cache[mode]) {
@ -77,7 +92,7 @@ export const _ = {
const run = action[pathname]; const run = action[pathname];
if (run) { if (run) {
await run(); return await run();
} }
}, },
}; };