-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecf9529
commit d2155f6
Showing
1 changed file
with
26 additions
and
37 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 |
---|---|---|
@@ -1,53 +1,42 @@ | ||
import { errorOnDisplaySearchedRecipes } from "../errors/recipes.errors"; | ||
|
||
import { TIMEOUT_SEC } from "../constants/recipes.constants"; | ||
|
||
// API requests for recipes | ||
|
||
// helpers functions | ||
|
||
const timeout = (seconds) => { | ||
return new Promise(function (_, reject) { | ||
setTimeout(function () { | ||
reject(new Error(`Request took too long! Timeout after ${seconds} seconds`)); | ||
}, seconds * 1000); | ||
}); | ||
}; | ||
|
||
// requests | ||
|
||
export const getRecipes = async (recipe) => { | ||
try { | ||
console.log(`${process.env.REACT_APP_RECIPES_URL}${recipe}?${process.env.REACT_APP_RECIPES_API_KEY_NAME}${process.env.REACT_APP_RECIPES_API_KEY}`); | ||
const fetchPromiseRecipes = fetch(`${process.env.REACT_APP_RECIPES_URL}${recipe}?${process.env.REACT_APP_RECIPES_API_KEY_NAME}${process.env.REACT_APP_RECIPES_API_KEY}`) | ||
const resRecipes = await Promise.race([fetchPromiseRecipes, timeout(TIMEOUT_SEC)]); | ||
const dataRecipes = await resRecipes.json(); | ||
|
||
if (!resRecipes.ok) { | ||
throw new Error(`${dataRecipes.message} (${dataRecipes.status})`); | ||
} | ||
|
||
return dataRecipes.data.recipes; | ||
console.log(`${process.env.REACT_APP_API_URL_RECIPES_RECIPES}`) | ||
const response = await fetch(`${process.env.REACT_APP_API_URL_RECIPES_RECIPES}`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "text/plain" | ||
}, | ||
body: String(recipe) | ||
}) | ||
const resJSON = await response.json() | ||
|
||
return resJSON.recipes | ||
} catch (error) { | ||
errorOnDisplaySearchedRecipes(recipe); | ||
console.log(error); | ||
console.log(error) | ||
errorOnDisplaySearchedRecipes() | ||
} | ||
}; | ||
|
||
export const getRecipe = async (recipe) => { | ||
try { | ||
const fetchPromiseRecipe = fetch(`${process.env.REACT_APP_RECIPE_URL}${recipe.id}?${process.env.REACT_APP_RECIPES_API_KEY_NAME}${process.env.REACT_APP_RECIPES_API_KEY}`); | ||
const resRecipe = await Promise.race([fetchPromiseRecipe, timeout(TIMEOUT_SEC)]); | ||
const dataRecipe = await resRecipe.json(); | ||
|
||
if (!resRecipe.ok) { | ||
throw new Error(`${dataRecipe.message} (${dataRecipe.status})`); | ||
} | ||
|
||
return dataRecipe.data.recipe; | ||
console.log(`${process.env.REACT_APP_API_URL_RECIPES_RECIPE}`) | ||
const response = await fetch(`${process.env.REACT_APP_API_URL_RECIPES_RECIPE}`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
body: JSON.stringify(recipe) | ||
}) | ||
const resJSON = await response.json() | ||
|
||
return resJSON.recipe | ||
} catch (error) { | ||
errorOnDisplaySearchedRecipes(recipe.title); | ||
console.log(error); | ||
console.log(error) | ||
errorOnDisplaySearchedRecipes() | ||
} | ||
}; | ||
|