UNPKG

5.67 kBTypeScriptView Raw
1
2import { Cookie, CookieOptions } from "./cookies.js";
3import { CookieParseOptions, CookieSerializeOptions } from "cookie-es";
4
5//#region lib/server-runtime/sessions.d.ts
6/**
7 * An object of name/value pairs to be used in the session.
8 */
9interface SessionData {
10 [name: string]: any;
11}
12/**
13 * Session persists data across HTTP requests.
14 *
15 * @see https://reactrouter.com/explanation/sessions-and-cookies#sessions
16 */
17interface Session<Data = SessionData, FlashData = Data> {
18 /**
19 * A unique identifier for this session.
20 *
21 * Note: This will be the empty string for newly created sessions and
22 * sessions that are not backed by a database (i.e. cookie-based sessions).
23 */
24 readonly id: string;
25 /**
26 * The raw data contained in this session.
27 *
28 * This is useful mostly for SessionStorage internally to access the raw
29 * session data to persist.
30 */
31 readonly data: FlashSessionData<Data, FlashData>;
32 /**
33 * Returns `true` if the session has a value for the given `name`, `false`
34 * otherwise.
35 */
36 has(name: (keyof Data | keyof FlashData) & string): boolean;
37 /**
38 * Returns the value for the given `name` in this session.
39 */
40 get<Key extends (keyof Data | keyof FlashData) & string>(name: Key): (Key extends keyof Data ? Data[Key] : undefined) | (Key extends keyof FlashData ? FlashData[Key] : undefined) | undefined;
41 /**
42 * Sets a value in the session for the given `name`.
43 */
44 set<Key extends keyof Data & string>(name: Key, value: Data[Key]): void;
45 /**
46 * Sets a value in the session that is only valid until the next `get()`.
47 * This can be useful for temporary values, like error messages.
48 */
49 flash<Key extends keyof FlashData & string>(name: Key, value: FlashData[Key]): void;
50 /**
51 * Removes a value from the session.
52 */
53 unset(name: keyof Data & string): void;
54}
55type FlashSessionData<Data, FlashData> = Partial<Data & { [Key in keyof FlashData as FlashDataKey<Key & string>]: FlashData[Key] }>;
56type FlashDataKey<Key extends string> = `__flash_${Key}__`;
57type CreateSessionFunction = <Data = SessionData, FlashData = Data>(initialData?: Data, id?: string) => Session<Data, FlashData>;
58/**
59 * Creates a new Session object.
60 *
61 * Note: This function is typically not invoked directly by application code.
62 * Instead, use a `SessionStorage` object's `getSession` method.
63 */
64declare const createSession: CreateSessionFunction;
65type IsSessionFunction = (object: any) => object is Session;
66/**
67 * Returns true if an object is a React Router session.
68 *
69 * @see https://reactrouter.com/api/utils/isSession
70 */
71declare const isSession: IsSessionFunction;
72/**
73 * SessionStorage stores session data between HTTP requests and knows how to
74 * parse and create cookies.
75 *
76 * A SessionStorage creates Session objects using a `Cookie` header as input.
77 * Then, later it generates the `Set-Cookie` header to be used in the response.
78 */
79interface SessionStorage<Data = SessionData, FlashData = Data> {
80 /**
81 * Parses a Cookie header from a HTTP request and returns the associated
82 * Session. If there is no session associated with the cookie, this will
83 * return a new Session with no data.
84 */
85 getSession: (cookieHeader?: string | null, options?: CookieParseOptions) => Promise<Session<Data, FlashData>>;
86 /**
87 * Stores all data in the Session and returns the Set-Cookie header to be
88 * used in the HTTP response.
89 */
90 commitSession: (session: Session<Data, FlashData>, options?: CookieSerializeOptions) => Promise<string>;
91 /**
92 * Deletes all data associated with the Session and returns the Set-Cookie
93 * header to be used in the HTTP response.
94 */
95 destroySession: (session: Session<Data, FlashData>, options?: CookieSerializeOptions) => Promise<string>;
96}
97/**
98 * SessionIdStorageStrategy is designed to allow anyone to easily build their
99 * own SessionStorage using `createSessionStorage(strategy)`.
100 *
101 * This strategy describes a common scenario where the session id is stored in
102 * a cookie but the actual session data is stored elsewhere, usually in a
103 * database or on disk. A set of create, read, update, and delete operations
104 * are provided for managing the session data.
105 */
106interface SessionIdStorageStrategy<Data = SessionData, FlashData = Data> {
107 /**
108 * The Cookie used to store the session id, or options used to automatically
109 * create one.
110 */
111 cookie?: Cookie | (CookieOptions & {
112 name?: string;
113 });
114 /**
115 * Creates a new record with the given data and returns the session id.
116 */
117 createData: (data: FlashSessionData<Data, FlashData>, expires?: Date) => Promise<string>;
118 /**
119 * Returns data for a given session id, or `null` if there isn't any.
120 */
121 readData: (id: string) => Promise<FlashSessionData<Data, FlashData> | null>;
122 /**
123 * Updates data for the given session id.
124 */
125 updateData: (id: string, data: FlashSessionData<Data, FlashData>, expires?: Date) => Promise<void>;
126 /**
127 * Deletes data for a given session id from the data store.
128 */
129 deleteData: (id: string) => Promise<void>;
130}
131/**
132 * Creates a SessionStorage object using a SessionIdStorageStrategy.
133 *
134 * Note: This is a low-level API that should only be used if none of the
135 * existing session storage options meet your requirements.
136 */
137declare function createSessionStorage<Data = SessionData, FlashData = Data>({
138 cookie: cookieArg,
139 createData,
140 readData,
141 updateData,
142 deleteData
143}: SessionIdStorageStrategy<Data, FlashData>): SessionStorage<Data, FlashData>;
144//#endregion
145export { FlashSessionData, IsSessionFunction, Session, SessionData, SessionIdStorageStrategy, SessionStorage, createSession, createSessionStorage, isSession };
\No newline at end of file