react-hooks-library
Advanced tools
Comparing version 0.0.35 to 0.0.36
@@ -1,12 +0,9 @@ | ||
import { AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
export declare type IQuery = Pick<AxiosRequestConfig, Exclude<keyof AxiosRequestConfig, 'params' | 'data' | 'cancelToken'>>; | ||
export interface IQueryProps<P, T = object> { | ||
query: IQuery; | ||
export interface IQueryProps<P> { | ||
initialData?: P; | ||
variable?: T; | ||
variable?: object; | ||
pollInterval?: number; | ||
skip?: boolean; | ||
autoQuery?: boolean; | ||
decorateData?(data: P): P; | ||
onSuccess?(result: P, response: AxiosResponse<P>): void; | ||
query?(params?: IQueryProps<P>['variable']): Promise<P>; | ||
onSuccess?(result: P): void; | ||
onFailure?(error: Error): void; | ||
@@ -16,14 +13,8 @@ } | ||
data: P | undefined; | ||
response: AxiosResponse<P> | undefined; | ||
reset: () => void; | ||
loading: boolean; | ||
error: Error | undefined; | ||
cancel: () => void; | ||
refetch: (variable?: object | undefined) => Promise<void | { | ||
isSuccess: boolean; | ||
response: AxiosResponse<P>; | ||
data: P; | ||
}>; | ||
startPolling: (interval: number, variable?: object | undefined) => void; | ||
refetch: (v?: object | undefined) => Promise<P | undefined>; | ||
startPolling: (timeout: number, v?: object | undefined) => void; | ||
stopPolling: () => void; | ||
}; |
import * as tslib_1 from "tslib"; | ||
import { useState, useEffect, useRef, useMemo } from 'react'; | ||
import { isEqual, merge } from 'lodash'; | ||
import axios from 'axios'; | ||
import { request } from '../request/axios'; | ||
import { usePrevious } from './usePrevious'; | ||
function createRequest(query, variable, cancelTokenSource) { | ||
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, cancelToken: cancelTokenSource.token }))]; | ||
} | ||
else { | ||
return [2 /*return*/, request(tslib_1.__assign({}, query, { params: variable, cancelToken: cancelTokenSource.token }))]; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); | ||
} | ||
import { useCancellablePromise } from './useCancellablePromise'; | ||
export function useQuery(props) { | ||
var _this = this; | ||
var mergedProps = useMemo(function () { | ||
var variable = props.variable, rest = tslib_1.__rest(props, ["variable"]); | ||
var mergedConfig = useMemo(function () { | ||
return merge({ | ||
skip: false, | ||
autoQuery: true, | ||
}, props); | ||
}, [props]); | ||
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 _f = useState(false), isCalled = _f[0], setIsCalled = _f[1]; | ||
var prevVariable = usePrevious(mergedProps.variable); | ||
var intervalIndexRef = useRef(intervalIndex); | ||
var cancelTokenSourceRef = useRef(); | ||
}, rest); | ||
}, [rest]); | ||
var _a = useState(mergedConfig.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 intervalIndexRef = useRef(undefined); | ||
var variableRef = useRef(variable); | ||
var _d = useState(false), isCalled = _d[0], setIsCalled = _d[1]; | ||
var prevVariable = usePrevious(variable); | ||
var _reset = function () { | ||
setData(mergedProps.initialData); | ||
setResponse(undefined); | ||
setData(mergedConfig.initialData); | ||
}; | ||
@@ -46,8 +29,8 @@ var _isShouldQuery = function () { | ||
if (!isCalled) { | ||
if (!mergedProps.skip) { | ||
if (!mergedConfig.skip) { | ||
return true; | ||
} | ||
} | ||
else if (mergedProps.autoQuery) { | ||
if (!isEqual(prevVariable, mergedProps.variable)) { | ||
else if (mergedConfig.autoQuery) { | ||
if (!isEqual(prevVariable, variableRef.current)) { | ||
return true; | ||
@@ -58,29 +41,19 @@ } | ||
}; | ||
var _queryTransaction = function (variable) { | ||
if (variable === void 0) { variable = mergedProps.variable; } | ||
var _queryTransaction = function (v) { | ||
if (v === void 0) { v = variableRef.current; } | ||
return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var query, queryResult, isSuccess, queryResponse, queryData, error_1; | ||
var response, error_1; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!mergedConfig.query) return [3 /*break*/, 4]; | ||
setLoading(true); | ||
cancelTokenSourceRef.current = axios.CancelToken.source(); | ||
query = createRequest(mergedProps.query, variable, cancelTokenSourceRef.current); | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, 4, 5]); | ||
return [4 /*yield*/, query]; | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, cancellablePromise(mergedConfig.query(v))]; | ||
case 2: | ||
queryResult = _a.sent(); | ||
if (queryResult) { | ||
isSuccess = queryResult.isSuccess, queryResponse = queryResult.response, queryData = queryResult.data; | ||
if (isSuccess) { | ||
setData(queryData); | ||
typeof props.onSuccess === 'function' && | ||
props.onSuccess(queryData, queryResponse); | ||
} | ||
else { | ||
_reset(); | ||
} | ||
setResponse(queryResponse); | ||
response = _a.sent(); | ||
if (response) { | ||
setData(response); | ||
} | ||
@@ -90,14 +63,11 @@ else { | ||
} | ||
return [3 /*break*/, 5]; | ||
setLoading(false); | ||
typeof props.onSuccess === 'function' && props.onSuccess(response); | ||
return [2 /*return*/, response]; | ||
case 3: | ||
error_1 = _a.sent(); | ||
_reset(); | ||
setError(error_1); | ||
typeof props.onFailure === 'function' && props.onFailure(error_1); | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
setLoading(false); | ||
cancelTokenSourceRef.current = undefined; | ||
return [7 /*endfinally*/]; | ||
case 5: return [2 /*return*/, query]; | ||
return [3 /*break*/, 4]; | ||
case 4: return [2 /*return*/, undefined]; | ||
} | ||
@@ -108,6 +78,6 @@ }); | ||
var refetch = _queryTransaction; | ||
var startPolling = function (interval, variable) { | ||
setIntervalIndex(window.setInterval(function () { | ||
_queryTransaction(variable); | ||
}, interval)); | ||
var startPolling = function (timeout, v) { | ||
intervalIndexRef.current = window.setInterval(function () { | ||
_queryTransaction(v); | ||
}, timeout); | ||
}; | ||
@@ -117,11 +87,8 @@ var stopPolling = function () { | ||
window.clearInterval(intervalIndexRef.current); | ||
setIntervalIndex(undefined); | ||
intervalIndexRef.current = undefined; | ||
} | ||
}; | ||
var cancel = function () { | ||
if (cancelTokenSourceRef.current) { | ||
cancelTokenSourceRef.current.cancel(); | ||
} | ||
}; | ||
useEffect(function () { | ||
// update variable | ||
variableRef.current = variable; | ||
if (_isShouldQuery()) { | ||
@@ -131,13 +98,8 @@ _queryTransaction(); | ||
setIsCalled(true); | ||
}, [mergedProps.variable]); | ||
useEffect(function () { | ||
intervalIndexRef.current = intervalIndex; | ||
}, [intervalIndex]); | ||
}, [variable]); | ||
return { | ||
data: data, | ||
response: response, | ||
reset: _reset, | ||
loading: loading, | ||
error: error, | ||
cancel: cancel, | ||
refetch: refetch, | ||
@@ -144,0 +106,0 @@ startPolling: startPolling, |
@@ -0,2 +1,2 @@ | ||
export * from './fields'; | ||
export * from './hooks'; | ||
export * from './request'; |
@@ -0,2 +1,2 @@ | ||
export * from './fields'; | ||
export * from './hooks'; | ||
export * from './request'; |
{ | ||
"name": "react-hooks-library", | ||
"version": "0.0.35", | ||
"version": "0.0.36", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "jest --env=jsdom --coverage", | ||
"build": "tsc --p tsconfig.types.json", | ||
"test": "jest --env=jsdom", | ||
"lint": "tslint -p .", | ||
@@ -16,4 +16,3 @@ "dev": "webpack-dev-server --config=webpack.config.dev.js --hot --inline" | ||
"demo/", | ||
"README.md", | ||
"LICENSE" | ||
"README.md" | ||
], | ||
@@ -24,4 +23,3 @@ "devDependencies": { | ||
"@babel/plugin-proposal-object-rest-spread": "^7.3.2", | ||
"@babel/plugin-transform-runtime": "^7.4.3", | ||
"@babel/polyfill": "^7.4.3", | ||
"@babel/polyfill": "^7.4.0", | ||
"@babel/preset-env": "^7.3.1", | ||
@@ -32,2 +30,3 @@ "@babel/preset-react": "^7.0.0", | ||
"@types/bluebird": "^3.5.25", | ||
"@types/chai": "^4.1.7", | ||
"@types/enzyme": "^3.1.17", | ||
@@ -37,2 +36,3 @@ "@types/humps": "^1.1.2", | ||
"@types/lodash": "^4.14.123", | ||
"@types/numeral": "^0.0.25", | ||
"@types/react": "^16.8.2", | ||
@@ -42,5 +42,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 +77,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 +94,5 @@ "react-dom": "^16.8.2" | ||
"dependencies": { | ||
"axios": "^0.18.0", | ||
"moment": "^2.24.0" | ||
"moment": "^2.24.0", | ||
"numeral": "^2.0.6" | ||
} | ||
} |
[![Build Status](https://travis-ci.com/linayanse/react-hooks-library.svg?token=Ptv7oMQGNquh2yFJwQxh&branch=master | ||
)](https://travis-ci.com/linayanse/react-hooks-library.svg?token=Ptv7oMQGNquh2yFJwQxh&branch=master) [![npm version](https://img.shields.io/npm/v/react-hooks-library.svg?style=flat)](https://www.npmjs.com/package/react-hooks-library) | ||
# react-hooks-library | ||
)](https://travis-ci.com/linayanse/react-hooks-library.svg?token=Ptv7oMQGNquh2yFJwQxh&branch=master) | ||
# common-hooks | ||
React hooks |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
28600
41
781
51
1
+ Addednumeral@^2.0.6
+ Addednumeral@2.0.6(transitive)
- Removedaxios@^0.18.0
- Removedaxios@0.18.1(transitive)
- Removedfollow-redirects@1.5.10(transitive)
- Removedis-buffer@2.0.5(transitive)