UNPKG

@botol/tg-session

Version:

## Example ```typescript import { BotolTg } from '@botol/tg-bot'; import { BotolSession } from '@botol/tg-session';

29 lines (28 loc) 1.2 kB
import { Handler } from '@botol/tg-bot'; import { ContextTG } from '@botol/tg-bot'; export declare type MaybePromise<T> = Promise<T> | T; export interface SessionStore<T> { get: (key: string) => MaybePromise<T | undefined>; set: (key: string, val: T) => MaybePromise<void>; delete: (key: string) => MaybePromise<void>; } export declare type ContextSessionInit<T> = { session?: T; }; export declare type ContextSession<T> = { session: T; }; export interface SessionOptions<T> { getKey?: (ctx: ContextTG) => Promise<string | undefined> | string | undefined; store?: SessionStore<T>; init: (ctx: ContextTG, key?: string) => MaybePromise<T>; update?: (ctx: ContextSession<T>, key?: string) => MaybePromise<void>; } export declare class MemorySession<T> implements SessionStore<Partial<T>> { private readonly store; get(key: string): Partial<T> | undefined; set(key: string, val: Partial<T>): void; delete(key: string): void; } export declare function defaultGetKey(ctx: ContextTG): string | undefined; export declare function BotolSession<T>(options?: SessionOptions<T>): Handler<Partial<ContextSession<T>> & ContextTG>;