2022-11-24 11:44:47 -08:00
|
|
|
/** An array returned by the API. (Can be `null` due to a quirk in Go.) */
|
|
|
|
export type Arr<T> = T[] | null;
|
|
|
|
|
2022-11-20 16:58:17 -08:00
|
|
|
export interface PartialPerson {
|
2022-05-10 07:33:29 -07:00
|
|
|
id: string;
|
|
|
|
name: string;
|
2022-11-20 12:09:29 -08:00
|
|
|
display_name: string | null;
|
2022-11-24 11:44:47 -08:00
|
|
|
avatar_urls: Arr<string>;
|
2022-10-27 08:11:10 -07:00
|
|
|
}
|
2022-11-24 11:44:47 -08:00
|
|
|
|
2022-11-20 16:58:17 -08:00
|
|
|
export type PartialUser = PartialPerson;
|
2022-11-24 11:44:47 -08:00
|
|
|
|
2022-11-20 16:58:17 -08:00
|
|
|
export type PartialMember = PartialPerson;
|
2022-10-27 08:11:10 -07:00
|
|
|
|
2022-11-24 11:44:47 -08:00
|
|
|
/** The shared interface of `Member` and `User`.
|
|
|
|
* A typical `_Person` is only one of those two, so consider using `Person` instead.
|
|
|
|
*/
|
|
|
|
export interface _Person extends PartialPerson {
|
2022-10-27 08:11:10 -07:00
|
|
|
bio: string | null;
|
2022-11-24 11:44:47 -08:00
|
|
|
links: Arr<string>;
|
2023-01-30 15:50:17 -08:00
|
|
|
names: Arr<FieldEntry>;
|
2022-11-24 11:44:47 -08:00
|
|
|
pronouns: Arr<Pronoun>;
|
|
|
|
fields: Arr<Field>;
|
2022-11-20 16:58:17 -08:00
|
|
|
}
|
2022-11-20 16:19:03 -08:00
|
|
|
|
2022-11-24 11:44:47 -08:00
|
|
|
export interface User extends _Person {
|
|
|
|
members: Arr<PartialMember>;
|
2022-05-10 07:33:29 -07:00
|
|
|
}
|
|
|
|
|
2022-11-24 11:44:47 -08:00
|
|
|
export interface Member extends _Person {
|
|
|
|
user: PartialUser;
|
2022-11-20 16:58:17 -08:00
|
|
|
}
|
|
|
|
|
2022-11-20 18:39:09 -08:00
|
|
|
export type Person = Member | User;
|
|
|
|
|
2022-11-20 16:58:17 -08:00
|
|
|
export interface MeUser extends User {
|
|
|
|
discord: string | null;
|
|
|
|
discord_username: string | null;
|
|
|
|
}
|
|
|
|
|
2022-09-20 05:12:46 -07:00
|
|
|
export interface Pronoun {
|
|
|
|
display_text?: string;
|
|
|
|
pronouns: string;
|
|
|
|
status: WordStatus;
|
|
|
|
}
|
|
|
|
|
2022-05-10 17:23:45 -07:00
|
|
|
export interface Field {
|
|
|
|
name: string;
|
2023-01-14 08:33:18 -08:00
|
|
|
entries: Arr<FieldEntry>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FieldEntry {
|
|
|
|
value: string;
|
|
|
|
status: WordStatus;
|
2022-05-10 17:23:45 -07:00
|
|
|
}
|
|
|
|
|
2022-05-05 07:33:44 -07:00
|
|
|
export interface APIError {
|
|
|
|
code: ErrorCode;
|
|
|
|
message?: string;
|
2022-05-26 07:11:22 -07:00
|
|
|
details?: string;
|
2022-05-05 07:33:44 -07:00
|
|
|
}
|
|
|
|
|
2022-09-20 05:12:46 -07:00
|
|
|
export enum WordStatus {
|
|
|
|
Favourite = 1,
|
|
|
|
Okay = 2,
|
|
|
|
Jokingly = 3,
|
|
|
|
FriendsOnly = 4,
|
|
|
|
Avoid = 5,
|
|
|
|
}
|
|
|
|
|
2022-05-05 07:33:44 -07:00
|
|
|
export enum ErrorCode {
|
|
|
|
BadRequest = 400,
|
|
|
|
Forbidden = 403,
|
2022-11-18 05:11:52 -08:00
|
|
|
NotFound = 404,
|
|
|
|
MethodNotAllowed = 405,
|
|
|
|
TooManyRequests = 429,
|
2022-05-05 07:33:44 -07:00
|
|
|
InternalServerError = 500,
|
|
|
|
|
|
|
|
InvalidState = 1001,
|
|
|
|
InvalidOAuthCode = 1002,
|
2022-06-16 15:00:52 -07:00
|
|
|
InvalidToken = 1003,
|
2022-11-18 05:11:52 -08:00
|
|
|
InviteRequired = 1004,
|
|
|
|
InvalidTicket = 1005,
|
|
|
|
InvalidUsername = 1006,
|
|
|
|
UsernameTaken = 1007,
|
|
|
|
InvitesDisabled = 1008,
|
|
|
|
InviteLimitReached = 1009,
|
|
|
|
InviteAlreadyUsed = 1010,
|
2022-05-05 07:33:44 -07:00
|
|
|
|
|
|
|
UserNotFound = 2001,
|
2022-11-18 05:11:52 -08:00
|
|
|
|
|
|
|
MemberNotFound = 3001,
|
|
|
|
MemberLimitReached = 3002,
|
|
|
|
|
|
|
|
RequestTooBig = 4001,
|
2022-05-05 07:33:44 -07:00
|
|
|
}
|
2022-05-26 07:11:22 -07:00
|
|
|
|
|
|
|
export interface SignupRequest {
|
|
|
|
username: string;
|
|
|
|
ticket: string;
|
|
|
|
invite_code?: string;
|
|
|
|
}
|
2022-10-27 08:11:10 -07:00
|
|
|
|
2022-11-17 17:17:27 -08:00
|
|
|
export interface SignupResponse {
|
|
|
|
user: MeUser;
|
|
|
|
token: string;
|
|
|
|
}
|
2022-11-24 11:44:47 -08:00
|
|
|
|
|
|
|
const apiBase = process.env.API_BASE ?? "/api";
|
|
|
|
|
|
|
|
export async function fetchAPI<T>(
|
|
|
|
path: string,
|
|
|
|
method = "GET",
|
|
|
|
body: any = null
|
|
|
|
) {
|
|
|
|
const token =
|
|
|
|
typeof localStorage !== "undefined" &&
|
|
|
|
localStorage.getItem("pronouns-token");
|
|
|
|
|
|
|
|
const resp = await fetch(`${apiBase}/v1${path}`, {
|
|
|
|
method,
|
|
|
|
headers: {
|
2022-11-24 17:27:46 -08:00
|
|
|
...(token ? { Authorization: token } : {}),
|
2022-11-24 11:44:47 -08:00
|
|
|
"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;
|
2022-11-24 17:27:46 -08:00
|
|
|
}
|