@benco112/use-req 1.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.
@@ -0,0 +1,50 @@
1
+ import { useState, useCallback } from 'react';
2
+
3
+ // src/index.js
4
+
5
+ const useReq = (baseUrl) => {
6
+ const [data, setData] = useState(null);
7
+ const [loading, setLoading] = useState(false);
8
+ const [error, setError] = useState(null);
9
+
10
+ const request = useCallback(
11
+ async (endpoint, options = {}) => {
12
+ setLoading(true);
13
+ setError(null);
14
+
15
+ try {
16
+ const response = await fetch(`${baseUrl}${endpoint}`, {
17
+ method: options.method || "GET",
18
+ headers: {
19
+ "Content-Type": "application/json",
20
+ ...options.headers,
21
+ },
22
+ body: options.body ? JSON.stringify(options.body) : null,
23
+ });
24
+
25
+ if (!response.ok) {
26
+ const errorData = await response.json().catch(() => ({}));
27
+ throw new Error(
28
+ errorData.message || `Error ${response.status}`,
29
+ );
30
+ }
31
+
32
+ if (response.status === 204) return null;
33
+
34
+ const result = await response.json();
35
+ setData(result);
36
+ return result;
37
+ } catch (err) {
38
+ setError(err.message);
39
+ throw err;
40
+ } finally {
41
+ setLoading(false);
42
+ }
43
+ },
44
+ [baseUrl],
45
+ );
46
+
47
+ return { data, loading, error, request };
48
+ };
49
+
50
+ export { useReq as default };
@@ -0,0 +1,50 @@
1
+ import { useState, useCallback } from 'react';
2
+
3
+ // src/index.js
4
+
5
+ const useReq = (baseUrl) => {
6
+ const [data, setData] = useState(null);
7
+ const [loading, setLoading] = useState(false);
8
+ const [error, setError] = useState(null);
9
+
10
+ const request = useCallback(
11
+ async (endpoint, options = {}) => {
12
+ setLoading(true);
13
+ setError(null);
14
+
15
+ try {
16
+ const response = await fetch(`${baseUrl}${endpoint}`, {
17
+ method: options.method || "GET",
18
+ headers: {
19
+ "Content-Type": "application/json",
20
+ ...options.headers,
21
+ },
22
+ body: options.body ? JSON.stringify(options.body) : null,
23
+ });
24
+
25
+ if (!response.ok) {
26
+ const errorData = await response.json().catch(() => ({}));
27
+ throw new Error(
28
+ errorData.message || `Error ${response.status}`,
29
+ );
30
+ }
31
+
32
+ if (response.status === 204) return null;
33
+
34
+ const result = await response.json();
35
+ setData(result);
36
+ return result;
37
+ } catch (err) {
38
+ setError(err.message);
39
+ throw err;
40
+ } finally {
41
+ setLoading(false);
42
+ }
43
+ },
44
+ [baseUrl],
45
+ );
46
+
47
+ return { data, loading, error, request };
48
+ };
49
+
50
+ export { useReq as default };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ var u=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var E=(e,t)=>{for(var s in t)u(e,s,{get:t[s],enumerable:!0})},b=(e,t,s,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of w(t))!g.call(e,r)&&r!==s&&u(e,r,{get:()=>t[r],enumerable:!(o=m(t,r))||o.enumerable});return e};var j=e=>b(u({},"__esModule",{value:!0}),e);var k={};E(k,{default:()=>$});module.exports=j(k);var n=require("react"),p=e=>{let[t,s]=(0,n.useState)(null),[o,r]=(0,n.useState)(!1),[i,c]=(0,n.useState)(null),f=(0,n.useCallback)(async(h,l={})=>{r(!0),c(null);try{let a=await fetch(`${e}${h}`,{method:l.method||"GET",headers:{"Content-Type":"application/json",...l.headers},body:l.body?JSON.stringify(l.body):null});if(!a.ok){let y=await a.json().catch(()=>({}));throw new Error(y.message||`Error ${a.status}`)}if(a.status===204)return null;let d=await a.json();return s(d),d}catch(a){throw c(a.message),a}finally{r(!1)}},[e]);return{data:t,loading:o,error:i,request:f}},$=p;
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import{useState as r,useCallback as y}from"react";var m=a=>{let[l,u]=r(null),[c,s]=r(!1),[d,n]=r(null),i=y(async(f,e={})=>{s(!0),n(null);try{let t=await fetch(`${a}${f}`,{method:e.method||"GET",headers:{"Content-Type":"application/json",...e.headers},body:e.body?JSON.stringify(e.body):null});if(!t.ok){let h=await t.json().catch(()=>({}));throw new Error(h.message||`Error ${t.status}`)}if(t.status===204)return null;let o=await t.json();return u(o),o}catch(t){throw n(t.message),t}finally{s(!1)}},[a]);return{data:l,loading:c,error:d,request:i}},g=m;export{g as default};
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@benco112/use-req",
3
+ "version": "1.0.0",
4
+ "description": "Simple and lightweight fetch hook by benco112",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "scripts": {
12
+ "build": "tsup src/index.js --format cjs,esm --dts --minify",
13
+ "prepublishOnly": "npm run build"
14
+ },
15
+ "peerDependencies": {
16
+ "react": ">=16.8.0"
17
+ },
18
+ "devDependencies": {
19
+ "tsup": "^8.0.0",
20
+ "typescript": "^5.0.0"
21
+ }
22
+ }
package/src/index.js ADDED
@@ -0,0 +1,49 @@
1
+ // src/index.js
2
+ import { useState, useCallback } from "react";
3
+
4
+ const useReq = (baseUrl) => {
5
+ const [data, setData] = useState(null);
6
+ const [loading, setLoading] = useState(false);
7
+ const [error, setError] = useState(null);
8
+
9
+ const request = useCallback(
10
+ async (endpoint, options = {}) => {
11
+ setLoading(true);
12
+ setError(null);
13
+
14
+ try {
15
+ const response = await fetch(`${baseUrl}${endpoint}`, {
16
+ method: options.method || "GET",
17
+ headers: {
18
+ "Content-Type": "application/json",
19
+ ...options.headers,
20
+ },
21
+ body: options.body ? JSON.stringify(options.body) : null,
22
+ });
23
+
24
+ if (!response.ok) {
25
+ const errorData = await response.json().catch(() => ({}));
26
+ throw new Error(
27
+ errorData.message || `Error ${response.status}`,
28
+ );
29
+ }
30
+
31
+ if (response.status === 204) return null;
32
+
33
+ const result = await response.json();
34
+ setData(result);
35
+ return result;
36
+ } catch (err) {
37
+ setError(err.message);
38
+ throw err;
39
+ } finally {
40
+ setLoading(false);
41
+ }
42
+ },
43
+ [baseUrl],
44
+ );
45
+
46
+ return { data, loading, error, request };
47
+ };
48
+
49
+ export default useReq;