gramio 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bot.d.ts CHANGED
@@ -2,9 +2,9 @@ import { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
2
2
  import type { APIMethods } from "@gramio/types";
3
3
  import "reflect-metadata";
4
4
  import { Plugin } from "#plugin";
5
- import { BotOptions, ErrorDefinitions, Handler, Hooks } from "./types";
5
+ import { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks } from "./types";
6
6
  import { Updates } from "./updates";
7
- export declare class Bot<Errors extends ErrorDefinitions = {}, Derives = {}> {
7
+ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
8
8
  readonly options: BotOptions;
9
9
  readonly api: APIMethods;
10
10
  private errorsDefinitions;
@@ -48,7 +48,7 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives = {}> {
48
48
  error<Name extends string, NewError extends {
49
49
  new (...args: any): any;
50
50
  prototype: Error;
51
- }>(kind: Name, error: NewError): Bot<Errors & { [name in Name]: InstanceType<NewError>; }, {}>;
51
+ }>(kind: Name, error: NewError): Bot<Errors & { [name in Name]: InstanceType<NewError>; }, Derives>;
52
52
  /**
53
53
  * Set error handler.
54
54
  * @example
@@ -58,10 +58,15 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives = {}> {
58
58
  * })
59
59
  * ```
60
60
  */
61
- onError<T extends UpdateName>(updateName: MaybeArray<T>, handler: Hooks.OnError<Errors, ContextType<typeof this, T>>): this;
62
- onError(handler: Hooks.OnError<Errors>): this;
63
- derive<Handler extends (context: Context<typeof this>) => object>(handler: Handler): Bot<Errors, Derives & ReturnType<Handler>>;
64
- on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives>): this;
65
- use(handler: Handler<Context<typeof this> & Derives>): this;
61
+ onError<T extends UpdateName>(updateName: MaybeArray<T>, handler: Hooks.OnError<Errors, ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this;
62
+ onError(handler: Hooks.OnError<Errors, Context<typeof this> & Derives["global"]>): this;
63
+ derive<Handler extends Hooks.Derive<Context<typeof this>>>(handler: Handler): Bot<Errors, Derives & {
64
+ global: ReturnType<Handler>;
65
+ }>;
66
+ derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<typeof this, Update>>>(updateName: Update, handler: Handler): Bot<Errors, Derives & {
67
+ [K in Update]: ReturnType<Handler>;
68
+ }>;
69
+ on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this;
70
+ use(handler: Handler<Context<typeof this> & Derives["global"]>): this;
66
71
  extend<NewPlugin extends Plugin>(plugin: NewPlugin): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>;
67
72
  }
package/dist/bot.js CHANGED
@@ -145,13 +145,21 @@ let Bot = class Bot {
145
145
  }
146
146
  return this;
147
147
  }
