remix-toast
Version:
Utility functions for server-side toast notifications
42 lines (40 loc) • 1.41 kB
JavaScript
import {
FLASH_SESSION,
getToast,
sessionStorage
} from "../chunk-C2TP66GL.js";
// src/middleware/index.ts
import { createContext } from "react-router";
var toastContext = createContext(null);
var sessionToastContext = 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;
export {
getToast2 as getToast,
setToast,
toastMiddleware,
unstable_toastMiddleware
};