UNPKG

2.75 kBTypeScriptView Raw
1import { Context } from './context';
2import { ExclusiveKeys, MaybePromise } from './core/helpers/util';
3import { MiddlewareFn } from './middleware';
4export interface SyncSessionStore<T> {
5 get: (name: string) => T | undefined;
6 set: (name: string, value: T) => void;
7 delete: (name: string) => void;
8}
9export 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}
14export type SessionStore<T> = SyncSessionStore<T> | AsyncSessionStore<T>;
15interface SessionOptions<S, C extends Context, P extends string> {
16 /** Customise the session prop. Defaults to "session" and is available as ctx.session. */
17 property?: P;
18 getSessionKey?: (ctx: C) => MaybePromise<string | undefined>;
19 store?: SessionStore<S>;
20 defaultSession?: (ctx: C) => S;
21}
22/** @deprecated session can use custom properties now. Construct this type directly. */
23export interface SessionContext<S extends object> extends Context {
24 session?: S;
25}
26/**
27 * Returns middleware that adds `ctx.session` for storing arbitrary state per session key.
28 *
29 * The default `getSessionKey` is `${ctx.from.id}:${ctx.chat.id}`.
30 * If either `ctx.from` or `ctx.chat` is `undefined`, default session key and thus `ctx.session` are also `undefined`.
31 *
32 * > ⚠️ Session data is kept only in memory by default, which means that all data will be lost when the process is terminated.
33 * >
34 * > If you want to persist data across process restarts, or share it among multiple instances, you should use
35 * [@telegraf/session](https://www.npmjs.com/package/@telegraf/session), or pass custom `storage`.
36 *
37 * @see {@link https://github.com/feathers-studio/telegraf-docs/blob/b694bcc36b4f71fb1cd650a345c2009ab4d2a2a5/guide/session.md Telegraf Docs | Session}
38 * @see {@link https://github.com/feathers-studio/telegraf-docs/blob/master/examples/session-bot.ts Example}
39 */
40export 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/** @deprecated Use `Map` */
44export 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 */
53export declare function isSessionContext<S extends object>(ctx: Context): ctx is SessionContext<S>;
54export {};
55//# sourceMappingURL=session.d.ts.map
\No newline at end of file