UNPKG

remix-utils

Version:

This package contains simple utility functions to use with [React Router](https://reactrouter.com/).

52 lines (51 loc) 2.53 kB
import type { Session, SessionData, SessionStorage, unstable_MiddlewareFunction, unstable_RouterContextProvider } from "react-router"; /** * Creates a session middleware that uses the provided session storage object * to manage session data. * * The session middleware will read the session data from the request cookies, * and store it in the context for the rest of the middleware or loaders and * actions to use. * * After the middleware chain is done, the session data will be compared to the * initial data, and if it changed, the session will be commited to the storage * and a Set-Cookie header will be added to the response. * * This middleware also helps implement rolling sessions by setting the session * cookie on every response. * * @param sessionStorage The session storage object used by the middleware * @param shouldCommit A function that compares the previous and next session data * to determine if it changed and needs to be commited. Defaults to a function that always returns true. * @returns The session middleware and a function to get the session from the context * * @example * // app/middlewares/session.ts * import { sessionStorage } from "~/session"; * import { unstable_createSessionMiddleware } from "remix-utils"; * * export const [sessionMiddleware, getSession] = * unstable_createSessionMiddleware(sessionStorage); * * // app/root.tsx * import { sessionMiddleware } from "~/middlewares/session"; * export const unstable_middleware = [sessionMiddleware]; * * // app/routes/_index.tsx * import { getSession } from "~/middlewares/session"; * * export async function loader({ context }: Route.LoaderArgs) { * let session = getSession(context); * session.set("key", "value"); // This will be commited automatically * return { data: session.get("data") }; * } */ export declare function unstable_createSessionMiddleware<Data = SessionData, FlashData = Data>(sessionStorage: SessionStorage<Data, FlashData>, shouldCommit?: unstable_createSessionMiddleware.ShouldCommitFunction<Data>): unstable_createSessionMiddleware.ReturnType<Data, FlashData>; export declare namespace unstable_createSessionMiddleware { type GetSessionFunction<Data, FlashData> = (context: unstable_RouterContextProvider) => Session<Data, FlashData>; type ShouldCommitFunction<Data> = (prev: Partial<Data>, next: Partial<Data>) => boolean; type ReturnType<Data, FlashData> = [ unstable_MiddlewareFunction<Response>, GetSessionFunction<Data, FlashData> ]; }