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

ahooks

Package Overview
Dependencies
Maintainers
5
Versions
111
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ahooks - npm Package Compare versions

Comparing version 3.0.0-alpha.13 to 3.0.0-alpha.14

5

es/useRequest/src/Fetch.d.ts
import type { MutableRefObject } from 'react';
import type { Options, FetchState, PluginReturn, Service, Subscribe } from './types';
import type { FetchState, Options, PluginReturn, Service, Subscribe } from './types';
export default class Fetch<TData, TParams extends any[]> {

@@ -7,6 +7,7 @@ serviceRef: MutableRefObject<Service<TData, TParams>>;

subscribe: Subscribe;
initState: Partial<FetchState<TData, TParams>>;
pluginImpls: PluginReturn<TData, TParams>[];
count: number;
state: FetchState<TData, TParams>;
constructor(serviceRef: MutableRefObject<Service<TData, TParams>>, options: Options<TData, TParams>, subscribe: Subscribe);
constructor(serviceRef: MutableRefObject<Service<TData, TParams>>, options: Options<TData, TParams>, subscribe: Subscribe, initState?: Partial<FetchState<TData, TParams>>);
setState(s?: Partial<FetchState<TData, TParams>>): void;

@@ -13,0 +14,0 @@ runPluginHandler(event: keyof PluginReturn<TData, TParams>, ...rest: any[]): any;

11

es/useRequest/src/Fetch.js

@@ -211,6 +211,11 @@ var __assign = this && this.__assign || function () {

function () {
function Fetch(serviceRef, options, subscribe) {
function Fetch(serviceRef, options, subscribe, initState) {
if (initState === void 0) {
initState = {};
}
this.serviceRef = serviceRef;
this.options = options;
this.subscribe = subscribe;
this.initState = initState;
this.count = 0;

@@ -223,5 +228,5 @@ this.state = {

};
this.state = __assign(__assign({}, this.state), {
this.state = __assign(__assign(__assign({}, this.state), {
loading: !options.manual
});
}), initState);
}

@@ -228,0 +233,0 @@

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

import type { Plugin } from '../types';
declare const useReadyPlugin: Plugin<any, any[]>;
export default useReadyPlugin;

@@ -1,20 +0,69 @@

// import useUpdateEffect from '../../../useUpdateEffect';
// import type { Plugin } from '../types';
// // TODO 等待重新确定 ready 逻辑,暂时不支持
// const useReadyPlugin: Plugin<any, any[]> = (fetchInstance, { manual, ready = true }) => {
// useUpdateEffect(() => {
// if (!manual && ready) {
// fetchInstance.refresh();
// }
// }, [ready]);
// return {
// onBefore: () => {
// if (!ready) {
// return {
// stopNow: true,
// };
// }
// },
// };
// };
// export default useReadyPlugin;
var __read = this && this.__read || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o),
r,
ar = [],
e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) {
ar.push(r.value);
}
} catch (error) {
e = {
error: error
};
} finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
} finally {
if (e) throw e.error;
}
}
return ar;
};
var __spread = this && this.__spread || function () {
for (var ar = [], i = 0; i < arguments.length; i++) {
ar = ar.concat(__read(arguments[i]));
}
return ar;
};
import useUpdateEffect from '../../../useUpdateEffect';
var useReadyPlugin = function useReadyPlugin(fetchInstance, _a) {
var manual = _a.manual,
_b = _a.ready,
ready = _b === void 0 ? true : _b,
_c = _a.defaultParams,
defaultParams = _c === void 0 ? [] : _c;
useUpdateEffect(function () {
if (!manual && ready) {
fetchInstance.run.apply(fetchInstance, __spread(defaultParams));
}
}, [ready]);
return {
onBefore: function onBefore() {
if (!ready) {
return {
stopNow: true
};
}
}
};
};
useReadyPlugin.onInit = function (_a) {
var _b = _a.ready,
ready = _b === void 0 ? true : _b,
manual = _a.manual;
return {
loading: !manual && ready
};
};
export default useReadyPlugin;

@@ -50,4 +50,8 @@ import type { DependencyList } from 'react';

