UNPKG

remix-toast-v2

Version:

Utility functions for server-side toast notifications

178 lines (174 loc) 6.86 kB
// src/index.ts import { createCookieSessionStorageFactory, createCookieFactory, redirect, replace, json } from "@remix-run/server-runtime"; // 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() }); var flashSessionValuesSchema = z.object({ toast: toastMessageSchema.optional() }); // src/crypto.ts async function sign(value, secret) { return value + "."; } async function unsign(signed, secret) { let index = signed.lastIndexOf("."); let value = signed.slice(0, index); return value; } // src/index.ts var FLASH_SESSION = "flash"; var createCookie = createCookieFactory({ sign, unsign }); var toastCookieOptions = { name: "toast-session", sameSite: "lax", path: "/", httpOnly: true, secrets: ["s3Cr3t"] }; var sessionStorage = createCookieSessionStorageFactory(createCookie)({ cookie: toastCookieOptions }); function setToastCookieOptions(options) { Object.assign(toastCookieOptions, options); Object.assign( sessionStorage, createCookieSessionStorageFactory(createCookie)({ 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 replaceWithFlash(url, flash, init, customSession) { return replace(url, { ...init, headers: await flashMessage(flash, init == null ? void 0 : init.headers, customSession) }); } async function jsonWithFlash(data, flash, init, customSession) { return json(data, { ...init, headers: await flashMessage(flash, init == null ? void 0 : init.headers, customSession) }); } var jsonWithToastFactory = ({ type, session }) => { return (data, messageOrToast, init, customSession) => { const finalInfo = typeof messageOrToast === "string" ? { message: messageOrToast } : messageOrToast; return jsonWithFlash(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); }; }; var replaceWithToastFactory = ({ type, session }) => { return (redirectUrl, messageOrToast, init, customSession) => { const finalInfo = typeof messageOrToast === "string" ? { message: messageOrToast } : messageOrToast; return replaceWithFlash(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 { jsonWithToast: (data, toast, init) => { return jsonWithFlash(data, { toast }, init, session); }, jsonWithSuccess: jsonWithToastFactory({ type: "success", session }), jsonWithError: jsonWithToastFactory({ type: "error", session }), jsonWithInfo: jsonWithToastFactory({ type: "info", session }), jsonWithWarning: jsonWithToastFactory({ 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 }), replaceWithToast: (redirectUrl, toast, init) => { return replaceWithFlash(redirectUrl, { toast }, init, session); }, replaceWithSuccess: replaceWithToastFactory({ type: "success", session }), replaceWithError: replaceWithToastFactory({ type: "error", session }), replaceWithInfo: replaceWithToastFactory({ type: "info", session }), replaceWithWarning: replaceWithToastFactory({ type: "warning", session }), getToast: (request) => getToast(request, session) }; }; var jsonWithToast = (data, toast, init, customSession) => { return jsonWithFlash(data, { toast }, init, customSession); }; var jsonWithSuccess = jsonWithToastFactory({ type: "success" }); var jsonWithError = jsonWithToastFactory({ type: "error" }); var jsonWithInfo = jsonWithToastFactory({ type: "info" }); var jsonWithWarning = jsonWithToastFactory({ 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" }); var replaceWithToast = (redirectUrl, toast, init, customSession) => { return replaceWithFlash(redirectUrl, { toast }, init, customSession); }; var replaceWithError = replaceWithToastFactory({ type: "error" }); var replaceWithSuccess = replaceWithToastFactory({ type: "success" }); var replaceWithWarning = replaceWithToastFactory({ type: "warning" }); var replaceWithInfo = replaceWithToastFactory({ type: "info" }); export { createToastUtilsWithCustomSession, getToast, jsonWithError, jsonWithInfo, jsonWithSuccess, jsonWithToast, jsonWithWarning, redirectWithError, redirectWithInfo, redirectWithSuccess, redirectWithToast, redirectWithWarning, replaceWithError, replaceWithInfo, replaceWithSuccess, replaceWithToast, replaceWithWarning, setToastCookieOptions };