// user/[id]/route.ts
const userByIdHandler = async (
req: NextRequest,
{ params: { id } }: { params: { id: string } },
) => {
try {
// Check for ID
if (!id || typeof id !== "string") {
return NextResponse.json({ message: "Invalid ID" }, { status: 400 });
}
// Get the key from req header and check key
const key = req.headers.get("Authorization");
if (!key) {
return NextResponse.json({ message: "Invalid key" }, { status: 400 });
}
// Create context and caller
const ctx = await createTRPCContext({ req });
const caller = createCaller(ctx);
const access = await caller.authorization.canAccess({ key: key });
if (!access) {
return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
}
const user = await caller.user.getById({ id });
return NextResponse.json(user, { status: 200 });
} catch (cause) {
if (cause instanceof TRPCError) {
// An error from tRPC occurred
const httpCode = getHTTPStatusCodeFromError(cause);
return NextResponse.json(cause, { status: httpCode });
}
// Another error occurred
console.error(cause);
return NextResponse.json(
{ message: "Internal server error" },
{ status: 500 },
);
}
};
export const GET = userByIdHandler;
// user/[id]/route.ts
const userByIdHandler = async (
req: NextRequest,
{ params: { id } }: { params: { id: string } },
) => {
try {
// Check for ID
if (!id || typeof id !== "string") {
return NextResponse.json({ message: "Invalid ID" }, { status: 400 });
}
// Get the key from req header and check key
const key = req.headers.get("Authorization");
if (!key) {
return NextResponse.json({ message: "Invalid key" }, { status: 400 });
}
// Create context and caller
const ctx = await createTRPCContext({ req });
const caller = createCaller(ctx);
const access = await caller.authorization.canAccess({ key: key });
if (!access) {
return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
}
const user = await caller.user.getById({ id });
return NextResponse.json(user, { status: 200 });
} catch (cause) {
if (cause instanceof TRPCError) {
// An error from tRPC occurred
const httpCode = getHTTPStatusCodeFromError(cause);
return NextResponse.json(cause, { status: httpCode });
}
// Another error occurred
console.error(cause);
return NextResponse.json(
{ message: "Internal server error" },
{ status: 500 },
);
}
};
export const GET = userByIdHandler;