diff --git a/package-lock.json b/package-lock.json index 65aa212..da4f394 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,6 @@ "": { "name": "core-assignment-week-10", "version": "1.0.0", - "hasInstallScript": true, "license": "CC BY-NC-SA 4.0", "dependencies": { "@inquirer/prompts": "^7.0.0", diff --git a/task-1/cocktail.js b/task-1/cocktail.js index 6fd5a62..721979a 100644 --- a/task-1/cocktail.js +++ b/task-1/cocktail.js @@ -1,6 +1,8 @@ // API documentation: https://www.thecocktaildb.com/api.php import path from 'path'; +import fs from 'fs/promises'; + const BASE_URL = 'https://www.thecocktaildb.com/api/json/v1/1'; @@ -20,10 +22,42 @@ export async function main() { try { // 1. Fetch data from the API at the given URL + const response = await fetch(url); + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + const jsonData = await response.json(); + + if (!jsonData.drinks) throw new Error('No cocktails found with that name.'); + const drinks = jsonData.drinks; + // 2. Generate markdown content to match the examples + let markdown = '# Cocktail Recipes\n\n'; + + drinks.forEach(drink => { + markdown += `## ${drink.strDrink}\n`; + markdown += `![${drink.strDrink}](${drink.strDrinkThumb}/medium)\n`; + markdown += `**Category**: ${drink.strCategory}\n`; + markdown += `**Alcoholic**: ${drink.strAlcoholic === 'Alcoholic' ? 'Yes' : 'No'}\n\n`; + markdown += `### Ingredients\n`; + for (let i = 1; i <= 15; i++) { + const ingredient = drink[`strIngredient${i}`]; + const measure = drink[`strMeasure${i}`]; + if (ingredient) { + markdown += `- ${measure ? measure : ''}${ingredient}\n`; + } + } + markdown += `\n### Instructions\n`; + markdown += `${drink.strInstructions}\n\n`; + markdown += `Serve in: ${drink.strGlass}\n\n`; + }); + + // 3. Write the generated content to a markdown file as given by outPath + await fs.writeFile(outPath, markdown, 'utf-8'); + console.log(`Markdown file created at ${outPath}`); + } catch (error) { // 4. Handle errors + console.error(error.message); } } @@ -31,3 +65,5 @@ export async function main() { if (!process.env.VITEST) { main(); } + + diff --git a/task-1/output/margarita.md b/task-1/output/margarita.md new file mode 100644 index 0000000..2f8ad0d --- /dev/null +++ b/task-1/output/margarita.md @@ -0,0 +1,18 @@ +# Cocktail Recipes + +## Margarita +![Margarita](https://www.thecocktaildb.com/images/media/drink/5noda61589575158.jpg/medium) +**Category**: Ordinary Drink +**Alcoholic**: Yes + +### Ingredients +- 1 1/2 oz Tequila +- 1/2 oz Triple sec +- 1 oz Lime juice +- Salt + +### Instructions +Rub the rim of the glass. + +Serve in: Cocktail glass + diff --git a/task-2/post-cli/src/services.js b/task-2/post-cli/src/services.js index 2601f57..2d25b6a 100644 --- a/task-2/post-cli/src/services.js +++ b/task-2/post-cli/src/services.js @@ -54,6 +54,20 @@ const getHello = async () => { */ const getMe = async () => { // TODO + const response = await fetch(`${BASE_URL}/users/me`, { + method: 'GET', + headers: { + Authorization: `Bearer ${getToken()}` + } + }); + + if (!response.ok) { + throw new Error( + `Failed to get user: HTTP ${response.status} ${response.statusText}` + ); + } + + return await response.json(); }; // ============================================================================ @@ -90,6 +104,21 @@ const createUser = async (name, password) => { */ const loginUser = async (name, password) => { // TODO + const response = await fetch(`${BASE_URL}/users/login`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ name, password }), + }); + + if (!response.ok) { + throw new Error( + `Failed to login: HTTP ${response.status} ${response.statusText}` + ); + } + + return await response.json(); }; // ============================================================================ @@ -104,6 +133,22 @@ const loginUser = async (name, password) => { */ const createPost = async (text) => { // TODO + const response = await fetch(`${BASE_URL}/posts`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${getToken()}` + }, + body: JSON.stringify({ text }), + }); + + if (!response.ok) { + throw new Error( + `Failed to create post: HTTP ${response.status} ${response.statusText}` + ); + } + + return await response.json(); }; /** @@ -113,6 +158,20 @@ const createPost = async (text) => { */ const getPosts = async () => { // TODO + const response = await fetch(`${BASE_URL}/posts/me`, { + method: 'GET', + headers: { + Authorization: `Bearer ${getToken()}` + } + }); + + if (!response.ok) { + throw new Error( + `Failed to get posts: HTTP ${response.status} ${response.statusText}` + ); + } + + return await response.json(); }; /** @@ -123,6 +182,22 @@ const getPosts = async () => { */ const updatePost = async (id, text) => { // TODO + const response = await fetch(`${BASE_URL}/posts/${id}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${getToken()}` + }, + body: JSON.stringify({ text }), + }); + + if (!response.ok) { + throw new Error( + `Failed to update post: HTTP ${response.status} ${response.statusText}` + ); + } + + return await response.json(); }; /** @@ -132,6 +207,20 @@ const updatePost = async (id, text) => { */ const deleteUser = async () => { // TODO + const response = await fetch(`${BASE_URL}/users/me`, { + method: 'DELETE', + headers: { + Authorization: `Bearer ${getToken()}` + } + }); + + if (!response.ok) { + throw new Error( + `Failed to delete user: HTTP ${response.status} ${response.statusText}` + ); + } + + return response.json ? await response.json() : null; }; /** @@ -141,6 +230,20 @@ const deleteUser = async () => { */ const deletePost = async (id) => { // TODO + const response = await fetch(`${BASE_URL}/posts/${id}`, { + method: 'DELETE', + headers: { + Authorization: `Bearer ${getToken()}` + } + }); + + if (!response.ok) { + throw new Error( + `Failed to delete post: HTTP ${response.status} ${response.statusText}` + ); + } + + return response.json ? await response.json() : null; }; // ============================================================================