#Route handler (problem)

4 messages · Page 1 of 1 (latest)

forest flint
#

After creating the API route handler, I have a function that handles GET. then I called fetch in some component and did await res.json() and after that I have no type for body. How to get the type so that typescript has values automatically based on the body instead of manually setting it? I want to infer data based on GET function.

dense bayBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

forest flint
#
import { NextResponse } from "next/server"

export async function GET(request: Request)
{
    const {searchParams} = new URL(request.url)
    const query = searchParams.get('query')
    const page = searchParams.get('page')

    const res = await fetch(`pixabayLink`) //I removed link
    const fetchedData: { totalHits: number, hits: {webformatURL: string}[]} = await res.json()
    const value = {
        isNext: fetchedData.totalHits > (Number(page) * 8),
        isPrevious: Number(page) > 1,
        images: fetchedData.hits.map(el => ({url: el.webformatURL}))
    }


    return Response.json({value})
    
}
``` ```
function Modal({ setValue }: ModalProps) {

  const getImages = async () => {
    const data = await fetch("/api/?query=plane&page=1");
    const d = await data.json();
    setImages(d.value.images); //HERE IS THE PROBLEM
  };
main oasis