remix-toast
Version:
Utility functions for server-side toast notifications
175 lines (170 loc) • 6.98 kB
JavaScript
;
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/index.ts
var src_exports = {};
__export(src_exports, {
createToastUtilsWithCustomSession: () => createToastUtilsWithCustomSession,
dataWithError: () => dataWithError,
dataWithInfo: () => dataWithInfo,
dataWithSuccess: () => dataWithSuccess,
dataWithToast: () => dataWithToast,
dataWithWarning: () => dataWithWarning,
getToast: () => getToast,
redirectWithError: () => redirectWithError,
redirectWithInfo: () => redirectWithInfo,
redirectWithSuccess: () => redirectWithSuccess,
redirectWithToast: () => redirectWithToast,
redirectWithWarning: () => redirectWithWarning,
setToastCookieOptions: () => setToastCookieOptions
});
module.exports = __toCommonJS(src_exports);
var import_react_router2 = require("react-router");
// src/schema.ts
var import_zod = require("zod");
var toastMessageSchema = import_zod.z.object({
message: import_zod.z.string(),
description: import_zod.z.string().optional(),
duration: import_zod.z.number().int().positive().optional(),
type: import_zod.z.custom()
}).passthrough();
var toastMessageWithoutTypeSchema = import_zod.z.object({
message: import_zod.z.string(),
description: import_zod.z.string().optional(),
duration: import_zod.z.number().int().positive().optional()
}).passthrough();
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
function setToastCookieOptions(options) {
Object.assign(toastCookieOptions, options);
Object.assign(
sessionStorage,
(0, import_react_router2.createCookieSessionStorage)({
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 (0, import_react_router2.redirect)(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);
};
};
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" });
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createToastUtilsWithCustomSession,
dataWithError,
dataWithInfo,
dataWithSuccess,
dataWithToast,
dataWithWarning,
getToast,
redirectWithError,
redirectWithInfo,
redirectWithSuccess,
redirectWithToast,
redirectWithWarning,
setToastCookieOptions
});