retryInterval?: number;
ready?: boolean;
}
export declare type Plugin<TData, TParams extends any[]> = (fetchInstance: Fetch<TData, TParams>, options: Options<TData, TParams>) => PluginReturn<TData, TParams>;
export declare type Plugin<TData, TParams extends any[]> = {
(fetchInstance: Fetch<TData, TParams>, options: Options<TData, TParams>): PluginReturn<TData, TParams>;
onInit?: (options: Options<TData, TParams>) => Partial<FetchState<TData, TParams>>;
};
export interface Result<TData, TParams extends any[]> {

@@ -54,0 +58,0 @@ loading: boolean;

@@ -36,10 +36,11 @@ var __read = this && this.__read || function (o, n) {

import useCachePlugin from './plugins/useCachePlugin';
import useDebouncePlugin from './plugins/useDebouncePlugin';
import useLoadingDelayPlugin from './plugins/useLoadingDelayPlugin';
import usePollingPlugin from './plugins/usePollingPlugin';
import useReadyPlugin from './plugins/useReadyPlugin';
import useRefreshDeps from './plugins/useRefreshDeps';
import useRefreshOnWindowFocusPlugin from './plugins/useRefreshOnWindowFocusPlugin';
import useRetryPlugin from './plugins/useRetryPlugin';
import useThrottlePlugin from './plugins/useThrottlePlugin';
import useRefreshDeps from './plugins/useRefreshDeps';
import useCachePlugin from './plugins/useCachePlugin';
import useRetryPlugin from './plugins/useRetryPlugin';
import useRequestImplement from './useRequestImplement'; // function useRequest<TData, TParams extends any[], TFormated, TTFormated extends TFormated = any>(

@@ -57,5 +58,5 @@ // service: Service<TData, TParams>,

function useRequest(service, options, plugins) {
return useRequestImplement(service, options, __spread(plugins || [], [useDebouncePlugin, useLoadingDelayPlugin, usePollingPlugin, useRefreshOnWindowFocusPlugin, useThrottlePlugin, useRefreshDeps, useCachePlugin, useRetryPlugin]));
return useRequestImplement(service, options, __spread(plugins || [], [useDebouncePlugin, useLoadingDelayPlugin, usePollingPlugin, useRefreshOnWindowFocusPlugin, useThrottlePlugin, useRefreshDeps, useCachePlugin, useRetryPlugin, useReadyPlugin]));
}
export default useRequest;

@@ -79,16 +79,6 @@ var __assign = this && this.__assign || function () {

manual = _a === void 0 ? false : _a,
defaultParams = options.defaultParams,
onSuccess = options.onSuccess,
onError = options.onError,
onBefore = options.onBefore,
onFinally = options.onFinally,
// formatResult = v => v,
rest = __rest(options, ["manual", "defaultParams", "onSuccess", "onError", "onBefore", "onFinally"]);
rest = __rest(options, ["manual"]);
var fetchOptions = __assign({
manual: manual,
onSuccess: onSuccess,
onError: onError,
onBefore: onBefore,
onFinally: onFinally
manual: manual
}, rest);

@@ -99,3 +89,8 @@

var fetchInstance = useCreation(function () {
return new Fetch(serviceRef, fetchOptions, update);
var initState = plugins.map(function (p) {
var _a;
return (_a = p === null || p === void 0 ? void 0 : p.onInit) === null || _a === void 0 ? void 0 : _a.call(p, fetchOptions);
}).filter(Boolean);
return new Fetch(serviceRef, fetchOptions, update, Object.assign.apply(Object, __spread([{}], initState)));
}, []);

@@ -110,3 +105,3 @@ fetchInstance.options = fetchOptions; // run all plugins hooks

// useCachePlugin can set fetchInstance.state.params from cache when init
var params = fetchInstance.state.params || defaultParams || []; // @ts-ignore
var params = fetchInstance.state.params || options.defaultParams || []; // @ts-ignore

@@ -113,0 +108,0 @@ fetchInstance.run.apply(fetchInstance, __spread(params));

import type { MutableRefObject } from 'react';
import type { Options, FetchState, PluginReturn, Service, Subscribe } from './types';
import type { FetchState, Options, PluginReturn, Service, Subscribe } from './types';
export default class Fetch<TData, TParams extends any[]> {

@@ -7,6 +7,7 @@ serviceRef: MutableRefObject<Service<TData, TParams>>;

subscribe: Subscribe;
initState: Partial<FetchState<TData, TParams>>;
pluginImpls: PluginReturn<TData, TParams>[];
count: number;
state: FetchState<TData, TParams>;
constructor(serviceRef: MutableRefObject<Service<TData, TParams>>, options: Options<TData, TParams>, subscribe: Subscribe);
constructor(serviceRef: MutableRefObject<Service<TData, TParams>>, options: Options<TData, TParams>, subscribe: Subscribe, initState?: Partial<FetchState<TData, TParams>>);
setState(s?: Partial<FetchState<TData, TParams>>): void;

@@ -13,0 +14,0 @@ runPluginHandler(event: keyof PluginReturn<TData, TParams>, ...rest: any[]): any;

@@ -217,6 +217,11 @@ "use strict";

function () {
function Fetch(serviceRef, options, subscribe) {
function Fetch(serviceRef, options, subscribe, initState) {
if (initState === void 0) {
initState = {};
}
this.serviceRef = serviceRef;
this.options = options;
this.subscribe = subscribe;
this.initState = initState;
this.count = 0;

@@ -229,5 +234,5 @@ this.state = {

};
this.state = __assign(__assign({}, this.state), {
this.state = __assign(__assign(__assign({}, this.state), {
loading: !options.manual
});
}), initState);
}

@@ -234,0 +239,0 @@

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

import type { Plugin } from '../types';
declare const useReadyPlugin: Plugin<any, any[]>;
export default useReadyPlugin;

@@ -1,20 +0,81 @@

// import useUpdateEffect from '../../../useUpdateEffect';
// import type { Plugin } from '../types';
// // TODO 等待重新确定 ready 逻辑,暂时不支持
// const useReadyPlugin: Plugin<any, any[]> = (fetchInstance, { manual, ready = true }) => {
// useUpdateEffect(() => {
// if (!manual && ready) {
// fetchInstance.refresh();
// }
// }, [ready]);
// return {
// onBefore: () => {
// if (!ready) {
// return {
// stopNow: true,
// };
// }
// },
// };
// };
// export default useReadyPlugin;
"use strict";
var __read = this && this.__read || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o),
r,
ar = [],
e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) {
ar.push(r.value);
}
} catch (error) {
e = {
error: error
};
} finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
} finally {
if (e) throw e.error;
}
}
return ar;
};
var __spread = this && this.__spread || function () {
for (var ar = [], i = 0; i < arguments.length; i++) {
ar = ar.concat(__read(arguments[i]));
}
return ar;
};
var __importDefault = this && this.__importDefault || function (mod) {
return mod && mod.__esModule ? mod : {
"default": mod
};
};
Object.defineProperty(exports, "__esModule", {
value: true
});
var useUpdateEffect_1 = __importDefault(require("../../../useUpdateEffect"));
var useReadyPlugin = function useReadyPlugin(fetchInstance, _a) {
var manual = _a.manual,
_b = _a.ready,
ready = _b === void 0 ? true : _b,
_c = _a.defaultParams,
defaultParams = _c === void 0 ? [] : _c;
useUpdateEffect_1["default"](function () {
if (!manual && ready) {
fetchInstance.run.apply(fetchInstance, __spread(defaultParams));
}
}, [ready]);
return {
onBefore: function onBefore() {
if (!ready) {
return {
stopNow: true
};
}
}
};
};
useReadyPlugin.onInit = function (_a) {
var _b = _a.ready,
ready = _b === void 0 ? true : _b,
manual = _a.manual;
return {
loading: !manual && ready
};
};
exports["default"] = useReadyPlugin;

