@aspida/swr
Version:
SWR wrapper for aspida
29 lines • 1.39 kB
JavaScript
import useSWR from 'swr';
function getAspidaSWRDefaultKey(api, maybeMethod, ...option) {
const method = typeof maybeMethod === 'string' ? maybeMethod : '$get';
const opt = typeof maybeMethod === 'string' ? option[0] : maybeMethod;
return [api.$path(opt), method];
}
export { getAspidaSWRDefaultKey as getSWRDefaultKey };
function useAspidaSWR(api, maybeMethod, ...option) {
var _a;
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
? getAspidaSWRDefaultKey(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);
return useSWR(key, fetcher, opt);
}
export default useAspidaSWR;
//# sourceMappingURL=index.js.map