-
Notifications
You must be signed in to change notification settings - Fork 18
Hannah N #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Hannah N #7
Changes from all commits
6479cee
608f0bb
767fa1e
2cdc355
315c62c
1771714
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,16 @@ | ||
| // API documentation: https://www.thecocktaildb.com/api.php | ||
|
|
||
| import path from 'path'; | ||
| import { error } from "console"; | ||
| import path from "path"; | ||
| import { writeFile } from "fs/promises"; | ||
|
|
||
| const BASE_URL = 'https://www.thecocktaildb.com/api/json/v1/1'; | ||
| const BASE_URL = "https://www.thecocktaildb.com/api/json/v1/1"; | ||
|
|
||
| // Add helper functions as needed here | ||
|
|
||
| export async function main() { | ||
| if (process.argv.length < 3) { | ||
| console.error('Please provide a cocktail name as a command line argument.'); | ||
| console.error("Please provide a cocktail name as a command line argument."); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -20,10 +22,77 @@ export async function main() { | |
|
|
||
| try { | ||
| // 1. Fetch data from the API at the given URL | ||
| const response = await fetch( | ||
| `https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${cocktailName}`, | ||
| ); | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| // 2. Generate markdown content to match the examples | ||
|
|
||
| let markdown = `# Cocktail Recipes\n\n`; | ||
|
|
||
| if (!data.drinks || data.drinks.length === 0) { | ||
| throw new Error("No cocktails found with that name."); | ||
| } else { | ||
| for (const drink of data.drinks) { | ||
| markdown += `## ${drink.strDrink}\n\n`; | ||
|
|
||
| // Use strDrinkThumb medium | ||
| if (drink.strDrinkThumb) { | ||
| const thumbUrl = `${drink.strDrinkThumb}/medium`; | ||
| markdown += `\n\n`; | ||
| } | ||
|
|
||
| // Category & Alcoholic | ||
| markdown += `**Category**: ${drink.strCategory || "Unknown"}\n`; | ||
|
|
||
| let alcoholicText = "—"; | ||
| if (drink.strAlcoholic === "Alcoholic") { | ||
| alcoholicText = "Yes"; | ||
| } else if ( | ||
| drink.strAlcoholic === "Non alcoholic" || | ||
| drink.strAlcoholic === "Non-Alcoholic" | ||
| ) { | ||
| alcoholicText = "No"; | ||
| } | ||
| markdown += `**Alcoholic**: ${alcoholicText}\n\n`; | ||
|
|
||
| // Ingredients | ||
| markdown += `### Ingredients\n\n`; | ||
| let ingredientCount = 0; | ||
|
|
||
| for (let i = 1; i <= 15; i++) { | ||
| const ingredient = drink[`strIngredient${i}`]; | ||
| if (ingredient && ingredient.trim()) { | ||
| ingredientCount++; | ||
| const measure = drink[`strMeasure${i}`]?.trim() || ""; | ||
| const line = measure ? `${measure} ${ingredient}` : ingredient; | ||
| markdown += `- ${line}\n`; | ||
| } | ||
| } | ||
|
|
||
| if (ingredientCount === 0) { | ||
| markdown += `- No ingredients listed\n`; | ||
| } | ||
| markdown += `\n`; | ||
|
|
||
| // Instructions | ||
| markdown += `### Instructions\n${drink.strInstructions || "No instructions provided."}\n\n`; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here is missing \n |
||
|
|
||
| // Glass | ||
| markdown += `Serve in: ${drink.strGlass || "Unknown"}\n\n`; | ||
|
|
||
| markdown += `---\n\n`; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bravo, I like that you introduced a line separator between recipes, this line could be even longer to make sure the user knows that this is the end. |
||
| } | ||
| } | ||
|
|
||
| // 3. Write the generated content to a markdown file as given by outPath | ||
| await writeFile(outPath, markdown, "utf-8"); | ||
| console.log(`Successfully wrote to ${outPath}`); | ||
| } catch (error) { | ||
| // 4. Handle errors | ||
| console.error(error.message); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Cocktail Recipes | ||
|
|
||
| ## Margarita | ||
|
|
||
|  | ||
|
|
||
| **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 | ||
|
|
||
| --- | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Cocktail Recipes | ||
|
|
||
| No cocktails found for "nonexistentcocktail". |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // Change base URL for API requests to the local IP of the Post Central API server | ||
| const BASE_URL = 'http://localhost:3000'; | ||
| const BASE_URL = "http://localhost:3000"; | ||
|
|
||
| // ============================================================================ | ||
| // AUTH TOKEN - Stored after login/register, sent with every request | ||
|
|
@@ -37,7 +37,7 @@ const getHello = async () => { | |
| const response = await fetch(`${BASE_URL}/posts/hello`); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to get hello: HTTP ${response.status} ${response.statusText}` | ||
| `Failed to get hello: HTTP ${response.status} ${response.statusText}`, | ||
| ); | ||
| } | ||
| return await response.json(); | ||
|
|
@@ -54,6 +54,22 @@ const getHello = async () => { | |
| */ | ||
| const getMe = async () => { | ||
| // TODO | ||
| const response = await fetch(`${BASE_URL}/users/me`, { | ||
| /** | ||
| * | ||
| */ | ||
| method: "GET", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: `Bearer ${getToken()}`, | ||
| }, | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to get user: HTTP ${response.status} ${response.statusText}`, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my main reviewed comment please about this line |
||
| ); | ||
| } | ||
| return await response.json(); | ||
| }; | ||
|
|
||
| // ============================================================================ | ||
|
|
@@ -68,15 +84,15 @@ const getMe = async () => { | |
| */ | ||
| const createUser = async (name, password) => { | ||
| const response = await fetch(`${BASE_URL}/users/register`, { | ||
| method: 'POST', | ||
| method: `POST`, | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| "Content-Type": `application/json`, | ||
| }, | ||
| body: JSON.stringify({ name, password }), | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to create user: HTTP ${response.status} ${response.statusText}` | ||
| `Failed to create user: HTTP ${response.status} ${response.statusText}`, | ||
| ); | ||
| } | ||
| return await response.json(); | ||
|
|
@@ -90,6 +106,19 @@ 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 create user: HTTP ${response.status} ${response.statusText}`, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my main reviewed comment please about this line |
||
| ); | ||
| } | ||
| return await response.json(); | ||
| }; | ||
|
|
||
| // ============================================================================ | ||
|
|
@@ -104,6 +133,20 @@ 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 get user: HTTP ${response.status} ${response.statusText}`, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my main reviewed comment please about this line |
||
| ); | ||
|
|
||
| return await response.json(); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -113,6 +156,22 @@ const createPost = async (text) => { | |
| */ | ||
| const getPosts = async () => { | ||
| // TODO | ||
| const response = await fetch(`${BASE_URL}/posts/me`, { | ||
| /** | ||
| * | ||
| */ | ||
| method: "GET", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: `Bearer ${getToken()}`, | ||
| }, | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to get user: HTTP ${response.status} ${response.statusText}`, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my main reviewed comment please about this line |
||
| ); | ||
| } | ||
| return await response.json(); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -123,6 +182,21 @@ 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 get user: HTTP ${response.status} ${response.statusText}`, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my main reviewed comment please about this line |
||
| ); | ||
| } | ||
| return await response.json(); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -132,6 +206,19 @@ const updatePost = async (id, text) => { | |
| */ | ||
| const deleteUser = async () => { | ||
| // TODO | ||
| const response = await fetch(`${BASE_URL}/users/me`, { | ||
| method: "DELETE", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: `Bearer ${getToken()}`, | ||
| }, | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to get user: HTTP ${response.status} ${response.statusText}`, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my main reviewed comment please about this line |
||
| ); | ||
| } | ||
| return await response.json(); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -141,6 +228,19 @@ const deleteUser = async () => { | |
| */ | ||
| const deletePost = async (id) => { | ||
| // TODO | ||
| const response = await fetch(`${BASE_URL}/posts/${id}`, { | ||
| method: "DELETE", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: `Bearer ${getToken()}`, | ||
| }, | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to delete post: HTTP ${response.status} ${response.statusText}`, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my main reviewed comment please about this line |
||
| ); | ||
| } | ||
| return await response.json(); | ||
| }; | ||
|
|
||
| // ============================================================================ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is missing an \n