@aspida/swr
Version:
SWR wrapper for aspida
35 lines • 1.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSWRDefaultKey = void 0;
const swr_1 = __importDefault(require("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];
}
exports.getSWRDefaultKey = getAspidaSWRDefaultKey;
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 (0, swr_1.default)(key, fetcher, opt);
}
exports.default = useAspidaSWR;
//# sourceMappingURL=index.js.map