2024-10-18 22:13:23 +02:00
|
|
|
export const TOKEN_KEY = "catalogger-token";
|
|
|
|
|
|
2024-10-19 20:47:55 +02:00
|
|
|
export type HttpMethod = "GET" | "POST" | "PATCH" | "DELETE";
|
|
|
|
|
|
|
|
|
|
export async function fastFetch(
|
|
|
|
|
method: HttpMethod,
|
|
|
|
|
path: string,
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
body: any = null,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const token = localStorage.getItem(TOKEN_KEY);
|
|
|
|
|
const headers = {
|
|
|
|
|
...(body != null
|
|
|
|
|
? { "Content-Type": "application/json; charset=utf-8" }
|
|
|
|
|
: {}),
|
|
|
|
|
...(token ? { Authorization: token } : {}),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const reqBody = body ? JSON.stringify(body) : undefined;
|
|
|
|
|
|
|
|
|
|
console.debug("Sending", method, "request to", path, "with body", reqBody);
|
|
|
|
|
|
|
|
|
|
const resp = await fetch(path, {
|
|
|
|
|
method,
|
|
|
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
|
|
|
headers,
|
|
|
|
|
});
|
|
|
|
|
if (resp.status < 200 || resp.status > 299)
|
|
|
|
|
throw (await resp.json()) as ApiError;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-18 22:13:23 +02:00
|
|
|
export default async function apiFetch<T>(
|
2024-10-19 20:47:55 +02:00
|
|
|
method: HttpMethod,
|
2024-10-18 22:13:23 +02:00
|
|
|
path: string,
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
body: any = null,
|
2024-10-19 20:47:55 +02:00
|
|
|
): Promise<T> {
|
2024-10-18 22:13:23 +02:00
|
|
|
const token = localStorage.getItem(TOKEN_KEY);
|
|
|
|
|
const headers = {
|
|
|
|
|
...(body != null
|
|
|
|
|
? { "Content-Type": "application/json; charset=utf-8" }
|
|
|
|
|
: {}),
|
|
|
|
|
...(token ? { Authorization: token } : {}),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const reqBody = body ? JSON.stringify(body) : undefined;
|
|
|
|
|
|
|
|
|
|
console.debug("Sending", method, "request to", path, "with body", reqBody);
|
|
|
|
|
|
|
|
|
|
const resp = await fetch(path, {
|
|
|
|
|
method,
|
|
|
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
|
|
|
headers,
|
|
|
|
|
});
|
|
|
|
|
if (resp.status < 200 || resp.status > 299)
|
|
|
|
|
throw (await resp.json()) as ApiError;
|
|
|
|
|
|
|
|
|
|
return (await resp.json()) as T;
|
|
|
|
|
}
|
2024-10-19 16:52:27 +02:00
|
|
|
|
|
|
|
|
export type User = {
|
|
|
|
|
id: string;
|
|
|
|
|
tag: string;
|
|
|
|
|
avatar_url: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type PartialGuild = {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
icon_url: string;
|
|
|
|
|
bot_in_guild: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type FullGuild = {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
icon_url: string;
|
|
|
|
|
categories: GuildCategory[];
|
|
|
|
|
channels_without_category: GuildChannel[];
|
|
|
|
|
config: GuildConfig;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type GuildCategory = {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
channels: GuildChannel[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type GuildChannel = {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
can_log_to: boolean;
|
|
|
|
|
can_redirect_from: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type CurrentUser = {
|
|
|
|
|
user: User;
|
|
|
|
|
guilds: PartialGuild[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type AuthCallback = CurrentUser & { token: string };
|
|
|
|
|
|
|
|
|
|
export type ApiError = {
|
|
|
|
|
error_code: string;
|
|
|
|
|
message: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type GuildConfig = GuildChannelConfig & {
|
|
|
|
|
ignored_channels: string[];
|
|
|
|
|
ignored_users: string[];
|
|
|
|
|
ignored_users_per_channel: Record<string, string[]>;
|
|
|
|
|
redirects: Record<string, string>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type GuildChannelConfig = {
|
|
|
|
|
guild_update: string;
|
|
|
|
|
guild_emojis_update: string;
|
|
|
|
|
guild_role_create: string;
|
|
|
|
|
guild_role_update: string;
|
|
|
|
|
guild_role_delete: string;
|
|
|
|
|
channel_create: string;
|
|
|
|
|
channel_update: string;
|
|
|
|
|
channel_delete: string;
|
|
|
|
|
guild_member_add: string;
|
|
|
|
|
guild_member_update: string;
|
|
|
|
|
guild_key_role_update: string;
|
|
|
|
|
guild_member_nick_update: string;
|
|
|
|
|
guild_member_avatar_update: string;
|
|
|
|
|
guild_member_timeout: string;
|
|
|
|
|
guild_member_remove: string;
|
|
|
|
|
guild_member_kick: string;
|
|
|
|
|
guild_ban_add: string;
|
|
|
|
|
guild_ban_remove: string;
|
|
|
|
|
invite_create: string;
|
|
|
|
|
invite_delete: string;
|
|
|
|
|
message_update: string;
|
|
|
|
|
message_delete: string;
|
|
|
|
|
message_delete_bulk: string;
|
|
|
|
|
};
|