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.

61 changes: 57 additions & 4 deletions task-1/cocktail.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
// API documentation: https://www.thecocktaildb.com/api.php

import path from 'path';
import path from "path";
import fs 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

function generatedMarkdown(data) {
return (
"# Cocktail Recipes\n" +
data
.map(
(drink) => `
## ${drink.strDrink}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This huge template string is spoiling the code indentation a bit, which makes it harder to read. While functionally correct, another approach would be to build up an array of strings that you join with .join("\n"). This way, your code could be nicely indented.


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

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

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

### Ingredients

${Array.from({ length: 15 }, (_, i) => i + 1)
.filter((i) => drink[`strIngredient${i}`])
.map((i) =>
drink[`strMeasure${i}`]
? `- ${drink[`strMeasure${i}`]}${drink[`strIngredient${i}`]}`
: `- ${drink[`strIngredient${i}`]}`,
)
.join("\n")}

### Instructions

${drink.strInstructions}

Serve in: ${drink.strGlass}
`,
)
.join("\n")
);
}

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;
}

Expand All @@ -19,11 +56,27 @@ export async function main() {
const outPath = path.join(__dirname, `./output/${cocktailName}.md`);

try {
const response = await fetch(url);
if (!response.ok) {
const error = new Error(data.error || response.statusText);
error.status = response.status;
throw error;
}
const data = await response.json();
console.log(data);
if (!data?.drinks) {
throw new Error("No cocktails found with that name.");
}

const normalisedData = generatedMarkdown(data.drinks);

const res = await fs.writeFile(outPath, normalisedData);
return res;
// 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
} catch (error) {
// 4. Handle errors
console.error(error.message);
}
}

Expand Down
41 changes: 41 additions & 0 deletions task-1/output/black_russian.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Cocktail Recipes

## Black Russian

![Black Russian](https://www.thecocktaildb.com/images/media/drink/8oxlqf1606772765.jpg/medium)

**Category**: Ordinary Drink

**Alcoholic**: Yes

### Ingredients

- 3/4 oz Coffee liqueur
- 1 1/2 oz Vodka

### Instructions

Pour the ingredients into an old fashioned glass filled with ice cubes. Stir gently.

Serve in: Old-fashioned glass


## Chocolate Black Russian

![Chocolate Black Russian](https://www.thecocktaildb.com/images/media/drink/yyvywx1472720879.jpg/medium)

**Category**: Ordinary Drink

**Alcoholic**: Yes

### Ingredients

- 1 oz Kahlua
- 1/2 oz Vodka
- 5 oz Chocolate ice-cream

### Instructions

Combine all ingredients in an electric blender and blend at a low speed for a short length of time. Pour into a chilled champagne flute and serve.

Serve in: Champagne flute
22 changes: 22 additions & 0 deletions task-1/output/margarita.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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
Loading