Skip to content

Commit

Permalink
Add api route for specific product
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniobiasotti committed Apr 26, 2024
1 parent 6631b72 commit 79dfaa5
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/app/api/products/[slug]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NextResponse } from 'next/server'
import data from '../data.json'
import { z } from 'zod'

export async function GET(_: Request,
{ params }: { params: { slug: string } }
) {
console.log(params.slug)

await new Promise(resolve => setTimeout(resolve, 1000))

const slug = z.string().parse(params.slug)

const product = data.products.find((product) => product.slug === slug)

if (!product) {
return Response.json({ message: 'Product not found.' }, { status: 400 })
}

return Response.json(product)
}

0 comments on commit 79dfaa5

Please sign in to comment.