Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and doc addition for failureToastOptions in applicable hooks #40

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/utils-reference/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ npm install --save @raycast/utils

## Changelog

### v1.16.5

- Fixed the bug where `failureToastOptions` did not apply for `useExec` and `useStreamJSON` hooks.

### v1.16.4

- Avoid throwing an error when `useFetch` can't parse the `Content-Type` header of the response
- Avoid throwing an error when `useFetch` can't parse the `Content-Type` header of the response.

### v1.16.3

Expand Down
4 changes: 3 additions & 1 deletion docs/utils-reference/react-hooks/useAI.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function useAI(
execute?: boolean;
onError?: (error: Error) => void;
onData?: (data: T) => void;
onWillExecute?: (args: string[]) -> void;
onWillExecute?: (args: Parameters<T>) => void;
failureToastOptions?: Partial<Pick<Toast.Options, "title" | "primaryAction" | "message">>;
}
): AsyncState<String> & {
revalidate: () => void;
Expand All @@ -37,6 +38,7 @@ Including the [usePromise](./usePromise.md)'s options:
- `options.onError` is a function called when an execution fails. By default, it will log the error and show a generic failure toast with an action to retry.
- `options.onData` is a function called when an execution succeeds.
- `options.onWillExecute` is a function called when an execution will start.
- `options.failureToastOptions` are the options to customize the title, message, and primary action of the failure toast.

### Return

Expand Down
2 changes: 2 additions & 0 deletions docs/utils-reference/react-hooks/useCachedPromise.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function useCachedPromise<T, U>(
onError?: (error: Error) => void;
onData?: (data: Result<T>) => void;
onWillExecute?: (args: Parameters<T>) => void;
failureToastOptions?: Partial<Pick<Toast.Options, "title" | "primaryAction" | "message">>;
},
): AsyncState<Result<T>> & {
revalidate: () => void;
Expand Down Expand Up @@ -54,6 +55,7 @@ Including the [usePromise](./usePromise.md)'s options:
- `options.onError` is a function called when an execution fails. By default, it will log the error and show a generic failure toast with an action to retry.
- `options.onData` is a function called when an execution succeeds.
- `options.onWillExecute` is a function called when an execution will start.
- `options.failureToastOptions` are the options to customize the title, message, and primary action of the failure toast.

### Return

Expand Down
7 changes: 5 additions & 2 deletions docs/utils-reference/react-hooks/useExec.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function useExec<T, U>(
execute?: boolean;
onError?: (error: Error) => void;
onData?: (data: T) => void;
onWillExecute?: (args: string[]) -> void;
onWillExecute?: (args: string[]) => void;
failureToastOptions?: Partial<Pick<Toast.Options, "title" | "primaryAction" | "message">>;
}
): AsyncState<T> & {
revalidate: () => void;
Expand Down Expand Up @@ -61,7 +62,8 @@ function useExec<T, U>(
execute?: boolean;
onError?: (error: Error) => void;
onData?: (data: T) => void;
onWillExecute?: (args: string[]) -> void;
onWillExecute?: (args: string[]) => void;
failureToastOptions?: Partial<Pick<Toast.Options, "title" | "primaryAction" | "message">>;
}
): AsyncState<T> & {
revalidate: () => void;
Expand Down Expand Up @@ -110,6 +112,7 @@ Including the [usePromise](./usePromise.md)'s options:
- `options.onError` is a function called when an execution fails. By default, it will log the error and show a generic failure toast with an action to retry.
- `options.onData` is a function called when an execution succeeds.
- `options.onWillExecute` is a function called when an execution will start.
- `options.failureToastOptions` are the options to customize the title, message, and primary action of the failure toast.

### Return

Expand Down
2 changes: 2 additions & 0 deletions docs/utils-reference/react-hooks/useFetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function useFetch<V, U, T = V>(
onError?: (error: Error) => void;
onData?: (data: T) => void;
onWillExecute?: (args: [string, RequestInit]) => void;
failureToastOptions?: Partial<Pick<Toast.Options, "title" | "primaryAction" | "message">>;
},
): AsyncState<T> & {
revalidate: () => void;
Expand Down Expand Up @@ -51,6 +52,7 @@ Including the [usePromise](./usePromise.md)'s options:
- `options.onError` is a function called when an execution fails. By default, it will log the error and show a generic failure toast with an action to retry.
- `options.onData` is a function called when an execution succeeds.
- `options.onWillExecute` is a function called when an execution will start.
- `options.failureToastOptions` are the options to customize the title, message, and primary action of the failure toast.

### Return

Expand Down
2 changes: 2 additions & 0 deletions docs/utils-reference/react-hooks/usePromise.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function usePromise<T>(
onError?: (error: Error) => void;
onData?: (data: Result<T>) => void;
onWillExecute?: (args: Parameters<T>) => void;
failureToastOptions?: Partial<Pick<Toast.Options, "title" | "primaryAction" | "message">>;
},
): AsyncState<Result<T>> & {
revalidate: () => void;
Expand All @@ -39,6 +40,7 @@ With a few options:
- `options.onError` is a function called when an execution fails. By default, it will log the error and show a generic failure toast with an action to retry.
- `options.onData` is a function called when an execution succeeds.
- `options.onWillExecute` is a function called when an execution will start.
- `options.failureToastOptions` are the options to customize the title, message, and primary action of the failure toast.

### Returns

Expand Down
4 changes: 3 additions & 1 deletion docs/utils-reference/react-hooks/useSQL.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ function useSQL<T>(
execute?: boolean;
onError?: (error: Error) => void;
onData?: (data: T) => void;
onWillExecute?: (args: string[]) -> void;
onWillExecute?: (args: string[]) => void;
failureToastOptions?: Partial<Pick<Toast.Options, "title" | "primaryAction" | "message">>;
}
): AsyncState<T> & {
revalidate: () => void;
Expand All @@ -37,6 +38,7 @@ Including the [usePromise](./usePromise.md)'s options:
- `options.onError` is a function called when an execution fails. By default, it will log the error and show a generic failure toast with an action to retry.
- `options.onData` is a function called when an execution succeeds.
- `options.onWillExecute` is a function called when an execution will start.
- `options.failureToastOptions` are the options to customize the title, message, and primary action of the failure toast.

### Return

Expand Down
4 changes: 3 additions & 1 deletion docs/utils-reference/react-hooks/useStreamJSON.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function useStreamJSON<T, U>(
onError?: (error: Error) => void;
onData?: (data: T) => void;
onWillExecute?: (args: [string, RequestInit]) => void;
failureToastOptions?: Partial<Pick<Toast.Options, "title" | "primaryAction" | "message">>;
},
): AsyncState<Result<T>> & {
revalidate: () => void;
Expand Down Expand Up @@ -48,7 +49,8 @@ Including the [usePromise](./usePromise.md)'s options:
- `options.execute` is a boolean to indicate whether to actually execute the function or not. This is useful for cases where one of the function's arguments depends on something that might not be available right away (for example, depends on some user inputs). Because React requires every hook to be defined on the render, this flag enables you to define the hook right away but wait until you have all the arguments ready to execute the function.
- `options.onError` is a function called when an execution fails. By default, it will log the error and show a generic failure toast with an action to retry.
- `options.onData` is a function called when an execution succeeds.
- `options.onWillExecute` is a function called when an execution will start..
- `options.onWillExecute` is a function called when an execution will start.
- `options.failureToastOptions` are the options to customize the title, message, and primary action of the failure toast.

### Return

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@raycast/utils",
"version": "1.16.4",
"version": "1.16.5",
"description": "Set of utilities to streamline building Raycast extensions",
"author": "Raycast Technologies Ltd.",
"homepage": "https://developers.raycast.com/utils-reference",
Expand Down
3 changes: 2 additions & 1 deletion src/useExec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export function useExec<T, U = undefined>(
} & ExecOptions &
ExecCachedPromiseOptions<T, U>,
): UseCachedPromiseReturnType<T, U> {
const { parseOutput, input, onData, onWillExecute, initialData, execute, keepPreviousData, onError, ...execOptions } =
const { parseOutput, input, onData, onWillExecute, initialData, execute, keepPreviousData, onError, failureToastOptions, ...execOptions } =
Array.isArray(optionsOrArgs) ? options || {} : optionsOrArgs || {};

const useCachedPromiseOptions: ExecCachedPromiseOptions<T, U> = {
Expand All @@ -180,6 +180,7 @@ export function useExec<T, U = undefined>(
onError,
onData,
onWillExecute,
failureToastOptions,
};

const abortable = useRef<AbortController>();
Expand Down
2 changes: 2 additions & 0 deletions src/useStreamJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ export function useStreamJSON<T, U extends any[] = any[]>(
onError,
onData,
onWillExecute,
failureToastOptions,
dataPath,
filter,
transform,
Expand All @@ -407,6 +408,7 @@ export function useStreamJSON<T, U extends any[] = any[]>(
onError,
onData,
onWillExecute,
failureToastOptions,
};

const generatorRef = useRef<AsyncGenerator<T extends unknown[] ? T : T[]> | null>(null);
Expand Down
Loading