From cd5d0fc840972bdb9ac4d8ccd4ed8509d7abf731 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 19 Feb 2024 20:08:07 +0800 Subject: [PATCH] :bug: Fix request issue in garfish --- pkg/view/src/index.tsx | 2 ++ pkg/view/src/pages/auth/connect.tsx | 5 +++-- pkg/view/src/pages/auth/login.tsx | 9 +++++---- pkg/view/src/pages/auth/register.tsx | 3 ++- pkg/view/src/pages/dashboard.tsx | 3 ++- pkg/view/src/pages/personalise.tsx | 5 +++-- pkg/view/src/pages/security.tsx | 9 +++++---- pkg/view/src/pages/users/confirm.tsx | 3 ++- pkg/view/src/scripts/request.ts | 4 ++++ pkg/view/src/stores/userinfo.tsx | 3 ++- pkg/view/src/stores/wellKnown.tsx | 3 ++- 11 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 pkg/view/src/scripts/request.ts diff --git a/pkg/view/src/index.tsx b/pkg/view/src/index.tsx index faf4caa..459afe9 100644 --- a/pkg/view/src/index.tsx +++ b/pkg/view/src/index.tsx @@ -41,6 +41,7 @@ declare const __GARFISH_EXPORTS__: { declare global { interface Window { __GARFISH__: boolean; + __LAUNCHPAD_TARGET__?: string; } } @@ -60,6 +61,7 @@ if (!window.__GARFISH__) { render(router, root!); } else if (typeof __GARFISH_EXPORTS__ !== "undefined") { console.log("Running in launchpad container!") + console.log("Launchpad target:", window.__LAUNCHPAD_TARGET__) if (__GARFISH_EXPORTS__.registerProvider) { __GARFISH_EXPORTS__.registerProvider(provider); } else { diff --git a/pkg/view/src/pages/auth/connect.tsx b/pkg/view/src/pages/auth/connect.tsx index 896505d..3ed65c6 100644 --- a/pkg/view/src/pages/auth/connect.tsx +++ b/pkg/view/src/pages/auth/connect.tsx @@ -1,6 +1,7 @@ import { createSignal, Show } from "solid-js"; import { useLocation, useSearchParams } from "@solidjs/router"; import { getAtk, useUserinfo } from "../../stores/userinfo.tsx"; +import { request } from "../../scripts/request.ts"; export default function OauthConnectPage() { const [title, setTitle] = createSignal("Connect Third-party"); @@ -18,7 +19,7 @@ export default function OauthConnectPage() { const location = useLocation(); async function preConnect() { - const res = await fetch(`/api/auth/o/connect${location.search}`, { + const res = await request(`/api/auth/o/connect${location.search}`, { headers: { "Authorization": `Bearer ${getAtk()}` } }); @@ -51,7 +52,7 @@ export default function OauthConnectPage() { setLoading(true); setStatus("Approving..."); - const res = await fetch("/api/auth/o/connect?" + new URLSearchParams({ + const res = await request("/api/auth/o/connect?" + new URLSearchParams({ client_id: searchParams["client_id"] as string, redirect_uri: encodeURIComponent(searchParams["redirect_uri"] as string), response_type: "code", diff --git a/pkg/view/src/pages/auth/login.tsx b/pkg/view/src/pages/auth/login.tsx index 5086d7c..51f2ec9 100644 --- a/pkg/view/src/pages/auth/login.tsx +++ b/pkg/view/src/pages/auth/login.tsx @@ -1,6 +1,7 @@ import { readProfiles } from "../../stores/userinfo.tsx"; import { useNavigate, useSearchParams } from "@solidjs/router"; import { createSignal, For, Match, Show, Switch } from "solid-js"; +import { request } from "../../scripts/request.ts"; export default function LoginPage() { const [title, setTitle] = createSignal("Sign in"); @@ -26,7 +27,7 @@ export default function LoginPage() { if (!data.id) return; setLoading(true); - const res = await fetch("/api/auth", { + const res = await request("/api/auth", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data) @@ -51,7 +52,7 @@ export default function LoginPage() { if (!data.factor) return; setLoading(true); - const res = await fetch(`/api/auth/factors/${data.factor}`, { + const res = await request(`/api/auth/factors/${data.factor}`, { method: "POST" }); if (res.status !== 200 && res.status !== 204) { @@ -72,7 +73,7 @@ export default function LoginPage() { if (!data.credentials) return; setLoading(true); - const res = await fetch(`/api/auth`, { + const res = await request(`/api/auth`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ @@ -102,7 +103,7 @@ export default function LoginPage() { }; async function grantToken(tk: string) { - const res = await fetch("/api/auth/token", { + const res = await request("/api/auth/token", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ diff --git a/pkg/view/src/pages/auth/register.tsx b/pkg/view/src/pages/auth/register.tsx index 3a35cae..cc06261 100644 --- a/pkg/view/src/pages/auth/register.tsx +++ b/pkg/view/src/pages/auth/register.tsx @@ -1,6 +1,7 @@ import { createSignal, Show } from "solid-js"; import { useWellKnown } from "../../stores/wellKnown.tsx"; import { useNavigate, useSearchParams } from "@solidjs/router"; +import { request } from "../../scripts/request.ts"; export default function RegisterPage() { const [title, setTitle] = createSignal("Create an account"); @@ -22,7 +23,7 @@ export default function RegisterPage() { if (!data.name || !data.nick || !data.email || !data.password) return; setLoading(true); - const res = await fetch("/api/users", { + const res = await request("/api/users", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data) diff --git a/pkg/view/src/pages/dashboard.tsx b/pkg/view/src/pages/dashboard.tsx index cc0f714..fb05010 100644 --- a/pkg/view/src/pages/dashboard.tsx +++ b/pkg/view/src/pages/dashboard.tsx @@ -1,5 +1,6 @@ import { getAtk, readProfiles, useUserinfo } from "../stores/userinfo.tsx"; import { createSignal, For, Show } from "solid-js"; +import { request } from "../scripts/request.ts"; export default function DashboardPage() { const userinfo = useUserinfo(); @@ -19,7 +20,7 @@ export default function DashboardPage() { } async function readNotification(item: any) { - const res = await fetch(`/api/notifications/${item.id}/read`, { + const res = await request(`/api/notifications/${item.id}/read`, { method: "PUT", headers: { Authorization: `Bearer ${getAtk()}` } }); diff --git a/pkg/view/src/pages/personalise.tsx b/pkg/view/src/pages/personalise.tsx index 4ebf616..099de36 100644 --- a/pkg/view/src/pages/personalise.tsx +++ b/pkg/view/src/pages/personalise.tsx @@ -1,5 +1,6 @@ import { getAtk, readProfiles, useUserinfo } from "../stores/userinfo.tsx"; import { createSignal, Show } from "solid-js"; +import { request } from "../scripts/request.ts"; export default function PersonalPage() { const userinfo = useUserinfo(); @@ -14,7 +15,7 @@ export default function PersonalPage() { const data = Object.fromEntries(new FormData(evt.target as HTMLFormElement)); setLoading(true); - const res = await fetch("/api/users/me", { + const res = await request("/api/users/me", { method: "PUT", headers: { "Content-Type": "application/json", @@ -38,7 +39,7 @@ export default function PersonalPage() { setLoading(true); const data = new FormData(evt.target as HTMLFormElement); - const res = await fetch("/api/avatar", { + const res = await request("/api/avatar", { method: "PUT", headers: { "Authorization": `Bearer ${getAtk()}` }, body: data diff --git a/pkg/view/src/pages/security.tsx b/pkg/view/src/pages/security.tsx index 4da6552..95280b8 100644 --- a/pkg/view/src/pages/security.tsx +++ b/pkg/view/src/pages/security.tsx @@ -1,5 +1,6 @@ import { getAtk } from "../stores/userinfo.tsx"; import { createSignal, For, Match, Show, Switch } from "solid-js"; +import { request } from "../scripts/request.ts"; export default function DashboardPage() { const [challenges, setChallenges] = createSignal([]); @@ -15,7 +16,7 @@ export default function DashboardPage() { const [contentTab, setContentTab] = createSignal(0); async function readChallenges() { - const res = await fetch("/api/users/me/challenges?take=10", { + const res = await request("/api/users/me/challenges?take=10", { headers: { Authorization: `Bearer ${getAtk()}` } }); if (res.status !== 200) { @@ -28,7 +29,7 @@ export default function DashboardPage() { } async function readSessions() { - const res = await fetch("/api/users/me/sessions?take=10", { + const res = await request("/api/users/me/sessions?take=10", { headers: { Authorization: `Bearer ${getAtk()}` } }); if (res.status !== 200) { @@ -41,7 +42,7 @@ export default function DashboardPage() { } async function readEvents() { - const res = await fetch("/api/users/me/events?take=10", { + const res = await request("/api/users/me/events?take=10", { headers: { Authorization: `Bearer ${getAtk()}` } }); if (res.status !== 200) { @@ -55,7 +56,7 @@ export default function DashboardPage() { async function killSession(item: any) { setSubmitting(true); - const res = await fetch(`/api/users/me/sessions/${item.id}`, { + const res = await request(`/api/users/me/sessions/${item.id}`, { method: "DELETE", headers: { Authorization: `Bearer ${getAtk()}` } }); diff --git a/pkg/view/src/pages/users/confirm.tsx b/pkg/view/src/pages/users/confirm.tsx index 8ff4e5c..5d3ee15 100644 --- a/pkg/view/src/pages/users/confirm.tsx +++ b/pkg/view/src/pages/users/confirm.tsx @@ -1,6 +1,7 @@ import { createSignal, Show } from "solid-js"; import { useNavigate, useSearchParams } from "@solidjs/router"; import { readProfiles } from "../../stores/userinfo.tsx"; +import { request } from "../../scripts/request.ts"; export default function ConfirmRegistrationPage() { const [error, setError] = createSignal(null); @@ -15,7 +16,7 @@ export default function ConfirmRegistrationPage() { setError("Bad Request: Code was not exists"); } - const res = await fetch("/api/users/me/confirm", { + const res = await request("/api/users/me/confirm", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ diff --git a/pkg/view/src/scripts/request.ts b/pkg/view/src/scripts/request.ts new file mode 100644 index 0000000..b85771b --- /dev/null +++ b/pkg/view/src/scripts/request.ts @@ -0,0 +1,4 @@ +export async function request(input: string, init?: RequestInit) { + const prefix = window.__LAUNCHPAD_TARGET__ ?? ""; + return await fetch(prefix + input, init) +} \ No newline at end of file diff --git a/pkg/view/src/stores/userinfo.tsx b/pkg/view/src/stores/userinfo.tsx index 16a87db..77f4dea 100644 --- a/pkg/view/src/stores/userinfo.tsx +++ b/pkg/view/src/stores/userinfo.tsx @@ -1,6 +1,7 @@ import Cookie from "universal-cookie"; import { createContext, useContext } from "solid-js"; import { createStore } from "solid-js/store"; +import { request } from "../scripts/request.ts"; export interface Userinfo { isLoggedIn: boolean, @@ -31,7 +32,7 @@ function checkLoggedIn(): boolean { export async function readProfiles() { if (!checkLoggedIn()) return; - const res = await fetch("/api/users/me", { + const res = await request("/api/users/me", { credentials: "include" }); diff --git a/pkg/view/src/stores/wellKnown.tsx b/pkg/view/src/stores/wellKnown.tsx index 5c5e821..42bebf3 100644 --- a/pkg/view/src/stores/wellKnown.tsx +++ b/pkg/view/src/stores/wellKnown.tsx @@ -1,12 +1,13 @@ import { createContext, useContext } from "solid-js"; import { createStore } from "solid-js/store"; +import { request } from "../scripts/request.ts"; const WellKnownContext = createContext(); const [wellKnown, setWellKnown] = createStore(null); export async function readWellKnown() { - const res = await fetch("/.well-known") + const res = await request("/.well-known") setWellKnown(await res.json()) }