🐛 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 {
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 {

View File

@ -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",

View File

@ -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({

View File

@ -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)

View File

@ -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()}` }
});

View File

@ -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

View File

@ -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<any[]>([]);
@ -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()}` }
});

View File

@ -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<string | null>(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({

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 { 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"
});

View File

@ -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<any>();
const [wellKnown, setWellKnown] = createStore<any>(null);
export async function readWellKnown() {
const res = await fetch("/.well-known")
const res = await request("/.well-known")
setWellKnown(await res.json())
}