remix-toast
Version:
Utility functions for server-side toast notifications
201 lines (196 loc) • 13.1 kB
TypeScript
import * as react_router from 'react-router';
import { SessionIdStorageStrategy, SessionStorage } from 'react-router';
import { T as ToastMessage, a as ToastMessageWithoutType } from './schema-797d9a10.js';
import 'zod';
type ToastCookieOptions = Partial<SessionIdStorageStrategy["cookie"]>;
/**
* Sets the cookie options to be used for the toast cookie
*
* @param options Cookie options to be used for the toast cookie
*/
declare function setToastCookieOptions(options: ToastCookieOptions): void;
/**
* Helper method used to get the toast data from the current request and purge the flash storage from the session
* @param request Current request
* @returns Returns the the toast notification if exists, undefined otherwise and the headers needed to purge it from the session
*/
declare function getToast(request: Request, customSession?: SessionStorage): Promise<{
toast: ToastMessage | undefined;
headers: Headers;
}>;
/**
* Helper method used to initialize the whole library using a custom session. Returns all the utilities enhanced with the custom session
* you provide.
*
* These utilities will not override the default session, but will use the custom one you provide. So be careful of imports if you plan to
* use both, or only plan to use this one.
* @param session Custom session to be used instead of the default one
* @returns Returns all the utilities you need to display toast notifications and redirect the user or return data functions with toast notifications
*/
declare const createToastUtilsWithCustomSession: (session: SessionStorage) => {
dataWithToast: <T>(data: T, toast: ToastMessage, init?: ResponseInit) => Promise<react_router.UNSAFE_DataWithResponseInit<T>>;
dataWithSuccess: <T>(data: T, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<react_router.UNSAFE_DataWithResponseInit<T>>;
dataWithError: <T>(data: T, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<react_router.UNSAFE_DataWithResponseInit<T>>;
dataWithInfo: <T>(data: T, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<react_router.UNSAFE_DataWithResponseInit<T>>;
dataWithWarning: <T>(data: T, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<react_router.UNSAFE_DataWithResponseInit<T>>;
redirectWithToast: (redirectUrl: string, toast: ToastMessage, init?: ResponseInit) => Promise<Response>;
replaceWithToast: (replaceUrl: string, toast: ToastMessage, init?: ResponseInit) => Promise<Response>;
redirectWithSuccess: (redirectUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
redirectWithError: (redirectUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
redirectWithInfo: (redirectUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
redirectWithWarning: (redirectUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
replaceWithSuccess: (replaceUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
replaceWithError: (replaceUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
replaceWithInfo: (replaceUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
replaceWithWarning: (replaceUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
getToast: (request: Request) => Promise<{
toast: ToastMessage | undefined;
headers: Headers;
}>;
};
/**
* Helper method used to display a toast notification without redirection
*
* @param data Generic object containing the data
* @param toast Toast message and it's type
* @param init Additional response options (status code, additional headers etc)
* @returns Returns data with toast cookie set
*/
declare const dataWithToast: <T>(data: T, toast: ToastMessage, init?: ResponseInit, customSession?: SessionStorage) => Promise<react_router.UNSAFE_DataWithResponseInit<T>>;
/**
* Helper method used to generate a data response object with a success toast message.
*
* @param data The data to be included in the response.
* @param message The message for the success toast notification.
* @param init Additional response options (status code, additional headers etc)
* @returns Returns a data response object with the specified success toast message.
*/
declare const dataWithSuccess: <T>(data: T, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<react_router.UNSAFE_DataWithResponseInit<T>>;
/**
* Helper method used to generate a data response object with an error toast message.
*
* @param data The data to be included in the response.
* @param message The message for the error toast notification.
* @param init Additional response options (status code, additional headers etc)
* @returns Returns a data response object with the specified error toast message.
*/
declare const dataWithError: <T>(data: T, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<react_router.UNSAFE_DataWithResponseInit<T>>;
/**
* Helper method used to generate a data response object with an info toast message.
*
* @param data The data to be included in the response.
* @param message The message for the info toast notification.
* @param init Additional response options (status code, additional headers etc)
* @returns Returns a data response object with the specified info toast message.
*/
declare const dataWithInfo: <T>(data: T, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<react_router.UNSAFE_DataWithResponseInit<T>>;
/**
* Helper method used to generate a data response object with a warning toast message.
*
* @param data The data to be included in the response.
* @param message The message for the warning toast notification.
* @param init Additional response options (status code, additional headers etc)
* @returns Returns a data response object with the specified warning toast message.
*/
declare const dataWithWarning: <T>(data: T, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<react_router.UNSAFE_DataWithResponseInit<T>>;
/**
* Helper method used to redirect the user to a new page with a toast notification
*
* If thrown it needs to be awaited
* @param url Redirect URL
* @param toast Toast message and it's type
* @param init Additional response options (status code, additional headers etc)
* @returns Returns redirect response with toast cookie set
*/
declare const redirectWithToast: (redirectUrl: string, toast: ToastMessage, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
/**
* Helper method used to replace the current route with a toast notification
*
* If thrown it needs to be awaited
* @param replaceUrl Replace URL
* @param toast Toast message and it's type
* @param init Additional response options (status code, additional headers etc)
* @returns Returns replace response with toast cookie set
*/
declare const replaceWithToast: (replaceUrl: string, toast: ToastMessage, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
/**
* Helper method used to redirect the user to a new page with an error toast notification
*
* If this method is thrown it needs to be awaited, otherwise it can just be returned
* @param redirectUrl Redirect url
* @param message Message to be shown as info
* @param init Additional response options (status code, additional headers etc)
* @returns Returns redirect response with toast cookie set
*/
declare const redirectWithError: (redirectUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
/**
* Helper method used to replace the current route with an error toast notification
*
* If this method is thrown it needs to be awaited, otherwise it can just be returned
* @param replaceUrl Replace url
* @param message Message to be shown as info
* @param init Additional response options (status code, additional headers etc)
* @returns Returns replace response with toast cookie set
*/
declare const replaceWithError: (replaceUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
/**
* Helper method used to redirect the user to a new page with a success toast notification
*
* If this method is thrown it needs to be awaited, otherwise it can just be returned
* @param redirectUrl Redirect url
* @param message Message to be shown as info
* @param init Additional response options (status code, additional headers etc)
* @returns Returns redirect response with toast cookie set
*/
declare const redirectWithSuccess: (redirectUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
/**
* Helper method used to replace the current route with a success toast notification
*
* If this method is thrown it needs to be awaited, otherwise it can just be returned
* @param replaceUrl Replace url
* @param message Message to be shown as info
* @param init Additional response options (status code, additional headers etc)
* @returns Returns replace response with toast cookie set
*/
declare const replaceWithSuccess: (replaceUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
/**
* Helper method used to redirect the user to a new page with a warning toast notification
*
* If this method is thrown it needs to be awaited, otherwise it can just be returned
* @param redirectUrl Redirect url
* @param message Message to be shown as info
* @param init Additional response options (status code, additional headers etc)
* @returns Returns redirect response with toast cookie set
*/
declare const redirectWithWarning: (redirectUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
/**
* Helper method used to replace the current route with a warning toast notification
*
* If this method is thrown it needs to be awaited, otherwise it can just be returned
* @param replaceUrl Replace url
* @param message Message to be shown as info
* @param init Additional response options (status code, additional headers etc)
* @returns Returns replace response with toast cookie set
*/
declare const replaceWithWarning: (replaceUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
/**
* Helper method used to redirect the user to a new page with a info toast notification
*
* If this method is thrown it needs to be awaited, otherwise it can just be returned
* @param redirectUrl Redirect url
* @param message Message to be shown as info
* @param init Additional response options (status code, additional headers etc)
* @returns Returns redirect response with toast cookie set
*/
declare const redirectWithInfo: (redirectUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
/**
* Helper method used to replace the current route with an info toast notification
*
* If this method is thrown it needs to be awaited, otherwise it can just be returned
* @param replaceUrl Replace url
* @param message Message to be shown as info
* @param init Additional response options (status code, additional headers etc)
* @returns Returns replace response with toast cookie set
*/
declare const replaceWithInfo: (replaceUrl: string, messageOrToast: string | ToastMessageWithoutType, init?: ResponseInit, customSession?: SessionStorage) => Promise<Response>;
export { ToastCookieOptions, ToastMessage, createToastUtilsWithCustomSession, dataWithError, dataWithInfo, dataWithSuccess, dataWithToast, dataWithWarning, getToast, redirectWithError, redirectWithInfo, redirectWithSuccess, redirectWithToast, redirectWithWarning, replaceWithError, replaceWithInfo, replaceWithSuccess, replaceWithToast, replaceWithWarning, setToastCookieOptions };