UNPKG

remix-toast

Version:

Utility functions for server-side toast notifications

171 lines (164 loc) 7.07 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/middleware/index.ts var middleware_exports = {}; __export(middleware_exports, { getToast: () => getToast2, setToast: () => setToast, toastMiddleware: () => toastMiddleware, unstable_toastMiddleware: () => unstable_toastMiddleware }); module.exports = __toCommonJS(middleware_exports); var import_react_router3 = require("react-router"); // src/index.ts var import_react_router2 = require("react-router"); // src/schema.ts var import_zod = require("zod"); var toastMessageSchema = import_zod.z.looseObject({ message: import_zod.z.string(), description: import_zod.z.string().optional(), duration: import_zod.z.number().int().nonnegative().optional(), type: import_zod.z.custom() }); var toastMessageWithoutTypeSchema = import_zod.z.looseObject({ message: import_zod.z.string(), description: import_zod.z.string().optional(), duration: import_zod.z.number().int().nonnegative().optional() }); var flashSessionValuesSchema = import_zod.z.object({ toast: toastMessageSchema.optional() }); var FLASH_SESSION = "flash"; // src/session.ts var import_react_router = require("react-router"); var toastCookieOptions = { name: "toast-session", sameSite: "lax", path: "/", httpOnly: true, secrets: ["s3Cr3t"] }; var sessionStorage = (0, import_react_router.createCookieSessionStorage)({ cookie: toastCookieOptions }); // src/index.ts 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 (0, import_react_router2.redirect)(url, { ...init, headers: await flashMessage(flash, init == null ? void 0 : init.headers, customSession) }); } async function replaceWithFlash(url, flash, init, customSession) { return (0, import_react_router2.replace)(url, { ...init, headers: await flashMessage(flash, init == null ? void 0 : init.headers, customSession) }); } async function dataWithFlash(data, flash, init, customSession) { return (0, import_react_router2.data)(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); }; }; var replaceWithToastFactory = ({ type, session }) => { return (replaceUrl, messageOrToast, init, customSession) => { const finalInfo = typeof messageOrToast === "string" ? { message: messageOrToast } : messageOrToast; return replaceWithFlash(replaceUrl, { 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 dataWithSuccess = dataWithToastFactory({ type: "success" }); var dataWithError = dataWithToastFactory({ type: "error" }); var dataWithInfo = dataWithToastFactory({ type: "info" }); var dataWithWarning = dataWithToastFactory({ type: "warning" }); var redirectWithError = redirectWithToastFactory({ type: "error" }); var replaceWithError = replaceWithToastFactory({ type: "error" }); var redirectWithSuccess = redirectWithToastFactory({ type: "success" }); var replaceWithSuccess = replaceWithToastFactory({ type: "success" }); var redirectWithWarning = redirectWithToastFactory({ type: "warning" }); var replaceWithWarning = replaceWithToastFactory({ type: "warning" }); var redirectWithInfo = redirectWithToastFactory({ type: "info" }); var replaceWithInfo = replaceWithToastFactory({ type: "info" }); // src/middleware/index.ts var toastContext = (0, import_react_router3.createContext)(null); var sessionToastContext = (0, import_react_router3.createContext)(null); function toastMiddleware(props) { const { customSession } = props || {}; const sessionToUse = customSession || sessionStorage; return async function toastMiddleware2({ request, context }, next) { var _a; const { toast, headers } = await getToast(request, customSession); context.set(toastContext, toast != null ? toast : null); const res = await next(); if (res instanceof Response && toast) { res.headers.append("Set-Cookie", (_a = headers.get("Set-Cookie")) != null ? _a : ""); } const toastToSet = context.get(sessionToastContext); if (res instanceof Response && toastToSet) { const session = await sessionToUse.getSession(request.headers.get("Cookie")); session.flash(FLASH_SESSION, { toast: toastToSet }); res.headers.append("Set-Cookie", await sessionToUse.commitSession(session)); } return res; }; } var setToast = (context, toast) => { context.set(sessionToastContext, toast); }; var getToast2 = (context) => context.get(toastContext); var unstable_toastMiddleware = toastMiddleware; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getToast, setToast, toastMiddleware, unstable_toastMiddleware });