In this task you will build a command-line tool that fetches cocktail recipes from an API and writes the results to a markdown file. All your work will be in the file task-1/cocktail.js.
The starter code already handles command-line arguments and sets up the output path for you. Your job is to fill in the try/catch block with working code. You may add helper functions above main() as needed.
-
Run the program with a cocktail name:
node task-1/cocktail.js margarita
-
When finished, the program should create a file
task-1/output/margarita.md. -
Run the tests to check your work:
npm run test:cocktail
Complete the four numbered steps inside main():
- Fetch data from the API — Use
fetch()withasync/awaitto call the API at the givenurl. Parse the JSON response. If the response is not OK, throw an error. - Generate markdown content — Transform the returned drink data into a markdown string. The API may return multiple drinks for a single search term (e.g. searching "margarita" returns several variations). Your output must include all of them.
- Write the file — Use
fs/promises(writeFile) to write the generated markdown to the file path given byoutPath. - Handle errors — In the
catchblock, log a helpful error message to the console.
Look at the example files in task-1/examples/ to see exactly what your output should look like. Each drink should include:
- A level-2 heading (
##) with the drink name - A thumbnail image (use the URL from
strDrinkThumbwith/mediumappended) - Category and Alcoholic (Yes/No) fields
- A list of ingredients with their measures
- Instructions and the glass to serve in
The API documentation is at: https://www.thecocktaildb.com/api.php
The search endpoint used in the starter code returns an object with a drinks array. Each drink object has properties like strDrink, strDrinkThumb, strCategory, strAlcoholic, strInstructions, strGlass, and numbered ingredient/measure pairs (strIngredient1…strIngredient15, strMeasure1…strMeasure15). Not all ingredient slots are filled — stop when the value is null or empty.
- You will need to import
fsfrom'fs/promises'to write the file. - The API can return
nullfor thedrinksproperty if no cocktail is found — handle this case. - Ingredient/measure pairs are numbered 1–15. Loop through them and skip any that are empty or
null. - Compare your output against the example files to make sure formatting matches.