ez-api-wrapper
Advanced tools
Comparing version 1.0.2 to 1.0.3
const axios = require('axios'); | ||
const NodeCache = require('node-cache'); | ||
const EventEmitter = require('eventemitter3'); | ||
// Create a new instance of NodeCache | ||
const cache = new 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) { | ||
@@ -22,6 +34,18 @@ const cacheKey = `${endpoint}-${JSON.stringify(params)}`; | ||
// Cache the response data with the specified TTL | ||
cache.set(cacheKey, responseData, cacheTTL); | ||
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}`); | ||
@@ -64,3 +88,2 @@ } | ||
} | ||
module.exports = { | ||
@@ -70,3 +93,4 @@ fetchData, | ||
fetchPosts, | ||
fetchUsers | ||
}; | ||
fetchUsers, | ||
}; |
{ | ||
"name": "ez-api-wrapper", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Wrapper for an API: If there's an API you frequently use, you could create a wrapper for it that simplifies the process of making requests and handling responses. This could be for anything from weather data to social media APIs.", | ||
@@ -13,2 +13,3 @@ "main": "index.js", | ||
"axios": "^1.6.8", | ||
"eventemitter3": "^5.0.1", | ||
"node-cache": "^5.1.2" | ||
@@ -15,0 +16,0 @@ }, |
@@ -60,23 +60,24 @@ ![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99) | ||
// Import the API wrapper package | ||
const { fetchPosts } = require('ez-api-wrapper'); | ||
// Import the API client module | ||
const { fetchData, fetchPosts, fetchUsers } = require('ez-api-wrapper'); | ||
// Define the API URL | ||
const apiUrl = 'https://jsonplaceholder.typicode.com'; | ||
// Function to fetch posts from the API | ||
async function getPosts() { | ||
// Example usage: Fetch posts from the API | ||
(async () => { | ||
try { | ||
// Fetch posts from the API using the package function | ||
const posts = await fetchPosts(apiUrl); | ||
// Log the fetched posts | ||
const posts = await fetchPosts('https://jsonplaceholder.typicode.com'); | ||
console.log('Fetched posts:', posts); | ||
} catch (error) { | ||
// Handle errors | ||
console.error('Error fetching posts:', error.message); | ||
} | ||
} | ||
})(); | ||
// Call the function to fetch posts | ||
getPosts(); | ||
// Example usage: Fetch users from the API | ||
(async () => { | ||
try { | ||
const users = await fetchUsers('https://jsonplaceholder.typicode.com'); | ||
console.log('Fetched users:', users); | ||
} catch (error) { | ||
console.error('Error fetching users:', error.message); | ||
} | ||
})(); | ||
@@ -83,0 +84,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49905
12
202
114
3
+ Addedeventemitter3@^5.0.1
+ Addedeventemitter3@5.0.1(transitive)