react-api-call-optimizer 0.1.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.
@@ -1,50 +1,63 @@
1
- import { useState as w, useRef as p, useCallback as E, useEffect as x } from "react";
2
- const c = {}, n = {};
3
- function L(t, g, D = {}) {
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 = {}) {
4
12
  const {
5
- retries: s = 0,
6
- cacheTime: v = 3e5,
7
- // 5 min cache
8
- throttleTime: l = 0,
9
- refreshInterval: o = 0
10
- // auto refetch
11
- } = D, [I, u] = w(null), [T, r] = w(!0), [b, i] = w(null), f = p(0), C = p(0), m = p(null), a = E(async () => {
12
- r(!0), i(null);
13
- const h = Date.now();
14
- if (!(l && h - C.current < l)) {
15
- if (C.current = h, c[t] && h - c[t].timestamp < v) {
16
- u(c[t].data), r(!1);
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);
17
39
  return;
18
40
  }
19
- if (n[t]) {
41
+ }
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; )
20
46
  try {
21
- const e = await n[t];
22
- u(e), r(!1);
23
- } catch (e) {
24
- i(e), r(!1);
25
- }
26
- return;
27
- }
28
- n[t] = (async () => {
29
- for (; f.current <= s; )
30
- try {
31
- const A = new AbortController().signal, d = await g({ signal: A });
32
- return c[t] = { timestamp: Date.now(), data: d }, u(d), r(!1), delete n[t], d;
33
- } catch (e) {
34
- if (f.current += 1, f.current > s) {
35
- i(e), r(!1), delete n[t];
36
- return;
37
- }
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;
38
55
  }
39
- })(), await n[t];
40
- }
41
- }, [t, g, s, v, l]);
42
- return x(() => (a(), o > 0 && (m.current = setInterval(() => {
43
- a();
44
- }, o)), () => {
45
- m.current && clearInterval(m.current);
46
- }), [a, o]), { data: I, loading: T, error: b, refetch: a };
56
+ }
57
+ })(), await a[e];
58
+ }, [e, p, A, f, l, r]);
59
+ return { data: $, loading: b, error: z, refetch: I, clearCache: C };
47
60
  }
48
61
  export {
49
- L as useApiOptimizer
62
+ R as useApiOptimizer
50
63
  };
@@ -1 +1 @@
1
- (function(n,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],e):(n=typeof globalThis<"u"?globalThis:n||self,e(n.ApiOptimizer={},n.React))})(this,(function(n,e){"use strict";const u={},r={};function S(t,g,b={}){const{retries:a=0,cacheTime:T=3e5,throttleTime:l=0,refreshInterval:c=0}=b,[A,f]=e.useState(null),[C,s]=e.useState(!0),[D,d]=e.useState(null),p=e.useRef(0),v=e.useRef(0),m=e.useRef(null),o=e.useCallback(async()=>{s(!0),d(null);const h=Date.now();if(!(l&&h-v.current<l)){if(v.current=h,u[t]&&h-u[t].timestamp<T){f(u[t].data),s(!1);return}if(r[t]){try{const i=await r[t];f(i),s(!1)}catch(i){d(i),s(!1)}return}r[t]=(async()=>{for(;p.current<=a;)try{const I=new AbortController().signal,w=await g({signal:I});return u[t]={timestamp:Date.now(),data:w},f(w),s(!1),delete r[t],w}catch(i){if(p.current+=1,p.current>a){d(i),s(!1),delete r[t];return}}})(),await r[t]}},[t,g,a,T,l]);return e.useEffect(()=>(o(),c>0&&(m.current=setInterval(()=>{o()},c)),()=>{m.current&&clearInterval(m.current)}),[o,c]),{data:A,loading:C,error:D,refetch:o}}n.useApiOptimizer=S,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
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"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-api-call-optimizer",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "A React hook to optimize API calls",
5
5
  "main": "dist/api-optimizer.umd.js",
6
6
  "module": "dist/api-optimizer.es.js",