@gravity-ui/data-source
Version:
A wrapper around data fetching
42 lines • 1.87 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/helpers/regeneratorRuntime";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import { skipToken } from '@tanstack/react-query';
import { composeFullKey, idle } from '../../../core';
import { formatNullableValue } from '../../utils/formatNullableValue';
import { parseNullableValue } from '../../utils/parseNullableValue';
export var composeOptions = function composeOptions(context, dataSource, params, options) {
var transformParams = dataSource.transformParams,
transformResponse = dataSource.transformResponse;
var queryFn = /*#__PURE__*/function () {
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(fetchContext) {
var response;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return dataSource.fetch(context, fetchContext, transformParams ? transformParams(params) : params);
case 2:
response = _context.sent;
return _context.abrupt("return", formatNullableValue(response));
case 4:
case "end":
return _context.stop();
}
}, _callee);
}));
return function queryFn(_x) {
return _ref.apply(this, arguments);
};
}();
var select = function select(response) {
var actualResponse = parseNullableValue(response);
return transformResponse ? transformResponse(actualResponse) : actualResponse;
};
return _objectSpread(_objectSpread({
queryKey: composeFullKey(dataSource, params),
queryFn: params === idle ? skipToken : queryFn,
select: select
}, dataSource.options), options);
};
// #sourceMappingURL=utils.js.map