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
1 change: 0 additions & 1 deletion package-lock.json

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

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

import path from 'path';
import fs from 'fs/promises';


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

Expand All @@ -20,14 +22,48 @@ export async function main() {

try {
// 1. Fetch data from the API at the given URL
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const jsonData = await response.json();

if (!jsonData.drinks) throw new Error('No cocktails found with that name.');
const drinks = jsonData.drinks;

// 2. Generate markdown content to match the examples
let markdown = '# Cocktail Recipes\n\n';

drinks.forEach(drink => {
markdown += `## ${drink.strDrink}\n`;
markdown += `![${drink.strDrink}](${drink.strDrinkThumb}/medium)\n`;
markdown += `**Category**: ${drink.strCategory}\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) {
markdown += `- ${measure ? measure : ''}${ingredient}\n`;
}
}
markdown += `\n### Instructions\n`;
markdown += `${drink.strInstructions}\n\n`;
markdown += `Serve in: ${drink.strGlass}\n\n`;
});


// 3. Write the generated content to a markdown file as given by outPath
await fs.writeFile(outPath, markdown, 'utf-8');
console.log(`Markdown file created at ${outPath}`);

} catch (error) {
// 4. Handle errors
console.error(error.message);
}
}

// Do not change the code below
if (!process.env.VITEST) {
main();
}


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

103 changes: 103 additions & 0 deletions task-2/post-cli/src/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ const getHello = async () => {
*/
const getMe = async () => {
// TODO
const response = await fetch(`${BASE_URL}/users/me`, {
method: 'GET',
headers: {
Authorization: `Bearer ${getToken()}`
}
});

if (!response.ok) {
throw new Error(
`Failed to get user: HTTP ${response.status} ${response.statusText}`
);
}

return await response.json();
};

// ============================================================================
Expand Down Expand Up @@ -90,6 +104,21 @@ const createUser = async (name, password) => {
*/
const loginUser = async (name, password) => {
// TODO
const response = await fetch(`${BASE_URL}/users/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name, password }),
});

if (!response.ok) {
throw new Error(
`Failed to login: HTTP ${response.status} ${response.statusText}`
);
}

return await response.json();
};

// ============================================================================
Expand All @@ -104,6 +133,22 @@ const loginUser = async (name, password) => {
*/
const createPost = async (text) => {
// TODO
const response = await fetch(`${BASE_URL}/posts`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${getToken()}`
},
body: JSON.stringify({ text }),
});

if (!response.ok) {
throw new Error(
`Failed to create post: HTTP ${response.status} ${response.statusText}`
);
}

return await response.json();
};

/**
Expand All @@ -113,6 +158,20 @@ const createPost = async (text) => {
*/
const getPosts = async () => {
// TODO
const response = await fetch(`${BASE_URL}/posts/me`, {
method: 'GET',
headers: {
Authorization: `Bearer ${getToken()}`
}
});

if (!response.ok) {
throw new Error(
`Failed to get posts: HTTP ${response.status} ${response.statusText}`
);
}

return await response.json();
};

/**
Expand All @@ -123,6 +182,22 @@ const getPosts = async () => {
*/
const updatePost = async (id, text) => {
// TODO
const response = await fetch(`${BASE_URL}/posts/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${getToken()}`
},
body: JSON.stringify({ text }),
});

if (!response.ok) {
throw new Error(
`Failed to update post: HTTP ${response.status} ${response.statusText}`
);
}

return await response.json();
};

/**
Expand All @@ -132,6 +207,20 @@ const updatePost = async (id, text) => {
*/
const deleteUser = async () => {
// TODO
const response = await fetch(`${BASE_URL}/users/me`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${getToken()}`
}
});

if (!response.ok) {
throw new Error(
`Failed to delete user: HTTP ${response.status} ${response.statusText}`
);
}

return response.json ? await response.json() : null;
};

/**
Expand All @@ -141,6 +230,20 @@ const deleteUser = async () => {
*/
const deletePost = async (id) => {
// TODO
const response = await fetch(`${BASE_URL}/posts/${id}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${getToken()}`
}
});

if (!response.ok) {
throw new Error(
`Failed to delete post: HTTP ${response.status} ${response.statusText}`
);
}

return response.json ? await response.json() : null;
};

// ============================================================================
Expand Down