init
This commit is contained in:
commit
90205a1243
29 changed files with 3216 additions and 0 deletions
46
src/lib/api/entities.ts
Normal file
46
src/lib/api/entities.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
export interface User {
|
||||
id: string;
|
||||
username: string;
|
||||
display_name: string | null;
|
||||
bio: string | null;
|
||||
avatar_urls: string[] | null;
|
||||
links: string[] | null;
|
||||
}
|
||||
|
||||
export interface MeUser extends User {
|
||||
discord: string | null;
|
||||
discord_username: string | null;
|
||||
}
|
||||
|
||||
export interface APIError {
|
||||
code: ErrorCode;
|
||||
message?: string;
|
||||
details?: string;
|
||||
}
|
||||
|
||||
export enum ErrorCode {
|
||||
BadRequest = 400,
|
||||
Forbidden = 403,
|
||||
NotFound = 404,
|
||||
MethodNotAllowed = 405,
|
||||
TooManyRequests = 429,
|
||||
InternalServerError = 500,
|
||||
|
||||
InvalidState = 1001,
|
||||
InvalidOAuthCode = 1002,
|
||||
InvalidToken = 1003,
|
||||
InviteRequired = 1004,
|
||||
InvalidTicket = 1005,
|
||||
InvalidUsername = 1006,
|
||||
UsernameTaken = 1007,
|
||||
InvitesDisabled = 1008,
|
||||
InviteLimitReached = 1009,
|
||||
InviteAlreadyUsed = 1010,
|
||||
|
||||
UserNotFound = 2001,
|
||||
|
||||
MemberNotFound = 3001,
|
||||
MemberLimitReached = 3002,
|
||||
|
||||
RequestTooBig = 4001,
|
||||
}
|
19
src/lib/api/fetch.ts
Normal file
19
src/lib/api/fetch.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import type { APIError } from "./entities";
|
||||
|
||||
export async function fetchAPI<T>(
|
||||
path: string,
|
||||
{ method, body, token }: { method?: string; body?: any; token?: string },
|
||||
) {
|
||||
const resp = await fetch(`${process.env.ORIGIN}/api/v1${path}`, {
|
||||
method,
|
||||
headers: {
|
||||
...(token ? { Authorization: token } : {}),
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: body ? JSON.stringify(body) : null,
|
||||
});
|
||||
|
||||
const data = await resp.json();
|
||||
if (resp.status < 200 || resp.status >= 300) throw data as APIError;
|
||||
return data as T;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue