🐛 Fix request issue in garfish

This commit is contained in:
LittleSheep 2024-02-19 20:08:07 +08:00
parent 3da3a6dcf0
commit cd5d0fc840
11 changed files with 32 additions and 17 deletions

View File

@ -41,6 +41,7 @@ declare const __GARFISH_EXPORTS__: {
declare global { declare global {
interface Window { interface Window {
__GARFISH__: boolean; __GARFISH__: boolean;
__LAUNCHPAD_TARGET__?: string;
} }
} }
@ -60,6 +61,7 @@ if (!window.__GARFISH__) {
render(router, root!); render(router, root!);
} else if (typeof __GARFISH_EXPORTS__ !== "undefined") { } else if (typeof __GARFISH_EXPORTS__ !== "undefined") {
console.log("Running in launchpad container!") console.log("Running in launchpad container!")
console.log("Launchpad target:", window.__LAUNCHPAD_TARGET__)
if (__GARFISH_EXPORTS__.registerProvider) { if (__GARFISH_EXPORTS__.registerProvider) {
__GARFISH_EXPORTS__.registerProvider(provider); __GARFISH_EXPORTS__.registerProvider(provider);
} else { } else {

View File

@ -1,6 +1,7 @@
import { createSignal, Show } from "solid-js"; import { createSignal, Show } from "solid-js";
import { useLocation, useSearchParams } from "@solidjs/router"; import { useLocation, useSearchParams } from "@solidjs/router";
import { getAtk, useUserinfo } from "../../stores/userinfo.tsx"; import { getAtk, useUserinfo } from "../../stores/userinfo.tsx";
import { request } from "../../scripts/request.ts";
export default function OauthConnectPage() { export default function OauthConnectPage() {
const [title, setTitle] = createSignal("Connect Third-party"); const [title, setTitle] = createSignal("Connect Third-party");
@ -18,7 +19,7 @@ export default function OauthConnectPage() {
const location = useLocation(); const location = useLocation();
async function preConnect() { 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()}` } headers: { "Authorization": `Bearer ${getAtk()}` }
}); });
@ -51,7 +52,7 @@ export default function OauthConnectPage() {
setLoading(true); setLoading(true);
setStatus("Approving..."); 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, client_id: searchParams["client_id"] as string,
redirect_uri: encodeURIComponent(searchParams["redirect_uri"] as string), redirect_uri: encodeURIComponent(searchParams["redirect_uri"] as string),
response_type: "code", response_type: "code",

View File

@ -1,6 +1,7 @@
import { readProfiles } from "../../stores/userinfo.tsx"; import { readProfiles } from "../../stores/userinfo.tsx";
import { useNavigate, useSearchParams } from "@solidjs/router"; import { useNavigate, useSearchParams } from "@solidjs/router";
import { createSignal, For, Match, Show, Switch } from "solid-js"; import { createSignal, For, Match, Show, Switch } from "solid-js";
import { request } from "../../scripts/request.ts";
export default function LoginPage() { export default function LoginPage() {
const [title, setTitle] = createSignal("Sign in"); const [title, setTitle] = createSignal("Sign in");
@ -26,7 +27,7 @@ export default function LoginPage() {
if (!data.id) return; if (!data.id) return;
setLoading(true); setLoading(true);
const res = await fetch("/api/auth", { const res = await request("/api/auth", {
method: "PUT", method: "PUT",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify(data) body: JSON.stringify(data)
@ -51,7 +52,7 @@ export default function LoginPage() {
if (!data.factor) return; if (!data.factor) return;
setLoading(true); setLoading(true);
const res = await fetch(`/api/auth/factors/${data.factor}`, { const res = await request(`/api/auth/factors/${data.factor}`, {
method: "POST" method: "POST"
}); });
if (res.status !== 200 && res.status !== 204) { if (res.status !== 200 && res.status !== 204) {
@ -72,7 +73,7 @@ export default function LoginPage() {
if (!data.credentials) return; if (!data.credentials) return;
setLoading(true); setLoading(true);
const res = await fetch(`/api/auth`, { const res = await request(`/api/auth`, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ body: JSON.stringify({
@ -102,7 +103,7 @@ export default function LoginPage() {
}; };
async function grantToken(tk: string) { async function grantToken(tk: string) {
const res = await fetch("/api/auth/token", { const res = await request("/api/auth/token", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ body: JSON.stringify({

View File

@ -1,6 +1,7 @@
import { createSignal, Show } from "solid-js"; import { createSignal, Show } from "solid-js";
import { useWellKnown } from "../../stores/wellKnown.tsx"; import { useWellKnown } from "../../stores/wellKnown.tsx";
import { useNavigate, useSearchParams } from "@solidjs/router"; import { useNavigate, useSearchParams } from "@solidjs/router";
import { request } from "../../scripts/request.ts";
export default function RegisterPage() { export default function RegisterPage() {
const [title, setTitle] = createSignal("Create an account"); 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; if (!data.name || !data.nick || !data.email || !data.password) return;
setLoading(true); setLoading(true);
const res = await fetch("/api/users", { const res = await request("/api/users", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify(data) body: JSON.stringify(data)

View File

@ -1,5 +1,6 @@
import { getAtk, readProfiles, useUserinfo } from "../stores/userinfo.tsx"; import { getAtk, readProfiles, useUserinfo } from "../stores/userinfo.tsx";
import { createSignal, For, Show } from "solid-js"; import { createSignal, For, Show } from "solid-js";
import { request } from "../scripts/request.ts";
export default function DashboardPage() { export default function DashboardPage() {
const userinfo = useUserinfo(); const userinfo = useUserinfo();
@ -19,7 +20,7 @@ export default function DashboardPage() {
} }
async function readNotification(item: any) { 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", method: "PUT",
headers: { Authorization: `Bearer ${getAtk()}` } headers: { Authorization: `Bearer ${getAtk()}` }
}); });

View File

@ -1,5 +1,6 @@
import { getAtk, readProfiles, useUserinfo } from "../stores/userinfo.tsx"; import { getAtk, readProfiles, useUserinfo } from "../stores/userinfo.tsx";
import { createSignal, Show } from "solid-js"; import { createSignal, Show } from "solid-js";
import { request } from "../scripts/request.ts";
export default function PersonalPage() { export default function PersonalPage() {
const userinfo = useUserinfo(); const userinfo = useUserinfo();
@ -14,7 +15,7 @@ export default function PersonalPage() {
const data = Object.fromEntries(new FormData(evt.target as HTMLFormElement)); const data = Object.fromEntries(new FormData(evt.target as HTMLFormElement));
setLoading(true); setLoading(true);
const res = await fetch("/api/users/me", { const res = await request("/api/users/me", {
method: "PUT", method: "PUT",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -38,7 +39,7 @@ export default function PersonalPage() {
setLoading(true); setLoading(true);
const data = new FormData(evt.target as HTMLFormElement); const data = new FormData(evt.target as HTMLFormElement);
const res = await fetch("/api/avatar", { const res = await request("/api/avatar", {
method: "PUT", method: "PUT",
headers: { "Authorization": `Bearer ${getAtk()}` }, headers: { "Authorization": `Bearer ${getAtk()}` },
body: data body: data

View File

@ -1,5 +1,6 @@
import { getAtk } from "../stores/userinfo.tsx"; import { getAtk } from "../stores/userinfo.tsx";
import { createSignal, For, Match, Show, Switch } from "solid-js"; import { createSignal, For, Match, Show, Switch } from "solid-js";
import { request } from "../scripts/request.ts";
export default function DashboardPage() { export default function DashboardPage() {
const [challenges, setChallenges] = createSignal<any[]>([]); const [challenges, setChallenges] = createSignal<any[]>([]);
@ -15,7 +16,7 @@ export default function DashboardPage() {
const [contentTab, setContentTab] = createSignal(0); const [contentTab, setContentTab] = createSignal(0);
async function readChallenges() { 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()}` } headers: { Authorization: `Bearer ${getAtk()}` }
}); });
if (res.status !== 200) { if (res.status !== 200) {
@ -28,7 +29,7 @@ export default function DashboardPage() {
} }
async function readSessions() { 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()}` } headers: { Authorization: `Bearer ${getAtk()}` }
}); });
if (res.status !== 200) { if (res.status !== 200) {
@ -41,7 +42,7 @@ export default function DashboardPage() {
} }
async function readEvents() { 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()}` } headers: { Authorization: `Bearer ${getAtk()}` }
}); });
if (res.status !== 200) { if (res.status !== 200) {
@ -55,7 +56,7 @@ export default function DashboardPage() {
async function killSession(item: any) { async function killSession(item: any) {
setSubmitting(true); setSubmitting(true);
const res = await fetch(`/api/users/me/sessions/${item.id}`, { const res = await request(`/api/users/me/sessions/${item.id}`, {
method: "DELETE", method: "DELETE",
headers: { Authorization: `Bearer ${getAtk()}` } headers: { Authorization: `Bearer ${getAtk()}` }
}); });

View File

@ -1,6 +1,7 @@
import { createSignal, Show } from "solid-js"; import { createSignal, Show } from "solid-js";
import { useNavigate, useSearchParams } from "@solidjs/router"; import { useNavigate, useSearchParams } from "@solidjs/router";
import { readProfiles } from "../../stores/userinfo.tsx"; import { readProfiles } from "../../stores/userinfo.tsx";
import { request } from "../../scripts/request.ts";
export default function ConfirmRegistrationPage() { export default function ConfirmRegistrationPage() {
const [error, setError] = createSignal<string | null>(null); const [error, setError] = createSignal<string | null>(null);
@ -15,7 +16,7 @@ export default function ConfirmRegistrationPage() {
setError("Bad Request: Code was not exists"); 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", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ body: JSON.stringify({

View File

@ -0,0 +1,4 @@
export async function request(input: string, init?: RequestInit) {
const prefix = window.__LAUNCHPAD_TARGET__ ?? "";
return await fetch(prefix + input, init)
}

View File

@ -1,6 +1,7 @@
import Cookie from "universal-cookie"; import Cookie from "universal-cookie";
import { createContext, useContext } from "solid-js"; import { createContext, useContext } from "solid-js";
import { createStore } from "solid-js/store"; import { createStore } from "solid-js/store";
import { request } from "../scripts/request.ts";
export interface Userinfo { export interface Userinfo {
isLoggedIn: boolean, isLoggedIn: boolean,
@ -31,7 +32,7 @@ function checkLoggedIn(): boolean {
export async function readProfiles() { export async function readProfiles() {
if (!checkLoggedIn()) return; if (!checkLoggedIn()) return;
const res = await fetch("/api/users/me", { const res = await request("/api/users/me", {
credentials: "include" credentials: "include"
}); });

View File

@ -1,12 +1,13 @@
import { createContext, useContext } from "solid-js"; import { createContext, useContext } from "solid-js";
import { createStore } from "solid-js/store"; import { createStore } from "solid-js/store";
import { request } from "../scripts/request.ts";
const WellKnownContext = createContext<any>(); const WellKnownContext = createContext<any>();
const [wellKnown, setWellKnown] = createStore<any>(null); const [wellKnown, setWellKnown] = createStore<any>(null);
export async function readWellKnown() { export async function readWellKnown() {
const res = await fetch("/.well-known") const res = await request("/.well-known")
setWellKnown(await res.json()) setWellKnown(await res.json())
} }