🐛 Bug fixes of garfish api

This commit is contained in:
LittleSheep 2024-02-19 19:37:43 +08:00
parent cd12b6ce8d
commit 65ee99c213
20 changed files with 38 additions and 32 deletions

View File

@ -12,7 +12,7 @@ export default function NameCard(props: { accountId: string, onError: (messasge:
async function readInfo() {
setLoading(true);
const res = await fetch(`/api/users/${props.accountId}`);
const res = await request(`/api/users/${props.accountId}`);
if (res.status !== 200) {
props.onError(await res.text());
} else {
@ -24,7 +24,7 @@ export default function NameCard(props: { accountId: string, onError: (messasge:
async function readIsFollowing() {
setLoading(true);
const res = await fetch(`/api/users/${props.accountId}/follow`, {
const res = await request(`/api/users/${props.accountId}/follow`, {
method: "GET",
headers: { Authorization: `Bearer ${getAtk()}` }
});
@ -37,7 +37,7 @@ export default function NameCard(props: { accountId: string, onError: (messasge:
async function follow() {
setSubmitting(true);
const res = await fetch(`/api/users/${props.accountId}/follow`, {
const res = await request(`/api/users/${props.accountId}/follow`, {
method: "POST",
headers: { "Authorization": `Bearer ${getAtk()}` }
});

View File

@ -25,7 +25,7 @@ export default function PostEditActions(props: {
const [attachmentMode, setAttachmentMode] = createSignal(0);
async function readCategories() {
const res = await fetch("/api/categories");
const res = await request("/api/categories");
if (res.status === 200) {
setAvailableCategories(await res.json());
}
@ -41,7 +41,7 @@ export default function PostEditActions(props: {
if (!data.get("attachment")) return;
setUploading(true);
const res = await fetch("/api/attachments", {
const res = await request("/api/attachments", {
method: "POST",
headers: { Authorization: `Bearer ${getAtk()}` },
body: data,

View File

@ -54,7 +54,7 @@ export default function PostEditor(props: {
}, [props.editing]);
async function listRealm() {
const res = await fetch("/api/realms/me/available", {
const res = await request("/api/realms/me/available", {
headers: { "Authorization": `Bearer ${getAtk()}` }
});
if (res.status === 200) {
@ -72,7 +72,7 @@ export default function PostEditor(props: {
if (!editor()?.getValue()) return;
setSubmitting(true);
const res = await fetch("/api/posts", {
const res = await request("/api/posts", {
method: "POST",
headers: {
"Content-Type": "application/json",
@ -107,7 +107,7 @@ export default function PostEditor(props: {
if (!editor()?.getValue()) return;
setSubmitting(true);
const res = await fetch(`/api/posts/${props.editing?.id}`, {
const res = await request(`/api/posts/${props.editing?.id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",

View File

@ -25,7 +25,7 @@ export default function PostItem(props: {
async function reactPost(item: any, type: string) {
setReacting(true);
const res = await fetch(`/api/posts/${item.id}/react/${type}`, {
const res = await request(`/api/posts/${item.id}/react/${type}`, {
method: "POST",
headers: { Authorization: `Bearer ${getAtk()}` },
});

View File

@ -34,7 +34,7 @@ export default function PostList(props: {
if (!confirm(`Are you sure to delete post#${item.id}?`)) return;
setLoading(true);
const res = await fetch(`/api/posts/${item.id}`, {
const res = await request(`/api/posts/${item.id}`, {
method: "DELETE",
headers: { "Authorization": `Bearer ${getAtk()}` }
});

View File

@ -48,7 +48,7 @@ export default function PostPublish(props: {
if (!data.content) return;
setSubmitting(true);
const res = await fetch("/api/posts", {
const res = await request("/api/posts", {
method: "POST",
headers: {
"Content-Type": "application/json",
@ -85,7 +85,7 @@ export default function PostPublish(props: {
if (!data.content) return;
setSubmitting(true);
const res = await fetch(`/api/posts/${props.editing?.id}`, {
const res = await request(`/api/posts/${props.editing?.id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",

View File

@ -52,6 +52,7 @@ declare const __GARFISH_EXPORTS__: {
declare global {
interface Window {
__GARFISH__: boolean;
__LAUNCHPAD_TARGET__?: string;
}
}
@ -71,6 +72,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

@ -23,7 +23,7 @@ export default function AccountPage() {
async function readPosts(pn?: number) {
if (pn) setSearchParams({ page: pn });
const res = await fetch(
const res = await request(
"/api/posts?" +
new URLSearchParams({
take: searchParams["take"] ? searchParams["take"] : (10).toString(),

View File

@ -10,7 +10,7 @@ export default function AuthCallback() {
const navigate = useNavigate();
async function callback() {
const res = await fetch(`/api/auth/callback${location.search}`);
const res = await request(`/api/auth/callback${location.search}`);
if (res.status !== 200) {
setError(await res.text());
} else {

View File

@ -5,7 +5,7 @@ export default function AuthCallout() {
const [status, setStatus] = createSignal("Communicating with Goatpass...");
async function communicate() {
const res = await fetch(`/api/auth${location.search}`);
const res = await request(`/api/auth${location.search}`);
if (res.status !== 200) {
setError(await res.text());
} else {

View File

@ -11,7 +11,7 @@ export default function PublishPost() {
const [post, setPost] = createSignal<any>();
async function readPost() {
const res = await fetch(`/api/creators/posts/${params["postId"]}`, {
const res = await request(`/api/creators/posts/${params["postId"]}`, {
headers: { "Authorization": `Bearer ${getAtk()}` }
});
if (res.status === 200) {

View File

@ -17,7 +17,7 @@ export default function CreatorHub() {
async function readPosts(pn?: number) {
if (pn) setPage(pn);
setLoading(true);
const res = await fetch("/api/creators/posts?" + new URLSearchParams({
const res = await request("/api/creators/posts?" + new URLSearchParams({
take: (10).toString(),
offset: ((page() - 1) * 10).toString()
}), { headers: { "Authorization": `Bearer ${getAtk()}` } });

View File

@ -19,7 +19,7 @@ export default function DashboardPage() {
async function readPosts(pn?: number) {
if (pn) setSearchParams({ page: pn });
const res = await fetch(
const res = await request(
"/api/posts?" +
new URLSearchParams({
take: searchParams["take"] ? searchParams["take"] : (10).toString(),

View File

@ -21,7 +21,7 @@ export default function PostPage() {
async function readPost(pn?: number) {
if (pn) setPage(pn);
const res = await fetch(`/api/posts/${params["postId"]}?` + new URLSearchParams({
const res = await request(`/api/posts/${params["postId"]}?` + new URLSearchParams({
take: (10).toString(),
offset: ((page() - 1) * 10).toString()
}));
@ -43,7 +43,7 @@ export default function PostPage() {
async function deletePost(item: any) {
if (!confirm(`Are you sure to delete post#${item.id}?`)) return;
const res = await fetch(`/api/posts/${item.id}`, {
const res = await request(`/api/posts/${item.id}`, {
method: "DELETE",
headers: { "Authorization": `Bearer ${getAtk()}` }
});

View File

@ -9,7 +9,7 @@ export default function RealmDirectoryPage() {
const [realms, setRealms] = createSignal<any>(null);
async function readRealms() {
const res = await fetch(`/api/realms`);
const res = await request(`/api/realms`);
if (res.status !== 200) {
setError(await res.text());
} else {
@ -26,7 +26,7 @@ export default function RealmDirectoryPage() {
const data = Object.fromEntries(new FormData(form));
setSubmitting(true);
const res = await fetch("/api/realms", {
const res = await request("/api/realms", {
method: "POST",
headers: { "Authorization": `Bearer ${getAtk()}`, "Content-Type": "application/json" },
body: JSON.stringify({

View File

@ -23,7 +23,7 @@ export default function RealmPage() {
const navigate = useNavigate();
async function readRealm() {
const res = await fetch(`/api/realms/${params["realmId"]}`);
const res = await request(`/api/realms/${params["realmId"]}`);
if (res.status !== 200) {
setError(await res.text());
} else {
@ -35,7 +35,7 @@ export default function RealmPage() {
async function readPosts(pn?: number) {
if (pn) setPage(pn);
const res = await fetch(`/api/posts?` + new URLSearchParams({
const res = await request(`/api/posts?` + new URLSearchParams({
take: (10).toString(),
offset: ((page() - 1) * 10).toString(),
realmId: params["realmId"]
@ -55,7 +55,7 @@ export default function RealmPage() {
const data = Object.fromEntries(new FormData(form));
setSubmitting(true);
const res = await fetch(`/api/realms/${params["realmId"]}`, {
const res = await request(`/api/realms/${params["realmId"]}`, {
method: "PUT",
headers: { "Authorization": `Bearer ${getAtk()}`, "Content-Type": "application/json" },
body: JSON.stringify({
@ -81,7 +81,7 @@ export default function RealmPage() {
const data = Object.fromEntries(new FormData(form));
setSubmitting(true);
const res = await fetch(`/api/realms/${params["realmId"]}/invite`, {
const res = await request(`/api/realms/${params["realmId"]}/invite`, {
method: "POST",
headers: { "Authorization": `Bearer ${getAtk()}`, "Content-Type": "application/json" },
body: JSON.stringify(data)
@ -103,7 +103,7 @@ export default function RealmPage() {
const data = Object.fromEntries(new FormData(form));
setSubmitting(true);
const res = await fetch(`/api/realms/${params["realmId"]}/kick`, {
const res = await request(`/api/realms/${params["realmId"]}/kick`, {
method: "POST",
headers: { "Authorization": `Bearer ${getAtk()}`, "Content-Type": "application/json" },
body: JSON.stringify(data)
@ -121,7 +121,7 @@ export default function RealmPage() {
async function breakRealm() {
if (!confirm("Are you sure about that? All posts in this realm will disappear forever.")) return;
const res = await fetch(`/api/realms/${params["realmId"]}`, {
const res = await request(`/api/realms/${params["realmId"]}`, {
method: "DELETE",
headers: { "Authorization": `Bearer ${getAtk()}` }
});

View File

@ -17,7 +17,7 @@ export default function SearchPage() {
async function readPosts(pn?: number) {
if (pn) setPage(pn);
const res = await fetch("/api/posts?" + new URLSearchParams({
const res = await request("/api/posts?" + new URLSearchParams({
take: (10).toString(),
offset: ((page() - 1) * 10).toString(),
...searchParams

View File

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

View File

@ -25,7 +25,7 @@ export function getAtk(): string {
export async function refreshAtk() {
const rtk = new Cookie().get("refresh_token");
const res = await fetch("/api/auth/refresh", {
const res = await request("/api/auth/refresh", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({
@ -48,7 +48,7 @@ function checkLoggedIn(): boolean {
export async function readProfiles(recovering = true) {
if (!checkLoggedIn()) return;
const res = await fetch("/api/users/me", {
const res = await request("/api/users/me", {
headers: {"Authorization": `Bearer ${getAtk()}`}
});

View File

@ -6,7 +6,7 @@ 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())
}