@hyper-fetch/core 9.4.2 → 9.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1875,6 +1875,44 @@ var mapResponseForSend = async (request, response) => {
1875
1875
  if (mapping instanceof Promise) return await mapping;
1876
1876
  return mapping || response;
1877
1877
  };
1878
+ /**
1879
+ * Rollback is user code running in the middle of the response handling, its failure can't affect the request
1880
+ */
1881
+ var rollbackOptimistic = (optimistic, args) => {
1882
+ const logFailure = (error) => {
1883
+ args.request.client.logger.error({
1884
+ title: "Optimistic rollback failed",
1885
+ type: "system",
1886
+ extra: {
1887
+ error,
1888
+ reason: args.reason,
1889
+ request: args.request
1890
+ }
1891
+ });
1892
+ };
1893
+ try {
1894
+ Promise.resolve(optimistic.rollback?.(args)).catch(logFailure);
1895
+ } catch (error) {
1896
+ logFailure(error);
1897
+ }
1898
+ };
1899
+ var invalidateOptimistic = (optimistic, request) => {
1900
+ optimistic.invalidate?.forEach((target) => {
1901
+ try {
1902
+ request.client.cache.invalidate(target);
1903
+ } catch (error) {
1904
+ request.client.logger.error({
1905
+ title: "Optimistic invalidation failed",
1906
+ type: "system",
1907
+ extra: {
1908
+ error,
1909
+ target,
1910
+ request
1911
+ }
1912
+ });
1913
+ }
1914
+ });
1915
+ };
1878
1916
  var sendRequest = async (request, options) => {
1879
1917
  const { client } = request;
1880
1918
  const { requestManager } = client;
@@ -1899,8 +1937,41 @@ var sendRequest = async (request, options) => {
1899
1937
  }
1900
1938
  return new Promise((resolve) => {
1901
1939
  let isResolved = false;
1940
+ let isDelivered = false;
1941
+ let isSettled = false;
1902
1942
  const requestId = dispatcher.add(request);
1903
1943
  const { $hooks } = request;
1944
+ const getWillBeResent = ({ response, details }) => {
1945
+ if (response.success || details.isCanceled) return false;
1946
+ return details.willRetry || request.offline && details.isOffline;
1947
+ };
1948
+ const mappings = /* @__PURE__ */ new WeakMap();
1949
+ const getMapping = (values) => {
1950
+ if (!mappings.has(values)) mappings.set(values, request.unstable_responseMapper?.(values.response));
1951
+ return mappings.get(values);
1952
+ };
1953
+ const unmountSettlement = mutationContext ? requestManager.events.onResponseById(requestId, (values) => {
1954
+ const optimistic = mutationContext;
1955
+ const willBeResent = getWillBeResent(values);
1956
+ const settle = (response) => {
1957
+ if (isSettled || !response.success && willBeResent) return;
1958
+ isSettled = true;
1959
+ if (values.details.isCanceled) rollbackOptimistic(optimistic, {
1960
+ reason: "abort",
1961
+ response,
1962
+ request
1963
+ });
1964
+ else if (response.success) invalidateOptimistic(optimistic, request);
1965
+ else rollbackOptimistic(optimistic, {
1966
+ reason: "error",
1967
+ response,
1968
+ request
1969
+ });
1970
+ };
1971
+ const mapping = getMapping(values);
1972
+ if (mapping instanceof Promise) mapping.then(settle);
1973
+ else settle(mapping || values.response);
1974
+ }) : void 0;
1904
1975
  const beforeSentData = {
1905
1976
  requestId,
1906
1977
  request,
@@ -1941,18 +2012,17 @@ var sendRequest = async (request, options) => {
1941
2012
  $hooks.__emit("onDownloadProgress", enriched);
1942
2013
  });
1943
2014
  const unmountResponse = requestManager.events.onResponseById(requestId, (values) => {
1944
- const { details, response } = values;
1945
- isResolved = true;
2015
+ const { response } = values;
2016
+ const willBeResent = getWillBeResent(values);
2017
+ isResolved = !willBeResent;
1946
2018
  const enrichedValues = {
1947
2019
  ...values,
1948
2020
  mutationContext
1949
2021
  };
1950
- const mapping = request.unstable_responseMapper?.(response);
1951
- const isOfflineStatus = request.offline && details.isOffline;
1952
- const { willRetry } = details;
2022
+ const mapping = getMapping(values);
1953
2023
  const handleResponse = (success, data) => {
1954
- if (!success && isOfflineStatus) return;
1955
- if (!success && willRetry) return;
2024
+ if (isDelivered || !success && willBeResent) return;
2025
+ isDelivered = true;
1956
2026
  options?.onResponse?.(enrichedValues);
1957
2027
  $hooks.__emit("onResponse", enrichedValues);
1958
2028
  resolve(data);
@@ -1971,6 +2041,15 @@ var sendRequest = async (request, options) => {
1971
2041
  });
1972
2042
  const unmountRemoveQueueElement = requestManager.events.onRemoveById(requestId, (data) => {
1973
2043
  if (!isResolved) {
2044
+ isResolved = true;
2045
+ isDelivered = true;
2046
+ if (mutationContext && !isSettled) {
2047
+ isSettled = true;
2048
+ rollbackOptimistic(mutationContext, {
2049
+ reason: "removed",
2050
+ request
2051
+ });
2052
+ }
1974
2053
  const enriched = {
1975
2054
  ...data,
1976
2055
  mutationContext
@@ -1990,6 +2069,7 @@ var sendRequest = async (request, options) => {
1990
2069
  }
1991
2070
  });
1992
2071
  function umountAll() {
2072
+ unmountSettlement?.();
1993
2073
  unmountRequestStart();
1994
2074
  unmountResponseStart();
1995
2075
  unmountUpload();
@@ -2174,10 +2254,10 @@ var Request = class Request {
2174
2254
  };
2175
2255
  /**
2176
2256
  * Configure optimistic update behavior for this request.
2177
- * The callback runs before the request is sent (in React's `useSubmit`) and receives
2178
- * the request, client, and payload. Return `context` (available in submit callbacks),
2179
- * `rollback` (called automatically on failure/abort), and `invalidate` (cache keys
2180
- * invalidated on success).
2257
+ * The callback runs before the request is sent and receives the request, client, and payload.
2258
+ * Return `context` (available in lifecycle callbacks), `rollback` (called automatically once on
2259
+ * failure, abort or queue removal), and `invalidate` (requests invalidated on success).
2260
+ * Both run for every way of sending the request, not only inside of framework hooks.
2181
2261
  */
2182
2262
  setOptimistic = (callback) => {
2183
2263
  const cloned = this.clone();