gramio 0.0.12 → 0.0.13

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
@@ -1,4 +1,4 @@
1
- import { Context } from "@gramio/contexts";
1
+ 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";
@@ -53,13 +53,15 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives = {}> {
53
53
  * Set error handler.
54
54
  * @example
55
55
  * ```ts
56
- * bot.updates.onError(({ context, kind, error }) => {
57
- * if(context.is("message")) return context.send(`${kind}: ${error.message}`);
56
+ * bot.onError("message", ({ context, kind, error }) => {
57
+ * return context.send(`${kind}: ${error.message}`);
58
58
  * })
59
59
  * ```
60
60
  */
61
+ onError<T extends UpdateName>(updateName: MaybeArray<T>, handler: Hooks.OnError<Errors, ContextType<typeof this, T>>): this;
61
62
  onError(handler: Hooks.OnError<Errors>): this;
62
- derive<Handler extends (context: Context) => object>(handler: Handler): Bot<Errors, Derives & ReturnType<Handler>>;
63
- use(handler: Handler<Context & Derives>): 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;
64
66
  extend<NewPlugin extends Plugin>(plugin: NewPlugin): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>;
65
67
  }
package/dist/bot.js CHANGED
@@ -130,17 +130,19 @@ let Bot = class Bot {
130
130
  this.errorsDefinitions[kind] = error;
131
131
  return this;
132
132
  }
133
- /**
134
- * Set error handler.
135
- * @example
136
- * ```ts
137
- * bot.updates.onError(({ context, kind, error }) => {
138
- * if(context.is("message")) return context.send(`${kind}: ${error.message}`);
139
- * })
140
- * ```
141
- */
142
- onError(handler) {
143
- this.hooks.onError.push(handler);
133
+ onError(updateNameOrHandler, handler) {
134
+ if (typeof updateNameOrHandler === "function") {
135
+ this.hooks.onError.push(updateNameOrHandler);
136
+ return this;
137
+ }
138
+ if (handler) {
139
+ this.hooks.onError.push(async (errContext) => {
140
+ if (errContext.context.is(updateNameOrHandler))
141
+ // TODO: Sorry... fix later
142
+ //@ts-expect-error
143
+ await handler(errContext);
144
+ });
145
+ }
144
146
  return this;
145
147
  }
146
148
  derive(handler) {
@@ -152,6 +154,10 @@ let Bot = class Bot {
152
154
  });
153
155
  return this;
154
156
  }
157
+ on(updateName, handler) {
158
+ this.updates.on(updateName, handler);
159
+ return this;
160
+ }
155
161
  use(handler) {
156
162
  this.updates.use(handler);
157
163
  return this;
package/dist/plugin.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { Context } from "@gramio/contexts";
1
+ import { BotLike, Context } from "@gramio/contexts";
2
2
  import { ErrorDefinitions } from "types";
3
3
  export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives = {}> {
4
4
  Errors: Errors;
5
5
  Derives: Derives;
6
- derives: ((context: Context) => object)[];
6
+ derives: ((context: Context<BotLike>) => object)[];
7
7
  name: string;
8
8
  errorsDefinitions: Record<string, {
9
9
  new (...args: any): any;
@@ -17,5 +17,5 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives = {}>
17
17
  new (...args: any): any;
18
18
  prototype: Error;
19
19
  }>(kind: Name, error: NewError): Plugin<Errors & { [name in Name]: InstanceType<NewError>; }, {}>;
20
- derive<Handler extends (context: Context) => object>(handler: Handler): Plugin<Errors, Derives & ReturnType<Handler>>;
20
+ derive<Handler extends (context: Context<BotLike>) => object>(handler: Handler): Plugin<Errors, Derives & ReturnType<Handler>>;
21
21
  }
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Context } from "@gramio/contexts";
1
+ import { BotLike, Context } from "@gramio/contexts";
2
2
  import { APIMethodParams, APIMethods } from "@gramio/types";
3
3
  import { NextMiddleware } from "middleware-io";
4
4
  import { TelegramError } from "./errors";
@@ -6,8 +6,8 @@ export interface BotOptions {
6
6
  token?: string;
7
7
  }
8
8
  export type Handler<T> = (context: T, next: NextMiddleware) => unknown;
