remix-toast-v2
Version:
Utility functions for server-side toast notifications
217 lines (212 loc) • 14.7 kB
TypeScript
import * as _remix_run_server_runtime from '@remix-run/server-runtime';
import { SessionIdStorageStrategy, SessionStorage } from '@remix-run/server-runtime';
import { z } from 'zod';
declare const toastMessageSchema: z.ZodObject<{
message: z.ZodString;
description: z.ZodOptional<z.ZodString>;
duration: z.ZodOptional<z.ZodNumber>;
type: z.ZodType<"info" | "success" | "error" | "warning", z.ZodTypeDef, "info" | "success" | "error" | "warning">;
}, "strip", z.ZodTypeAny, {
message: string;
type: "info" | "success" | "error" | "warning";
description?: string | undefined;
duration?: number | undefined;
}, {
message: string;
type: "info" | "success" | "error" | "warning";
description?: string | undefined;
duration?: number | undefined;
}>;
type ToastMessage = z.infer<typeof toastMessageSchema>;
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 jsons with toast notifications
*/
declare const createToastUtilsWithCustomSession: (session: SessionStorage) => {
jsonWithToast: <T>(data: T, toast: ToastMessage, init?: ResponseInit) => Promise<_remix_run_server_runtime.TypedResponse<T>>;
jsonWithSuccess: <T_1>(data: T_1, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<T_1>>;
jsonWithError: <T_1>(data: T_1, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<T_1>>;
jsonWithInfo: <T_1>(data: T_1, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<T_1>>;
jsonWithWarning: <T_1>(data: T_1, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<T_1>>;
redirectWithToast: (redirectUrl: string, toast: ToastMessage, init?: ResponseInit) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
redirectWithSuccess: (redirectUrl: string, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
redirectWithError: (redirectUrl: string, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
redirectWithInfo: (redirectUrl: string, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
redirectWithWarning: (redirectUrl: string, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
replaceWithToast: (redirectUrl: string, toast: ToastMessage, init?: ResponseInit) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
replaceWithSuccess: (redirectUrl: string, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
replaceWithError: (redirectUrl: string, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
replaceWithInfo: (redirectUrl: string, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
replaceWithWarning: (redirectUrl: string, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
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 jsonWithToast: <T>(data: T, toast: ToastMessage, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<T>>;
/**
* Helper method used to generate a JSON 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 JSON response object with the specified success toast message.
*/
declare const jsonWithSuccess: <T>(data: T, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<T>>;
/**
* Helper method used to generate a JSON 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 JSON response object with the specified error toast message.
*/
declare const jsonWithError: <T>(data: T, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<T>>;
/**
* Helper method used to generate a JSON 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 JSON response object with the specified info toast message.
*/
declare const jsonWithInfo: <T>(data: T, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<T>>;
/**
* Helper method used to generate a JSON 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 JSON response object with the specified warning toast message.
*/
declare const jsonWithWarning: <T>(data: T, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<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<_remix_run_server_runtime.TypedResponse<never>>;
/**
* 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 | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
/**
* 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 | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
/**
* 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 | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
/**
* 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 | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
/**
* Helper method used to redirect the user to a new page with a toast notification while replacing the url in the browser history
*
* 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 replaceWithToast: (redirectUrl: string, toast: ToastMessage, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
/**
* Helper method used to redirect the user to a new page with an error toast notification while replacing the url in the browser history
*
* 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 replaceWithError: (redirectUrl: string, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
/**
* 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 replaceWithSuccess: (redirectUrl: string, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
/**
* Helper method used to redirect the user to a new page with a warning toast notification while replacing the url in the browser history
*
* 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 replaceWithWarning: (redirectUrl: string, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
/**
* Helper method used to redirect the user to a new page with a info toast notification while replacing the url in the browser history
*
* 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 replaceWithInfo: (redirectUrl: string, messageOrToast: string | Omit<ToastMessage, "type">, init?: ResponseInit, customSession?: SessionStorage) => Promise<_remix_run_server_runtime.TypedResponse<never>>;
export { ToastCookieOptions, ToastMessage, createToastUtilsWithCustomSession, getToast, jsonWithError, jsonWithInfo, jsonWithSuccess, jsonWithToast, jsonWithWarning, redirectWithError, redirectWithInfo, redirectWithSuccess, redirectWithToast, redirectWithWarning, replaceWithError, replaceWithInfo, replaceWithSuccess, replaceWithToast, replaceWithWarning, setToastCookieOptions };