@aspida/swrv
Version:
SWRV wrapper for aspida
38 lines • 1.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAspidaSWRVDefaultKey = void 0;
const swrv_1 = __importDefault(require("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];
}
exports.getAspidaSWRVDefaultKey = 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 (0, swrv_1.default)(key, fetcher, opt);
}
exports.default = useAspidaSWRV;
//# sourceMappingURL=index.js.map