With Next.js 13 Routes API, what are TypeScript declarations for GET?

2.1k views Asked by At

I'm trying to not use the any type when creating a GET handler with the new Next.js 13 experimental routing API.

Here is my code:

export async function GET(request: any, {params}: any) {
  function getRandomInt(min: number, max: number) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
  }

  const youTubeId = params.id;

I've tried HttpWebResponse but it doesn't help.

1

There are 1 answers

0
Yilmaz On
export async function GET(request: Request) {
  return new Response('Hello world')
}

Request and Response are globally available. its type will be inferred as function GET(request: Request): Promise<Response>

or you could use NextResponse

import { NextResponse } from "next/server";

export async function GET(request: Request) {
    return NextResponse.json({ message:"success" }, { status: 200 });
}

and its type will be inferred as function GET(request: Request): Promise<NextResponse>