Build error with my api
Unanswered
kubas posted this in #help-forum
kubasOP
Hi, that's my route.tsx code:
When I type "build" in the terminal, after a while I get this error:
### Localy(on my pc):
### on hosting:
5:05:20 PM: Type error: Route "app/api/webhooks/reload-blog-page/route.tsx" has an invalid "GET" export:
5:05:20 PM: Type "NextApiRequest" is not a valid type for the function's first argument.
Expected "Request | NextRequest", got "NextApiRequest".
What's this? How to fix it? I'm using the latest next version of js with ./app.
import { NextApiRequest, NextApiResponse } from "next";
import { revalidatePath } from "next/cache";
import { NextResponse } from "next/server";
export async function GET(req: NextApiRequest) {
const url = new URL(req.url || '', `http://${req.headers.host}`);
const auth = url.searchParams.get('auth');
if(!auth) return NextResponse.json({revalidated: false, message: 'NO AUTH ID'}, {status: 400})
if(auth != process.env.HYGRAPH_WEBHOOK_REVALIDATE_AUTHID) return NextResponse.json({revalidated: false, message: 'INVALID AUTH ID'}, {status: 401})
revalidatePath('/blog')
return NextResponse.json({revalidated: true, message: 'OK'}, {status: 200})
}When I type "build" in the terminal, after a while I get this error:
### Localy(on my pc):
- info Linting and checking validity of types ..Failed to compile.
.next/types/app/api/webhooks/reload-blog-page/route.ts:32:7
Type error: Type '{ __tag__: "GET"; __param_position__: "first"; __param_type__: NextApiRequest; }' does not satisfy the constraint 'ParamCheck<Request | NextRequest>'.
Types of property '__param_type__' are incompatible.
Type 'NextApiRequest' is not assignable to type 'Request | NextRequest'.
Type 'NextApiRequest' is missing the following properties from type 'NextRequest': geo, ip, nextUrl, page, and 19 more.
30 | Diff<
31 | ParamCheck<Request | NextRequest>,
> 32 | {
| ^
33 | __tag__: 'GET'
34 | __param_position__: 'first'
35 | __param_type__: FirstArg<MaybeField<TEntry, 'GET'>>### on hosting:
5:05:20 PM: Type error: Route "app/api/webhooks/reload-blog-page/route.tsx" has an invalid "GET" export:
5:05:20 PM: Type "NextApiRequest" is not a valid type for the function's first argument.
Expected "Request | NextRequest", got "NextApiRequest".
What's this? How to fix it? I'm using the latest next version of js with ./app.
1 Reply
Greater Swiss Mountain Dog
hey you may have to combined types for NextApiRequest with Request; here
type CombineRequest = Request & NextApiRequest;
type CombineResponse = Response & NextApiResponse;
then pass CombineRequest and CombineResponse in the parameter instead of NextApiRequest or NextApiResponse
type CombineRequest = Request & NextApiRequest;
type CombineResponse = Response & NextApiResponse;
then pass CombineRequest and CombineResponse in the parameter instead of NextApiRequest or NextApiResponse