mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-09 04:02:40 -05:00
49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
import { json, useMatches } from "remix";
|
||
|
||
export const makeTitle = (endOfTitle: string) => `sendou.ink | ${endOfTitle}`;
|
||
|
||
/** Get logged in user from context. Throws with 401 error if no user found. */
|
||
export const requireUser = (ctx: any) => {
|
||
const user = ctx.user;
|
||
|
||
if (!user) {
|
||
throw json("Log in required", { status: 401 });
|
||
}
|
||
|
||
return user as NonNullable<LoggedInUser>;
|
||
};
|
||
|
||
/** Get logged in user from context. Doesn't throw. */
|
||
export const getUser = (ctx: any) => {
|
||
const user = ctx.user;
|
||
|
||
return user as LoggedInUser;
|
||
};
|
||
|
||
export type LoggedInUser = {
|
||
id: number;
|
||
discordId: string;
|
||
discordAvatar: string;
|
||
} | null;
|
||
|
||
export const useUser = () => {
|
||
const [root] = useMatches();
|
||
|
||
return root.data as LoggedInUser;
|
||
};
|
||
|
||
export type Serialized<T> = {
|
||
[P in keyof T]: T[P] extends Date ? string : Serialized<T[P]>;
|
||
};
|
||
|
||
export type Unpacked<T> = T extends (infer U)[]
|
||
? U
|
||
: T extends (...args: any[]) => infer U
|
||
? U
|
||
: T extends Promise<infer U>
|
||
? U
|
||
: T;
|
||
|
||
// TODO:
|
||
// export type InferredSerializedAPI<T> = Serialized<Prisma.PromiseReturnType<T>>;
|