@@ -50,4 +50,8 @@ import type { DependencyList } from 'react';

retryInterval?: number;
ready?: boolean;
}
export declare type Plugin<TData, TParams extends any[]> = (fetchInstance: Fetch<TData, TParams>, options: Options<TData, TParams>) => PluginReturn<TData, TParams>;
export declare type Plugin<TData, TParams extends any[]> = {
(fetchInstance: Fetch<TData, TParams>, options: Options<TData, TParams>): PluginReturn<TData, TParams>;
onInit?: (options: Options<TData, TParams>) => Partial<FetchState<TData, TParams>>;
};
export interface Result<TData, TParams extends any[]> {

@@ -54,0 +58,0 @@ loading: boolean;

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

var useCachePlugin_1 = __importDefault(require("./plugins/useCachePlugin"));
var useDebouncePlugin_1 = __importDefault(require("./plugins/useDebouncePlugin"));

@@ -55,12 +57,12 @@

var useRefreshOnWindowFocusPlugin_1 = __importDefault(require("./plugins/useRefreshOnWindowFocusPlugin"));
var useReadyPlugin_1 = __importDefault(require("./plugins/useReadyPlugin"));
var useThrottlePlugin_1 = __importDefault(require("./plugins/useThrottlePlugin"));
var useRefreshDeps_1 = __importDefault(require("./plugins/useRefreshDeps"));
var useCachePlugin_1 = __importDefault(require("./plugins/useCachePlugin"));
var useRefreshOnWindowFocusPlugin_1 = __importDefault(require("./plugins/useRefreshOnWindowFocusPlugin"));
var useRetryPlugin_1 = __importDefault(require("./plugins/useRetryPlugin"));
var useThrottlePlugin_1 = __importDefault(require("./plugins/useThrottlePlugin"));
var useRequestImplement_1 = __importDefault(require("./useRequestImplement")); // function useRequest<TData, TParams extends any[], TFormated, TTFormated extends TFormated = any>(

@@ -79,5 +81,5 @@ // service: Service<TData, TParams>,

function useRequest(service, options, plugins) {
return useRequestImplement_1["default"](service, options, __spread(plugins || [], [useDebouncePlugin_1["default"], useLoadingDelayPlugin_1["default"], usePollingPlugin_1["default"], useRefreshOnWindowFocusPlugin_1["default"], useThrottlePlugin_1["default"], useRefreshDeps_1["default"], useCachePlugin_1["default"], useRetryPlugin_1["default"]]));
return useRequestImplement_1["default"](service, options, __spread(plugins || [], [useDebouncePlugin_1["default"], useLoadingDelayPlugin_1["default"], usePollingPlugin_1["default"], useRefreshOnWindowFocusPlugin_1["default"], useThrottlePlugin_1["default"], useRefreshDeps_1["default"], useCachePlugin_1["default"], useRetryPlugin_1["default"], useReadyPlugin_1["default"]]));
}
exports["default"] = useRequest;

@@ -92,16 +92,6 @@ "use strict";

manual = _a === void 0 ? false : _a,
defaultParams = options.defaultParams,
onSuccess = options.onSuccess,
onError = options.onError,
onBefore = options.onBefore,
onFinally = options.onFinally,
// formatResult = v => v,
rest = __rest(options, ["manual", "defaultParams", "onSuccess", "onError", "onBefore", "onFinally"]);
rest = __rest(options, ["manual"]);
var fetchOptions = __assign({
manual: manual,
onSuccess: onSuccess,
onError: onError,
onBefore: onBefore,
onFinally: onFinally
manual: manual
}, rest);

@@ -112,3 +102,8 @@

var fetchInstance = index_1.useCreation(function () {
return new Fetch_1["default"](serviceRef, fetchOptions, update);
var initState = plugins.map(function (p) {
var _a;
return (_a = p === null || p === void 0 ? void 0 : p.onInit) === null || _a === void 0 ? void 0 : _a.call(p, fetchOptions);
}).filter(Boolean);
return new Fetch_1["default"](serviceRef, fetchOptions, update, Object.assign.apply(Object, __spread([{}], initState)));
}, []);

@@ -123,3 +118,3 @@ fetchInstance.options = fetchOptions; // run all plugins hooks

// useCachePlugin can set fetchInstance.state.params from cache when init
var params = fetchInstance.state.params || defaultParams || []; // @ts-ignore
var params = fetchInstance.state.params || options.defaultParams || []; // @ts-ignore

@@ -126,0 +121,0 @@ fetchInstance.run.apply(fetchInstance, __spread(params));

{
"name": "ahooks",
"version": "3.0.0-alpha.13",
"version": "3.0.0-alpha.14",
"description": "react hooks library",

@@ -61,3 +61,3 @@ "keywords": [

"license": "MIT",
"gitHead": "d4e588c1285240a75c7e2864b28fc17440580da3"
"gitHead": "dd8a26f014d442ddba318c657d11e92d72004b8a"
}

Sorry, the diff of this file is too big to display

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