Skip to content

An opinionated toast component for React.

Handle async functions in `toast.promise` results (#389)

Arnau Jiménez497eccd9c65d
diff --git a/src/state.ts b/src/state.ts
index 601f92f..67260e7 100644
--- a/src/state.ts
+++ b/src/state.ts
@@ -125,32 +125,29 @@ class Observer {
let shouldDismiss = id !== undefined;
- p.then((response) => {
- // TODO: Clean up TS here, response has incorrect type
- // @ts-expect-error
- if (response && typeof response.ok === 'boolean' && !response.ok) {
+ p.then(async (response) => {
+ if (isHttpResponse(response) && !response.ok) {
shouldDismiss = false;
const message =
- // @ts-expect-error
- typeof data.error === 'function' ? data.error(`HTTP error! status: ${response.status}`) : data.error;
+ typeof data.error === 'function' ? await data.error(`HTTP error! status: ${response.status}`) : data.error;
const description =
typeof data.description === 'function'
- ? // @ts-expect-error
- data.description(`HTTP error! status: ${response.status}`)
+ ? await data.description(`HTTP error! status: ${response.status}`)
: data.description;
this.create({ id, type: 'error', message, description });
} else if (data.success !== undefined) {
shouldDismiss = false;
- const message = typeof data.success === 'function' ? data.success(response) : data.success;
- const description = typeof data.description === 'function' ? data.description(response) : data.description;
+ const message = typeof data.success === 'function' ? await data.success(response) : data.success;
+ const description =
+ typeof data.description === 'function' ? await data.description(response) : data.description;
this.create({ id, type: 'success', message, description });
}
})
- .catch((error) => {
+ .catch(async (error) => {
if (data.error !== undefined) {
shouldDismiss = false;
- const message = typeof data.error === 'function' ? data.error(error) : data.error;
- const description = typeof data.description === 'function' ? data.description(error) : data.description;
+ const message = typeof data.error === 'function' ? await data.error(error) : data.error;
+ const description = typeof data.description === 'function' ? await data.description(error) : data.description;
this.create({ id, type: 'error', message, description });
}
})
@@ -188,6 +185,17 @@ const toastFunction = (message: string | React.ReactNode, data?: ExternalToast)
return id;
};
+const isHttpResponse = (data: any): data is Response => {
+ return (
+ data &&
+ typeof data === 'object' &&
+ 'ok' in data &&
+ typeof data.ok === 'boolean' &&
+ 'status' in data &&
+ typeof data.status === 'number'
+ );
+};
+
const basicToast = toastFunction;
const getHistory = () => ToastState.toasts;
diff --git a/src/types.ts b/src/types.ts
index 4d7d216..b654ffe 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -4,13 +4,18 @@ export type ToastTypes = 'normal' | 'action' | 'success' | 'info' | 'warning' |
export type PromiseT<Data = any> = Promise<Data> | (() => Promise<Data>);
+export type PromiseTResult<Data = any> =
+ | string
+ | React.ReactNode
+ | ((data: Data) => React.ReactNode | string | Promise<React.ReactNode | string>);
+
export type PromiseExternalToast = Omit<ExternalToast, 'description'>;
export type PromiseData<ToastData = any> = PromiseExternalToast & {
loading?: string | React.ReactNode;
- success?: string | React.ReactNode | ((data: ToastData) => React.ReactNode | string);
- error?: string | React.ReactNode | ((error: any) => React.ReactNode | string);
- description?: string | React.ReactNode | ((data: any) => React.ReactNode | string);
+ success?: PromiseTResult<ToastData>;
+ error?: PromiseTResult;
+ description?: PromiseTResult;
finally?: () => void | Promise<void>;
};