import { createNextApiHandler } from "@trpc/server/adapters/next";

import { env } from "~/env";
import { appRouter } from "~/server/api/root";
import { createTRPCContext } from "~/server/api/trpc";

// export API handler
export default createNextApiHandler({
	createContext: createTRPCContext,

	onError: ({ path, error }) => {
		if (
			error.message.startsWith("SESSION_EXPIRED") ||
			error.code === "UNAUTHORIZED"
		) {
			return;
		}
		if (env.NODE_ENV === "development") {
			console.error(
				`❌ tRPC failed on ${path ?? "<no-path>"}, msg: ${error.message}`,
			);
		}

		console.error(error);
	},
	router: appRouter,
});

// /pages/api/[trpc].ts

export const config = {
	api: {
		bodyParser: {
			sizeLimit: "4mb",
		},
		responseLimit: "4mb",
	},
};
