Comparing version 2.0.0-beta.7 to 2.0.0-rc.0
@@ -211,2 +211,3 @@ import react, { FC, PropsWithChildren, MutableRefObject } from 'react'; | ||
rollbackOnError?: boolean; | ||
throwOnError?: boolean; | ||
}; | ||
@@ -234,3 +235,9 @@ declare type MutatorConfig = { | ||
interface SWRResponse<Data = any, Error = any> { | ||
/** | ||
* The returned data of the fetcher function. | ||
*/ | ||
data: Data | undefined; | ||
/** | ||
* The error object thrown by the fetcher function. | ||
*/ | ||
error: Error | undefined; | ||
@@ -237,0 +244,0 @@ mutate: KeyedMutator<Data>; |
@@ -356,3 +356,3 @@ import React, { useEffect, useLayoutEffect, createContext, useContext, useMemo, useState, createElement, useRef, useCallback } from 'react'; | ||
_internalMutate = _asyncToGenerator(function() { | ||
var _len, args, _key, cache, _key1, _data, _opts, options, _tmp, _tmp1, populateCache, optimisticData, revalidate, rollbackOnError, keyFilter, matchedKeys, _i, _iter, key; | ||
var _len, args, _key, cache, _key1, _data, _opts, options, _tmp, _tmp1, _tmp2, populateCache, optimisticData, revalidate, rollbackOnError, throwOnError, keyFilter, matchedKeys, it, keyIt, key; | ||
var _arguments = arguments; | ||
@@ -364,3 +364,3 @@ function mutateByKey(_k) { | ||
_mutateByKey = _asyncToGenerator(function(_k) { | ||
var ref, key, ref1, get, set, ref2, EVENT_REVALIDATORS, MUTATION, FETCH, revalidators, startRevalidate, data, error, beforeMutationTs, hasOptimisticData, state, displayedData, committedData, _tmp, _tmp1, _tmp2, _tmp3, res, _tmp4; | ||
var ref, key, ref1, get, set, ref2, EVENT_REVALIDATORS, MUTATION, FETCH, revalidators, startRevalidate, data, error, beforeMutationTs, hasOptimisticData, state, displayedData, committedData, _tmp, _tmp1, _tmp2, res, _tmp3; | ||
return __generator(this, function(_state) { | ||
@@ -458,3 +458,2 @@ switch(_state.label){ | ||
_tmp2 = {}; | ||
_tmp3 = {}; | ||
// If we should write back the cache after request. | ||
@@ -470,4 +469,2 @@ if (populateCache) { | ||
} | ||
// Always update error and original data here. | ||
set((_tmp3.error = error, _tmp3)); | ||
} | ||
@@ -482,8 +479,13 @@ // Reset the timestamp to mark the mutation has ended. | ||
res = _state.sent(); | ||
_tmp4 = {}; | ||
_tmp3 = {}; | ||
// The mutation and revalidation are ended, we can clear it since the data is | ||
// not an optimistic value anymore. | ||
set((_tmp4._c = UNDEFINED, _tmp4)); | ||
set((_tmp3._c = UNDEFINED, _tmp3)); | ||
// Throw error or return data | ||
if (error) throw error; | ||
if (error) { | ||
if (throwOnError) throw error; | ||
return [ | ||
2 | ||
]; | ||
} | ||
return [ | ||
@@ -505,7 +507,9 @@ 2, | ||
_tmp1 = {}; | ||
options = typeof _opts === "boolean" ? (_tmp.revalidate = _opts, _tmp) : _opts || _tmp1; | ||
populateCache = isUndefined(options.populateCache) ? true : options.populateCache; | ||
_tmp2 = {}; | ||
options = mergeObjects((_tmp.populateCache = true, _tmp.throwOnError = true, _tmp), typeof _opts === "boolean" ? (_tmp1.revalidate = _opts, _tmp1) : _opts || _tmp2); | ||
populateCache = options.populateCache; | ||
optimisticData = options.optimisticData; | ||
revalidate = options.revalidate !== false; | ||
rollbackOnError = options.rollbackOnError !== false; | ||
throwOnError = options.throwOnError; | ||
// If the second argument is a key filter, return the mutation results for all | ||
@@ -516,4 +520,5 @@ // filtered keys. | ||
matchedKeys = []; | ||
for(_i = 0, _iter = cache.keys(); _i < _iter.length; _i++){ | ||
key = _iter[_i]; | ||
it = cache.keys(); | ||
for(keyIt = it.next(); !keyIt.done; keyIt = it.next()){ | ||
key = keyIt.value; | ||
if (// Skip the special useSWRInfinite keys. | ||
@@ -520,0 +525,0 @@ !key.startsWith("$inf$") && keyFilter(cache.get(key)._k)) { |
@@ -362,3 +362,3 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
_internalMutate = _asyncToGenerator(function() { | ||
var _len, args, _key, cache, _key1, _data, _opts, options, _tmp, _tmp1, populateCache, optimisticData, revalidate, rollbackOnError, keyFilter, matchedKeys, _i, _iter, key; | ||
var _len, args, _key, cache, _key1, _data, _opts, options, _tmp, _tmp1, _tmp2, populateCache, optimisticData, revalidate, rollbackOnError, throwOnError, keyFilter, matchedKeys, it, keyIt, key; | ||
var _arguments = arguments; | ||
@@ -370,3 +370,3 @@ function mutateByKey(_k) { | ||
_mutateByKey = _asyncToGenerator(function(_k) { | ||
var ref, key, ref1, get, set, ref2, EVENT_REVALIDATORS, MUTATION, FETCH, revalidators, startRevalidate, data, error, beforeMutationTs, hasOptimisticData, state, displayedData, committedData, _tmp, _tmp1, _tmp2, _tmp3, res, _tmp4; | ||
var ref, key, ref1, get, set, ref2, EVENT_REVALIDATORS, MUTATION, FETCH, revalidators, startRevalidate, data, error, beforeMutationTs, hasOptimisticData, state, displayedData, committedData, _tmp, _tmp1, _tmp2, res, _tmp3; | ||
return __generator(this, function(_state) { | ||
@@ -464,3 +464,2 @@ switch(_state.label){ | ||
_tmp2 = {}; | ||
_tmp3 = {}; | ||
// If we should write back the cache after request. | ||
@@ -476,4 +475,2 @@ if (populateCache) { | ||
} | ||
// Always update error and original data here. | ||
set((_tmp3.error = error, _tmp3)); | ||
} | ||
@@ -488,8 +485,13 @@ // Reset the timestamp to mark the mutation has ended. | ||
res = _state.sent(); | ||
_tmp4 = {}; | ||
_tmp3 = {}; | ||
// The mutation and revalidation are ended, we can clear it since the data is | ||
// not an optimistic value anymore. | ||
set((_tmp4._c = UNDEFINED, _tmp4)); | ||
set((_tmp3._c = UNDEFINED, _tmp3)); | ||
// Throw error or return data | ||
if (error) throw error; | ||
if (error) { | ||
if (throwOnError) throw error; | ||
return [ | ||
2 | ||
]; | ||
} | ||
return [ | ||
@@ -511,7 +513,9 @@ 2, | ||
_tmp1 = {}; | ||
options = typeof _opts === "boolean" ? (_tmp.revalidate = _opts, _tmp) : _opts || _tmp1; | ||
populateCache = isUndefined(options.populateCache) ? true : options.populateCache; | ||
_tmp2 = {}; | ||
options = mergeObjects((_tmp.populateCache = true, _tmp.throwOnError = true, _tmp), typeof _opts === "boolean" ? (_tmp1.revalidate = _opts, _tmp1) : _opts || _tmp2); | ||
populateCache = options.populateCache; | ||
optimisticData = options.optimisticData; | ||
revalidate = options.revalidate !== false; | ||
rollbackOnError = options.rollbackOnError !== false; | ||
throwOnError = options.throwOnError; | ||
// If the second argument is a key filter, return the mutation results for all | ||
@@ -522,4 +526,5 @@ // filtered keys. | ||
matchedKeys = []; | ||
for(_i = 0, _iter = cache.keys(); _i < _iter.length; _i++){ | ||
key = _iter[_i]; | ||
it = cache.keys(); | ||
for(keyIt = it.next(); !keyIt.done; keyIt = it.next()){ | ||
key = keyIt.value; | ||
if (// Skip the special useSWRInfinite keys. | ||
@@ -526,0 +531,0 @@ !key.startsWith("$inf$") && keyFilter(cache.get(key)._k)) { |
@@ -185,2 +185,3 @@ import * as react from 'react'; | ||
rollbackOnError?: boolean; | ||
throwOnError?: boolean; | ||
}; | ||
@@ -200,3 +201,9 @@ declare type State<Data = any, Error = any> = { | ||
interface SWRResponse<Data = any, Error = any> { | ||
/** | ||
* The returned data of the fetcher function. | ||
*/ | ||
data: Data | undefined; | ||
/** | ||
* The error object thrown by the fetcher function. | ||
*/ | ||
error: Error | undefined; | ||
@@ -232,3 +239,5 @@ mutate: KeyedMutator<Data>; | ||
/** | ||
* @link https://swr.vercel.app/ | ||
* A hook to fetch data. | ||
* | ||
* @link https://swr.vercel.app | ||
* @example | ||
@@ -235,0 +244,0 @@ * ```jsx |
@@ -192,3 +192,3 @@ import { useRef, useMemo, useCallback, useDebugValue } from 'react'; | ||
if (t === "data" && isUndefined(prev[t])) { | ||
if (!compare(current[t], fallback)) { | ||
if (!compare(current[t], returnedData)) { | ||
equal = false; | ||
@@ -617,3 +617,5 @@ } | ||
/** | ||
* @link https://swr.vercel.app/ | ||
* A hook to fetch data. | ||
* | ||
* @link https://swr.vercel.app | ||
* @example | ||
@@ -620,0 +622,0 @@ * ```jsx |
@@ -193,3 +193,3 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
if (t === "data" && _internal.isUndefined(prev[t])) { | ||
if (!compare(current[t], fallback)) { | ||
if (!compare(current[t], returnedData)) { | ||
equal = false; | ||
@@ -618,3 +618,5 @@ } | ||
/** | ||
* @link https://swr.vercel.app/ | ||
* A hook to fetch data. | ||
* | ||
* @link https://swr.vercel.app | ||
* @example | ||
@@ -621,0 +623,0 @@ * ```jsx |
import { useRef, useCallback } from 'react'; | ||
import useSWR, { useSWRConfig } from 'swr'; | ||
import { withMiddleware, useStateWithDeps, UNDEFINED, getTimestamp, useIsomorphicLayoutEffect, serialize } from 'swr/_internal'; | ||
import { withMiddleware, useStateWithDeps, UNDEFINED, getTimestamp, useIsomorphicLayoutEffect, mergeObjects, serialize } from 'swr/_internal'; | ||
@@ -143,3 +143,3 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
var trigger = useCallback(/*#__PURE__*/ _asyncToGenerator(function(arg, opts) { | ||
var ref, serializedKey, resolvedKey, options, _tmp, mutationStartedAt, _tmp1, data, _tmp2, _tmp3, error, _tmp4; | ||
var ref, serializedKey, resolvedKey, options, _tmp, mutationStartedAt, _tmp1, data, _tmp2, _tmp3, _tmp4, error, _tmp5; | ||
return __generator(this, function(_state) { | ||
@@ -153,6 +153,6 @@ switch(_state.label){ | ||
if (!serializedKey) { | ||
throw new Error("Can’t trigger the mutation: key isn’t ready."); | ||
throw new Error("Can’t trigger the mutation: missing key."); | ||
} | ||
_tmp = {}; | ||
options = Object.assign((_tmp.populateCache = false, _tmp), config, opts); | ||
options = mergeObjects(mergeObjects((_tmp.populateCache = false, _tmp.throwOnError = true, _tmp), config), opts); | ||
mutationStartedAt = getTimestamp(); | ||
@@ -171,13 +171,14 @@ ditchMutationsUntilRef.current = mutationStartedAt; | ||
_tmp2 = {}; | ||
_tmp3 = {}; | ||
return [ | ||
4, | ||
mutate(serializedKey, // FIXME: Error shouldn't be broadcasted here. | ||
(fetcher)(resolvedKey, (_tmp2.arg = arg, _tmp2)), options) | ||
mutate(serializedKey, fetcher(resolvedKey, (_tmp2.arg = arg, _tmp2)), // We must throw the error here so we can catch and update the states. | ||
mergeObjects(options, (_tmp3.throwOnError = true, _tmp3))) | ||
]; | ||
case 2: | ||
data = _state.sent(); | ||
_tmp3 = {}; | ||
_tmp4 = {}; | ||
// If it's reset after the mutation, we don't broadcast any state change. | ||
if (ditchMutationsUntilRef.current <= mutationStartedAt) { | ||
setState((_tmp3.data = data, _tmp3.isMutating = false, _tmp3.error = undefined, _tmp3)); | ||
setState((_tmp4.data = data, _tmp4.isMutating = false, _tmp4.error = undefined, _tmp4)); | ||
options.onSuccess == null ? void 0 : options.onSuccess(data, serializedKey, options); | ||
@@ -191,8 +192,11 @@ } | ||
error = _state.sent(); | ||
_tmp4 = {}; | ||
// If it's reset after the mutation, we don't broadcast any state change. | ||
_tmp5 = {}; | ||
// If it's reset after the mutation, we don't broadcast any state change | ||
// or throw because it's discarded. | ||
if (ditchMutationsUntilRef.current <= mutationStartedAt) { | ||
setState((_tmp4.error = error, _tmp4.isMutating = false, _tmp4)); | ||
setState((_tmp5.error = error, _tmp5.isMutating = false, _tmp5)); | ||
options.onError == null ? void 0 : options.onError(error, serializedKey, options); | ||
throw error; | ||
if (options.throwOnError) { | ||
throw error; | ||
} | ||
} | ||
@@ -244,4 +248,20 @@ return [ | ||
}; | ||
var index = withMiddleware(useSWR, mutation); | ||
/** | ||
* A hook to define and manually trigger remote mutations like POST, PUT, DELETE and PATCH use cases. | ||
* | ||
* @link https://swr.vercel.app/docs/mutation | ||
* @example | ||
* ```jsx | ||
* import useSWRMutation from 'swr/mutation' | ||
* | ||
* const { | ||
* data, | ||
* error, | ||
* trigger, | ||
* reset, | ||
* isMutating | ||
* } = useSWRMutation(key, fetcher, options?) | ||
* ``` | ||
*/ var index = withMiddleware(useSWR, mutation); | ||
export { index as default }; |
@@ -149,3 +149,3 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
var trigger = react.useCallback(/*#__PURE__*/ _asyncToGenerator(function(arg, opts) { | ||
var ref, serializedKey, resolvedKey, options, _tmp, mutationStartedAt, _tmp1, data, _tmp2, _tmp3, error, _tmp4; | ||
var ref, serializedKey, resolvedKey, options, _tmp, mutationStartedAt, _tmp1, data, _tmp2, _tmp3, _tmp4, error, _tmp5; | ||
return __generator(this, function(_state) { | ||
@@ -159,6 +159,6 @@ switch(_state.label){ | ||
if (!serializedKey) { | ||
throw new Error("Can’t trigger the mutation: key isn’t ready."); | ||
throw new Error("Can’t trigger the mutation: missing key."); | ||
} | ||
_tmp = {}; | ||
options = Object.assign((_tmp.populateCache = false, _tmp), config, opts); | ||
options = _internal.mergeObjects(_internal.mergeObjects((_tmp.populateCache = false, _tmp.throwOnError = true, _tmp), config), opts); | ||
mutationStartedAt = _internal.getTimestamp(); | ||
@@ -177,13 +177,14 @@ ditchMutationsUntilRef.current = mutationStartedAt; | ||
_tmp2 = {}; | ||
_tmp3 = {}; | ||
return [ | ||
4, | ||
mutate(serializedKey, // FIXME: Error shouldn't be broadcasted here. | ||
(fetcher)(resolvedKey, (_tmp2.arg = arg, _tmp2)), options) | ||
mutate(serializedKey, fetcher(resolvedKey, (_tmp2.arg = arg, _tmp2)), // We must throw the error here so we can catch and update the states. | ||
_internal.mergeObjects(options, (_tmp3.throwOnError = true, _tmp3))) | ||
]; | ||
case 2: | ||
data = _state.sent(); | ||
_tmp3 = {}; | ||
_tmp4 = {}; | ||
// If it's reset after the mutation, we don't broadcast any state change. | ||
if (ditchMutationsUntilRef.current <= mutationStartedAt) { | ||
setState((_tmp3.data = data, _tmp3.isMutating = false, _tmp3.error = undefined, _tmp3)); | ||
setState((_tmp4.data = data, _tmp4.isMutating = false, _tmp4.error = undefined, _tmp4)); | ||
options.onSuccess == null ? void 0 : options.onSuccess(data, serializedKey, options); | ||
@@ -197,8 +198,11 @@ } | ||
error = _state.sent(); | ||
_tmp4 = {}; | ||
// If it's reset after the mutation, we don't broadcast any state change. | ||
_tmp5 = {}; | ||
// If it's reset after the mutation, we don't broadcast any state change | ||
// or throw because it's discarded. | ||
if (ditchMutationsUntilRef.current <= mutationStartedAt) { | ||
setState((_tmp4.error = error, _tmp4.isMutating = false, _tmp4)); | ||
setState((_tmp5.error = error, _tmp5.isMutating = false, _tmp5)); | ||
options.onError == null ? void 0 : options.onError(error, serializedKey, options); | ||
throw error; | ||
if (options.throwOnError) { | ||
throw error; | ||
} | ||
} | ||
@@ -250,4 +254,20 @@ return [ | ||
}; | ||
var index = _internal.withMiddleware(useSWR__default["default"], mutation); | ||
/** | ||
* A hook to define and manually trigger remote mutations like POST, PUT, DELETE and PATCH use cases. | ||
* | ||
* @link https://swr.vercel.app/docs/mutation | ||
* @example | ||
* ```jsx | ||
* import useSWRMutation from 'swr/mutation' | ||
* | ||
* const { | ||
* data, | ||
* error, | ||
* trigger, | ||
* reset, | ||
* isMutating | ||
* } = useSWRMutation(key, fetcher, options?) | ||
* ``` | ||
*/ var index = _internal.withMiddleware(useSWR__default["default"], mutation); | ||
exports["default"] = index; |
@@ -14,14 +14,59 @@ import { Key, MutatorOptions, SWRResponse } from 'swr'; | ||
interface SWRMutationResponse<Data = any, Error = any, ExtraArg = any, SWRMutationKey extends Key = Key> extends Pick<SWRResponse<Data, Error>, 'data' | 'error'> { | ||
/** | ||
* Indicates if the mutation is in progress. | ||
*/ | ||
isMutating: boolean; | ||
/** | ||
* Function to trigger the mutation. You can also pass an extra argument to | ||
* the fetcher, and override the options for the mutation hook. | ||
*/ | ||
trigger: (extraArgument?: ExtraArg, options?: SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey>) => Promise<Data | undefined>; | ||
/** | ||
* Function to reset the mutation state (`data`, `error`, and `isMutating`). | ||
*/ | ||
reset: () => void; | ||
} | ||
declare type SWRMutationHook = <Data = any, Error = any, SWRMutationKey extends Key = Key, ExtraArg = any>(...args: readonly [SWRMutationKey, MutationFetcher<Data, ExtraArg, SWRMutationKey>] | readonly [ | ||
SWRMutationKey, | ||
MutationFetcher<Data, ExtraArg, SWRMutationKey>, | ||
SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey> | ||
]) => SWRMutationResponse<Data, Error, ExtraArg, SWRMutationKey>; | ||
declare type SWRMutationHook = <Data = any, Error = any, SWRMutationKey extends Key = Key, ExtraArg = any>( | ||
/** | ||
* The key of the resource that will be mutated. It should be the same key | ||
* used in the `useSWR` hook so SWR can handle revalidation and race | ||
* conditions for that resource. | ||
*/ | ||
key: SWRMutationKey, | ||
/** | ||
* The function to trigger the mutation that accepts the key, extra argument | ||
* and options. For example: | ||
* | ||
* ```jsx | ||
* (api, data) => fetch(api, { | ||
* method: 'POST', | ||
* body: JSON.stringify(data) | ||
* }) | ||
* ``` | ||
*/ | ||
fetcher: MutationFetcher<Data, ExtraArg, SWRMutationKey>, | ||
/** | ||
* Extra options for the mutation hook. | ||
*/ | ||
options?: SWRMutationConfiguration<Data, Error, ExtraArg, SWRMutationKey>) => SWRMutationResponse<Data, Error, ExtraArg, SWRMutationKey>; | ||
/** | ||
* A hook to define and manually trigger remote mutations like POST, PUT, DELETE and PATCH use cases. | ||
* | ||
* @link https://swr.vercel.app/docs/mutation | ||
* @example | ||
* ```jsx | ||
* import useSWRMutation from 'swr/mutation' | ||
* | ||
* const { | ||
* data, | ||
* error, | ||
* trigger, | ||
* reset, | ||
* isMutating | ||
* } = useSWRMutation(key, fetcher, options?) | ||
* ``` | ||
*/ | ||
declare const _default: SWRMutationHook; | ||
export { SWRMutationConfiguration, SWRMutationHook, SWRMutationResponse, _default as default }; |
{ | ||
"name": "swr", | ||
"version": "2.0.0-beta.7", | ||
"version": "2.0.0-rc.0", | ||
"description": "React Hooks library for remote data fetching", | ||
@@ -65,7 +65,2 @@ "keywords": [ | ||
"license": "MIT", | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged && pnpm types:check" | ||
} | ||
}, | ||
"lint-staged": { | ||
@@ -105,3 +100,3 @@ "*.{ts,tsx}": [ | ||
"rimraf": "3.0.2", | ||
"swr": "2.0.0-beta.7", | ||
"swr": "2.0.0-rc.0", | ||
"tslib": "2.4.0", | ||
@@ -108,0 +103,0 @@ "typescript": "4.8.2" |
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
311236
7285