-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathproxy.js
More file actions
56 lines (35 loc) · 1.41 KB
/
proxy.js
File metadata and controls
56 lines (35 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export default {
async fetch(request) {
const url = new URL(request.url);
const apiKey = "1234567890";
const spoonacularBaseUrl = "https://api.spoonacular.com";
let targetUrl;
if(url.pathname.startsWith("/recipes/search")) {
const query = url.searchParams.get("query");
targetUrl = `${spoonacularBaseUrl}/recipes/complexSearch?apiKey=${apiKey}&query=${query}`;
} else if (url.pathname.startsWith("/recipes/details")) {
const recipeId = url.searchParams.get("id");
targetUrl = `${spoonacularBaseUrl}/recipes/${recipeId}/information?apiKey=${apiKey}`;
} else {
return new Response("Invalid endpoint", {status: 404});
}
try {
const response = await fetch(targetUrl);
const data = await response.json();
return new Response(JSON.stringify(data),{
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
},
});
} catch (error) {
return new Response(JSON.stringify({error: "Failed to fetch data"}),{
status: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
},
});
}
},
};