react-api-call-optimizer 0.0.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 +18 -0
- package/dist/api-optimizer.es.js +46 -0
- package/dist/api-optimizer.umd.js +1 -0
- package/dist/vite.svg +1 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# React + Vite
|
|
2
|
+
|
|
3
|
+
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
|
|
4
|
+
|
|
5
|
+
Currently, two official plugins are available:
|
|
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.
|
|
@@ -0,0 +1,46 @@
|
|
|
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;
|
|
14
|
+
}
|
|
15
|
+
if (n[t]) {
|
|
16
|
+
try {
|
|
17
|
+
const e = await n[t];
|
|
18
|
+
if (i) return;
|
|
19
|
+
s(e), r(!1);
|
|
20
|
+
} 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
|
+
}
|
|
35
|
+
}
|
|
36
|
+
})(), await n[t];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return E(), () => {
|
|
40
|
+
i = !0;
|
|
41
|
+
};
|
|
42
|
+
}, [t, f]), { data: g, loading: T, error: C };
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
A as useApiOptimizer
|
|
46
|
+
};
|
|
@@ -0,0 +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"})}));
|
package/dist/vite.svg
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-api-call-optimizer",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "A React hook to optimize API calls",
|
|
5
|
+
"main": "dist/api-optimizer.umd.js",
|
|
6
|
+
"module": "dist/api-optimizer.es.js",
|
|
7
|
+
"files": ["dist"],
|
|
8
|
+
"keywords": ["react", "hook", "api", "optimizer"],
|
|
9
|
+
"author": "anupam",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
14
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"dev": "vite",
|
|
18
|
+
"build": "vite build",
|
|
19
|
+
"prepare": "vite build",
|
|
20
|
+
"prepublishOnly": "npm run build",
|
|
21
|
+
"lint": "eslint .",
|
|
22
|
+
"preview": "vite preview"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"react": "^19.2.3",
|
|
26
|
+
"react-dom": "^19.2.3"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@vitejs/plugin-react": "^5.1.1",
|
|
30
|
+
"vite": "^7.2.4",
|
|
31
|
+
"@eslint/js": "^9.39.1",
|
|
32
|
+
"eslint": "^9.39.1",
|
|
33
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
34
|
+
"eslint-plugin-react-refresh": "^0.4.24",
|
|
35
|
+
"@types/react": "^19.2.5",
|
|
36
|
+
"@types/react-dom": "^19.2.3",
|
|
37
|
+
"babel-plugin-react-compiler": "^1.0.0",
|
|
38
|
+
"globals": "^16.5.0"
|
|
39
|
+
}
|
|
40
|
+
}
|