create-api-hooks
Advanced tools
Comparing version 0.0.17 to 0.0.18
@@ -27,2 +27,6 @@ /// <reference types="react" /> | ||
} | ||
export declare const createApiHooksConfig: { | ||
setCacheFn: (name: string, value: any) => any; | ||
getCacheFn: (name: string) => any; | ||
}; | ||
export default function createApiHooks<P, D>(request: (params: P) => Promise<{ | ||
@@ -29,0 +33,0 @@ data: D; |
import { useEffect, useState, useCallback, useRef } from 'react'; | ||
var cacheObj = {}; | ||
var createApiHooksConfig = { | ||
setCacheFn: function (name, value) { return (cacheObj[name] = value); }, | ||
getCacheFn: function (name) { return cacheObj[name]; } | ||
}; | ||
function createApiHooks(request) { | ||
@@ -8,6 +12,9 @@ var getRequestString = function (params) { | ||
}; | ||
var getCache = function (params) { return cacheObj[getRequestString(params)]; }; | ||
var setCacheFn = createApiHooksConfig.setCacheFn, getCacheFn = createApiHooksConfig.getCacheFn; | ||
var Hooks = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.cache, cache = _c === void 0 ? true : _c, _d = _b.initValue, initValue = _d === void 0 ? {} : _d, params = _b.params, _e = _b.needInit, needInit = _e === void 0 ? true : _e, _f = _b.deepCache, deepCache = _f === void 0 ? false : _f; | ||
initValue = cache && needInit ? getCache(params) || initValue : initValue; | ||
initValue = | ||
cache && needInit | ||
? getCacheFn(getRequestString(params)) || initValue | ||
: initValue; | ||
var _g = useState(), error = _g[0], setError = _g[1]; | ||
@@ -27,3 +34,3 @@ var _h = useState(initValue), data = _h[0], setData = _h[1]; | ||
!isOutRequest) || | ||
(deepCache && getCache(params))) | ||
(deepCache && getCacheFn(getRequestString(params)))) | ||
return Promise.resolve(data); | ||
@@ -40,6 +47,3 @@ isOutRequestRef.current = false; | ||
} | ||
cacheObj[getRequestString(params)] = | ||
typeof data !== 'string' | ||
? JSON.parse(JSON.stringify(data)) | ||
: data; | ||
setCacheFn(getRequestString(params), typeof data !== 'string' ? JSON.parse(JSON.stringify(data)) : data); | ||
return Promise.resolve(data); | ||
@@ -90,2 +94,3 @@ }) | ||
export default createApiHooks; | ||
export { createApiHooksConfig }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -8,2 +8,6 @@ 'use strict'; | ||
var cacheObj = {}; | ||
var createApiHooksConfig = { | ||
setCacheFn: function (name, value) { return (cacheObj[name] = value); }, | ||
getCacheFn: function (name) { return cacheObj[name]; } | ||
}; | ||
function createApiHooks(request) { | ||
@@ -13,6 +17,9 @@ var getRequestString = function (params) { | ||
}; | ||
var getCache = function (params) { return cacheObj[getRequestString(params)]; }; | ||
var setCacheFn = createApiHooksConfig.setCacheFn, getCacheFn = createApiHooksConfig.getCacheFn; | ||
var Hooks = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.cache, cache = _c === void 0 ? true : _c, _d = _b.initValue, initValue = _d === void 0 ? {} : _d, params = _b.params, _e = _b.needInit, needInit = _e === void 0 ? true : _e, _f = _b.deepCache, deepCache = _f === void 0 ? false : _f; | ||
initValue = cache && needInit ? getCache(params) || initValue : initValue; | ||
initValue = | ||
cache && needInit | ||
? getCacheFn(getRequestString(params)) || initValue | ||
: initValue; | ||
var _g = react.useState(), error = _g[0], setError = _g[1]; | ||
@@ -32,3 +39,3 @@ var _h = react.useState(initValue), data = _h[0], setData = _h[1]; | ||
!isOutRequest) || | ||
(deepCache && getCache(params))) | ||
(deepCache && getCacheFn(getRequestString(params)))) | ||
return Promise.resolve(data); | ||
@@ -45,6 +52,3 @@ isOutRequestRef.current = false; | ||
} | ||
cacheObj[getRequestString(params)] = | ||
typeof data !== 'string' | ||
? JSON.parse(JSON.stringify(data)) | ||
: data; | ||
setCacheFn(getRequestString(params), typeof data !== 'string' ? JSON.parse(JSON.stringify(data)) : data); | ||
return Promise.resolve(data); | ||
@@ -94,3 +98,4 @@ }) | ||
exports.createApiHooksConfig = createApiHooksConfig; | ||
exports.default = createApiHooks; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "create-api-hooks", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "author": "mushan0x0", |
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
26054
227