-
Notifications
You must be signed in to change notification settings - Fork 18
Mareh A. #19
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?
Mareh A. #19
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,13 @@ | ||
| // API documentation: https://www.thecocktaildb.com/api.php | ||
|
|
||
| import path from 'path'; | ||
| import path from "path"; | ||
| import { writeFile } from "fs/promises"; | ||
|
|
||
| const BASE_URL = 'https://www.thecocktaildb.com/api/json/v1/1'; | ||
|
|
||
| // Add helper functions as needed here | ||
| const BASE_URL = "https://www.thecocktaildb.com/api/json/v1/1"; | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
|
|
@@ -19,15 +18,58 @@ export async function main() { | |
| const outPath = path.join(__dirname, `./output/${cocktailName}.md`); | ||
|
|
||
| try { | ||
| // 1. Fetch data from the API at the given URL | ||
| // 2. Generate markdown content to match the examples | ||
| // 3. Write the generated content to a markdown file as given by outPath | ||
| const response = await fetch(url, { | ||
| method: "GET", | ||
| }); | ||
| const data = await response.json(); | ||
| if (!response.ok) { | ||
| const error = new Error(data.error || response.statusText); | ||
| error.status = response.status; | ||
| throw error; | ||
| } | ||
| if (data.drinks === null) { | ||
| return; | ||
| } | ||
| const markdown = []; | ||
| for (const drink of data.drinks) { | ||
| const name = `## ${drink.strDrink}`; | ||
| const image = ``; | ||
| const category = `**Category:** ${drink.strCategory}`; | ||
|
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. The colon should be outside the bold marker |
||
| const alcoholic = `**Alcoholic:** ${drink.strAlcoholic}`; | ||
| const instructions = `### Instructions\n${drink.strInstructions}`; | ||
| const glass = `**Glass:** ${drink.strGlass}`; | ||
|
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. This should be |
||
| let ingredients = "### Ingredients\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. Lists in markdown should have blank lines before and after, so another |
||
| for (let i = 1; i <= 15; i++) { | ||
| const ingredient = drink["strIngredient" + i]; | ||
| const measure = drink["strMeasure" + i]; | ||
| if (ingredient) { | ||
| ingredients += `- ${measure ? measure : ""}${measure ? " " : ""}${ingredient}\n`; | ||
| } | ||
| } | ||
| const block = | ||
| name + | ||
| "\n\n" + | ||
| image + | ||
| "\n\n" + | ||
| category + | ||
| "\n" + | ||
| alcoholic + | ||
| "\n\n" + | ||
| ingredients + | ||
| "\n" + | ||
| instructions + | ||
| "\n\n" + | ||
| glass; | ||
| markdown.push(block); | ||
| } | ||
| const content = markdown.join("\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. The file should start with |
||
| await writeFile(outPath, content); | ||
| } catch (error) { | ||
| // 4. Handle errors | ||
| console.error("Failed to get or to write cocktail data:", error.message); | ||
| } | ||
| //return data; | ||
|
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. the commented-out code should be deleted |
||
| } | ||
|
|
||
| // Do not change the code below | ||
| if (!process.env.VITEST) { | ||
| main(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| ## Margarita | ||
|
|
||
|  | ||
|
|
||
| **Category:** Ordinary Drink | ||
| **Alcoholic:** Alcoholic | ||
|
|
||
| ### Ingredients | ||
| - 1 1/2 oz Tequila | ||
| - 1/2 oz Triple sec | ||
| - 1 oz Lime juice | ||
| - Salt | ||
|
|
||
| ### Instructions | ||
| Rub the rim of the glass. | ||
|
|
||
| **Glass:** Cocktail glass |
| 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(); | ||
|
|
@@ -53,7 +53,18 @@ const getHello = async () => { | |
| * Response: { user: string } | ||
| */ | ||
| const getMe = async () => { | ||
| // TODO | ||
| const response = await fetch(`${BASE_URL}/users/me`, { | ||
| method: "Get", | ||
|
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.
|
||
| headers: { | ||
| Authorization: `Bearer ${getToken()}`, | ||
| }, | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to get user information: HTTP ${response.status} ${response.statusText}`, | ||
| ); | ||
| } | ||
| return await response.json(); | ||
| }; | ||
|
|
||
| // ============================================================================ | ||
|
|
@@ -68,15 +79,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(); | ||
|
|
@@ -89,7 +100,17 @@ const createUser = async (name, password) => { | |
| * Response: { user: string, token: string } | ||
| */ | ||
| 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 user: HTTP ${response.status} ${response.statusText}`, | ||
| ); | ||
| } | ||
| return await response.json(); | ||
| }; | ||
|
|
||
| // ============================================================================ | ||
|
|
@@ -103,7 +124,20 @@ const loginUser = async (name, password) => { | |
| * Response: { id: number, text: string, user: string } | ||
| */ | ||
| 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 a post: HTTP ${response.status} ${response.statusText}`, | ||
| ); | ||
| } | ||
| return await response.json(); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -112,7 +146,18 @@ const createPost = async (text) => { | |
| * Response: Array of { id, text, user } | ||
| */ | ||
| const getPosts = async () => { | ||
| // TODO | ||
| const response = await fetch(`${BASE_URL}/posts/me`, { | ||
| method: "Get", | ||
|
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.
|
||
| headers: { | ||
| Authorization: `Bearer ${getToken()}`, | ||
| }, | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to get a post: HTTP ${response.status} ${response.statusText}`, | ||
| ); | ||
| } | ||
| return await response.json(); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -122,7 +167,20 @@ const getPosts = async () => { | |
| * Response: { id: number, text: string } | ||
| */ | ||
| const updatePost = async (id, text) => { | ||
| // TODO | ||
| const response = await fetch(`${BASE_URL}/posts/id`, { | ||
|
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. the |
||
| method: "PUT", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: `Bearer ${getToken()}`, | ||
| }, | ||
| body: JSON.stringify({ text }), | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to update a post: HTTP ${response.status} ${response.statusText}`, | ||
| ); | ||
| } | ||
| return await response.json(); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -131,7 +189,18 @@ const updatePost = async (id, text) => { | |
| * Response: { user: string, message: string } | ||
| */ | ||
| 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 a user: HTTP ${response.status} ${response.statusText}`, | ||
| ); | ||
| } | ||
| return await response.json(); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -140,7 +209,18 @@ const deleteUser = async () => { | |
| * Response: { id: number, text: string, message: string } | ||
| */ | ||
| const deletePost = async (id) => { | ||
| // TODO | ||
| const response = await fetch(`${BASE_URL}/posts/:id `, { | ||
|
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. You should add the parameter as a variable to the url: |
||
| method: "DELETE", | ||
| headers: { | ||
| Authorization: `Bearer ${getToken()}`, | ||
| }, | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to delete a post: HTTP ${response.status} ${response.statusText}`, | ||
| ); | ||
| } | ||
| 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 should be error handling