1 | import { Context } from './context';
|
2 | import { ExclusiveKeys, MaybePromise } from './core/helpers/util';
|
3 | import { MiddlewareFn } from './middleware';
|
4 | export interface SyncSessionStore<T> {
|
5 | get: (name: string) => T | undefined;
|
6 | set: (name: string, value: T) => void;
|
7 | delete: (name: string) => void;
|
8 | }
|
9 | export interface AsyncSessionStore<T> {
|
10 | get: (name: string) => Promise<T | undefined>;
|
11 | set: (name: string, value: T) => Promise<unknown>;
|
12 | delete: (name: string) => Promise<unknown>;
|
13 | }
|
14 | export type SessionStore<T> = SyncSessionStore<T> | AsyncSessionStore<T>;
|
15 | interface SessionOptions<S, C extends Context, P extends string> {
|
16 |
|
17 | property?: P;
|
18 | getSessionKey?: (ctx: C) => MaybePromise<string | undefined>;
|
19 | store?: SessionStore<S>;
|
20 | defaultSession?: (ctx: C) => S;
|
21 | }
|
22 |
|
23 | export interface SessionContext<S extends object> extends Context {
|
24 | session?: S;
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | export declare function session<S extends NonNullable<C[P]>, C extends Context & {
|
41 | [key in P]?: C[P];
|
42 | }, P extends (ExclusiveKeys<C, Context> & string) | 'session' = 'session'>(options?: SessionOptions<S, C, P>): MiddlewareFn<C>;
|
43 |
|
44 | export declare class MemorySessionStore<T> implements SyncSessionStore<T> {
|
45 | private readonly ttl;
|
46 | private readonly store;
|
47 | constructor(ttl?: number);
|
48 | get(name: string): T | undefined;
|
49 | set(name: string, value: T): void;
|
50 | delete(name: string): void;
|
51 | }
|
52 | /** @deprecated session can use custom properties now. Directly use `'session' in ctx` instead */
|
53 | export declare function isSessionContext<S extends object>(ctx: Context): ctx is SessionContext<S>;
|
54 | export {};
|
55 |
|
\ | No newline at end of file |