UNPKG

@antdp/hooks

Version:
100 lines 2.52 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose"; var _excluded = ["url", "cache", "credentials", "headers", "integrity", "keepalive", "method", "mode", "redirect", "referrer", "referrerPolicy", "signal", "window", "contentType"], _excluded2 = ["url", "cache", "credentials", "headers", "integrity", "keepalive", "method", "mode", "redirect", "referrer", "referrerPolicy", "signal", "window", "contentType"]; // @ts-ignore import { useMutation, useQuery } from '@umijs/max'; import { fetchFn } from './fetch'; export function useReactQuery(options) { var _ref = options || {}, { url = '', cache, credentials = 'include', headers, integrity, keepalive, method = 'GET', mode, redirect, referrer, referrerPolicy, signal, window, contentType = 'json' } = _ref, opts = _objectWithoutPropertiesLoose(_ref, _excluded); var fetchOption = { cache, credentials, headers, integrity, keepalive, method, mode, redirect, referrer, referrerPolicy, signal, window }; var queryOptions = _extends({}, opts); if (url) { var Fn = () => fetchFn(url, _extends({ contentType }, fetchOption)); queryOptions.queryFn = queryOptions.queryFn || Fn; } return useQuery(_extends({}, queryOptions)); } export function useReactMutation(options) { var _ref2 = options || {}, { url, cache, credentials, headers, integrity, keepalive, method = 'POST', mode, redirect, referrer, referrerPolicy, signal, window, contentType = 'json' } = _ref2, opts = _objectWithoutPropertiesLoose(_ref2, _excluded2); var fetchOption = { cache, credentials, headers, integrity, keepalive, method, mode, redirect, referrer, referrerPolicy, signal, window }; var mutationOptions = _extends({}, opts); if (url) { var Fn = newData => { var body = newData; if (Object.prototype.toString.call(newData).slice(8, -1) !== 'FormData') { body = JSON.stringify(newData); } return fetchFn(url, _extends({ contentType }, fetchOption, { body: body })); }; mutationOptions.mutationFn = mutationOptions.mutationFn || Fn; } return useMutation(mutationOptions); } export * from "./fetch";