Skip to content

Instantly share code, notes, and snippets.

export const safeTry = async <T>(
promise: Promise<T>
): Promise<[T, null] | [null, Error]> => {
try {
const data = await promise;
return [data, null];
} catch (throwable) {
if (throwable instanceof Error) return [null, throwable];
throw throwable;
}
@jln13x
jln13x / example.tsx
Last active March 29, 2024 00:46
tRPC Multiple Clients
import { type userRouter } from "@/server/api/routers/user.router";
import { createTRPCReact } from "@trpc/react-query";
import { type AnyTRPCRouter } from "@trpc/server";
// create-api.ts
export const createApi = <TRouter extends AnyTRPCRouter>(root: string) => {
const router = createTRPCReact<TRouter>();
// Access the proxy already so we dont have to repeat ourselves e.g. `userApi.user.accounts...`
return (router as any)[root] as Omit<
@jln13x
jln13x / page-props.ts
Last active August 23, 2024 17:09
Typed PageProps Helper for Next.js App Router
type R = Record<string, string>;
export type PageProps<
PathParams extends R | undefined = undefined,
SearchParams extends R | undefined = undefined,
> = {
params: PathParams extends R ? PathParams : never;
searchParams: SearchParams extends R
? Partial<{
[K in keyof SearchParams]: string | string[];