148
- derive(handler) {
149
- this.updates.use((context, next) => {
150
- for (const [key, value] of Object.entries(handler(context))) {
151
- context[key] = value;
152
- }
153
- next();
154
- });
148
+ derive(updateNameOrHandler, handler) {
149
+ if (typeof updateNameOrHandler === "function")
150
+ this.updates.use((context, next) => {
151
+ for (const [key, value] of Object.entries(updateNameOrHandler(context))) {
152
+ context[key] = value;
153
+ }
154
+ next();
155
+ });
156
+ else if (handler)
157
+ this.updates.on(updateNameOrHandler, (context, next) => {
158
+ for (const [key, value] of Object.entries(handler(context))) {
159
+ context[key] = value;
160
+ }
161
+ next();
162
+ });
155
163
  return this;
156
164
  }
157
165
  on(updateName, handler) {
@@ -167,8 +175,12 @@ let Bot = class Bot {
167
175
  if (this.errorsDefinitions[key])
168
176
  this.errorsDefinitions[key] = value;
169
177
  }
170
- for (const derive of plugin.derives) {
171
- this.derive(derive);
178
+ for (const value of plugin.derives) {
179
+ const [derive, updateName] = value;
180
+ if (!updateName)
181
+ this.derive(derive);
182
+ else
183
+ this.derive(updateName, derive);
172
184
  }
173
185
  return this;
174
186
  }
package/dist/plugin.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { BotLike, Context } from "@gramio/contexts";
2
- import { ErrorDefinitions } from "types";
3
- export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives = {}> {
1
+ import { BotLike, Context, ContextType, UpdateName } from "@gramio/contexts";
2
+ import { DeriveDefinitions, ErrorDefinitions, Hooks } from "types";
3
+ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
4
4
  Errors: Errors;
5
5
  Derives: Derives;
6
- derives: ((context: Context<BotLike>) => object)[];
6
+ derives: [Hooks.Derive<any>, UpdateName | undefined][];
7
7
  name: string;
8
8
  errorsDefinitions: Record<string, {
9
9
  new (...args: any): any;
@@ -16,6 +16,11 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives = {}>
16
16
  error<Name extends string, NewError extends {
17
17
  new (...args: any): any;
18
18
  prototype: Error;
19
- }>(kind: Name, error: NewError): Plugin<Errors & { [name in Name]: InstanceType<NewError>; }, {}>;
20
- derive<Handler extends (context: Context<BotLike>) => object>(handler: Handler): Plugin<Errors, Derives & ReturnType<Handler>>;
19
+ }>(kind: Name, error: NewError): Plugin<Errors & { [name in Name]: InstanceType<NewError>; }, DeriveDefinitions>;
20
+ derive<Handler extends Hooks.Derive<Context<BotLike>>>(handler: Handler): Plugin<Errors, Derives & {
21
+ global: ReturnType<Handler>;
22
+ }>;
23
+ derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<BotLike, Update>>>(updateName: Update, handler: Handler): Plugin<Errors, Derives & {
24
+ [K in Update]: ReturnType<Handler>;
25
+ }>;
21
26
  }
package/dist/plugin.js CHANGED
@@ -21,8 +21,11 @@ class Plugin {
21
21
  this.errorsDefinitions[kind] = error;
22
22
  return this;
23
23
  }
24
- derive(handler) {
25
- this.derives.push(handler);
24
+ derive(updateNameOrHandler, handler) {
25
+ if (typeof updateNameOrHandler === "string" && handler)
26
+ this.derives.push([handler, updateNameOrHandler]);
27
+ else if (typeof updateNameOrHandler === "function")
28
+ this.derives.push([updateNameOrHandler, undefined]);
26
29
  return this;
27
30
  }
28
31
  }
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { BotLike, Context } from "@gramio/contexts";
1
+ import { BotLike, Context, UpdateName } from "@gramio/contexts";
2
2
  import { APIMethodParams, APIMethods } from "@gramio/types";
3
3
  import { NextMiddleware } from "middleware-io";
4
4
  import { TelegramError } from "./errors";
@@ -22,6 +22,7 @@ type AnyTelegramMethod = {
22
22
  }[keyof APIMethods];
23
23
  type MaybePromise<T> = T | Promise<T>;
24
24
  export declare namespace Hooks {
25
+ type Derive<Ctx> = (context: Ctx) => object;
25
26
  type PreRequestContext = AnyTelegramMethod;
26
27
  type PreRequest = (ctx: PreRequestContext) => MaybePromise<PreRequestContext>;
27
28
  type OnErrorContext<Ctx extends Context<BotLike>, T extends ErrorDefinitions> = ErrorHandlerParams<Ctx, "TELEGRAM", AnyTelegramError> | ErrorHandlerParams<Ctx, "UNKNOWN", Error> | {
@@ -34,4 +35,5 @@ export declare namespace Hooks {
34
35
  }
35
36
  }
36
37
  export type ErrorDefinitions = Record<string, Error>;
38
+ export type DeriveDefinitions = Record<UpdateName | "global", {}>;
37
39
  export {};
package/package.json CHANGED
@@ -1,9 +1,20 @@
1
1
  {
2
2
  "name": "gramio",
3
- "version": "0.0.13",
4
- "description": "WIP",
3
+ "version": "0.0.14",
4
+ "description": "Powerful Telegram Bot API framework",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
+ "keywords": [
8
+ "telegram",
9
+ "telegram-bot",
10
+ "telegram-bot-api",
11
+ "bot",
12
+ "framework",
13
+ "types",
14
+ "client",
15
+ "webhook",
16
+ "long-polling"
17
+ ],
7
18
  "scripts": {
8
19
  "type": "tsc --noEmit",
9
20
  "lint": "bun check ./src",