-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add event callbacks onSuccess and onError (#11)
* docs: add JSDocs to SWR options * feat: add function to merge functions * refactor: use SWRConfig type as the value provided by default config * feat: add generics to SWRConfig * feat: add onError and onSuccess callbacks on types * feat: implement logic to merge callbacks in config * feat: add onError and onSuccess callbacks * docs: document onError event and onSuccess event
- Loading branch information
Showing
13 changed files
with
232 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Error Handling | ||
|
||
If an error is thrown inside [fetcher](./data-fetching), it will be returned as `error` by the composable | ||
|
||
```ts | ||
// ... | ||
const { data, error } = useSWR('/api/user', fetcher); | ||
``` | ||
|
||
The `error` object will be defined if the fetch promise is rejected or `fetcher` contains an syntax error. | ||
|
||
## Global Error Report | ||
|
||
You can always get the `error` object inside the component reactively. But in case you want to handle the error globally, to notify the UI to show a toast or a snackbar, or report it somewhere such as [Sentry](https://sentry.io/), there's an `onError` event: | ||
|
||
```ts | ||
configureGlobalSWR({ | ||
onError: (error, key) => { | ||
// We can send the error to Sentry, | ||
|
||
if (error.status !== 403 && error.status !== 404) { | ||
// or show a notification UI. | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
This is also available in the composable options. | ||
|
||
In case that you pass an global `onError` and in a composable inside the same context also pass a `onError` the two of them will be called. First the local one followed by the global |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { chainFns } from '.'; | ||
|
||
describe('chainFn', () => { | ||
it('should call all cahined functions', () => { | ||
const functions = [vi.fn(), vi.fn(), vi.fn(), vi.fn(), vi.fn(), vi.fn(), vi.fn(), vi.fn()]; | ||
const chainedFn = chainFns(...functions); | ||
|
||
chainedFn(); | ||
|
||
functions.forEach((fn) => expect(fn).toHaveBeenCalledOnce()); | ||
}); | ||
|
||
it('should call all chained functions with the same arguments', () => { | ||
const functions = [vi.fn(), vi.fn(), vi.fn(), vi.fn(), vi.fn(), vi.fn(), vi.fn(), vi.fn()]; | ||
const chainedFn = chainFns(...functions); | ||
|
||
chainedFn('arg1', 'arg2', 3); | ||
|
||
functions.forEach((fn) => expect(fn).toHaveBeenCalledWith('arg1', 'arg2', 3)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { AnyFunction } from '@/types'; | ||
|
||
export const chainFns = <F extends (AnyFunction | undefined)[]>(...fns: F) => { | ||
const validFns = fns.filter(<T>(maybeFn: T | undefined): maybeFn is T => !!maybeFn); | ||
|
||
return (...params: Parameters<Exclude<F[number], undefined>>) => | ||
validFns.forEach((fn) => fn(...params)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
import { SWRConfig } from '@/types'; | ||
|
||
import { chainFns } from '../chain-fn'; | ||
|
||
export const mergeConfig = <T extends Partial<SWRConfig>, D extends Partial<SWRConfig>>( | ||
fallbackConfig: T, | ||
config: D, | ||
) => ({ | ||
...fallbackConfig, | ||
...config, | ||
}); | ||
) => { | ||
const onSuccess = [config.onSuccess, fallbackConfig.onSuccess].filter(Boolean); | ||
const onError = [config.onError, fallbackConfig.onError].filter(Boolean); | ||
|
||
return { | ||
...fallbackConfig, | ||
...config, | ||
|
||
onSuccess: onSuccess.length > 0 ? chainFns(...onSuccess) : undefined, | ||
onError: onError.length > 0 ? chainFns(...onError) : undefined, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters