Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@umijs/use-request

Package Overview
Dependencies
Maintainers
12
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@umijs/use-request - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0-beta.1

dist/antdTypes.d.ts

31

es/antdTypes.d.ts

@@ -1,3 +0,32 @@

export declare type PaginationConfig = any;
/// <reference types="react" />
export interface PaginationConfig {
total?: number;
defaultCurrent?: number;
disabled?: boolean;
current?: number;
defaultPageSize?: number;
pageSize?: number;
onChange?: (page: number, pageSize?: number) => void;
hideOnSinglePage?: boolean;
showSizeChanger?: boolean;
pageSizeOptions?: string[];
onShowSizeChange?: (current: number, size: number) => void;
showQuickJumper?: boolean | {
goButton?: React.ReactNode;
};
showTotal?: (total: number, range: [number, number]) => React.ReactNode;
size?: string;
simple?: boolean;
style?: React.CSSProperties;
locale?: Object;
className?: string;
prefixCls?: string;
selectPrefixCls?: string;
itemRender?: (page: number, type: 'page' | 'prev' | 'next' | 'jump-prev' | 'jump-next', originalElement: React.ReactElement<HTMLElement>) => React.ReactNode;
role?: string;
showLessItems?: boolean;
position?: 'top' | 'bottom' | 'both';
[key: string]: any;
}
export declare type Sorter = any;
export declare type Filter = any;

8

es/index.d.ts

@@ -8,8 +8,8 @@ /// <reference types="react" />

declare function useRequest<R = any, P extends any[] = any>(service: CombineService<R, P>, options?: BaseOptions<R, P>): BaseResult<R, P>;
declare function useRequest<R = any, Item = any, U extends Item = any>(service: CombineService<R, LoadMoreParams>, options: LoadMoreOptionsWithFormat<R, Item, U>): LoadMoreResult<Item>;
declare function useRequest<R = any, Item = any, U extends Item = any>(service: CombineService<LoadMoreFormatReturn<Item>, LoadMoreParams>, options: LoadMoreOptions<U>): LoadMoreResult<Item>;
declare function useRequest<R = any, Item = any, U extends Item = any>(service: CombineService<R, PaginatedParams<U>>, options: PaginatedOptionsWithFormat<R, Item, U>): PaginatedResult<Item>;
declare function useRequest<R = any, Item = any, U extends Item = any>(service: CombineService<PaginatedFormatReturn<Item>, PaginatedParams<U>>, options: BasePaginatedOptions<U>): PaginatedResult<Item>;
declare function useRequest<R extends LoadMoreFormatReturn, RR>(service: CombineService<RR, LoadMoreParams<R>>, options: LoadMoreOptionsWithFormat<R, RR>): LoadMoreResult<R>;
declare function useRequest<R extends LoadMoreFormatReturn, RR extends R>(service: CombineService<R, LoadMoreParams<R>>, options: LoadMoreOptions<RR>): LoadMoreResult<R>;
declare function useRequest<R = any, Item = any, U extends Item = any>(service: CombineService<R, PaginatedParams>, options: PaginatedOptionsWithFormat<R, Item, U>): PaginatedResult<Item>;
declare function useRequest<R = any, Item = any, U extends Item = any>(service: CombineService<PaginatedFormatReturn<Item>, PaginatedParams>, options: BasePaginatedOptions<U>): PaginatedResult<Item>;
declare const UseAPIProvider: import("react").Provider<BaseOptions<any, any> | OptionsWithFormat<any, any, any, any> | BasePaginatedOptions<any> | LoadMoreOptions<any>>;
export { useAsync, usePaginated, useLoadMore, UseAPIProvider };
export default useRequest;

@@ -5,3 +5,3 @@ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

@@ -34,7 +34,7 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

if (paginatedRef.current !== paginated) {
throw Error('You should not modify this paginated of options');
throw Error('You should not modify the paginated of options');
}
if (loadMoreRef.current !== loadMore) {
throw Error('You should not modify this loadMore of options');
throw Error('You should not modify the loadMore of options');
}

@@ -96,7 +96,9 @@

return useLoadMore(promiseService, finalOptions);
} else if (paginated) {
}
if (paginated) {
return usePaginated(promiseService, finalOptions);
} else {
return useAsync(promiseService, finalOptions);
}
return useAsync(promiseService, finalOptions);
}

@@ -103,0 +105,0 @@

@@ -0,7 +1,7 @@

