remix-toast-v2
Version:
Utility functions for server-side toast notifications
214 lines (209 loc) • 8.82 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,
getToast: () => getToast,
jsonWithError: () => jsonWithError,
jsonWithInfo: () => jsonWithInfo,
jsonWithSuccess: () => jsonWithSuccess,
jsonWithToast: () => jsonWithToast,
jsonWithWarning: () => jsonWithWarning,
redirectWithError: () => redirectWithError,
redirectWithInfo: () => redirectWithInfo,
redirectWithSuccess: () => redirectWithSuccess,
redirectWithToast: () => redirectWithToast,
redirectWithWarning: () => redirectWithWarning,
replaceWithError: () => replaceWithError,
replaceWithInfo: () => replaceWithInfo,
replaceWithSuccess: () => replaceWithSuccess,
replaceWithToast: () => replaceWithToast,
replaceWithWarning: () => replaceWithWarning,
setToastCookieOptions: () => setToastCookieOptions
});
module.exports = __toCommonJS(src_exports);
var import_server_runtime = require("@remix-run/server-runtime");
// 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()
});
var flashSessionValuesSchema = import_zod.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 = (0, import_server_runtime.createCookieFactory)({ sign, unsign });
var toastCookieOptions = {
name: "toast-session",
sameSite: "lax",
path: "/",
httpOnly: true,
secrets: ["s3Cr3t"]
};
var sessionStorage = (0, import_server_runtime.createCookieSessionStorageFactory)(createCookie)({
cookie: toastCookieOptions
});
function setToastCookieOptions(options) {
Object.assign(toastCookieOptions, options);
Object.assign(
sessionStorage,
(0, import_server_runtime.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 (0, import_server_runtime.redirect)(url, {
...init,
headers: await flashMessage(flash, init == null ? void 0 : init.headers, customSession)
});
}
async function replaceWithFlash(url, flash, init, customSession) {
return (0, import_server_runtime.replace)(url, {
...init,
headers: await flashMessage(flash, init == null ? void 0 : init.headers, customSession)
});
}
async function jsonWithFlash(data, flash, init, customSession) {
return (0, import_server_runtime.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" });
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createToastUtilsWithCustomSession,
getToast,
jsonWithError,
jsonWithInfo,
jsonWithSuccess,
jsonWithToast,
jsonWithWarning,
redirectWithError,
redirectWithInfo,
redirectWithSuccess,
redirectWithToast,
redirectWithWarning,
replaceWithError,
replaceWithInfo,
replaceWithSuccess,
replaceWithToast,
replaceWithWarning,
setToastCookieOptions
});