This repository has been archived by the owner on Dec 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from Joewizy/dev
feat: implement product verification
- Loading branch information
Showing
4 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ RPC_URL= | |
|
||
DATABASE_URL= | ||
|
||
BACKEND_BASE_URL=http://localhost:3000/api | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
type ProductDetails = { | ||
product_id: string; | ||
name: string; | ||
image: string; | ||
manufacturer: string; | ||
manufactureDate: string; | ||
expiryDate: string; | ||
}; | ||
|
||
type ErrorResponse = { | ||
error: string; | ||
}; | ||
|
||
const BASE_URL = process.env.BACKEND_BASE_URL || 'http://localhost:3000'; // Update to match your backend URL | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<ProductDetails | ErrorResponse> | ||
) { | ||
const { productId } = req.query; | ||
|
||
if (req.method !== 'GET') { | ||
return res.status(405).json({ error: 'Method Not Allowed' }); | ||
} | ||
|
||
if (!productId || typeof productId !== 'string') { | ||
return res.status(400).json({ error: 'Invalid or missing productId' }); | ||
} | ||
|
||
try { | ||
const response = await fetch(`${BASE_URL}/products/${productId}`); | ||
|
||
if (!response.ok) { | ||
const errorBody = await response.json(); | ||
return res | ||
.status(response.status) | ||
.json({ error: errorBody.error || 'Failed to fetch product details' }); | ||
} | ||
|
||
const productDetails: ProductDetails = await response.json(); | ||
return res.status(200).json(productDetails); | ||
} catch (error: any) { | ||
console.error('Error fetching product details:', error.message); | ||
return res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters