UNPKG

remix-toast

Version:

Utility functions for server-side toast notifications

141 lines (136 loc) 5.31 kB
// src/index.ts import { createCookieSessionStorage as createCookieSessionStorage2, data as dataFn, redirect } from "react-router"; // src/schema.ts import { z } from "zod"; var toastMessageSchema = z.object({ message: z.string(), description: z.string().optional(), duration: z.number().int().positive().optional(), type: z.custom() }).passthrough(); var toastMessageWithoutTypeSchema = z.object({ message: z.string(), description: z.string().optional(), duration: z.number().int().positive().optional() }).passthrough(); var flashSessionValuesSchema = z.object({ toast: toastMessageSchema.optional() }); var FLASH_SESSION = "flash"; // src/session.ts import { createCookieSessionStorage } from "react-router"; var toastCookieOptions = { name: "toast-session", sameSite: "lax", path: "/", httpOnly: true, secrets: ["s3Cr3t"] }; var sessionStorage = createCookieSessionStorage({ cookie: toastCookieOptions }); // src/index.ts function setToastCookieOptions(options) { Object.assign(toastCookieOptions, options); Object.assign( sessionStorage, createCookieSessionStorage2({ cookie: toastCookieOptions }) ); } async function flashMessage(flash, headers, customSession) { const sessionToUse = customSession ? customSession : sessionStorage; const session = await sessionToUse.getSession(); session.flash(FLASH_SESSION, flash); const cookie = await sessionToUse.commitSession(session); const newHeaders = new Headers(headers); newHeaders.append("Set-Cookie", cookie); return newHeaders; } async function redirectWithFlash(url, flash, init, customSession) { return redirect(url, { ...init, headers: await flashMessage(flash, init == null ? void 0 : init.headers, customSession) }); } async function dataWithFlash(data, flash, init, customSession) { return dataFn(data, { ...init, headers: await flashMessage(flash, init == null ? void 0 : init.headers, customSession) }); } var dataWithToastFactory = ({ type, session }) => { return (data, messageOrToast, init, customSession) => { const finalInfo = typeof messageOrToast === "string" ? { message: messageOrToast } : messageOrToast; return dataWithFlash(data, { toast: { ...finalInfo, type } }, init, customSession != null ? customSession : session); }; }; var redirectWithToastFactory = ({ type, session }) => { return (redirectUrl, messageOrToast, init, customSession) => { const finalInfo = typeof messageOrToast === "string" ? { message: messageOrToast } : messageOrToast; return redirectWithFlash(redirectUrl, { toast: { ...finalInfo, type } }, init, customSession != null ? customSession : session); }; }; async function getToast(request, customSession) { const sessionToUse = customSession ? customSession : sessionStorage; const cookie = request.headers.get("Cookie"); const session = await sessionToUse.getSession(cookie); const result = flashSessionValuesSchema.safeParse(session.get(FLASH_SESSION)); const flash = result.success ? result.data : void 0; const headers = new Headers({ "Set-Cookie": await sessionToUse.commitSession(session) }); const toast = flash == null ? void 0 : flash.toast; return { toast, headers }; } var createToastUtilsWithCustomSession = (session) => { return { dataWithToast: (data, toast, init) => { return dataWithFlash(data, { toast }, init, session); }, dataWithSuccess: dataWithToastFactory({ type: "success", session }), dataWithError: dataWithToastFactory({ type: "error", session }), dataWithInfo: dataWithToastFactory({ type: "info", session }), dataWithWarning: dataWithToastFactory({ type: "warning", session }), redirectWithToast: (redirectUrl, toast, init) => { return redirectWithFlash(redirectUrl, { toast }, init, session); }, redirectWithSuccess: redirectWithToastFactory({ type: "success", session }), redirectWithError: redirectWithToastFactory({ type: "error", session }), redirectWithInfo: redirectWithToastFactory({ type: "info", session }), redirectWithWarning: redirectWithToastFactory({ type: "warning", session }), getToast: (request) => getToast(request, session) }; }; var dataWithToast = (data, toast, init, customSession) => { return dataWithFlash(data, { toast }, init, customSession); }; var dataWithSuccess = dataWithToastFactory({ type: "success" }); var dataWithError = dataWithToastFactory({ type: "error" }); var dataWithInfo = dataWithToastFactory({ type: "info" }); var dataWithWarning = dataWithToastFactory({ type: "warning" }); var redirectWithToast = (redirectUrl, toast, init, customSession) => { return redirectWithFlash(redirectUrl, { toast }, init, customSession); }; var redirectWithError = redirectWithToastFactory({ type: "error" }); var redirectWithSuccess = redirectWithToastFactory({ type: "success" }); var redirectWithWarning = redirectWithToastFactory({ type: "warning" }); var redirectWithInfo = redirectWithToastFactory({ type: "info" }); export { FLASH_SESSION, sessionStorage, setToastCookieOptions, getToast, createToastUtilsWithCustomSession, dataWithToast, dataWithSuccess, dataWithError, dataWithInfo, dataWithWarning, redirectWithToast, redirectWithError, redirectWithSuccess, redirectWithWarning, redirectWithInfo };