style: format with prettier

This commit is contained in:
Sam 2022-11-25 02:27:46 +01:00
parent ba24815320
commit f2306b8b1d
3 changed files with 54 additions and 32 deletions

View file

@ -124,7 +124,7 @@ export async function fetchAPI<T>(
const resp = await fetch(`${apiBase}/v1${path}`, { const resp = await fetch(`${apiBase}/v1${path}`, {
method, method,
headers: { headers: {
...token ? { Authorization: token } : {}, ...(token ? { Authorization: token } : {}),
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: body ? JSON.stringify(body) : null, body: body ? JSON.stringify(body) : null,
@ -133,4 +133,4 @@ export async function fetchAPI<T>(
const data = await resp.json(); const data = await resp.json();
if (resp.status < 200 || resp.status >= 300) throw data as APIError; if (resp.status < 200 || resp.status >= 300) throw data as APIError;
return data as T; return data as T;
} }

View file

@ -1,9 +1,10 @@
import * as API from "./api-fetch"; import * as API from "./api-fetch";
import { fetchAPI } from './api-fetch'; import { fetchAPI } from "./api-fetch";
function getDomain(): string { function getDomain(): string {
const domain = typeof window !== "undefined" ? window.location.origin : process.env.DOMAIN; const domain =
if (!domain) throw new Error('process.env.DOMAIN not set'); typeof window !== "undefined" ? window.location.origin : process.env.DOMAIN;
if (!domain) throw new Error("process.env.DOMAIN not set");
return domain; return domain;
} }
@ -39,18 +40,18 @@ abstract class _Person extends PartialPerson {
const { bio, links, names, pronouns, fields } = apiData; const { bio, links, names, pronouns, fields } = apiData;
this.bio = bio; this.bio = bio;
this.links = links ?? []; this.links = links ?? [];
this.names = (names ?? []).map(x => new Name(x)); this.names = (names ?? []).map((x) => new Name(x));
this.pronouns = (pronouns ?? []).map(x => new Pronouns(x)); this.pronouns = (pronouns ?? []).map((x) => new Pronouns(x));
this.fields = (fields ?? []).map(x => new Field(x)); this.fields = (fields ?? []).map((x) => new Field(x));
} }
abstract fullHandle(): string abstract fullHandle(): string;
shortHandle(): string { shortHandle(): string {
return this.fullHandle(); return this.fullHandle();
} }
abstract relativeURL(): string abstract relativeURL(): string;
absoluteURL(): string { absoluteURL(): string {
return `${getDomain()}${this.relativeURL()}`; return `${getDomain()}${this.relativeURL()}`;
@ -62,7 +63,7 @@ export class User extends _Person {
constructor(apiData: API.User) { constructor(apiData: API.User) {
super(apiData); super(apiData);
const { members } = apiData; const { members } = apiData;
this.partialMembers = (members ?? []).map(x => new PartialMember(x)); this.partialMembers = (members ?? []).map((x) => new PartialMember(x));
} }
static async fetchFromName(name: string): Promise<User> { static async fetchFromName(name: string): Promise<User> {
@ -90,8 +91,13 @@ export class Member extends _Person {
this.partialUser = new PartialUser(user); this.partialUser = new PartialUser(user);
} }
static async fetchFromUserAndMemberName(userName: string, memberName: string): Promise<Member> { static async fetchFromUserAndMemberName(
return new Member(await fetchAPI<API.Member>(`/users/${userName}/members/${memberName}`)); userName: string,
memberName: string
): Promise<Member> {
return new Member(
await fetchAPI<API.Member>(`/users/${userName}/members/${memberName}`)
);
} }
fullHandle(): string { fullHandle(): string {
@ -130,10 +136,10 @@ export const LabelStatus = API.WordStatus;
export type LabelStatus = API.WordStatus; export type LabelStatus = API.WordStatus;
export interface LabelData { export interface LabelData {
type?: LabelType type?: LabelType;
displayText: string | null displayText: string | null;
text: string text: string;
status: LabelStatus status: LabelStatus;
} }
export class Label { export class Label {
@ -178,32 +184,46 @@ export class Pronouns extends Label {
}); });
} }
get pronouns(): string[] { return this.text.split('/'); } get pronouns(): string[] {
set pronouns(to: string[]) { this.text = to.join('/'); } return this.text.split("/");
}
set pronouns(to: string[]) {
this.text = to.join("/");
}
shortDisplay(): string { shortDisplay(): string {
return this.displayText ?? this.pronouns.splice(0, 2).join('/'); return this.displayText ?? this.pronouns.splice(0, 2).join("/");
} }
} }
export class Field { export class Field {
name: string; name: string;
labels: Label[]; labels: Label[];
constructor({ name, favourite, okay, jokingly, friends_only, avoid }: API.Field) { constructor({
name,
favourite,
okay,
jokingly,
friends_only,
avoid,
}: API.Field) {
this.name = name; this.name = name;
function transpose(arr: API.Arr<string>, status: LabelStatus): Label[] { function transpose(arr: API.Arr<string>, status: LabelStatus): Label[] {
return (arr ?? []).map(text => new Label({ return (arr ?? []).map(
displayText: null, (text) =>
text, new Label({
status, displayText: null,
})); text,
status,
})
);
} }
this.labels = [ this.labels = [
...transpose(favourite, LabelStatus.Favourite), ...transpose(favourite, LabelStatus.Favourite),
...transpose(okay, LabelStatus.Okay), ...transpose(okay, LabelStatus.Okay),
...transpose(jokingly, LabelStatus.Jokingly), ...transpose(jokingly, LabelStatus.Jokingly),
...transpose(friends_only, LabelStatus.FriendsOnly), ...transpose(friends_only, LabelStatus.FriendsOnly),
...transpose(avoid, LabelStatus.Avoid), ...transpose(avoid, LabelStatus.Avoid),
]; ];
} }
} }

View file

@ -20,7 +20,9 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
try { try {
return { return {
props: { props: {
member: await API.fetchAPI<API.Member>(`/users/${context.params!.user}/members/${context.params!.member}`), member: await API.fetchAPI<API.Member>(
`/users/${context.params!.user}/members/${context.params!.member}`
),
}, },
}; };
} catch (e) { } catch (e) {