@decathlon/moon-axios
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -14,3 +14,4 @@ import { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosInterceptorManager } from "axios"; | ||
put<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>; | ||
patch<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>; | ||
} | ||
export default function createAxiosClient(config?: AxiosRequestConfig, interceptors?: IInterceptors<AxiosRequestConfig, AxiosResponse>): MoonAxiosInstance; |
@@ -697,2 +697,9 @@ import axios from 'axios'; | ||
}; | ||
MoonAxiosInstance.prototype.patch = function (url, data, config) { | ||
var cancel = updateCancelToken(config); | ||
var patchPromise = this.instance.patch(url, data, config); | ||
//@ts-ignore | ||
patchPromise.cancel = cancel; | ||
return patchPromise; | ||
}; | ||
return MoonAxiosInstance; | ||
@@ -699,0 +706,0 @@ }()); |
@@ -705,2 +705,9 @@ (function (global, factory) { | ||
}; | ||
MoonAxiosInstance.prototype.patch = function (url, data, config) { | ||
var cancel = updateCancelToken(config); | ||
var patchPromise = this.instance.patch(url, data, config); | ||
//@ts-ignore | ||
patchPromise.cancel = cancel; | ||
return patchPromise; | ||
}; | ||
return MoonAxiosInstance; | ||
@@ -707,0 +714,0 @@ }()); |
@@ -95,2 +95,9 @@ "use strict"; | ||
}; | ||
MoonAxiosInstance.prototype.patch = function (url, data, config) { | ||
var cancel = updateCancelToken(config); | ||
var patchPromise = this.instance.patch(url, data, config); | ||
//@ts-ignore | ||
patchPromise.cancel = cancel; | ||
return patchPromise; | ||
}; | ||
return MoonAxiosInstance; | ||
@@ -97,0 +104,0 @@ }()); |
@@ -38,11 +38,14 @@ "use strict"; | ||
var utils_1 = require("./utils"); | ||
function getAdaptedQueryState(query) { | ||
if (!query) { | ||
function getAdaptedQueryState(query, result, isInfinite) { | ||
if (result === void 0) { result = query === null || query === void 0 ? void 0 : query.state; } | ||
if (isInfinite === void 0) { isInfinite = false; } | ||
if (!query || !result) { | ||
return undefined; | ||
} | ||
var networkOnly = (query === null || query === void 0 ? void 0 : query.cacheTime) === 0; | ||
var state = query.state; | ||
var isFetching = state.isFetching, data = state.data; | ||
var queryData = isFetching && networkOnly ? undefined : data; | ||
return __assign(__assign({}, state), { data: queryData }); | ||
var newData = result.data, dataUpdatedAt = result.dataUpdatedAt, isFetching = result.isFetching, error = result.error, errorUpdatedAt = result.errorUpdatedAt, status = result.status; | ||
var networkOnly = query.cacheTime === 0; | ||
var cachedData = query.state.data; | ||
var data = newData || cachedData; | ||
var queryData = !isInfinite && isFetching && networkOnly ? undefined : data; | ||
return __assign(__assign({}, query === null || query === void 0 ? void 0 : query.state), { dataUpdatedAt: dataUpdatedAt, isFetching: isFetching, error: error, errorUpdatedAt: errorUpdatedAt, status: status, data: queryData }); | ||
} | ||
@@ -60,3 +63,5 @@ exports.getAdaptedQueryState = getAdaptedQueryState; | ||
var createObserver = React.useCallback(function () { | ||
return isInfinite ? new react_query_1.InfiniteQueryObserver(store, defaultOptions) : new react_query_1.QueryObserver(store, defaultOptions); | ||
return isInfinite | ||
? new react_query_1.InfiniteQueryObserver(store, defaultOptions) | ||
: new react_query_1.QueryObserver(store, defaultOptions); | ||
}, [isInfinite, store, defaultOptions]); | ||
@@ -66,7 +71,8 @@ var observer = observerRef.current || createObserver(); | ||
if (observer.hasListeners()) { | ||
// @ts-ignore InfiniteQueryObserver | QueryObserver | ||
observer.setOptions(defaultOptions); | ||
} | ||
var listener = React.useCallback(function () { | ||
var listener = React.useCallback(function (result) { | ||
if (isMounted()) { | ||
var queryState = getState(query); | ||
var queryState = getState(query, result); | ||
if (!utils_1.equal(state || null, queryState || null)) { | ||
@@ -96,3 +102,5 @@ setState(queryState); | ||
var queries = React.useMemo(function () { | ||
return queriesIds.map(function (queryId) { return store.defaultQueryObserverOptions({ queryKey: queryId, enabled: false }); }); | ||
return queriesIds.map(function (queryId) { | ||
return store.defaultQueryObserverOptions({ queryKey: queryId, enabled: false }); | ||
}); | ||
}, [currentQueriesIds]); | ||
@@ -105,8 +113,8 @@ var observerRef = React.useRef(); | ||
} | ||
var listener = React.useCallback(function () { | ||
var listener = React.useCallback(function (results) { | ||
if (isMounted()) { | ||
queriesIds.forEach(function (queryId) { | ||
queriesIds.forEach(function (queryId, index) { | ||
var _a; | ||
var query = store.getQueryCache().get(react_query_1.hashQueryKey(queryId)); | ||
var queryState = getState(query); | ||
var queryState = getState(query, results[index]); | ||
if (!utils_1.equal(states[queryId] || null, queryState || null)) { | ||
@@ -126,7 +134,7 @@ setStates((_a = {}, _a[queryId] = queryState, _a)); | ||
if (isInfinite === void 0) { isInfinite = false; } | ||
var getState = function (query) { | ||
var queryState = getAdaptedQueryState(query); | ||
var getData = function (query, result) { | ||
var queryState = getAdaptedQueryState(query, result); | ||
return queryState === null || queryState === void 0 ? void 0 : queryState.data; | ||
}; | ||
var queryResult = useQueryObserver(queryId, getState, isInfinite); | ||
var queryResult = useQueryObserver(queryId, getData, isInfinite); | ||
return resultToProps ? resultToProps(queryResult) : queryResult; | ||
@@ -136,4 +144,4 @@ } | ||
function useQueriesResults(queriesIds, resultsToProps) { | ||
var getState = function (query) { | ||
var queryState = getAdaptedQueryState(query); | ||
var getState = function (query, result) { | ||
var queryState = getAdaptedQueryState(query, result); | ||
return queryState === null || queryState === void 0 ? void 0 : queryState.data; | ||
@@ -140,0 +148,0 @@ }; |
@@ -16,3 +16,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useInfiniteQuery = exports.InfiniteQuery = exports.useMutation = exports.Mutation = exports.useQuery = exports.Query = exports.MoonProvider = exports.MoonClient = void 0; | ||
exports.useInfiniteQuery = exports.InfiniteQuery = exports.useMutation = exports.Mutation = exports.usePrefetchQuery = exports.useQuery = exports.Query = exports.MoonProvider = exports.MoonClient = void 0; | ||
__exportStar(require("react-query"), exports); | ||
@@ -31,2 +31,5 @@ var moonClient_1 = require("./moonClient"); | ||
__exportStar(require("./useQuery"), exports); | ||
var usePrefetchQuery_1 = require("./usePrefetchQuery"); | ||
Object.defineProperty(exports, "usePrefetchQuery", { enumerable: true, get: function () { return __importDefault(usePrefetchQuery_1).default; } }); | ||
__exportStar(require("./usePrefetchQuery"), exports); | ||
var mutation_1 = require("./mutation"); | ||
@@ -33,0 +36,0 @@ Object.defineProperty(exports, "Mutation", { enumerable: true, get: function () { return __importDefault(mutation_1).default; } }); |
@@ -21,2 +21,3 @@ "use strict"; | ||
MutateType["Put"] = "PUT"; | ||
MutateType["Patch"] = "PATCH"; | ||
})(MutateType = exports.MutateType || (exports.MutateType = {})); | ||
@@ -53,2 +54,4 @@ var MoonClient = /** @class */ (function () { | ||
return client.put(clientEndPoint, variables, options); | ||
case MutateType.Patch: | ||
return client.patch(clientEndPoint, variables, options); | ||
default: | ||
@@ -55,0 +58,0 @@ return client.post(clientEndPoint, variables, options); |
@@ -64,5 +64,3 @@ "use strict"; | ||
var queryResult = hooks_1.useQueryResult(queryId, resutToProps); | ||
var queryProps = resutToProps | ||
? queryResult | ||
: { queryResult: queryResult }; | ||
var queryProps = resutToProps ? queryResult : { queryResult: queryResult }; | ||
var componentProps = __assign(__assign({}, queryProps), rest); | ||
@@ -69,0 +67,0 @@ return React.createElement(WrappedComponent, __assign({ ref: forwardedRef }, componentProps)); |
@@ -77,2 +77,3 @@ "use strict"; | ||
// remove cache if networkOnly | ||
// @ts-ignore @react-query must update to undefined | ||
store.setQueryData(queryId, queryConfig === null || queryConfig === void 0 ? void 0 : queryConfig.initialData); | ||
@@ -88,6 +89,10 @@ } | ||
var refetch = queryResult.refetch, remove = queryResult.remove, others = __rest(queryResult, ["refetch", "remove"]); | ||
function cancel() { | ||
return store.cancelQueries(queryId, { exact: true }); | ||
} | ||
React.useEffect(function () { | ||
if (prevValue.queryId === value.queryId && !isInitialMount.current && (queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.enabled)) { | ||
// refetch on update and when only client options have been changed | ||
refetch(); | ||
var _a; | ||
if (prevValue.queryId === value.queryId && !isInitialMount.current && ((_a = queryOptions.enabled) !== null && _a !== void 0 ? _a : true)) { | ||
// cancel the prev query then refetch on update and when only client options have been changed | ||
cancel().then(function () { return refetch(); }); | ||
} | ||
@@ -98,5 +103,2 @@ }, [value.clientProps]); | ||
}, []); | ||
function cancel() { | ||
store.cancelQueries(queryId, { exact: true }); | ||
} | ||
var data = networkOnly && others.isFetching ? undefined : others.data; | ||
@@ -103,0 +105,0 @@ return [ |
{ | ||
"name": "@decathlon/moon-axios", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Axios for moon", | ||
@@ -64,3 +64,3 @@ "author": "Decathlon", | ||
}, | ||
"gitHead": "7f07547169c8036d64994235b59f4b4bf3de008d" | ||
"gitHead": "02d1d5c45b5af47abf3c86543fb3478063013588" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
254600
40
2363
3