react-cancelable
Advanced tools
Comparing version 0.0.1-alpha.3 to 0.0.2-alpha.0
@@ -10,2 +10,3 @@ declare type UseCancelableReqParams = { | ||
declare type BaseCancelableReturn = { | ||
res?: Response; | ||
error?: Error; | ||
@@ -17,3 +18,3 @@ isLoading: boolean; | ||
declare type UseCancelableReqReturn = BaseCancelableReturn & { | ||
res?: Response; | ||
data?: Response; | ||
}; | ||
@@ -20,0 +21,0 @@ declare type UseCancelableImgReturn = BaseCancelableReturn & { |
@@ -8,12 +8,21 @@ "use strict"; | ||
var _d = (0, react_1.useState)(!isLazy), isLoading = _d[0], setIsLoading = _d[1]; | ||
var _e = (0, react_1.useState)(), res = _e[0], setRes = _e[1]; | ||
var resRef = (0, react_1.useRef)(); | ||
var _e = (0, react_1.useState)(), resData = _e[0], setResData = _e[1]; | ||
var _f = (0, react_1.useState)(), error = _f[0], setError = _f[1]; | ||
var abortController = (0, react_1.useMemo)(function () { return controller || new AbortController(); }, []); | ||
function setResponse(response) { | ||
resRef.current = response; | ||
} | ||
function handleSetError(data) { | ||
setError(data); | ||
var errorBody = (0, utils_1.getErrorBody)(data); | ||
// Set response object for axios | ||
if (data.request) { | ||
setResponse(data); | ||
} | ||
setError(errorBody); | ||
setIsLoading(false); | ||
onFail === null || onFail === void 0 ? void 0 : onFail(data); | ||
onFail === null || onFail === void 0 ? void 0 : onFail(errorBody); | ||
} | ||
function handleSetResData(data) { | ||
setRes(data); | ||
setResData(data); | ||
setIsLoading(false); | ||
@@ -29,2 +38,3 @@ onComplete === null || onComplete === void 0 ? void 0 : onComplete(data); | ||
var resHandler = (0, utils_1.getResParser)(contentheader); | ||
resRef.current = response; | ||
if (response['data']) { | ||
@@ -65,3 +75,4 @@ // Get `axios` res data | ||
return { | ||
res: res, | ||
res: resRef.current, | ||
data: resData, | ||
error: error, | ||
@@ -68,0 +79,0 @@ isLoading: isLoading, |
@@ -9,1 +9,2 @@ export declare function getResParser(contentType: string): string; | ||
export declare function cancelable(fn: CancelableRequestFn, controller?: AbortController): ExtendedPromise; | ||
export declare function getErrorBody(data: any): any; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.cancelable = exports.getCTypeHeaderVal = exports.rejectOrCb = exports.stopPromiseChain = exports.getResParser = void 0; | ||
exports.getErrorBody = exports.cancelable = exports.getCTypeHeaderVal = exports.rejectOrCb = exports.stopPromiseChain = exports.getResParser = void 0; | ||
var RES_TYPES_TO_PARSER = { | ||
@@ -54,1 +54,8 @@ 'json': 'json', | ||
exports.cancelable = cancelable; | ||
function getErrorBody(data) { | ||
if (typeof data === 'object' && 'data' in data) { | ||
return data.data; | ||
} | ||
return data; | ||
} | ||
exports.getErrorBody = getErrorBody; |
@@ -10,2 +10,3 @@ declare type UseCancelableReqParams = { | ||
declare type BaseCancelableReturn = { | ||
res?: Response; | ||
error?: Error; | ||
@@ -17,3 +18,3 @@ isLoading: boolean; | ||
declare type UseCancelableReqReturn = BaseCancelableReturn & { | ||
res?: Response; | ||
data?: Response; | ||
}; | ||
@@ -20,0 +21,0 @@ declare type UseCancelableImgReturn = BaseCancelableReturn & { |
@@ -1,16 +0,25 @@ | ||
import { useState, useEffect, useMemo } from "react"; | ||
import { rejectOrCb, getResParser, getCTypeHeaderVal } from "./utils"; | ||
import { useState, useEffect, useMemo, useRef } from "react"; | ||
import { rejectOrCb, getResParser, getCTypeHeaderVal, getErrorBody } from "./utils"; | ||
export default function useCancelableReq(fn, opts) { | ||
var _a = opts || {}, _b = _a.isLazy, isLazy = _b === void 0 ? false : _b, _c = _a.cancelOnUnmount, cancelOnUnmount = _c === void 0 ? true : _c, controller = _a.controller, onComplete = _a.onComplete, onFail = _a.onFail, onCancel = _a.onCancel; | ||
var _d = useState(!isLazy), isLoading = _d[0], setIsLoading = _d[1]; | ||
var _e = useState(), res = _e[0], setRes = _e[1]; | ||
var resRef = useRef(); | ||
var _e = useState(), resData = _e[0], setResData = _e[1]; | ||
var _f = useState(), error = _f[0], setError = _f[1]; | ||
var abortController = useMemo(function () { return controller || new AbortController(); }, []); | ||
function setResponse(response) { | ||
resRef.current = response; | ||
} | ||
function handleSetError(data) { | ||
setError(data); | ||
var errorBody = getErrorBody(data); | ||
// Set response object for axios | ||
if (data.request) { | ||
setResponse(data); | ||
} | ||
setError(errorBody); | ||
setIsLoading(false); | ||
onFail === null || onFail === void 0 ? void 0 : onFail(data); | ||
onFail === null || onFail === void 0 ? void 0 : onFail(errorBody); | ||
} | ||
function handleSetResData(data) { | ||
setRes(data); | ||
setResData(data); | ||
setIsLoading(false); | ||
@@ -26,2 +35,3 @@ onComplete === null || onComplete === void 0 ? void 0 : onComplete(data); | ||
var resHandler = getResParser(contentheader); | ||
resRef.current = response; | ||
if (response['data']) { | ||
@@ -62,3 +72,4 @@ // Get `axios` res data | ||
return { | ||
res: res, | ||
res: resRef.current, | ||
data: resData, | ||
error: error, | ||
@@ -65,0 +76,0 @@ isLoading: isLoading, |
@@ -9,1 +9,2 @@ export declare function getResParser(contentType: string): string; | ||
export declare function cancelable(fn: CancelableRequestFn, controller?: AbortController): ExtendedPromise; | ||
export declare function getErrorBody(data: any): any; |
@@ -46,1 +46,7 @@ var RES_TYPES_TO_PARSER = { | ||
} | ||
export function getErrorBody(data) { | ||
if (typeof data === 'object' && 'data' in data) { | ||
return data.data; | ||
} | ||
return data; | ||
} |
{ | ||
"name": "react-cancelable", | ||
"version": "0.0.1-alpha.3", | ||
"version": "0.0.2-alpha.0", | ||
"main": "./lib/cjs/index.js", | ||
@@ -8,3 +8,8 @@ "module": "./lib/esm/index.js", | ||
"license": "MIT", | ||
"keywords": ["cancelable", "request", "hooks", "react"], | ||
"keywords": [ | ||
"cancelable", | ||
"request", | ||
"hooks", | ||
"react" | ||
], | ||
"description": "Make cancelable requests with react-hooks", | ||
@@ -18,4 +23,4 @@ "author": "Vladyslav Ohirenko <aguretsvlad@gmail.com>", | ||
"bugs": { | ||
"url" : "https://github.com/vladagurets/react-cancelable/issues", | ||
"email" : "aguretsvlad@gmail.com" | ||
"url": "https://github.com/vladagurets/react-cancelable/issues", | ||
"email": "aguretsvlad@gmail.com" | ||
}, | ||
@@ -33,3 +38,4 @@ "scripts": { | ||
"peerDependencies": { | ||
"react": ">=16.8" | ||
"react": ">=16.8", | ||
"react-dom": ">=16.8" | ||
}, | ||
@@ -39,5 +45,5 @@ "devDependencies": { | ||
"@types/react-dom": "^17.0.11", | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2", | ||
"typescript": "^4.5.5" | ||
"typescript": "^4.5.5", | ||
"react": "17", | ||
"react-dom": "17" | ||
}, | ||
@@ -44,0 +50,0 @@ "files": [ |
@@ -89,3 +89,4 @@ <h1 align="center">react-cancelable<i>(alpha)</i></h1> | ||
type Artefacts = { | ||
res?: any; | ||
res?: Response; | ||
data?: any; | ||
error?: any; | ||
@@ -145,5 +146,10 @@ isLoading: boolean; | ||
<td>res</td> | ||
<td>Response of a request</td> | ||
<td>Response object</td> | ||
<td>undefined</td> | ||
</tr> | ||
<tr> | ||
<td>data</td> | ||
<td>Payload of a request</td> | ||
<td>undefined</td> | ||
</tr> | ||
<tr> | ||
@@ -190,3 +196,3 @@ <td>error</td> | ||
function Item() { | ||
const { res, isLoading, error } = useCancelableReq(makeRequest) | ||
const { data, isLoading, error } = useCancelableReq(makeRequest) | ||
@@ -197,3 +203,3 @@ return ( | ||
{error && <span>Error occured</span>} | ||
{res && <span>Result is fetched</span>} | ||
{data && <span>Data is fetched</span>} | ||
</> | ||
@@ -231,2 +237,3 @@ ) | ||
type Artefacts = { | ||
res?: Response; | ||
src?: string; | ||
@@ -286,2 +293,7 @@ error?: any; | ||
<tr> | ||
<td>res</td> | ||
<td>Response object</td> | ||
<td>undefined</td> | ||
</tr> | ||
<tr> | ||
<td>src</td> | ||
@@ -288,0 +300,0 @@ <td>Generated ObjectURI to Blob image after request done</td> |
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
463
29677
2
22
460