feat(!): return 204 instead of useless json responses, add fastFetch
This commit is contained in:
parent
abc78f3a9a
commit
9c8b6a8f91
16 changed files with 63 additions and 31 deletions
|
@ -1,3 +1,4 @@
|
|||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import type { APIError } from "./entities";
|
||||
import { PUBLIC_BASE_URL } from "$env/static/public";
|
||||
|
||||
|
@ -21,9 +22,36 @@ export async function apiFetch<T>(
|
|||
});
|
||||
|
||||
const data = await resp.json();
|
||||
if (resp.status < 200 || resp.status >= 300) throw data as APIError;
|
||||
if (resp.status < 200 || resp.status >= 400) throw data as APIError;
|
||||
return data as T;
|
||||
}
|
||||
|
||||
export const apiFetchClient = async <T>(path: string, method: string = "GET", body: any = null) =>
|
||||
export const apiFetchClient = async <T>(path: string, method = "GET", body: any = null) =>
|
||||
apiFetch<T>(path, { method, body, token: localStorage.getItem("pronouns-token") || undefined });
|
||||
|
||||
/** Fetches the specified path without parsing the response body. */
|
||||
export async function fastFetch(
|
||||
path: string,
|
||||
{
|
||||
method,
|
||||
body,
|
||||
token,
|
||||
headers,
|
||||
}: { method?: string; body?: any; token?: string; headers?: Record<string, string> },
|
||||
) {
|
||||
const resp = await fetch(`${PUBLIC_BASE_URL}/api/v1${path}`, {
|
||||
method: method || "GET",
|
||||
headers: {
|
||||
...(token ? { Authorization: token } : {}),
|
||||
...(headers ? headers : {}),
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: body ? JSON.stringify(body) : null,
|
||||
});
|
||||
|
||||
if (resp.status < 200 || resp.status >= 400) throw (await resp.json()) as APIError;
|
||||
}
|
||||
|
||||
/** Fetches the specified path without parsing the response body. */
|
||||
export const fastFetchClient = async (path: string, method = "GET", body: any = null) =>
|
||||
fastFetch(path, { method, body, token: localStorage.getItem("pronouns-token") || undefined });
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<script lang="ts">
|
||||
import type { APIError } from "$lib/api/entities";
|
||||
import { apiFetchClient } from "$lib/api/fetch";
|
||||
import { fastFetchClient } from "$lib/api/fetch";
|
||||
import ErrorAlert from "$lib/components/ErrorAlert.svelte";
|
||||
import { addToast } from "$lib/toast";
|
||||
import { Button, FormGroup, Icon, Modal, ModalBody, ModalFooter } from "sveltestrap";
|
||||
|
@ -18,7 +18,7 @@
|
|||
|
||||
const doReport = async () => {
|
||||
try {
|
||||
await apiFetchClient<any>(reportUrl, "POST", { reason: reason });
|
||||
await fastFetchClient(reportUrl, "POST", { reason: reason });
|
||||
error = null;
|
||||
addToast({ header: "Sent report", body: "Successfully sent report!" });
|
||||
toggle();
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<script lang="ts">
|
||||
import { goto } from "$app/navigation";
|
||||
import type { APIError, MeUser } from "$lib/api/entities";
|
||||
import { apiFetch } from "$lib/api/fetch";
|
||||
import { fastFetch } from "$lib/api/fetch";
|
||||
import { usernameRegex } from "$lib/api/regex";
|
||||
import ErrorAlert from "$lib/components/ErrorAlert.svelte";
|
||||
import { userStore } from "$lib/store";
|
||||
|
@ -58,7 +58,7 @@
|
|||
|
||||
const forceDeleteAccount = async () => {
|
||||
try {
|
||||
await apiFetch<any>("/auth/force-delete", {
|
||||
await fastFetch("/auth/force-delete", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"X-Delete-Token": token!,
|
||||
|
@ -75,7 +75,7 @@
|
|||
|
||||
const cancelDelete = async () => {
|
||||
try {
|
||||
await apiFetch<any>("/auth/cancel-delete", {
|
||||
await fastFetch("/auth/cancel-delete", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"X-Delete-Token": token!,
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
ModalFooter,
|
||||
} from "sveltestrap";
|
||||
import { encode } from "base64-arraybuffer";
|
||||
import { apiFetchClient } from "$lib/api/fetch";
|
||||
import { apiFetchClient, fastFetchClient } from "$lib/api/fetch";
|
||||
import IconButton from "$lib/components/IconButton.svelte";
|
||||
import EditableField from "../../EditableField.svelte";
|
||||
import EditableName from "../../EditableName.svelte";
|
||||
|
@ -242,9 +242,13 @@
|
|||
|
||||
const deleteMember = async () => {
|
||||
try {
|
||||
await apiFetchClient<any>(`/members/${data.member.id}`, "DELETE");
|
||||
await fastFetchClient(`/members/${data.member.id}`, "DELETE");
|
||||
|
||||
toggleDeleteOpen();
|
||||
addToast({
|
||||
header: "Deleted member",
|
||||
body: `Successfully deleted member ${data.member.name}!`,
|
||||
});
|
||||
goto(`/@${data.member.user.name}`);
|
||||
} catch (e) {
|
||||
deleteName = "";
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
} from "$lib/api/entities";
|
||||
import FallbackImage from "$lib/components/FallbackImage.svelte";
|
||||
import { userStore } from "$lib/store";
|
||||
import { Alert, Button, ButtonGroup, FormGroup, Icon, Input, Popover } from "sveltestrap";
|
||||
import { Button, ButtonGroup, FormGroup, Icon, Input, Popover } from "sveltestrap";
|
||||
import { encode } from "base64-arraybuffer";
|
||||
import { apiFetchClient } from "$lib/api/fetch";
|
||||
import IconButton from "$lib/components/IconButton.svelte";
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<script lang="ts">
|
||||
import type { APIError } from "$lib/api/entities";
|
||||
import { apiFetchClient } from "$lib/api/fetch";
|
||||
import { fastFetchClient } from "$lib/api/fetch";
|
||||
import ErrorAlert from "$lib/components/ErrorAlert.svelte";
|
||||
import { addToast } from "$lib/toast";
|
||||
import { Button, FormGroup, Modal, ModalBody, ModalFooter } from "sveltestrap";
|
||||
|
@ -42,7 +42,7 @@
|
|||
|
||||
const warnUser = async () => {
|
||||
try {
|
||||
await apiFetchClient<any>(`/admin/reports/${data.reports[reportIndex].id}`, "PATCH", {
|
||||
await fastFetchClient(`/admin/reports/${data.reports[reportIndex].id}`, "PATCH", {
|
||||
warn: true,
|
||||
reason: reason,
|
||||
});
|
||||
|
@ -58,7 +58,7 @@
|
|||
|
||||
const deactivateUser = async () => {
|
||||
try {
|
||||
await apiFetchClient<any>(`/admin/reports/${data.reports[reportIndex].id}`, "PATCH", {
|
||||
await fastFetchClient(`/admin/reports/${data.reports[reportIndex].id}`, "PATCH", {
|
||||
warn: true,
|
||||
ban: true,
|
||||
delete: deleteUser,
|
||||
|
@ -76,7 +76,7 @@
|
|||
|
||||
const ignoreReport = async () => {
|
||||
try {
|
||||
await apiFetchClient<any>(`/admin/reports/${data.reports[reportIndex].id}`, "PATCH", {
|
||||
await fastFetchClient(`/admin/reports/${data.reports[reportIndex].id}`, "PATCH", {
|
||||
reason: reason,
|
||||
});
|
||||
error = null;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<script lang="ts">
|
||||
import { goto } from "$app/navigation";
|
||||
import { type MeUser, userAvatars, type APIError, MAX_MEMBERS } from "$lib/api/entities";
|
||||
import { apiFetchClient } from "$lib/api/fetch";
|
||||
import { apiFetchClient, fastFetchClient } from "$lib/api/fetch";
|
||||
import { usernameRegex } from "$lib/api/regex";
|
||||
import ErrorAlert from "$lib/components/ErrorAlert.svelte";
|
||||
import FallbackImage from "$lib/components/FallbackImage.svelte";
|
||||
|
@ -37,7 +37,7 @@
|
|||
|
||||
const deleteAccount = async () => {
|
||||
try {
|
||||
await apiFetchClient<any>("/users/@me", "DELETE");
|
||||
await fastFetchClient("/users/@me", "DELETE");
|
||||
|
||||
userStore.set(null);
|
||||
localStorage.removeItem("pronouns-token");
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
import { DateTime, Duration } from "luxon";
|
||||
import { Alert, Button } from "sveltestrap";
|
||||
import { PUBLIC_BASE_URL } from "$env/static/public";
|
||||
import { apiFetchClient } from "$lib/api/fetch";
|
||||
import { fastFetchClient } from "$lib/api/fetch";
|
||||
import type { APIError } from "$lib/api/entities";
|
||||
import { addToast } from "$lib/toast";
|
||||
import ErrorAlert from "$lib/components/ErrorAlert.svelte";
|
||||
|
@ -23,7 +23,7 @@
|
|||
|
||||
const requestExport = async () => {
|
||||
try {
|
||||
await apiFetchClient<any>("/users/@me/export/start");
|
||||
await fastFetchClient("/users/@me/export/start");
|
||||
|
||||
addToast({
|
||||
header: "Export in progress",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<script lang="ts">
|
||||
import type { APIError } from "$lib/api/entities";
|
||||
import { apiFetchClient } from "$lib/api/fetch";
|
||||
import { apiFetchClient, fastFetchClient } from "$lib/api/fetch";
|
||||
import ErrorAlert from "$lib/components/ErrorAlert.svelte";
|
||||
import { addToast } from "$lib/toast";
|
||||
import { DateTime } from "luxon";
|
||||
|
@ -12,7 +12,7 @@
|
|||
|
||||
const acknowledgeWarning = async (idx: number) => {
|
||||
try {
|
||||
await apiFetchClient<any>(`/auth/warnings/${data.warnings[idx].id}/ack`, "POST");
|
||||
await fastFetchClient(`/auth/warnings/${data.warnings[idx].id}/ack`, "POST");
|
||||
addToast({
|
||||
header: "Acknowledged",
|
||||
body: `Marked warning #${data.warnings[idx].id} as read.`,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue