react-api-call-optimizer 0.0.0 → 0.1.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/README.md CHANGED
@@ -1,18 +1,9 @@
1
- # React + Vite
1
+ # react-api-call-optimizer
2
2
 
3
- This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
3
+ **A lightweight React hook to optimize API calls with caching, retries, throttling, and polling.**
4
4
 
5
- Currently, two official plugins are available:
5
+ ## Installation
6
6
 
7
- - [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) (or [oxc](https://oxc.rs) when used in [rolldown-vite](https://vite.dev/guide/rolldown)) for Fast Refresh
8
- - [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
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
+ ```
@@ -1,46 +1,50 @@
1
- import { useState as o, useRef as w, useEffect as x } from "react";
2
- const a = {}, n = {};
3
- function A(t, f, p = {}) {
4
- const { retries: m = 0, cacheTime: D = 3e5, throttleTime: d = 0 } = p, [g, s] = o(null), [T, r] = o(!0), [C, c] = o(null), u = w(0), h = w(0);
5
- return x(() => {
6
- let i = !1;
7
- async function E() {
8
- r(!0), c(null);
9
- const l = Date.now();
10
- if (!(d && l - h.current < d)) {
11
- if (h.current = l, a[t] && l - a[t].timestamp < D) {
12
- s(a[t].data), r(!1);
13
- return;
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 = {}) {
4
+ 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);
17
+ return;
18
+ }
19
+ if (n[t]) {
20
+ try {
21
+ const e = await n[t];
22
+ u(e), r(!1);
23
+ } catch (e) {
24
+ i(e), r(!1);
14
25
  }
15
- if (n[t]) {
26
+ return;
27
+ }
28
+ n[t] = (async () => {
29
+ for (; f.current <= s; )
16
30
  try {
17
- const e = await n[t];
18
- if (i) return;
19
- s(e), r(!1);
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;
20
33
  } catch (e) {
21
- c(e), r(!1);
22
- }
23
- return;
24
- }
25
- n[t] = (async () => {
26
- for (; u.current <= m; )
27
- try {
28
- const e = await f();
29
- return i ? void 0 : (a[t] = { timestamp: Date.now(), data: e }, s(e), r(!1), delete n[t], e);
30
- } catch (e) {
31
- if (u.current += 1, u.current > m) {
32
- c(e), r(!1), delete n[t];
33
- return;
34
- }
34
+ if (f.current += 1, f.current > s) {
35
+ i(e), r(!1), delete n[t];
36
+ return;
35
37
  }
36
- })(), await n[t];
37
- }
38
+ }
39
+ })(), await n[t];
38
40
  }
39
- return E(), () => {
40
- i = !0;
41
- };
42
- }, [t, f]), { data: g, loading: T, error: C };
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 };
43
47
  }
44
48
  export {
45
- A as useApiOptimizer
49
+ L as useApiOptimizer
46
50
  };
@@ -1 +1 @@
1
- (function(r,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],t):(r=typeof globalThis<"u"?globalThis:r||self,t(r.ApiOptimizer={},r.React))})(this,(function(r,t){"use strict";const u={},i={};function w(e,d,T={}){const{retries:p=0,cacheTime:S=3e5,throttleTime:m=0}=T,[g,a]=t.useState(null),[D,s]=t.useState(!0),[O,o]=t.useState(null),c=t.useRef(0),h=t.useRef(0);return t.useEffect(()=>{let f=!1;async function z(){s(!0),o(null);const l=Date.now();if(!(m&&l-h.current<m)){if(h.current=l,u[e]&&l-u[e].timestamp<S){a(u[e].data),s(!1);return}if(i[e]){try{const n=await i[e];if(f)return;a(n),s(!1)}catch(n){o(n),s(!1)}return}i[e]=(async()=>{for(;c.current<=p;)try{const n=await d();return f?void 0:(u[e]={timestamp:Date.now(),data:n},a(n),s(!1),delete i[e],n)}catch(n){if(c.current+=1,c.current>p){o(n),s(!1),delete i[e];return}}})(),await i[e]}}return z(),()=>{f=!0}},[e,d]),{data:g,loading:D,error:O}}r.useApiOptimizer=w,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
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"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-api-call-optimizer",
3
- "version": "0.0.0",
3
+ "version": "0.1.0",
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",