sendou.ink/app/utils/index.ts
2021-11-29 12:52:33 +02:00

49 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>>;