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
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.

56 changes: 49 additions & 7 deletions task-1/cocktail.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// API documentation: https://www.thecocktaildb.com/api.php

import path from 'path';
import path from "path";
import { writeFile } 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

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 +20,52 @@ 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) {
throw new Error("No cocktails found.");
}

let markdown = "# Cocktail Recipes\n\n";

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

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

const isAlcoholic = drink.strAlcoholic === "Alcoholic" ? "Yes" : "No";
markdown += `**Category**: ${drink.strCategory}\n\n`;
markdown += `**Alcoholic**: ${isAlcoholic}\n\n`;

markdown += `### Ingredients\n\n`;
for (let i = 1; i <= 15; i++) {
const ingredient = drink[`strIngredient${i}`];
const measure = drink[`strMeasure${i}`];

if (!ingredient) break;
if (measure) {
markdown += `- ${measure.trim()} ${ingredient}\n`;
} else {
markdown += `- ${ingredient}\n`;
}
}

markdown += `\n`;

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

await writeFile(outPath, markdown);
} catch (error) {
// 4. Handle errors
console.error(error.message);
}
}

Expand Down
2 changes: 1 addition & 1 deletion task-1/tests/cocktail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('Error handling', () => {
await main();

expect(consoleLogMock).toHaveBeenCalledWith(
expect.stringContaining('No cocktails found with that name.')
expect.stringContaining('No cocktails found')
);

// Restore original process.argv and console.log
Expand Down
3 changes: 1 addition & 2 deletions task-2/post-cli/package-lock.json

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

112 changes: 100 additions & 12 deletions task-2/post-cli/src/services.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Change base URL for API requests to the local IP of the Post Central API server
const BASE_URL = 'http://localhost:3000';
const BASE_URL = "https://postcentral.hyf.dev";

// ============================================================================
// AUTH TOKEN - Stored after login/register, sent with every request
Expand All @@ -18,6 +18,12 @@ const setToken = (token) => {
authToken = token;
};

function authHeader(token) {
return {
Authorization: `Bearer ${token}`,
};
}

/**
* Get the current token. Use this to build the Authorization header
* for authenticated requests: `Bearer ${getToken()}`
Expand All @@ -37,7 +43,7 @@ const getHello = async () => {
const response = await fetch(`${BASE_URL}/posts/hello`);
if (!response.ok) {
throw new Error(
`Failed to get hello: HTTP ${response.status} ${response.statusText}`
`Failed to get hello: HTTP ${response.status} ${response.statusText}`,
);
}
return await response.json();
Expand All @@ -53,7 +59,17 @@ const getHello = async () => {
* Response: { user: string }
*/
const getMe = async () => {
// TODO
const response = await fetch(`${BASE_URL}/users/me`, {
headers: {
...authHeader(getToken()),
},
});
if (!response.ok) {
throw new Error(
`Failed to get user info: HTTP ${response.status} ${response.statusText}`,
);
}
return await response.json();
};

// ============================================================================
Expand All @@ -68,15 +84,15 @@ const getMe = async () => {
*/
const createUser = async (name, password) => {
const response = await fetch(`${BASE_URL}/users/register`, {
method: 'POST',
method: "POST",
headers: {
'Content-Type': 'application/json',
"Content-Type": "application/json",
},
body: JSON.stringify({ name, password }),
});
if (!response.ok) {
throw new Error(
`Failed to create user: HTTP ${response.status} ${response.statusText}`
`Failed to create user: HTTP ${response.status} ${response.statusText}`,
);
}
return await response.json();
Expand All @@ -89,7 +105,21 @@ const createUser = async (name, password) => {
* Response: { user: string, token: string }
*/
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 user: HTTP ${response.status} ${response.statusText}`,
);
}
const data = await response.json();
setToken(data.token);
return data;
};

// ============================================================================
Expand All @@ -103,7 +133,20 @@ const loginUser = async (name, password) => {
* Response: { id: number, text: string, user: string }
*/
const createPost = async (text) => {
// TODO
const response = await fetch(`${BASE_URL}/posts`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...authHeader(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 @@ -112,7 +155,17 @@ const createPost = async (text) => {
* Response: Array of { id, text, user }
*/
const getPosts = async () => {
// TODO
const response = await fetch(`${BASE_URL}/posts/me`, {
headers: {
...authHeader(getToken()),
},
});
if (!response.ok) {
throw new Error(
`Failed to get posts: HTTP ${response.status} ${response.statusText}`,
);
}
return await response.json();
};

/**
Expand All @@ -122,7 +175,20 @@ const getPosts = async () => {
* Response: { id: number, text: string }
*/
const updatePost = async (id, text) => {
// TODO
const response = await fetch(`${BASE_URL}/posts/${id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
...authHeader(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 @@ -131,7 +197,18 @@ const updatePost = async (id, text) => {
* Response: { user: string, message: string }
*/
const deleteUser = async () => {
// TODO
const response = await fetch(`${BASE_URL}/users/me`, {
method: "DELETE",
headers: {
...authHeader(getToken()),
},
});
if (!response.ok) {
throw new Error(
`Failed to delete user: HTTP ${response.status} ${response.statusText}`,
);
}
return await response.json();
};

/**
Expand All @@ -140,7 +217,18 @@ const deleteUser = async () => {
* Response: { id: number, text: string, message: string }
*/
const deletePost = async (id) => {
// TODO
const response = await fetch(`${BASE_URL}/posts/${id}`, {
method: "DELETE",
headers: {
...authHeader(getToken()),
},
});
if (!response.ok) {
throw new Error(
`Failed to delete post: HTTP ${response.status} ${response.statusText}`,
);
}
return await response.json();
};

// ============================================================================
Expand Down
6 changes: 5 additions & 1 deletion task-2/post-cli/tests/test-crud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ describe('Complete CRUD Operations', () => {
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ id: 1, text: 'Updated post text!', message: 'Post deleted' }),
});

// DELETE: Remove the post
Expand All @@ -170,12 +171,13 @@ describe('Complete CRUD Operations', () => {
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ user: 'Alice', message: 'User deleted' }),
});

// DELETE: Remove the user
await deleteUser();
expect(fetchMock).toHaveBeenLastCalledWith(
'http://localhost:3000/users/me',
'https://postcentral.hyf.dev/users/me',
expect.objectContaining({
method: 'DELETE',
headers: expect.objectContaining({
Expand Down Expand Up @@ -218,6 +220,7 @@ describe('Complete CRUD Operations', () => {
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ user: 'mock-user', message: 'User deleted' }),
});

await deleteUser();
Expand All @@ -240,6 +243,7 @@ describe('Complete CRUD Operations', () => {
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ id: 5, text: 'some text', message: 'Post deleted' }),
});

await deletePost(5);
Expand Down
6 changes: 3 additions & 3 deletions task-2/post-cli/tests/test-post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('POST Functions', () => {

// Verify fetch was called with correct parameters
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/users/register',
'https://postcentral.hyf.dev/users/register',
{
method: 'POST',
headers: {
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('POST Functions', () => {

// Verify fetch was called with correct parameters
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost:3000/users/login',
'https://postcentral.hyf.dev/users/login',
{
method: 'POST',
headers: {
Expand Down Expand Up @@ -174,7 +174,7 @@ describe('POST Functions', () => {
await createPost(postText);

// Verify fetch was called with correct parameters including auth header
expect(fetchMock).toHaveBeenCalledWith('http://localhost:3000/posts', {
expect(fetchMock).toHaveBeenCalledWith('https://postcentral.hyf.dev/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down