Some checks failed
Deploy to Coolify / Code Quality (pull_request) Has been cancelled
Deploy to Coolify / Run Tests (pull_request) Has been cancelled
Deploy to Coolify / Deploy to Development (pull_request) Has been cancelled
Deploy to Coolify / Deploy to Production (pull_request) Has been cancelled
Deploy to Coolify / Deploy to Test (pull_request) Has been cancelled
Pull Request Checks / Validate PR (pull_request) Has been cancelled
- Fetch product data from Open Food Facts API - Cache results in products table - Handle product not found gracefully - CORS enabled for frontend access - Returns cached data for performance Closes #24
84 lines
1.4 KiB
Markdown
84 lines
1.4 KiB
Markdown
# Product Lookup Edge Function
|
|
|
|
Fetches product data from Open Food Facts API by barcode and caches results in the database.
|
|
|
|
## Endpoint
|
|
|
|
`POST /functions/v1/product-lookup`
|
|
|
|
## Request
|
|
|
|
```json
|
|
{
|
|
"barcode": "8000500310427"
|
|
}
|
|
```
|
|
|
|
## Response
|
|
|
|
### Success (200)
|
|
|
|
```json
|
|
{
|
|
"barcode": "8000500310427",
|
|
"name": "Nutella",
|
|
"brand": "Ferrero",
|
|
"quantity": "750g",
|
|
"image_url": "https://...",
|
|
"category": "spreads",
|
|
"cached": false
|
|
}
|
|
```
|
|
|
|
### Not Found (404)
|
|
|
|
```json
|
|
{
|
|
"barcode": "1234567890123",
|
|
"name": "Unknown Product (1234567890123)",
|
|
"cached": false
|
|
}
|
|
```
|
|
|
|
### Error (500)
|
|
|
|
```json
|
|
{
|
|
"error": "Error message",
|
|
"barcode": null,
|
|
"name": null
|
|
}
|
|
```
|
|
|
|
## Features
|
|
|
|
- ✅ Queries Open Food Facts API
|
|
- ✅ Caches results in `products` table
|
|
- ✅ Returns cached data for subsequent requests
|
|
- ✅ Handles product not found gracefully
|
|
- ✅ CORS enabled for frontend access
|
|
|
|
## Environment Variables
|
|
|
|
- `SUPABASE_URL`: Auto-injected by Supabase
|
|
- `SUPABASE_SERVICE_ROLE_KEY`: Auto-injected by Supabase
|
|
|
|
## Testing
|
|
|
|
```bash
|
|
# Local (with Supabase CLI)
|
|
supabase functions serve product-lookup
|
|
|
|
# Test request
|
|
curl -X POST http://localhost:54321/functions/v1/product-lookup \
|
|
-H "Content-Type: application/json" \
|
|
-H "Authorization: Bearer YOUR_ANON_KEY" \
|
|
-d '{"barcode":"8000500310427"}'
|
|
```
|
|
|
|
## Deployment
|
|
|
|
```bash
|
|
supabase functions deploy product-lookup
|
|
```
|