UNPKG

remix-utils

Version:

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

42 lines (41 loc) 1.71 kB
import type { unstable_MiddlewareFunction, unstable_RouterContextProvider } from "react-router"; /** * Creates a context storage middleware that stores the context, and request, * in an AsyncLocalStorage. * * This is useful for accessing the context in any part of the application * inside loaders or actions, but without needing to pass the context around * from loaders or actions to those functions. * * After the middleware is added, any function that needs the context can * call the `getContext` function to retrieve the context from the storage. * * @returns The context storage middleware and a function to get the context from the storage * @example * // app/middlewares/context-storage.ts * import { unstable_createContextStorageMiddleware } from "remix-utils"; * * export const [contextStorageMiddleware, getContext, getRequest] = unstable_createContextStorageMiddleware(); * * // app/root.tsx * import { contextStorageMiddleware } from "~/middlewares/context-storage"; * export const unstable_middleware = [contextStorageMiddleware]; * * // app/helpers/authenticate.ts * import { getContext } from "~/middlewares/context-storage"; * import { getSession } from "~/middlewares/session"; * * export function authenticate() { * let context = getContext(); * let session = getSession(context); * // code to authenticate the user * } */ export declare function unstable_createContextStorageMiddleware(): unstable_createContextStorageMiddleware.ReturnType; export declare namespace unstable_createContextStorageMiddleware { type ReturnType = [ unstable_MiddlewareFunction<Response>, () => unstable_RouterContextProvider, () => Request ]; }