UNPKG

2 kBTypeScriptView Raw
1import { Context } from './context';
2import { MaybePromise } from './composer';
3import { MiddlewareFn } from './middleware';
4export interface SessionStore<T> {
5 get: (name: string) => MaybePromise<T | undefined>;
6 set: (name: string, value: T) => MaybePromise<void>;
7 delete: (name: string) => MaybePromise<void>;
8}
9interface SessionOptions<S extends object> {
10 getSessionKey?: (ctx: Context) => Promise<string | undefined>;
11 store?: SessionStore<S>;
12}
13export interface SessionContext<S extends object> extends Context {
14 session?: S;
15}
16/**
17 * Returns middleware that adds `ctx.session` for storing arbitrary state per session key.
18 *
19 * The default `getSessionKey` is <code>\`${ctx.from.id}:${ctx.chat.id}\`</code>.
20 * If either `ctx.from` or `ctx.chat` is `undefined`, default session key and thus `ctx.session` are also `undefined`.
21 *
22 * Session data is kept only in memory by default,
23 * which means that all data will be lost when the process is terminated.
24 * If you want to store data across restarts, or share it among workers,
25 * you can [install persistent session middleware from npm](https://www.npmjs.com/search?q=telegraf-session),
26 * or pass custom `storage`.
27 *
28 * @example https://github.com/telegraf/telegraf/blob/develop/docs/examples/session-bot.ts
29 * @deprecated https://github.com/telegraf/telegraf/issues/1372#issuecomment-782668499
30 */
31export declare function session<S extends object>(options?: SessionOptions<S>): MiddlewareFn<SessionContext<S>>;
32/** @deprecated https://github.com/telegraf/telegraf/issues/1372#issuecomment-782668499 */
33export declare class MemorySessionStore<T> implements SessionStore<T> {
34 private readonly ttl;
35 private readonly store;
36 constructor(ttl?: number);
37 get(name: string): T | undefined;
38 set(name: string, value: T): void;
39 delete(name: string): void;
40}
41export declare function isSessionContext<S extends object>(ctx: Context): ctx is SessionContext<S>;
42export {};
43//# sourceMappingURL=session.d.ts.map
\No newline at end of file