react-hooks-library
Advanced tools
Comparing version 0.0.25 to 0.0.27
@@ -0,2 +1,5 @@ | ||
import { AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
export declare type IQuery = Pick<AxiosRequestConfig, Exclude<keyof AxiosRequestConfig, 'params' | 'data' | 'cancelToken'>>; | ||
export interface IQueryProps<P> { | ||
query: IQuery; | ||
initialData?: P; | ||
@@ -7,3 +10,2 @@ variable?: object; | ||
autoQuery?: boolean; | ||
query?(params?: IQueryProps<P>['variable']): Promise<P>; | ||
onSuccess?(result: P): void; | ||
@@ -14,8 +16,9 @@ onFailure?(error: Error): void; | ||
data: P | undefined; | ||
response: AxiosResponse<P> | undefined; | ||
reset: () => void; | ||
loading: boolean; | ||
error: Error | undefined; | ||
refetch: (variable?: object | undefined) => Promise<P | undefined>; | ||
refetch: (variable?: object | undefined) => Promise<void | [boolean, AxiosResponse<P>]>; | ||
startPolling: (interval: number, variable?: object | undefined) => void; | ||
stopPolling: () => void; | ||
}; |
import * as tslib_1 from "tslib"; | ||
import { useState, useEffect, useRef, useMemo } from 'react'; | ||
import { isEqual, merge } from 'lodash'; | ||
import { request } from '../request/axios'; | ||
import { usePrevious } from './usePrevious'; | ||
import { useCancellablePromise } from './useCancellablePromise'; | ||
function createRequest(query, variable) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
if (query.method === 'PUT' || | ||
query.method === 'POST' || | ||
query.method === 'PATCH') { | ||
return [2 /*return*/, request(tslib_1.__assign({}, query, { data: variable }))]; | ||
} | ||
else { | ||
return [2 /*return*/, request(tslib_1.__assign({}, query, { params: variable }))]; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); | ||
} | ||
export function useQuery(props) { | ||
@@ -14,12 +29,13 @@ var _this = this; | ||
}, [props]); | ||
var _a = useState(mergedProps.initialData), data = _a[0], setData = _a[1]; | ||
var cancellablePromise = useCancellablePromise().cancellablePromise; | ||
var _b = useState(undefined), error = _b[0], setError = _b[1]; | ||
var _c = useState(false), loading = _c[0], setLoading = _c[1]; | ||
var _d = useState(undefined), intervalIndex = _d[0], setIntervalIndex = _d[1]; | ||
var _a = useState(props.initialData), data = _a[0], setData = _a[1]; | ||
var _b = useState(), response = _b[0], setResponse = _b[1]; | ||
var _c = useState(), error = _c[0], setError = _c[1]; | ||
var _d = useState(false), loading = _d[0], setLoading = _d[1]; | ||
var _e = useState(), intervalIndex = _e[0], setIntervalIndex = _e[1]; | ||
var intervalIndexRef = useRef(intervalIndex); | ||
var _e = useState(false), isCalled = _e[0], setIsCalled = _e[1]; | ||
var _f = useState(false), isCalled = _f[0], setIsCalled = _f[1]; | ||
var prevVariable = usePrevious(mergedProps.variable); | ||
var _reset = function () { | ||
setData(mergedProps.initialData); | ||
setResponse(undefined); | ||
}; | ||
@@ -43,16 +59,25 @@ var _isShouldQuery = function () { | ||
return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var response, error_1; | ||
var query, queryResult, isSuccess, queryResponse, error_1; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!mergedProps.query) return [3 /*break*/, 4]; | ||
setLoading(true); | ||
query = createRequest(mergedProps.query, variable); | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, cancellablePromise(mergedProps.query(variable))]; | ||
_a.trys.push([1, 3, 4, 5]); | ||
return [4 /*yield*/, query]; | ||
case 2: | ||
response = _a.sent(); | ||
if (response) { | ||
setData(response); | ||
queryResult = _a.sent(); | ||
if (queryResult) { | ||
isSuccess = queryResult[0], queryResponse = queryResult[1]; | ||
if (isSuccess) { | ||
setData(queryResponse.data); | ||
typeof props.onSuccess === 'function' && | ||
props.onSuccess(queryResponse.data); | ||
} | ||
else { | ||
_reset(); | ||
} | ||
setResponse(queryResponse); | ||
} | ||
@@ -62,11 +87,13 @@ else { | ||
} | ||
setLoading(false); | ||
typeof props.onSuccess === 'function' && props.onSuccess(response); | ||
return [2 /*return*/, response]; | ||
return [3 /*break*/, 5]; | ||
case 3: | ||
error_1 = _a.sent(); | ||
_reset(); | ||
setError(error_1); | ||
typeof props.onFailure === 'function' && props.onFailure(error_1); | ||
return [3 /*break*/, 4]; | ||
case 4: return [2 /*return*/, undefined]; | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
setLoading(false); | ||
return [7 /*endfinally*/]; | ||
case 5: return [2 /*return*/, query]; | ||
} | ||
@@ -99,2 +126,3 @@ }); | ||
data: data, | ||
response: response, | ||
reset: _reset, | ||
@@ -101,0 +129,0 @@ loading: loading, |
@@ -1,2 +0,2 @@ | ||
export * from './fields'; | ||
export * from './hooks'; | ||
export * from './request/axios'; |
@@ -1,2 +0,2 @@ | ||
export * from './fields'; | ||
export * from './hooks'; | ||
export * from './request/axios'; |
{ | ||
"name": "react-hooks-library", | ||
"version": "0.0.25", | ||
"version": "0.0.27", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "jest --env=jsdom --coverage", | ||
"build": "tsc --p tsconfig.types.json", | ||
"test": "jest --env=jsdom", | ||
"lint": "tslint -p .", | ||
@@ -16,3 +16,4 @@ "dev": "webpack-dev-server --config=webpack.config.dev.js --hot --inline" | ||
"demo/", | ||
"README.md" | ||
"README.md", | ||
"LICENSE" | ||
], | ||
@@ -23,3 +24,3 @@ "devDependencies": { | ||
"@babel/plugin-proposal-object-rest-spread": "^7.3.2", | ||
"@babel/polyfill": "^7.4.0", | ||
"@babel/polyfill": "^7.4.3", | ||
"@babel/preset-env": "^7.3.1", | ||
@@ -30,3 +31,2 @@ "@babel/preset-react": "^7.0.0", | ||
"@types/bluebird": "^3.5.25", | ||
"@types/chai": "^4.1.7", | ||
"@types/enzyme": "^3.1.17", | ||
@@ -36,3 +36,2 @@ "@types/humps": "^1.1.2", | ||
"@types/lodash": "^4.14.123", | ||
"@types/numeral": "^0.0.25", | ||
"@types/react": "^16.8.2", | ||
@@ -42,5 +41,5 @@ "@types/react-dom": "^16.8.1", | ||
"@types/webpack": "^4.4.26", | ||
"axios-mock-adapter": "^1.16.0", | ||
"babel-jest": "^24.5.0", | ||
"babel-loader": "^8.0.5", | ||
"chai": "^4.2.0", | ||
"core-js": "3", | ||
@@ -77,5 +76,5 @@ "css-loader": "^2.1.1", | ||
"peerDependencies": { | ||
"axios": "^0.18.0", | ||
"lodash": "^4.17.11", | ||
"moment": "^2.24.0", | ||
"numeral": "^2.0.6", | ||
"react": "^16.8.2", | ||
@@ -94,5 +93,5 @@ "react-dom": "^16.8.2" | ||
"dependencies": { | ||
"moment": "^2.24.0", | ||
"numeral": "^2.0.6" | ||
"axios": "^0.18.0", | ||
"moment": "^2.24.0" | ||
} | ||
} |
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
49
18703
25
468
+ Addedaxios@^0.18.0
+ Addedaxios@0.18.1(transitive)
+ Addedfollow-redirects@1.5.10(transitive)
+ Addedis-buffer@2.0.5(transitive)
- Removednumeral@^2.0.6
- Removednumeral@2.0.6(transitive)