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.

58 changes: 54 additions & 4 deletions task-1/cocktail.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
// API documentation: https://www.thecocktaildb.com/api.php

import path from 'path';
import fs from 'node:fs';

const BASE_URL = 'https://www.thecocktaildb.com/api/json/v1/1';

// Add helper functions as needed here

function generateMarkdown(cocktail) {
let markdown = `# Cocktail Recipes`;

for (const drink of cocktail.drinks) {
const ingredients = [];
for (let i = 1; i <= 15; i++) {
const ingredient = drink[`strIngredient${i}`];
const measure = drink[`strMeasure${i}`];
if (ingredient) {
ingredients.push(
`- ${measure ? measure.trim() + ' ' : ''}${ingredient.trim()}`,
);
}
}
markdown += `

## ${drink.strDrink}

![${drink.strDrink}](${drink.strDrinkThumb}/medium)

**Category**: ${drink.strCategory}

**Alcoholic**: ${drink.strAlcoholic === 'Alcoholic' ? 'Yes' : 'No'}

### Ingredients

${ingredients.join('\n')}

### Instructions

${drink.strInstructions}

Serve in: ${drink.strGlass}`;
}

return markdown;
}

export async function main() {
if (process.argv.length < 3) {
console.error('Please provide a cocktail name as a command line argument.');
Expand All @@ -19,11 +58,22 @@ 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(`Request failed with status ${response.status}`);
}

const data = await response.json();
if (data.drinks === null) {
throw new Error('No cocktails found with that name.');
}

const markdown = generateMarkdown(data);

fs.writeFileSync(outPath, markdown);
console.log(`Successfully created: ${cocktailName}`);
} catch (error) {
// 4. Handle errors
console.error(error.message);
}
}

Expand Down
Loading