diff --git a/package-lock.json b/package-lock.json index 65aa212..da4f394 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,6 @@ "": { "name": "core-assignment-week-10", "version": "1.0.0", - "hasInstallScript": true, "license": "CC BY-NC-SA 4.0", "dependencies": { "@inquirer/prompts": "^7.0.0", diff --git a/task-1/cocktail.js b/task-1/cocktail.js index 6fd5a62..f023072 100644 --- a/task-1/cocktail.js +++ b/task-1/cocktail.js @@ -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; } @@ -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); } } diff --git a/task-1/tests/cocktail.test.js b/task-1/tests/cocktail.test.js index 185945d..76aa4aa 100644 --- a/task-1/tests/cocktail.test.js +++ b/task-1/tests/cocktail.test.js @@ -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 diff --git a/task-2/post-cli/package-lock.json b/task-2/post-cli/package-lock.json index 8c03ed4..776be27 100644 --- a/task-2/post-cli/package-lock.json +++ b/task-2/post-cli/package-lock.json @@ -7,8 +7,7 @@ "": { "name": "post-central-cli", "version": "1.0.0", - "hasInstallScript": true, - "license": "ISC", + "license": "CC BY-NC-SA 4.0", "dependencies": { "@inquirer/prompts": "^7.0.0", "chalk": "^5.6.2", diff --git a/task-2/post-cli/src/services.js b/task-2/post-cli/src/services.js index 2601f57..12d2aa0 100644 --- a/task-2/post-cli/src/services.js +++ b/task-2/post-cli/src/services.js @@ -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 @@ -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()}` @@ -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(); @@ -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(); }; // ============================================================================ @@ -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(); @@ -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; }; // ============================================================================ @@ -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(); }; /** @@ -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(); }; /** @@ -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(); }; /** @@ -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(); }; /** @@ -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(); }; // ============================================================================ diff --git a/task-2/post-cli/tests/test-crud.test.js b/task-2/post-cli/tests/test-crud.test.js index fbf7ea4..8c31dee 100644 --- a/task-2/post-cli/tests/test-crud.test.js +++ b/task-2/post-cli/tests/test-crud.test.js @@ -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 @@ -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({ @@ -218,6 +220,7 @@ describe('Complete CRUD Operations', () => { fetchMock.mockResolvedValueOnce({ ok: true, status: 200, + json: async () => ({ user: 'mock-user', message: 'User deleted' }), }); await deleteUser(); @@ -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); diff --git a/task-2/post-cli/tests/test-post.test.js b/task-2/post-cli/tests/test-post.test.js index 0431c68..58f1766 100644 --- a/task-2/post-cli/tests/test-post.test.js +++ b/task-2/post-cli/tests/test-post.test.js @@ -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: { @@ -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: { @@ -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',