9
- interface ErrorHandlerParams<Kind extends string, Err> {
10
- context: Context;
9
+ interface ErrorHandlerParams<Ctx extends Context<BotLike>, Kind extends string, Err> {
10
+ context: Ctx;
11
11
  kind: Kind;
12
12
  error: Err;
13
13
  }
@@ -24,10 +24,10 @@ type MaybePromise<T> = T | Promise<T>;
24
24
  export declare namespace Hooks {
25
25
  type PreRequestContext = AnyTelegramMethod;
26
26
  type PreRequest = (ctx: PreRequestContext) => MaybePromise<PreRequestContext>;
27
- type OnErrorContext<T extends ErrorDefinitions> = ErrorHandlerParams<"TELEGRAM", AnyTelegramError> | ErrorHandlerParams<"UNKNOWN", Error> | {
28
- [K in keyof T]: ErrorHandlerParams<K & string, T[K & string]>;
27
+ type OnErrorContext<Ctx extends Context<BotLike>, T extends ErrorDefinitions> = ErrorHandlerParams<Ctx, "TELEGRAM", AnyTelegramError> | ErrorHandlerParams<Ctx, "UNKNOWN", Error> | {
28
+ [K in keyof T]: ErrorHandlerParams<Ctx, K & string, T[K & string]>;
29
29
  }[keyof T];
30
- type OnError<T extends ErrorDefinitions> = (options: OnErrorContext<T>) => unknown;
30
+ type OnError<T extends ErrorDefinitions, Ctx extends Context<BotLike> = Context<BotLike>> = (options: OnErrorContext<Ctx, T>) => unknown;
31
31
  interface Store<T extends ErrorDefinitions> {
32
32
  preRequest: PreRequest[];
33
33
  onError: OnError<T>[];
package/dist/updates.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Context, UpdateName, contextsMappings } from "@gramio/contexts";
1
+ import { Context, MaybeArray, UpdateName } from "@gramio/contexts";
2
2
  import type { TelegramUpdate } from "@gramio/types";
3
3
  import { CaughtMiddlewareHandler } from "middleware-io";
4
4
  import type { Bot } from "./bot";
@@ -9,9 +9,9 @@ export declare class Updates {
9
9
  private offset;
10
10
  private composer;
11
11
  private onError;
12
- constructor(bot: Bot<any, any>, onError: CaughtMiddlewareHandler<Context>);
13
- on<T extends UpdateName>(updateName: T, handler: Handler<InstanceType<(typeof contextsMappings)[T]>>): this;
14
- use(handler: Handler<Context & any>): this;
12
+ constructor(bot: Bot<any, any>, onError: CaughtMiddlewareHandler<Context<any>>);
13
+ on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<any>): this;
14
+ use(handler: Handler<any>): this;
15
15
  handleUpdate(data: TelegramUpdate): Promise<void>;
16
16
  startPolling(): Promise<void>;
17
17
  startFetchLoop(): Promise<void>;
package/dist/updates.js CHANGED
@@ -13,6 +13,7 @@ class Updates {
13
13
  this.bot = bot;
14
14
  this.onError = onError;
15
15
  }
16
+ //TODO: FIX
16
17
  on(updateName, handler) {
17
18
  return this.use(async (context, next) => {
18
19
  //TODO: fix typings
@@ -34,7 +35,6 @@ class Updates {
34
35
  return;
35
36
  try {
36
37
  let context = new UpdateContext({
37
- //@ts-expect-error
38
38
  bot: this.bot,
39
39
  update: data,
40
40
  //@ts-expect-error
@@ -44,7 +44,6 @@ class Updates {
44
44
  });
45
45
  if ("isEvent" in context && context.isEvent() && context.eventType) {
46
46
  context = new contexts_1.contextsMappings[context.eventType]({
47
- //@ts-expect-error
48
47
  bot: this.bot,
49
48
  update: data,
50
49
  //@ts-expect-error
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gramio",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "description": "WIP",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -19,7 +19,7 @@
19
19
  "typescript": "^5.3.3"
20
20
  },
21
21
  "dependencies": {
22
- "@gramio/contexts": "^0.0.2",
22
+ "@gramio/contexts": "^0.0.4",
23
23
  "@gramio/files": "^0.0.3",
24
24
  "@gramio/format": "^0.0.8",
25
25
  "@gramio/keyboards": "^0.1.6",