fenextjs-hook
Advanced tools
Comparing version 1.2.35 to 1.2.36
@@ -27,1 +27,10 @@ import { RequestProps, RequestResultDataProps, RequestResultTypeProps } from "fenextjs-interface/cjs/Request"; | ||
}; | ||
export interface useRequestFunctionProps<FP, FR> { | ||
f: RequestProps<FP, FR>; | ||
} | ||
export declare const useRequestFunction: <FP = any, FR = any>({ f, }: useRequestFunctionProps<FP, FR>) => { | ||
loader: boolean; | ||
error: undefined; | ||
result: FR | undefined; | ||
onRequest: (props: FP) => Promise<any>; | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useRequest = void 0; | ||
exports.useRequestFunction = exports.useRequest = void 0; | ||
const react_1 = require("react"); | ||
@@ -45,2 +45,34 @@ /** | ||
exports.useRequest = useRequest; | ||
const useRequestFunction = ({ f, }) => { | ||
const [loader, setLoader] = (0, react_1.useState)(false); | ||
const [error, setError] = (0, react_1.useState)(undefined); | ||
const [result, setResult] = (0, react_1.useState)(undefined); | ||
const onRequest = async (props) => { | ||
setLoader(true); | ||
setError(undefined); | ||
setResult(undefined); | ||
try { | ||
const r = await f(props); | ||
if (r.error) { | ||
throw r.error; | ||
} | ||
setResult(r.result); | ||
return r; | ||
} | ||
catch (error) { | ||
setError(error); | ||
return error; | ||
} | ||
finally { | ||
setLoader(false); | ||
} | ||
}; | ||
return { | ||
loader, | ||
error, | ||
result, | ||
onRequest, | ||
}; | ||
}; | ||
exports.useRequestFunction = useRequestFunction; | ||
//# sourceMappingURL=useRequest.js.map |
@@ -27,1 +27,10 @@ import { RequestProps, RequestResultDataProps, RequestResultTypeProps } from "fenextjs-interface/cjs/Request"; | ||
}; | ||
export interface useRequestFunctionProps<FP, FR> { | ||
f: RequestProps<FP, FR>; | ||
} | ||
export declare const useRequestFunction: <FP = any, FR = any>({ f, }: useRequestFunctionProps<FP, FR>) => { | ||
loader: boolean; | ||
error: undefined; | ||
result: FR | undefined; | ||
onRequest: (props: FP) => Promise<any>; | ||
}; |
@@ -41,2 +41,33 @@ import { useCallback, useEffect, useState } from "react"; | ||
}; | ||
export const useRequestFunction = ({ f, }) => { | ||
const [loader, setLoader] = useState(false); | ||
const [error, setError] = useState(undefined); | ||
const [result, setResult] = useState(undefined); | ||
const onRequest = async (props) => { | ||
setLoader(true); | ||
setError(undefined); | ||
setResult(undefined); | ||
try { | ||
const r = await f(props); | ||
if (r.error) { | ||
throw r.error; | ||
} | ||
setResult(r.result); | ||
return r; | ||
} | ||
catch (error) { | ||
setError(error); | ||
return error; | ||
} | ||
finally { | ||
setLoader(false); | ||
} | ||
}; | ||
return { | ||
loader, | ||
error, | ||
result, | ||
onRequest, | ||
}; | ||
}; | ||
//# sourceMappingURL=useRequest.js.map |
{ | ||
"name": "fenextjs-hook", | ||
"version": "1.2.35", | ||
"version": "1.2.36", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
103443
1961