The expected type of the request argument, is wrong for route handlers
Gawdfrey opened this issue · comments
When trying to use the sanity webhook in a route handler, I get an error saying that the expected type is NextApiRequest
but the type of the request in the route handler is NextRequest.
The examples under the next-sanity/webhook title shows the type of the request being NextRequest and then being passed to the parseBody without errors, this is not correct.
Hey I had the same problem and then this Type Error was returning me a 500 from the server.
I used this code found in the revalidate-tag folder of the next-sanity repo: next-sanity/app/api/revalidate-tag/route.ts
I just changed the import of the parseAppBody.
This code works good for me, let me know if it's okay.
/* eslint-disable no-process-env */
import {revalidateTag} from 'next/cache'
import {NextRequest} from 'next/server'
import {NextResponse} from 'next/server'
import {parseAppBody} from 'next-sanity/webhook'
// Triggers a revalidation of the static data in the example above
export async function POST(req: NextRequest): Promise<any> {
try {
const {body, isValidSignature} = await parseAppBody<{
_type: string
_id: string
slug?: string | undefined
}>(req, process.env.SANITY_REVALIDATE_SECRET)
if (isValidSignature === false) {
const message = 'Invalid signature'
return new Response(message, {status: 401})
}
if (!body._type) {
return new Response('Bad Request', {status: 400})
}
await Promise.all(
[body.slug, body._type, body._id].map(
(tag) => typeof tag === 'string' && revalidateTag(tag),
),
)
return NextResponse.json(body)
} catch (err: any) {
console.error(err)
return new Response(err.message, {status: 500})
}
}
Hmm, parseAppBody
is deprecated.. But when I now tried with parseBody
, things suddenly worked??
Appreciate the response!