nextemos 2.0.1 → 2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextemos",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "For helpers and hooks used in NextJS projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -18,7 +18,7 @@
18
18
  "scripts": {
19
19
  "clean": "rimraf dist",
20
20
  "prebuild": "npm run clean",
21
- "build": "tsc && tsup",
21
+ "build": "tsc",
22
22
  "preversion": "npm run build",
23
23
  "version": "npm publish"
24
24
  },
@@ -29,7 +29,6 @@
29
29
  "react": "^18.3.1",
30
30
  "react-dom": "^18.3.1",
31
31
  "rimraf": "^5.0.7",
32
- "tsup": "^8.1.0",
33
32
  "typescript": "^5.4.5"
34
33
  },
35
34
  "peerDependencies": {
package/dist/index.mjs DELETED
@@ -1,2 +0,0 @@
1
- import{useState as c,useEffect as S}from"react";var E=({method:o="GET",url:s,requestOptions:n={}})=>{let[l,u]=c(null),[t,e]=c(!0),[i,f]=c(null),[m,p]=c(0),d=()=>p(a=>a+1);return S(()=>{let a=new AbortController,g=a.signal;return(async()=>{e(!0);try{let r=await fetch(s,{method:o.toUpperCase(),...n,signal:g});if(!r.ok)throw new Error(`Error: ${r.statusText}`);let h=await r.json();u(h)}catch(r){r instanceof Error?f(r.message):f("An unknown error occurred")}finally{e(!1)}})(),()=>a.abort()},[s,o,n,m]),{response:l,loading:t,error:i,refetch:d}},T=E;import{useState as w}from"react";var x=(o,s)=>{let[n,l]=w(()=>{try{let t=window.localStorage.getItem(o);return t?JSON.parse(t):s}catch(t){return console.error(t),s}});return[n,t=>{try{let e=t instanceof Function?t(n):t;l(e),window.localStorage.setItem(o,JSON.stringify(e))}catch(e){console.error(e)}}]},y=x;export{T as useFetch,y as useLocalStorage};
2
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/useFetch.ts","../src/hooks/useLocalStorage.ts"],"sourcesContent":["import { useState, useEffect } from \"react\";\n\n/**\n * Interface for the fetch props.\n */\ninterface IFetchProps {\n method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS';\n url: string;\n requestOptions?: RequestInit;\n}\n\n/**\n * Custom hook for making HTTP requests using the fetch API.\n *\n * @param {IFetchProps} param0 - The fetch props.\n * @returns {Object} - An object containing the response, loading state, error, and refetch function.\n */\nconst useFetch = <T,>({ method = \"GET\", url, requestOptions = {} }: IFetchProps) => {\n // State for storing the response data.\n const [response, setResponse] = useState<T | null>(null);\n\n // State for tracking the loading state.\n const [loading, setLoading] = useState<boolean>(true);\n\n // State for storing the error message.\n const [error, setError] = useState<string | null>(null);\n\n // State for triggering a refetch.\n const [reload, setReload] = useState<number>(0);\n\n /**\n * Function for triggering a refetch.\n */\n const refetch = () => setReload((prev: number) => prev + 1);\n\n useEffect(() => {\n // Create an AbortController instance to cancel the fetch request if needed.\n const controller = new AbortController();\n const signal = controller.signal;\n\n /**\n * Function for fetching the data.\n */\n const fetchData = async () => {\n setLoading(true);\n try {\n // Make the fetch request with the provided method, URL, and options.\n const res = await fetch(url, {\n method: method.toUpperCase(),\n ...requestOptions,\n signal\n });\n\n // Check if the response is not OK and throw an error.\n if (!res.ok) {\n throw new Error(`Error: ${res.statusText}`);\n }\n\n // Parse the response data as JSON.\n const data = await res.json() as T;\n\n // Update the response state with the fetched data.\n setResponse(data);\n } catch (error) {\n // Handle any errors that occur during the fetch request.\n if (error instanceof Error) {\n setError(error.message);\n } else {\n setError('An unknown error occurred');\n }\n } finally {\n setLoading(false);\n }\n };\n\n // Call the fetchData function.\n fetchData();\n\n // Cleanup function to abort the fetch request if the component unmounts or the dependencies change.\n return () => controller.abort();\n }, [url, method, requestOptions, reload]);\n\n // Return the response, loading state, error, and refetch function.\n return {\n response,\n loading,\n error,\n refetch\n };\n}\n\nexport default useFetch;\n","import { useState } from 'react';\n\n/**\n * Custom hook that synchronizes state with localStorage.\n *\n * @param {string} key - The key to store the value under in localStorage.\n * @param {any} initialValue - The initial value to use if the key does not exist in localStorage.\n * @returns {[any, function]} - The current value stored in localStorage and a function to update it.\n */\nconst useLocalStorage = (key: string, initialValue: any) => {\n // Initialize the state with a function to read from localStorage or use the initial value\n const [storedValue, setStoredValue] = useState(() => {\n try {\n // Attempt to get the item from localStorage using the key\n const item = window.localStorage.getItem(key);\n // If the item exists, parse it from JSON; otherwise, return the initial value\n return item ? JSON.parse(item) : initialValue;\n } catch (error) {\n // Log any errors and return the initial value\n console.error(error);\n return initialValue;\n }\n });\n\n /**\n * Function to update the state and localStorage.\n *\n * @param {any} value - The new value to store, or a function that returns the new value.\n */\n const setValue = (value: any) => {\n try {\n // Determine the new value to store, supporting functional updates\n const valueToStore = value instanceof Function ? value(storedValue) : value;\n // Update the state with the new value\n setStoredValue(valueToStore);\n // Store the new value in localStorage as a JSON string\n window.localStorage.setItem(key, JSON.stringify(valueToStore));\n } catch (error) {\n // Log any errors that occur during the update\n console.error(error);\n }\n };\n\n // Return the current value and the function to update it\n return [storedValue, setValue];\n};\n\nexport default useLocalStorage;\n"],"mappings":"AAAA,OAAS,YAAAA,EAAU,aAAAC,MAAiB,QAiBpC,IAAMC,EAAW,CAAK,CAAE,OAAAC,EAAS,MAAO,IAAAC,EAAK,eAAAC,EAAiB,CAAC,CAAE,IAAmB,CAEhF,GAAM,CAACC,EAAUC,CAAW,EAAIP,EAAmB,IAAI,EAGjD,CAACQ,EAASC,CAAU,EAAIT,EAAkB,EAAI,EAG9C,CAACU,EAAOC,CAAQ,EAAIX,EAAwB,IAAI,EAGhD,CAACY,EAAQC,CAAS,EAAIb,EAAiB,CAAC,EAKxCc,EAAU,IAAMD,EAAWE,GAAiBA,EAAO,CAAC,EAE1D,OAAAd,EAAU,IAAM,CAEZ,IAAMe,EAAa,IAAI,gBACjBC,EAASD,EAAW,OAsC1B,OAjCkB,SAAY,CAC1BP,EAAW,EAAI,EACf,GAAI,CAEA,IAAMS,EAAM,MAAM,MAAMd,EAAK,CACzB,OAAQD,EAAO,YAAY,EAC3B,GAAGE,EACH,OAAAY,CACJ,CAAC,EAGD,GAAI,CAACC,EAAI,GACL,MAAM,IAAI,MAAM,UAAUA,EAAI,UAAU,EAAE,EAI9C,IAAMC,EAAO,MAAMD,EAAI,KAAK,EAG5BX,EAAYY,CAAI,CACpB,OAAST,EAAO,CAERA,aAAiB,MACjBC,EAASD,EAAM,OAAO,EAEtBC,EAAS,2BAA2B,CAE5C,QAAE,CACEF,EAAW,EAAK,CACpB,CACJ,GAGU,EAGH,IAAMO,EAAW,MAAM,CAClC,EAAG,CAACZ,EAAKD,EAAQE,EAAgBO,CAAM,CAAC,EAGjC,CACH,SAAAN,EACA,QAAAE,EACA,MAAAE,EACA,QAAAI,CACJ,CACJ,EAEOM,EAAQlB,EC3Ff,OAAS,YAAAmB,MAAgB,QASzB,IAAMC,EAAkB,CAACC,EAAaC,IAAsB,CAExD,GAAM,CAACC,EAAaC,CAAc,EAAIL,EAAS,IAAM,CACjD,GAAI,CAEA,IAAMM,EAAO,OAAO,aAAa,QAAQJ,CAAG,EAE5C,OAAOI,EAAO,KAAK,MAAMA,CAAI,EAAIH,CACrC,OAASI,EAAO,CAEZ,eAAQ,MAAMA,CAAK,EACZJ,CACX,CACJ,CAAC,EAsBD,MAAO,CAACC,EAfUI,GAAe,CAC7B,GAAI,CAEA,IAAMC,EAAeD,aAAiB,SAAWA,EAAMJ,CAAW,EAAII,EAEtEH,EAAeI,CAAY,EAE3B,OAAO,aAAa,QAAQP,EAAK,KAAK,UAAUO,CAAY,CAAC,CACjE,OAASF,EAAO,CAEZ,QAAQ,MAAMA,CAAK,CACvB,CACJ,CAG6B,CACjC,EAEOG,EAAQT","names":["useState","useEffect","useFetch","method","url","requestOptions","response","setResponse","loading","setLoading","error","setError","reload","setReload","refetch","prev","controller","signal","res","data","useFetch_default","useState","useLocalStorage","key","initialValue","storedValue","setStoredValue","item","error","value","valueToStore","useLocalStorage_default"]}