react-api-call-optimizer 0.0.0 → 0.1.1
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/README.md +6 -15
- package/dist/api-optimizer.es.js +58 -41
- package/dist/api-optimizer.umd.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# react-api-call-optimizer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**A lightweight React hook to optimize API calls with caching, retries, throttling, and polling.**
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
## React Compiler
|
|
11
|
-
|
|
12
|
-
The React Compiler is enabled on this template. See [this documentation](https://react.dev/learn/react-compiler) for more information.
|
|
13
|
-
|
|
14
|
-
Note: This will impact Vite dev & build performances.
|
|
15
|
-
|
|
16
|
-
## Expanding the ESLint configuration
|
|
17
|
-
|
|
18
|
-
If you are developing a production application, we recommend using TypeScript with type-aware lint rules enabled. Check out the [TS template](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) for information on how to integrate TypeScript and [`typescript-eslint`](https://typescript-eslint.io) in your project.
|
|
7
|
+
```bash
|
|
8
|
+
npm install react-api-call-optimizer
|
|
9
|
+
```
|
package/dist/api-optimizer.es.js
CHANGED
|
@@ -1,46 +1,63 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
const
|
|
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
|
-
|
|
1
|
+
import { useState as d, useRef as y, useCallback as O } from "react";
|
|
2
|
+
const n = {}, a = {};
|
|
3
|
+
let u = {
|
|
4
|
+
cacheTime: 3e5,
|
|
5
|
+
// 5 min
|
|
6
|
+
retries: 0,
|
|
7
|
+
throttleTime: 0,
|
|
8
|
+
debug: !1
|
|
9
|
+
};
|
|
10
|
+
const k = (c) => Array.isArray(c) ? JSON.stringify(c) : c;
|
|
11
|
+
function R(c, p, T = {}) {
|
|
12
|
+
const {
|
|
13
|
+
retries: l = u.retries,
|
|
14
|
+
cacheTime: A = u.cacheTime,
|
|
15
|
+
throttleTime: f = u.throttleTime,
|
|
16
|
+
debug: r = u.debug
|
|
17
|
+
} = T, e = k(c), [$, h] = d(
|
|
18
|
+
() => n[e] ? n[e].data : null
|
|
19
|
+
), [b, o] = d(!1), [z, m] = d(null), i = y(0), w = y(0), s = y(null), C = O(() => {
|
|
20
|
+
r && console.log(`[API Optimizer] Cache cleared for key: ${e}`), delete n[e];
|
|
21
|
+
}, [e, r]), I = O(async () => {
|
|
22
|
+
o(!0), m(null);
|
|
23
|
+
const g = Date.now();
|
|
24
|
+
if (f && g - w.current < f) {
|
|
25
|
+
r && console.log(`[API Optimizer] Throttled request for key: ${e}`);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (w.current = g, n[e] && g - n[e].timestamp < A)
|
|
29
|
+
return r && console.log(`[API Optimizer] Cache hit for key: ${e}`), h(n[e].data), o(!1), n[e].data;
|
|
30
|
+
if (a[e]) {
|
|
31
|
+
r && console.log(
|
|
32
|
+
`[API Optimizer] Awaiting inflight request for key: ${e}`
|
|
33
|
+
);
|
|
34
|
+
try {
|
|
35
|
+
const t = await a[e];
|
|
36
|
+
return h(t), o(!1), t;
|
|
37
|
+
} catch (t) {
|
|
38
|
+
m(t), o(!1);
|
|
39
|
+
return;
|
|
37
40
|
}
|
|
38
41
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
s.current && s.current.abort(), s.current = new AbortController();
|
|
43
|
+
const P = s.current.signal;
|
|
44
|
+
return a[e] = (async () => {
|
|
45
|
+
for (; i.current <= l; )
|
|
46
|
+
try {
|
|
47
|
+
const t = await p({ signal: P });
|
|
48
|
+
return n[e] = { timestamp: Date.now(), data: t }, h(t), o(!1), delete a[e], r && console.log(`[API Optimizer] Fetched and cached key: ${e}`), i.current = 0, t;
|
|
49
|
+
} catch (t) {
|
|
50
|
+
if (i.current += 1, r && console.log(
|
|
51
|
+
`[API Optimizer] Retry ${i.current}/${l} for key: ${e}`
|
|
52
|
+
), i.current > l) {
|
|
53
|
+
m(t), o(!1), delete a[e];
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
})(), await a[e];
|
|
58
|
+
}, [e, p, A, f, l, r]);
|
|
59
|
+
return { data: $, loading: b, error: z, refetch: I, clearCache: C };
|
|
43
60
|
}
|
|
44
61
|
export {
|
|
45
|
-
|
|
62
|
+
R as useApiOptimizer
|
|
46
63
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(o,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],t):(o=typeof globalThis<"u"?globalThis:o||self,t(o.ApiOptimizer={},o.React))})(this,(function(o,t){"use strict";const n={},s={};let u={cacheTime:3e5,retries:0,throttleTime:0,debug:!1};const T=l=>Array.isArray(l)?JSON.stringify(l):l;function b(l,y,w={}){const{retries:f=u.retries,cacheTime:A=u.cacheTime,throttleTime:h=u.throttleTime,debug:i=u.debug}=w,e=T(l),[z,m]=t.useState(()=>n[e]?n[e].data:null),[C,c]=t.useState(!1),[$,p]=t.useState(null),a=t.useRef(0),O=t.useRef(0),d=t.useRef(null),P=t.useCallback(()=>{i&&console.log(`[API Optimizer] Cache cleared for key: ${e}`),delete n[e]},[e,i]),k=t.useCallback(async()=>{c(!0),p(null);const g=Date.now();if(h&&g-O.current<h){i&&console.log(`[API Optimizer] Throttled request for key: ${e}`);return}if(O.current=g,n[e]&&g-n[e].timestamp<A)return i&&console.log(`[API Optimizer] Cache hit for key: ${e}`),m(n[e].data),c(!1),n[e].data;if(s[e]){i&&console.log(`[API Optimizer] Awaiting inflight request for key: ${e}`);try{const r=await s[e];return m(r),c(!1),r}catch(r){p(r),c(!1);return}}d.current&&d.current.abort(),d.current=new AbortController;const I=d.current.signal;return s[e]=(async()=>{for(;a.current<=f;)try{const r=await y({signal:I});return n[e]={timestamp:Date.now(),data:r},m(r),c(!1),delete s[e],i&&console.log(`[API Optimizer] Fetched and cached key: ${e}`),a.current=0,r}catch(r){if(a.current+=1,i&&console.log(`[API Optimizer] Retry ${a.current}/${f} for key: ${e}`),a.current>f){p(r),c(!1),delete s[e];return}}})(),await s[e]},[e,y,A,h,f,i]);return{data:z,loading:C,error:$,refetch:k,clearCache:P}}o.useApiOptimizer=b,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})}));
|