-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiClient.js
91 lines (78 loc) · 2.59 KB
/
apiClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const axios = require('axios');
const EventEmitter = require('eventemitter3');
// Create a new instance of NodeCache
let cache;
if (typeof window === 'undefined') {
// Node.js environment
const NodeCache = require('node-cache');
cache = new NodeCache();
} else {
// Browser environment
cache = new Map();
}
// Create an event emitter instance
const emitter = new EventEmitter();
// Function to fetch data from the API
async function fetchData(apiUrl, endpoint, params, cacheTTL = 60) {
const cacheKey = `${endpoint}-${JSON.stringify(params)}`;
// Check if the response is cached
const cachedData = cache.get(cacheKey);
if (cachedData !== undefined) {
console.log('Data retrieved from cache');
return cachedData;
}
try {
const response = await axios.get(`${apiUrl}/${endpoint}`, { params });
const responseData = response.data;
// Cache the response data with the specified TTL
if (typeof window === 'undefined') {
cache.set(cacheKey, responseData, cacheTTL);
} else {
// Simple expiration mechanism for browser environment
setTimeout(() => cache.delete(cacheKey), cacheTTL * 1000);
cache.set(cacheKey, responseData);
}
// Emit a success event
emitter.emit('fetchSuccess', responseData);
return responseData;
} catch (error) {
// Emit an error event
emitter.emit('fetchError', error);
throw new Error(`Error fetching data from ${endpoint}: ${error.message}`);
}
}
async function batchRequest(apiUrl, requests) {
try {
const responses = await axios.post(`${apiUrl}/batch`, requests);
return responses.data;
} catch (error) {
throw new Error(`Error making batch request: ${error.message}`);
}
}
// Function to fetch posts from the API
async function fetchPosts(apiUrl) {
try {
const response = await axios.get(`${apiUrl}/posts`);
return response.data;
} catch (error) {
throw new Error(`Error fetching posts: ${error.message}`);
}
}
// Function to fetch users from the API
async function fetchUsers(apiUrl) {
// Example implementation to fetch users
try {
// Code to make HTTP request and retrieve users from the API
const users = await axios.get(`${apiUrl}/users`);
return users.data;
} catch (error) {
throw new Error('Error fetching users: Network Error');
}
}
module.exports = {
fetchData,
batchRequest,
fetchPosts,
fetchUsers,
emitter: emitter // Expose emitter for event handling
};