@aspida/swrv
Version:
SWRV wrapper for aspida
32 lines • 1.5 kB
JavaScript
import useSWRV from 'swrv';
function getAspidaSWRVDefaultKey(api, maybeMethod, ...option) {
const method = typeof maybeMethod === 'string' ? maybeMethod : '$get';
const opt = typeof maybeMethod === 'string' ? option[0] : maybeMethod;
return [api.$path(opt), method];
}
export { getAspidaSWRVDefaultKey };
function useAspidaSWRV(api, maybeMethod, ...option) {
var _a;
if (typeof api === 'function')
api = api();
const method = typeof maybeMethod === 'string' ? maybeMethod : '$get';
const opt = typeof maybeMethod === 'string' ? option[0] : maybeMethod;
const enabled = (_a = opt === null || opt === void 0 ? void 0 : opt.enabled) !== null && _a !== void 0 ? _a : true;
const key = (opt === null || opt === void 0 ? void 0 : opt.key) !== undefined
? typeof opt.key === 'function'
? opt.key()
: opt.key
: enabled
? getAspidaSWRVDefaultKey(api, method, opt)
: null;
const fetcherInterv = (opt === null || opt === void 0 ? void 0 : opt.fetcher) === undefined ? (enabled ? (f) => f : null) : opt === null || opt === void 0 ? void 0 : opt.fetcher;
const fetcher = !api || typeof fetcherInterv !== 'function'
? null
: (api => () => {
return fetcherInterv(api[method])(opt);
})(api);
// TODO: Why as any? https://github.com/Kong/swrv/pull/274.
return useSWRV(key, fetcher, opt);
}
export default useAspidaSWRV;
//# sourceMappingURL=index.js.map