Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

133 changes: 123 additions & 10 deletions task-1/cocktail.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,142 @@
// 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';
const BASE_URL = "https://www.thecocktaildb.com/api/json/v1/1";

// Add helper functions as needed here
/**
* Generate markdown content for the cocktail recipes.
* The output format must match the provided example exactly.
*
* @param {Array<Object>} drinks - Array of drink objects from the API
* @returns {string} markdown string
*/
function generateMarkdown(drinks) {
/**
* Start the markdown with the main title.
* This is required to match the expected output format.
*/
let markdown = "# Cocktail Recipes\n\n";

// Loop through each drink in the API response
for (const drink of drinks) {
/**
* Add drink name as a level-2 heading
*/
markdown += `## ${drink.strDrink}\n\n`;

/**
* Add drink image
* The assignment requires appending '/medium' to the image URL
*/
markdown += `![${drink.strDrink}](${drink.strDrinkThumb}/medium)\n\n`;

/**
* Add basic metadata about the drink
*/
markdown += `**Category**: ${drink.strCategory}\n\n`;
const alcoholic = drink.strAlcoholic === "Alcoholic" ? "Yes" : "No";
markdown += `**Alcoholic**: ${alcoholic}\n\n`;

/**
* Ingredients section
* Ingredients and measures are stored in numbered fields:
* strIngredient1 ... strIngredient15
* strMeasure1 ... strMeasure15
*/
markdown += `### Ingredients\n\n`;

for (let i = 1; i <= 15; i++) {
const ingredient = drink[`strIngredient${i}`];
const measure = drink[`strMeasure${i}`];

/**
* Stop looping when no more ingredients exist
*/
if (!ingredient) break;

/**
* Add ingredient line
* If measure exists, include it; otherwise only show ingredient
*/
if (measure) {
markdown += `- ${measure.trim()} ${ingredient}\n`;
} else {
markdown += `- ${ingredient}\n`;
}
}

/**
* Instructions section
*/
markdown += `\n### Instructions\n\n${drink.strInstructions}\n\n`;

/**
* Add serving glass information
* Must match exact format: "Serve in: ..."
*/
markdown += `Serve in: ${drink.strGlass}\n\n`;
}

return markdown;
}
export async function main() {
// Ensure a cocktail name is provided via command line
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;
}

const cocktailName = process.argv[2];
// Construct API endpoint for searching cocktails
const url = `${BASE_URL}/search.php?s=${cocktailName}`;

// Resolve output file path
const __dirname = import.meta.dirname;
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
/**
* STEP 1: Fetch data from the API
*/
const response = await fetch(url);

// Handle HTTP errors (non-2xx status codes)
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}

/**
* STEP 2: Parse JSON response
*/
const data = await response.json();

// Handle case where no drinks are found
if (!data.drinks) {
console.error("No cocktails found with that name.");
return;
}

/**
* STEP 3: Transform API data into markdown format
*/
const markdown = generateMarkdown(data.drinks);

/**
* STEP 4: Write markdown content to file
*/
await writeFile(outPath, markdown, "utf-8");

console.log(`File created successfully: ${outPath}`);
} catch (error) {
// 4. Handle errors
/**
* STEP 5: Handle errors gracefully
* This will catch:
* - Network errors
* - HTTP errors
* - JSON parsing errors
* - File system errors
*/
console.error(error.message);
}
}

Expand Down
23 changes: 23 additions & 0 deletions task-1/output/margarita.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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

5 changes: 1 addition & 4 deletions task-2/post-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading