sendou.ink/app/utils/index.ts
2021-12-11 10:46:34 +02:00

71 lines
1.7 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 } from "remix";
import invariant from "tiny-invariant";
export const makeTitle = (endOfTitle?: string) =>
endOfTitle ? `sendou.ink | ${endOfTitle}` : "sendou.ink";
/** 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;
};
/** Get fields from `request.formData()`. Throw an error if the formData doesn't contain a requested field. */
export const formDataFromRequest = async <T extends string>({
request,
fields,
}: {
request: Request;
fields: T[];
}): Promise<Record<T, string>> => {
const formData = await request.formData();
let result: Partial<Record<T, string>> = {};
for (const field of fields) {
const value = formData.get(field);
invariant(typeof value === "string", `Expected ${field} to be string`);
result[field] = value;
}
return result as Record<T, string>;
};
export type LoggedInUser = {
id: string;
discordId: string;
discordAvatar: string;
} | null;
export type Serialized<T> = {
[P in keyof T]: T[P] extends Date
? string
: T[P] extends Date | null
? string | null
: 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>>;