import { DependencyList, RefObject } from 'react';
import { PaginationConfig, Filter, Sorter } from './antdTypes';
import { DependencyList } from 'react';
export declare type noop = (...args: any[]) => void;
export declare type Service<R, P extends any[]> = (...args: P) => Promise<R>;
export declare type Subscribe<R, P extends any[]> = (data: FetchResult<R, P>) => void;
export declare type Mutate<R> = (x: (data: R) => R) => void | ((data: R) => void);
export declare type Mutate<R> = (x: (R | ((data: R) => R))) => void;
export declare type RequestService = string | {

@@ -67,3 +67,3 @@ [key: string]: any;

export declare type Options<R, P extends any[], U, UU extends U> = BaseOptions<R, P> | OptionsWithFormat<R, P, U, UU>;
export declare type PaginatedParams<Item> = [{
export declare type PaginatedParams = [{
current: number;

@@ -79,3 +79,3 @@ pageSize: number;

}
export interface PaginatedResult<Item> extends BaseResult<PaginatedFormatReturn<Item>, PaginatedParams<Item>> {
export interface PaginatedResult<Item> extends BaseResult<PaginatedFormatReturn<Item>, PaginatedParams> {
pagination: {

@@ -101,7 +101,7 @@ current: number;

}
export interface BasePaginatedOptions<U> extends Omit<BaseOptions<PaginatedFormatReturn<U>, PaginatedParams<U>>, 'paginated'> {
export interface BasePaginatedOptions<U> extends Omit<BaseOptions<PaginatedFormatReturn<U>, PaginatedParams>, 'paginated'> {
paginated: true;
defaultPageSize?: number;
}
export interface PaginatedOptionsWithFormat<R, Item, U> extends Omit<BaseOptions<PaginatedFormatReturn<U>, PaginatedParams<U>>, 'paginated'> {
export interface PaginatedOptionsWithFormat<R, Item, U> extends Omit<BaseOptions<PaginatedFormatReturn<U>, PaginatedParams>, 'paginated'> {
paginated: true;

@@ -111,17 +111,27 @@ defaultPageSize?: number;

}
export declare type LoadMoreParams = any[];
export interface LoadMoreFormatReturn<Item> {
list: Item[];
nextId?: string;
export declare type LoadMoreParams<R> = [R | undefined, ...any[]];
export interface LoadMoreFormatReturn {
list: any[];
[key: string]: any;
}
export interface LoadMoreResult<Item> extends BaseResult<LoadMoreFormatReturn<Item>, LoadMoreParams> {
export interface LoadMoreResult<R> extends BaseResult<R, LoadMoreParams<R>> {
noMore?: boolean;
loadMore: () => void;
reload: () => void;
loadingMore: boolean;
}
export interface LoadMoreOptions<U> extends Omit<BaseOptions<LoadMoreFormatReturn<U>, LoadMoreParams>, 'loadMore'> {
export interface LoadMoreOptions<R extends LoadMoreFormatReturn> extends Omit<BaseOptions<R, LoadMoreParams<R>>, 'loadMore'> {
loadMore: true;
fetchKey: (...args: LoadMoreParams<R>) => string;
ref?: RefObject<any>;
isNoMore?: (r: R) => boolean;
threshold?: number;
}
export interface LoadMoreOptionsWithFormat<R, Item, U> extends Omit<BaseOptions<LoadMoreFormatReturn<U>, LoadMoreParams>, 'loadMore'> {
export interface LoadMoreOptionsWithFormat<R extends LoadMoreFormatReturn, RR> extends Omit<BaseOptions<R, LoadMoreParams<R>>, 'loadMore'> {
loadMore: true;
formatResult: (data: R) => LoadMoreFormatReturn<Item>;
fetchKey: (...args: LoadMoreParams<R>) => string;
formatResult: (data: RR) => R;
ref?: RefObject<any>;
isNoMore?: (r: R) => boolean;
threshold?: number;
}

@@ -125,3 +125,3 @@ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

this.setState({
loading: this.config.loadingDelay ? false : true,
loading: !this.config.loadingDelay,
params: args

@@ -251,2 +251,3 @@ });

this.setState({
// eslint-disable-next-line react/no-access-state-in-setstate
data: data(this.state.data) || {}

@@ -309,3 +310,4 @@ });

if ("formatResult" in _options) {
if ('formatResult' in _options) {
// eslint-disable-next-line prefer-destructuring
formatResult = _options.formatResult;

@@ -329,2 +331,3 @@ }

setFeches(function (s) {
// eslint-disable-next-line no-param-reassign
s[key] = data;

@@ -386,2 +389,3 @@ return _objectSpread({}, s);

setFeches(function (s) {
// eslint-disable-next-line no-param-reassign
s[currentFetchKey] = currentFetch;

@@ -388,0 +392,0 @@ return _objectSpread({}, s);

import { LoadMoreParams, LoadMoreOptionsWithFormat, LoadMoreResult, LoadMoreFormatReturn, LoadMoreOptions } from './types';
declare function useLoadMore<R, Item, U extends Item = any>(service: (...p: LoadMoreParams) => Promise<R>, options: LoadMoreOptionsWithFormat<R, Item, U>): LoadMoreResult<Item>;
declare function useLoadMore<R, Item, U extends Item = any>(service: (...p: LoadMoreParams) => Promise<LoadMoreFormatReturn<Item>>, options: LoadMoreOptions<U>): LoadMoreResult<Item>;
declare function useLoadMore<R extends LoadMoreFormatReturn, RR>(service: (...p: LoadMoreParams<R>) => Promise<RR>, options: LoadMoreOptionsWithFormat<R, RR>): LoadMoreResult<R>;
declare function useLoadMore<R extends LoadMoreFormatReturn, RR extends R = any>(service: (...p: LoadMoreParams<RR>) => Promise<R>, options: LoadMoreOptions<RR>): LoadMoreResult<R>;
export default useLoadMore;

@@ -29,4 +29,4 @@ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

import { useRef, useCallback, useMemo, useEffect, useState } from 'react';
import useAsync from './useAsync';
import { useRef, useCallback, useMemo, useEffect, useState } from 'react';
import useUpdateEffect from './utils/useUpdateEffect';

@@ -37,11 +37,8 @@

refreshDeps = _options$refreshDeps === void 0 ? [] : _options$refreshDeps,
fetchKey = options.fetchKey,
restOptions = _objectWithoutProperties(options, ["refreshDeps", "fetchKey"]);
ref = options.ref,
isNoMore = options.isNoMore,
_options$threshold = options.threshold,
threshold = _options$threshold === void 0 ? 100 : _options$threshold,
restOptions = _objectWithoutProperties(options, ["refreshDeps", "ref", "isNoMore", "threshold"]);
useEffect(function () {
if (fetchKey) {
console.warn("useRequest loadMore's fetchKey will not work!");
}
}, []);
var _useState = useState(false),

@@ -52,19 +49,22 @@ _useState2 = _slicedToArray(_useState, 2),

var _useAsync = useAsync(service, _objectSpread({}, restOptions, {
fetchKey: function fetchKey(nextId) {
// 仅仅为了让 nextId 变成字符串,保证 object 的顺序
return nextId + '-';
},
useEffect(function () {
if (!options.fetchKey) {
console.error('useRequest loadMore must have fetchKey!');
}
}, []);
var result = useAsync(service, _objectSpread({}, restOptions, {
onSuccess: function onSuccess() {
setLoadingMore(false);
if (options.onSuccess) {
options.onSuccess.apply(options, arguments);
}
}
})),
data = _useAsync.data,
run = _useAsync.run,
params = _useAsync.params,
reset = _useAsync.reset,
loading = _useAsync.loading,
fetches = _useAsync.fetches,
rest = _objectWithoutProperties(_useAsync, ["data", "run", "params", "reset", "loading", "fetches"]);
}));
var data = result.data,
run = result.run,
params = result.params,
reset = result.reset,
loading = result.loading,
fetches = result.fetches;
var reload = useCallback(function () {

@@ -74,6 +74,5 @@ reset();

var _params = _toArray(params),
_ = _params[0],
rest = _params.slice(1);
restParams = _params.slice(1);
run.apply(void 0, [undefined].concat(_toConsumableArray(rest)));
run.apply(void 0, [undefined].concat(_toConsumableArray(restParams)));
}, [run, reset, params]);

@@ -91,3 +90,5 @@ var reloadRef = useRef(reload);

var dataGroup = useMemo(function () {
var listGroup = [];
var listGroup = []; // 在 loadMore 时,不希望清空上一次的 data。需要把最后一个 非 loading 的请求 data,放回去。
var lastNoLoadingData = data;
Object.values(fetches).forEach(function (h) {

@@ -101,28 +102,59 @@ var _h$data;

}
if (!h.loading) {
lastNoLoadingData = h.data;
}
});
return _objectSpread({}, data, {
return _objectSpread({}, lastNoLoadingData, {
list: listGroup
});
}, [fetches, data]);
var noMore = isNoMore ? !loading && !loadingMore && isNoMore(dataGroup) : false;
var loadMore = useCallback(function () {
if (noMore) {
return;
}
setLoadingMore(true);
var _params2 = _toArray(params),
_ = _params2[0],
rest = _params2.slice(1);
restParams = _params2.slice(1);
run.apply(void 0, [data.nextId].concat(_toConsumableArray(rest)));
}, [run, data, params]);
return _objectSpread({
run.apply(void 0, [dataGroup].concat(_toConsumableArray(restParams)));
}, [noMore, run, dataGroup, params]);
/* 上拉加载的方法 */
var scrollMethod = useCallback(function () {
if (loading || !ref || !ref.current) {
return;
}
if (ref.current.scrollHeight - ref.current.scrollTop <= ref.current.clientHeight + threshold) {
loadMore();
}
}, [loading, ref, loadMore]);
/* 如果有 ref,则会上拉加载更多 */
useEffect(function () {
if (!ref || !ref.current) {
return function () {};
}
ref.current.addEventListener('scroll', scrollMethod);
return function () {
if (ref && ref.current) {
ref.current.removeEventListener('scroll', scrollMethod);
}
};
}, [scrollMethod]);
return _objectSpread({}, result, {
data: dataGroup,
run: run,
params: params,
reset: reset,
reload: reload,
loading: loading && dataGroup.list.length === 0,
loadMore: loadMore,
loadingMore: loadingMore,
fetches: fetches
}, rest);
noMore: noMore
});
}
export default useLoadMore;
import { BasePaginatedOptions, PaginatedFormatReturn, PaginatedOptionsWithFormat, PaginatedParams, PaginatedResult } from './types';
declare function usePaginated<R, Item, U extends Item = any>(service: (...p: PaginatedParams<U>) => Promise<R>, options: PaginatedOptionsWithFormat<R, Item, U>): PaginatedResult<Item>;
declare function usePaginated<R, Item, U extends Item = any>(service: (...p: PaginatedParams<U>) => Promise<PaginatedFormatReturn<Item>>, options: BasePaginatedOptions<U>): PaginatedResult<Item>;
declare function usePaginated<R, Item, U extends Item = any>(service: (...p: PaginatedParams) => Promise<R>, options: PaginatedOptionsWithFormat<R, Item, U>): PaginatedResult<Item>;
declare function usePaginated<R, Item, U extends Item = any>(service: (...p: PaginatedParams) => Promise<PaginatedFormatReturn<Item>>, options: BasePaginatedOptions<U>): PaginatedResult<Item>;
export default usePaginated;

@@ -29,4 +29,2 @@ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

var isEqual = require('lodash.isequal');
function usePaginated(service, options) {

@@ -43,3 +41,3 @@ var paginated = options.paginated,

if (fetchKey) {
console.warn("useRequest pagination's fetchKey will not work!");
console.error('useRequest pagination\'s fetchKey will not work!');
}

@@ -74,5 +72,5 @@ }, []);

oldPaginationParams = _params[0],
rest = _params.slice(1);
restParams = _params.slice(1);
run.apply(void 0, [_objectSpread({}, oldPaginationParams, {}, paginationParams)].concat(_toConsumableArray(rest)));
run.apply(void 0, [_objectSpread({}, oldPaginationParams, {}, paginationParams)].concat(_toConsumableArray(restParams)));
}, [run, params]);

@@ -79,0 +77,0 @@ var total = (data === null || data === void 0 ? void 0 : data.total) || 0;

@@ -5,3 +5,3 @@ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

@@ -8,0 +8,0 @@ import request from 'umi-request';

@@ -1,3 +0,32 @@

export declare type PaginationConfig = any;
/// <reference types="react" />
export interface PaginationConfig {
total?: number;
defaultCurrent?: number;
disabled?: boolean;
current?: number;
defaultPageSize?: number;
pageSize?: number;
onChange?: (page: number, pageSize?: number) => void;
hideOnSinglePage?: boolean;
showSizeChanger?: boolean;
pageSizeOptions?: string[];
onShowSizeChange?: (current: number, size: number) => void;
showQuickJumper?: boolean | {
goButton?: React.ReactNode;
};
showTotal?: (total: number, range: [number, number]) => React.ReactNode;
size?: string;
simple?: boolean;
style?: React.CSSProperties;
locale?: Object;
className?: string;
prefixCls?: string;
selectPrefixCls?: string;
itemRender?: (page: number, type: 'page' | 'prev' | 'next' | 'jump-prev' | 'jump-next', originalElement: React.ReactElement<HTMLElement>) => React.ReactNode;
role?: string;
showLessItems?: boolean;
position?: 'top' | 'bottom' | 'both';
[key: string]: any;
}
export declare type Sorter = any;
export declare type Filter = any;

@@ -8,8 +8,8 @@ /// <reference types="react" />

declare function useRequest<R = any, P extends any[] = any>(service: CombineService<R, P>, options?: BaseOptions<R, P>): BaseResult<R, P>;
declare function useRequest<R = any, Item = any, U extends Item = any>(service: CombineService<R, LoadMoreParams>, options: LoadMoreOptionsWithFormat<R, Item, U>): LoadMoreResult<Item>;
declare function useRequest<R = any, Item = any, U extends Item = any>(service: CombineService<LoadMoreFormatReturn<Item>, LoadMoreParams>, options: LoadMoreOptions<U>): LoadMoreResult<Item>;
declare function useRequest<R = any, Item = any, U extends Item = any>(service: CombineService<R, PaginatedParams<U>>, options: PaginatedOptionsWithFormat<R, Item, U>): PaginatedResult<Item>;
declare function useRequest<R = any, Item = any, U extends Item = any>(service: CombineService<PaginatedFormatReturn<Item>, PaginatedParams<U>>, options: BasePaginatedOptions<U>): PaginatedResult<Item>;
declare function useRequest<R extends LoadMoreFormatReturn, RR>(service: CombineService<RR, LoadMoreParams<R>>, options: LoadMoreOptionsWithFormat<R, RR>): LoadMoreResult<R>;
declare function useRequest<R extends LoadMoreFormatReturn, RR extends R>(service: CombineService<R, LoadMoreParams<R>>, options: LoadMoreOptions<RR>): LoadMoreResult<R>;
declare function useRequest<R = any, Item = any, U extends Item = any>(service: CombineService<R, PaginatedParams>, options: PaginatedOptionsWithFormat<R, Item, U>): PaginatedResult<Item>;
declare function useRequest<R = any, Item = any, U extends Item = any>(service: CombineService<PaginatedFormatReturn<Item>, PaginatedParams>, options: BasePaginatedOptions<U>): PaginatedResult<Item>;
declare const UseAPIProvider: import("react").Provider<BaseOptions<any, any> | OptionsWithFormat<any, any, any, any> | BasePaginatedOptions<any> | LoadMoreOptions<any>>;
export { useAsync, usePaginated, useLoadMore, UseAPIProvider };
export default useRequest;

@@ -44,3 +44,3 @@ "use strict";

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

@@ -66,7 +66,7 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

if (paginatedRef.current !== paginated) {
throw Error('You should not modify this paginated of options');
throw Error('You should not modify the paginated of options');
}
if (loadMoreRef.current !== loadMore) {
throw Error('You should not modify this loadMore of options');
throw Error('You should not modify the loadMore of options');
}

@@ -128,7 +128,9 @@

return (0, _useLoadMore.default)(promiseService, finalOptions);
} else if (paginated) {
}
if (paginated) {
return (0, _usePaginated.default)(promiseService, finalOptions);
} else {
return (0, _useAsync.default)(promiseService, finalOptions);
}
return (0, _useAsync.default)(promiseService, finalOptions);
}

@@ -135,0 +137,0 @@

@@ -0,7 +1,7 @@

import { DependencyList, RefObject } from 'react';
import { PaginationConfig, Filter, Sorter } from './antdTypes';
import { DependencyList } from 'react';
export declare type noop = (...args: any[]) => void;
export declare type Service<R, P extends any[]> = (...args: P) => Promise<R>;
export declare type Subscribe<R, P extends any[]> = (data: FetchResult<R, P>) => void;
export declare type Mutate<R> = (x: (data: R) => R) => void | ((data: R) => void);
export declare type Mutate<R> = (x: (R | ((data: R) => R))) => void;
export declare type RequestService = string | {

@@ -67,3 +67,3 @@ [key: string]: any;

export declare type Options<R, P extends any[], U, UU extends U> = BaseOptions<R, P> | OptionsWithFormat<R, P, U, UU>;
export declare type PaginatedParams<Item> = [{
export declare type PaginatedParams = [{
current: number;

@@ -79,3 +79,3 @@ pageSize: number;

}
export interface PaginatedResult<Item> extends BaseResult<PaginatedFormatReturn<Item>, PaginatedParams<Item>> {
export interface PaginatedResult<Item> extends BaseResult<PaginatedFormatReturn<Item>, PaginatedParams> {
pagination: {

@@ -101,7 +101,7 @@ current: number;

}
export interface BasePaginatedOptions<U> extends Omit<BaseOptions<PaginatedFormatReturn<U>, PaginatedParams<U>>, 'paginated'> {
export interface BasePaginatedOptions<U> extends Omit<BaseOptions<PaginatedFormatReturn<U>, PaginatedParams>, 'paginated'> {
paginated: true;
defaultPageSize?: number;
}
export interface PaginatedOptionsWithFormat<R, Item, U> extends Omit<BaseOptions<PaginatedFormatReturn<U>, PaginatedParams<U>>, 'paginated'> {
export interface PaginatedOptionsWithFormat<R, Item, U> extends Omit<BaseOptions<PaginatedFormatReturn<U>, PaginatedParams>, 'paginated'> {
paginated: true;

@@ -111,17 +111,27 @@ defaultPageSize?: number;

}
export declare type LoadMoreParams = any[];
export interface LoadMoreFormatReturn<Item> {
list: Item[];
nextId?: string;
export declare type LoadMoreParams<R> = [R | undefined, ...any[]];
export interface LoadMoreFormatReturn {
list: any[];
[key: string]: any;
}
export interface LoadMoreResult<Item> extends BaseResult<LoadMoreFormatReturn<Item>, LoadMoreParams> {
export interface LoadMoreResult<R> extends BaseResult<R, LoadMoreParams<R>> {
noMore?: boolean;
loadMore: () => void;
reload: () => void;
loadingMore: boolean;
}
export interface LoadMoreOptions<U> extends Omit<BaseOptions<LoadMoreFormatReturn<U>, LoadMoreParams>, 'loadMore'> {
export interface LoadMoreOptions<R extends LoadMoreFormatReturn> extends Omit<BaseOptions<R, LoadMoreParams<R>>, 'loadMore'> {
loadMore: true;
fetchKey: (...args: LoadMoreParams<R>) => string;
ref?: RefObject<any>;
isNoMore?: (r: R) => boolean;
threshold?: number;
}
export interface LoadMoreOptionsWithFormat<R, Item, U> extends Omit<BaseOptions<LoadMoreFormatReturn<U>, LoadMoreParams>, 'loadMore'> {
export interface LoadMoreOptionsWithFormat<R extends LoadMoreFormatReturn, RR> extends Omit<BaseOptions<R, LoadMoreParams<R>>, 'loadMore'> {
loadMore: true;
formatResult: (data: R) => LoadMoreFormatReturn<Item>;
fetchKey: (...args: LoadMoreParams<R>) => string;
formatResult: (data: RR) => R;
ref?: RefObject<any>;
isNoMore?: (r: R) => boolean;
threshold?: number;
}

@@ -144,3 +144,3 @@ "use strict";

this.setState({
loading: this.config.loadingDelay ? false : true,
loading: !this.config.loadingDelay,
params: args

@@ -270,2 +270,3 @@ });

this.setState({
// eslint-disable-next-line react/no-access-state-in-setstate
data: data(this.state.data) || {}

@@ -328,3 +329,4 @@ });

if ("formatResult" in _options) {
if ('formatResult' in _options) {
// eslint-disable-next-line prefer-destructuring
formatResult = _options.formatResult;

@@ -348,2 +350,3 @@ }

setFeches(function (s) {
// eslint-disable-next-line no-param-reassign
s[key] = data;

@@ -405,2 +408,3 @@ return _objectSpread({}, s);

setFeches(function (s) {
// eslint-disable-next-line no-param-reassign
s[currentFetchKey] = currentFetch;

@@ -407,0 +411,0 @@ return _objectSpread({}, s);

import { LoadMoreParams, LoadMoreOptionsWithFormat, LoadMoreResult, LoadMoreFormatReturn, LoadMoreOptions } from './types';
declare function useLoadMore<R, Item, U extends Item = any>(service: (...p: LoadMoreParams) => Promise<R>, options: LoadMoreOptionsWithFormat<R, Item, U>): LoadMoreResult<Item>;
declare function useLoadMore<R, Item, U extends Item = any>(service: (...p: LoadMoreParams) => Promise<LoadMoreFormatReturn<Item>>, options: LoadMoreOptions<U>): LoadMoreResult<Item>;
declare function useLoadMore<R extends LoadMoreFormatReturn, RR>(service: (...p: LoadMoreParams<R>) => Promise<RR>, options: LoadMoreOptionsWithFormat<R, RR>): LoadMoreResult<R>;
declare function useLoadMore<R extends LoadMoreFormatReturn, RR extends R = any>(service: (...p: LoadMoreParams<RR>) => Promise<R>, options: LoadMoreOptions<RR>): LoadMoreResult<R>;
export default useLoadMore;

@@ -8,6 +8,6 @@ "use strict";

var _useAsync2 = _interopRequireDefault(require("./useAsync"));
var _react = require("react");
var _useAsync = _interopRequireDefault(require("./useAsync"));
var _useUpdateEffect = _interopRequireDefault(require("./utils/useUpdateEffect"));

@@ -48,11 +48,8 @@

refreshDeps = _options$refreshDeps === void 0 ? [] : _options$refreshDeps,
fetchKey = options.fetchKey,
restOptions = _objectWithoutProperties(options, ["refreshDeps", "fetchKey"]);
ref = options.ref,
isNoMore = options.isNoMore,
_options$threshold = options.threshold,
threshold = _options$threshold === void 0 ? 100 : _options$threshold,
restOptions = _objectWithoutProperties(options, ["refreshDeps", "ref", "isNoMore", "threshold"]);
(0, _react.useEffect)(function () {
if (fetchKey) {
console.warn("useRequest loadMore's fetchKey will not work!");
}
}, []);
var _useState = (0, _react.useState)(false),

@@ -63,19 +60,22 @@ _useState2 = _slicedToArray(_useState, 2),

var _useAsync = (0, _useAsync2.default)(service, _objectSpread({}, restOptions, {
fetchKey: function fetchKey(nextId) {
// 仅仅为了让 nextId 变成字符串,保证 object 的顺序
return nextId + '-';
},
(0, _react.useEffect)(function () {
if (!options.fetchKey) {
console.error('useRequest loadMore must have fetchKey!');
}
}, []);
var result = (0, _useAsync.default)(service, _objectSpread({}, restOptions, {
onSuccess: function onSuccess() {
setLoadingMore(false);
if (options.onSuccess) {
options.onSuccess.apply(options, arguments);
}
}
})),
data = _useAsync.data,
run = _useAsync.run,
params = _useAsync.params,
reset = _useAsync.reset,
loading = _useAsync.loading,
fetches = _useAsync.fetches,
rest = _objectWithoutProperties(_useAsync, ["data", "run", "params", "reset", "loading", "fetches"]);
}));
var data = result.data,
run = result.run,
params = result.params,
reset = result.reset,
loading = result.loading,
fetches = result.fetches;
var reload = (0, _react.useCallback)(function () {

@@ -85,6 +85,5 @@ reset();

var _params = _toArray(params),
_ = _params[0],
rest = _params.slice(1);
restParams = _params.slice(1);
run.apply(void 0, [undefined].concat(_toConsumableArray(rest)));
run.apply(void 0, [undefined].concat(_toConsumableArray(restParams)));
}, [run, reset, params]);

@@ -102,3 +101,5 @@ var reloadRef = (0, _react.useRef)(reload);

var dataGroup = (0, _react.useMemo)(function () {
var listGroup = [];
var listGroup = []; // 在 loadMore 时,不希望清空上一次的 data。需要把最后一个 非 loading 的请求 data,放回去。
var lastNoLoadingData = data;
Object.values(fetches).forEach(function (h) {

@@ -112,26 +113,57 @@ var _h$data;

}
if (!h.loading) {
lastNoLoadingData = h.data;
}
});
return _objectSpread({}, data, {
return _objectSpread({}, lastNoLoadingData, {
list: listGroup
});
}, [fetches, data]);
var noMore = isNoMore ? !loading && !loadingMore && isNoMore(dataGroup) : false;
var loadMore = (0, _react.useCallback)(function () {
if (noMore) {
return;
}
setLoadingMore(true);
var _params2 = _toArray(params),
_ = _params2[0],
rest = _params2.slice(1);
restParams = _params2.slice(1);
run.apply(void 0, [data.nextId].concat(_toConsumableArray(rest)));
}, [run, data, params]);
return _objectSpread({
run.apply(void 0, [dataGroup].concat(_toConsumableArray(restParams)));
}, [noMore, run, dataGroup, params]);
/* 上拉加载的方法 */
var scrollMethod = (0, _react.useCallback)(function () {
if (loading || !ref || !ref.current) {
return;
}
if (ref.current.scrollHeight - ref.current.scrollTop <= ref.current.clientHeight + threshold) {
loadMore();
}
}, [loading, ref, loadMore]);
/* 如果有 ref,则会上拉加载更多 */
(0, _react.useEffect)(function () {
if (!ref || !ref.current) {
return function () {};
}
ref.current.addEventListener('scroll', scrollMethod);
return function () {
if (ref && ref.current) {
ref.current.removeEventListener('scroll', scrollMethod);
}
};
}, [scrollMethod]);
return _objectSpread({}, result, {
data: dataGroup,
run: run,
params: params,
reset: reset,
reload: reload,
loading: loading && dataGroup.list.length === 0,
loadMore: loadMore,
loadingMore: loadingMore,
fetches: fetches
}, rest);
noMore: noMore
});
}

@@ -138,0 +170,0 @@

import { BasePaginatedOptions, PaginatedFormatReturn, PaginatedOptionsWithFormat, PaginatedParams, PaginatedResult } from './types';
declare function usePaginated<R, Item, U extends Item = any>(service: (...p: PaginatedParams<U>) => Promise<R>, options: PaginatedOptionsWithFormat<R, Item, U>): PaginatedResult<Item>;
declare function usePaginated<R, Item, U extends Item = any>(service: (...p: PaginatedParams<U>) => Promise<PaginatedFormatReturn<Item>>, options: BasePaginatedOptions<U>): PaginatedResult<Item>;
declare function usePaginated<R, Item, U extends Item = any>(service: (...p: PaginatedParams) => Promise<R>, options: PaginatedOptionsWithFormat<R, Item, U>): PaginatedResult<Item>;
declare function usePaginated<R, Item, U extends Item = any>(service: (...p: PaginatedParams) => Promise<PaginatedFormatReturn<Item>>, options: BasePaginatedOptions<U>): PaginatedResult<Item>;
export default usePaginated;

@@ -40,4 +40,2 @@ "use strict";

var isEqual = require('lodash.isequal');
function usePaginated(service, options) {

@@ -54,3 +52,3 @@ var paginated = options.paginated,

if (fetchKey) {
console.warn("useRequest pagination's fetchKey will not work!");
console.error('useRequest pagination\'s fetchKey will not work!');
}

@@ -85,5 +83,5 @@ }, []);

oldPaginationParams = _params[0],
rest = _params.slice(1);
restParams = _params.slice(1);
run.apply(void 0, [_objectSpread({}, oldPaginationParams, {}, paginationParams)].concat(_toConsumableArray(rest)));
run.apply(void 0, [_objectSpread({}, oldPaginationParams, {}, paginationParams)].concat(_toConsumableArray(restParams)));
}, [run, params]);

@@ -90,0 +88,0 @@ var total = (data === null || data === void 0 ? void 0 : data.total) || 0;

@@ -18,3 +18,3 @@ "use strict";

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

@@ -21,0 +21,0 @@ function useRequest(service, options) {

{
"name": "@umijs/use-request",
"version": "1.0.0",
"version": "1.1.0-beta.1",
"description": "React Hooks for fetching, caching and updating asynchronous data",

@@ -8,7 +8,2 @@ "main": "./lib/index.js",

"types": "./lib/index.d.ts",
"scripts": {
"build": "father build && tsc --declarationDir ./lib && tsc --declarationDir ./es",
"test": "father test",
"precommit": "father pre-commit"
},
"files": [

@@ -18,4 +13,2 @@ "dist",

"es",
"README.md",
"LICENSE",
"package.json"

@@ -25,24 +18,18 @@ ],

"type": "git",
"url": "git+https://github.com/umijs/use-request.git"
"url": "git+https://github.com/umijs/hooks.git"
},
"scripts": {
"compile": "tsc --declarationDir ./lib && tsc --declarationDir ./es"
},
"publishConfig": {
"registry": "https://registry.npmjs.com/"
},
"author": "",
"license": "ISC",
"license": "MIT",
"bugs": {
"url": "https://github.com/umijs/use-request/issues"
"url": "https://github.com/umijs/hooks/issues"
},
"homepage": "https://github.com/umijs/use-request#readme",
"homepage": "https://github.com/umijs/hooks",
"peerDependencies": {
"react": "^16.8.6"
},
"devDependencies": {
"@types/jest": "^24.0.23",
"@types/lodash.debounce": "^4.0.6",
"@types/lodash.throttle": "^4.1.6",
"father": "^2.29.1",
"react": "^16.12.0",
"typescript": "^3.7.2"
},
"dependencies": {

@@ -52,3 +39,8 @@ "lodash.debounce": "^4.0.8",

"umi-request": "^1.2.17"
}
},
"devDependencies": {
"@umijs/hooks": "^1.6.0-beta.1",
"axios": "^0.19.2"
},
"gitHead": "21435f47cbe4f9241863ae005b8afedaed02a106"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc