Skip to content
Closed
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.

62 changes: 54 additions & 8 deletions task-1/cocktail.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
// API documentation: https://www.thecocktaildb.com/api.php

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
function generateMarkdown(drinks) {
let markdown = '# Cocktail Recipes\n\n';

for (const drink of drinks) {
markdown += `## ${drink.strDrink}\n\n`;

if (drink.strDrinkThumb) {
markdown += `![${drink.strDrink}](${drink.strDrinkThumb}/medium)\n\n`;
}

markdown += `**Category**: ${drink.strCategory}\n\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) break;

markdown += `- ${measure ? measure.trim() + ' ' : ''}${ingredient}\n`;
}

markdown += `\n`;
markdown += `### Instructions\n`;
markdown += `${drink.strInstructions}\n\n`;
markdown += `Serve in: ${drink.strGlass}\n\n`;
markdown += `---\n\n`;
}

return markdown;
}

export async function main() {
if (process.argv.length < 3) {
Expand All @@ -19,15 +50,30 @@ 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);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();

if (!data.drinks) {
console.error('No cocktails found with that name.');
await writeFile(outPath, `# No results found for "${cocktailName}"`);
return;
}

const markdown = generateMarkdown(data.drinks);

await writeFile(outPath, markdown);
console.log(`File created at ${outPath}`);
} catch (error) {
// 4. Handle errors
console.error('Something went wrong:', error.message);
}
}

// Do not change the code below
if (!process.env.VITEST) {
main();
}
}
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

---

1 change: 1 addition & 0 deletions task-1/output/nonexistentcocktail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# No results found for "nonexistentcocktail"
Loading