reqforge 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reqforge",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "A lightweight, flexible HTTP client library with request interception, automatic retry, and logging capabilities, designed for modern web applications.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/cache.js ADDED
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Cache module for ReqForge
3
+ * @module cache
4
+ */
5
+
6
+ /**
7
+ * Simple in-memory cache with TTL support
8
+ */
9
+ class MemoryCache {
10
+ constructor(options = {}) {
11
+ this.store = new Map();
12
+ this.defaultTTL = options.defaultTTL || 60000;
13
+ }
14
+
15
+ /**
16
+ * Get value from cache
17
+ * @param {string} key - Cache key
18
+ * @returns {*|null} Cached value or null
19
+ */
20
+ get(key) {
21
+ const entry = this.store.get(key);
22
+ if (!entry) return null;
23
+
24
+ if (Date.now() > entry.expiry) {
25
+ this.store.delete(key);
26
+ return null;
27
+ }
28
+
29
+ return entry.value;
30
+ }
31
+
32
+ /**
33
+ * Set value in cache
34
+ * @param {string} key - Cache key
35
+ * @param {*} value - Value to cache
36
+ * @param {number} ttl - Time to live in ms
37
+ */
38
+ set(key, value, ttl) {
39
+ this.store.set(key, {
40
+ value,
41
+ expiry: Date.now() + (ttl || this.defaultTTL)
42
+ });
43
+ }
44
+
45
+ /**
46
+ * Check if key exists and is not expired
47
+ * @param {string} key - Cache key
48
+ * @returns {boolean} True if valid
49
+ */
50
+ has(key) {
51
+ return this.get(key) !== null;
52
+ }
53
+
54
+ /**
55
+ * Delete key from cache
56
+ * @param {string} key - Cache key
57
+ */
58
+ delete(key) {
59
+ this.store.delete(key);
60
+ }
61
+
62
+ /**
63
+ * Clear all cached entries
64
+ */
65
+ clear() {
66
+ this.store.clear();
67
+ }
68
+
69
+ /**
70
+ * Get cache size
71
+ * @returns {number} Number of entries
72
+ */
73
+ size() {
74
+ let count = 0;
75
+ this.store.forEach((entry, key) => {
76
+ if (Date.now() <= entry.expiry) {
77
+ count++;
78
+ } else {
79
+ this.store.delete(key);
80
+ }
81
+ });
82
+ return count;
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Create cache key from URL and params
88
+ * @param {string} url - Request URL
89
+ * @param {Object} params - Request params
90
+ * @returns {string} Cache key
91
+ */
92
+ function createCacheKey(url, params = {}) {
93
+ const normalized = { url, ...params };
94
+ return JSON.stringify(normalized);
95
+ }
96
+
97
+ module.exports = {
98
+ MemoryCache,
99
+ createCacheKey
100
+ };
package/src/index.js CHANGED
@@ -11,6 +11,7 @@ const auth = require('./auth');
11
11
  const errors = require('./errors');
12
12
  const retry = require('./retry');
13
13
  const interceptor = require('./interceptor');
14
+ const cache = require('./cache');
14
15
 
15
16
  module.exports = {
16
17
  config,
@@ -20,5 +21,6 @@ module.exports = {
20
21
  auth,
21
22
  errors,
22
23
  retry,
23
- interceptor
24
+ interceptor,
25
+ cache
24
26
  };