From 442c5b3e12c0ea5819aa1b0eff5b7ba341957f85 Mon Sep 17 00:00:00 2001 From: Vedant Sinha <142380309+Vedant1607@users.noreply.github.com> Date: Tue, 25 Nov 2025 00:40:34 +0530 Subject: [PATCH] Add API utility functions for user data handling This file contains utility functions for API interactions, including fetching user data, updating user information, calculating totals, and processing data. --- test-api.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 test-api.js diff --git a/test-api.js b/test-api.js new file mode 100644 index 0000000..50a7ab3 --- /dev/null +++ b/test-api.js @@ -0,0 +1,38 @@ +// API utility functions +const API_URL = 'https://api.example.com' + +function fetchUserData(userId) { + var data = fetch(API_URL + '/users/' + userId) + .then(response => response.json()) + .catch(err => console.log(err)) + return data +} + +async function updateUser(userId, userData) { + const response = await fetch(API_URL + '/users/' + userId, { + method: 'POST', + body: JSON.stringify(userData) + }) + return response.json() +} + +function calculateTotal(items) { + let total = 0 + for (var i = 0; i < items.length; i++) { + total = total + items[i].price + } + return total +} + +const processData = (data) => { + if (data == null) { + return null + } + return data.map(item => { + return { + id: item.id, + name: item.name, + price: item.price + } + }) +}