fix: use Next RouteContext helpers for dynamic API routes

This commit is contained in:
Esdras Renan 2025-11-10 02:47:36 -03:00
parent d859c1196c
commit a7921ffffb
3 changed files with 16 additions and 14 deletions

View file

@ -1,3 +1,4 @@
import type { NextRequest } from "next/server"
import { NextResponse } from "next/server" import { NextResponse } from "next/server"
import { ConvexHttpClient } from "convex/browser" import { ConvexHttpClient } from "convex/browser"
@ -10,18 +11,14 @@ import { buildMachinesInventoryWorkbook, type MachineInventoryRecord } from "@/s
export const runtime = "nodejs" export const runtime = "nodejs"
type RouteContext = { type Ctx = RouteContext<"/api/admin/devices/[id]/inventory.xlsx">
params: Promise<{
id: string
}>
}
function sanitizeFilename(hostname: string, fallback: string): string { function sanitizeFilename(hostname: string, fallback: string): string {
const safe = hostname.replace(/[^a-z0-9_-]/gi, "-").replace(/-{2,}/g, "-").toLowerCase() const safe = hostname.replace(/[^a-z0-9_-]/gi, "-").replace(/-{2,}/g, "-").toLowerCase()
return safe || fallback return safe || fallback
} }
export async function GET(_request: Request, context: RouteContext) { export async function GET(request: NextRequest, ctx: Ctx) {
const session = await assertAuthenticatedSession() const session = await assertAuthenticatedSession()
if (!session) return NextResponse.json({ error: "Não autorizado" }, { status: 401 }) if (!session) return NextResponse.json({ error: "Não autorizado" }, { status: 401 })
@ -30,7 +27,7 @@ export async function GET(_request: Request, context: RouteContext) {
return NextResponse.json({ error: "Convex não configurado" }, { status: 500 }) return NextResponse.json({ error: "Convex não configurado" }, { status: 500 })
} }
const { id } = await context.params const { id } = await ctx.params
const machineId = id as Id<"machines"> const machineId = id as Id<"machines">
const client = new ConvexHttpClient(convexUrl) const client = new ConvexHttpClient(convexUrl)
const tenantId = session.user.tenantId ?? DEFAULT_TENANT_ID const tenantId = session.user.tenantId ?? DEFAULT_TENANT_ID

View file

@ -1,3 +1,4 @@
import type { NextRequest } from "next/server"
import { NextResponse } from "next/server" import { NextResponse } from "next/server"
import { assertAdminSession } from "@/lib/auth-server" import { assertAdminSession } from "@/lib/auth-server"
@ -12,10 +13,10 @@ import {
export const runtime = "nodejs" export const runtime = "nodejs"
type RouteContext = { params: Promise<{ id: string }> } type Ctx = RouteContext<"/api/reports/schedules/[id]">
export async function PATCH(request: Request, context: RouteContext) { export async function PATCH(request: NextRequest, ctx: Ctx) {
const { id } = await context.params const { id } = await ctx.params
const session = await assertAdminSession() const session = await assertAdminSession()
if (!session) { if (!session) {
return NextResponse.json({ error: "Não autorizado" }, { status: 401 }) return NextResponse.json({ error: "Não autorizado" }, { status: 401 })
@ -143,8 +144,8 @@ export async function PATCH(request: Request, context: RouteContext) {
}) })
} }
export async function DELETE(_: Request, context: RouteContext) { export async function DELETE(_: NextRequest, ctx: Ctx) {
const { id } = await context.params const { id } = await ctx.params
const session = await assertAdminSession() const session = await assertAdminSession()
if (!session) { if (!session) {
return NextResponse.json({ error: "Não autorizado" }, { status: 401 }) return NextResponse.json({ error: "Não autorizado" }, { status: 401 })

View file

@ -1,3 +1,4 @@
import type { NextRequest } from "next/server"
import { NextResponse } from "next/server" import { NextResponse } from "next/server"
import { assertAdminSession } from "@/lib/auth-server" import { assertAdminSession } from "@/lib/auth-server"
@ -6,14 +7,17 @@ import { prisma } from "@/lib/prisma"
export const runtime = "nodejs" export const runtime = "nodejs"
export async function GET(request: Request, { params }: { params: { id: string } }) { type Ctx = RouteContext<"/api/reports/schedules/runs/[id]">
export async function GET(request: NextRequest, ctx: Ctx) {
const { id } = await ctx.params
const session = await assertAdminSession() const session = await assertAdminSession()
if (!session) { if (!session) {
return NextResponse.json({ error: "Não autorizado" }, { status: 401 }) return NextResponse.json({ error: "Não autorizado" }, { status: 401 })
} }
const tenantId = session.user.tenantId ?? DEFAULT_TENANT_ID const tenantId = session.user.tenantId ?? DEFAULT_TENANT_ID
const run = await prisma.reportExportRun.findFirst({ const run = await prisma.reportExportRun.findFirst({
where: { id: params.id, tenantId }, where: { id, tenantId },
}) })
if (!run || !run.artifacts) { if (!run || !run.artifacts) {
return NextResponse.json({ error: "Arquivo não encontrado" }, { status: 404 }) return NextResponse.json({ error: "Arquivo não encontrado" }, { status: 404 })