gramio 0.0.11 → 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/README.md CHANGED
@@ -1,7 +1,7 @@
1
- # GramIO
2
-
3
- Work in progress
4
-
5
- Currently support Bot API 7.1
6
-
7
- See [Documentation](https://gramio.netlify.app/)
1
+ # GramIO
2
+
3
+ Work in progress
4
+
5
+ Currently support Bot API 7.1
6
+
7
+ See [Documentation](https://gramio.netlify.app/)
package/dist/bot.d.ts CHANGED
@@ -1,8 +1,10 @@
1
+ import { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
1
2
  import type { APIMethods } from "@gramio/types";
2
3
  import "reflect-metadata";
3
- import { BotOptions, ErrorDefinitions, Hooks } from "./types";
4
+ import { Plugin } from "#plugin";
5
+ import { BotOptions, ErrorDefinitions, Handler, Hooks } from "./types";
4
6
  import { Updates } from "./updates";
5
- export declare class Bot<Errors extends ErrorDefinitions = {}> {
7
+ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives = {}> {
6
8
  readonly options: BotOptions;
7
9
  readonly api: APIMethods;
8
10
  private errorsDefinitions;
@@ -46,15 +48,20 @@ export declare class Bot<Errors extends ErrorDefinitions = {}> {
46
48
  error<Name extends string, NewError extends {
47
49
  new (...args: any): any;
48
50
  prototype: Error;
49
- }>(kind: Name, error: NewError): Bot<Errors & { [name in Name]: InstanceType<NewError>; }>;
51
+ }>(kind: Name, error: NewError): Bot<Errors & { [name in Name]: InstanceType<NewError>; }, {}>;
50
52
  /**
51
53
  * Set error handler.
52
54
  * @example
53
55
  * ```ts
54
- * bot.updates.onError(({ context, kind, error }) => {
55
- * if(context.is("message")) return context.send(`${kind}: ${error.message}`);
56
+ * bot.onError("message", ({ context, kind, error }) => {
57
+ * return context.send(`${kind}: ${error.message}`);
56
58
  * })
57
59
  * ```
58
60
  */
61
+ onError<T extends UpdateName>(updateName: MaybeArray<T>, handler: Hooks.OnError<Errors, ContextType<typeof this, T>>): this;
59
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;
66
+ extend<NewPlugin extends Plugin>(plugin: NewPlugin): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>;
60
67
  }
package/dist/bot.js CHANGED
@@ -38,6 +38,7 @@ let Bot = class Bot {
38
38
  if (!ctx.params)
39
39
  return ctx;
40
40
  const formattable = format_1.FormattableMap[ctx.method];
41
+ // @ts-ignore add AnyTelegramMethod to @gramio/format
41
42
  if (formattable)
42
43
  ctx.params = formattable(ctx.params);
43
44
  return ctx;
@@ -129,17 +130,46 @@ let Bot = class Bot {
129
130
  this.errorsDefinitions[kind] = error;
130
131
  return this;
131
132
  }
132
- /**
133
- * Set error handler.
134
- * @example
135
- * ```ts
136
- * bot.updates.onError(({ context, kind, error }) => {
137
- * if(context.is("message")) return context.send(`${kind}: ${error.message}`);
138
- * })
139
- * ```
140
- */
141
- onError(handler) {
142
- 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
+ }
146
+ return this;
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
+ });
155
+ return this;
156
+ }
157
+ on(updateName, handler) {
158
+ this.updates.on(updateName, handler);
159
+ return this;
160
+ }
161
+ use(handler) {
162
+ this.updates.use(handler);
163
+ return this;
164
+ }
165
+ extend(plugin) {
166
+ for (const [key, value] of Object.entries(plugin.errorsDefinitions)) {
167
+ if (this.errorsDefinitions[key])
168
+ this.errorsDefinitions[key] = value;
169
+ }
170
+ for (const derive of plugin.derives) {
171
+ this.derive(derive);
172
+ }
143
173
  return this;
144
174
  }
145
175
  };
@@ -148,5 +178,4 @@ exports.Bot = Bot = __decorate([
148
178
  (0, inspectable_1.Inspectable)({
149
179
  serialize: () => ({}),
150
180
  })
151
- // biome-ignore lint/complexity/noBannedTypes: <explanation>
152
181
  ], Bot);
package/dist/errors.d.ts CHANGED
File without changes
package/dist/errors.js CHANGED
File without changes
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from "./bot";
2
2
  export * from "./errors";
3
3
  export * from "./types";
4
+ export * from "./plugin";
4
5
  export * from "@gramio/contexts";
5
6
  export * from "@gramio/files";
6
7
  export * from "@gramio/keyboards";
package/dist/index.js CHANGED
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./bot"), exports);
18
18
  __exportStar(require("./errors"), exports);
19
19
  __exportStar(require("./types"), exports);
20
+ __exportStar(require("./plugin"), exports);
20
21
  __exportStar(require("@gramio/contexts"), exports);
21
22
  __exportStar(require("@gramio/files"), exports);
22
23
  __exportStar(require("@gramio/keyboards"), exports);
@@ -0,0 +1,21 @@
1
+ import { BotLike, Context } from "@gramio/contexts";
2
+ import { ErrorDefinitions } from "types";
3
+ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives = {}> {
4
+ Errors: Errors;
5
+ Derives: Derives;
6
+ derives: ((context: Context<BotLike>) => object)[];
7
+ name: string;
8
+ errorsDefinitions: Record<string, {
9
+ new (...args: any): any;
10
+ prototype: Error;
11
+ }>;
12
+ constructor(name: string);
13
+ /**
14
+ * Register custom class-error in plugin
15
+ **/
16
+ error<Name extends string, NewError extends {
17
+ new (...args: any): any;
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>>;
21
+ }
package/dist/plugin.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Plugin = void 0;
4
+ const _errors_1 = require("#errors");
5
+ class Plugin {
6
+ // TODO: fix that dump error. https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBDAnmYcCiUrQCLAGYCWAdgTAREQM5wC8cASsAMbQAmAPAFACQlMUxAOYAaHhixROAPgDcnTqEiw4TADYBDStQAKqgK6Di7cdGqgYwIq2omouQiTIVqdAN4BfKXFc8i6gLbAAFxwfAJEgnI8wJim9sSk5FQhjCxQHDy8-EKi3NyucETAAO5wABQAdFXqUIKUIepEiACUDU0ycGBYMBBIKCG2cO48Xm7uUdwsVPx6TD1QZX6BIWFCzd6ZMAAWBJQVS6h0B3LcwzwA9ABUlzxwlwzAhnwxKnp8EP4qGloAtDEScGInX0hiIt2u52isSgXDyADkAqhzJZrKFshFctw4SVBsirNQCkVSpVqrV6nBGi02ogOl1er1kMF0NChrkpGUANbEVghBGBYRwf7QXk46HrHx5c7nAACMEof3AzBgfxZAGVgPBbABpbmZIVQADa2u5AF1aHAuVYTtxNjs9vrKPFHElKAbLawzXR9RNuFANXooEQEHaKdQ9EQOUQIMUg5o4LoDEZMtxbNQAGTeOAGg6AoN84AmkIASWmjSYwAAKoz2NjirYvMM8rIeMMzgpwNB4GpNNQAEK9YzQswgCz45kSJ2JZzmjxeHzybh4ji1hOgwUjlE6EFGSlSdmZMDbogi4qr4i5VpwFdH9ej1FnojsYh4F4P1NeAD8cH7MEHEnT8ZHu+cAhNsuwbHkfowAGQZgdQcaUicrbyO2Shdt81BwhA9AEIIWxyrem7jtAEFFMArD0BAqhMgAROorD+MQNFwAAPnANH+BArAxOo8w0RMUxhLM8xlFg1EhHRDFMax7GcdxUC8dANHipklB6CgCzNNacH7MA5GUdR5picASGcGcgnwBYfDmkSgGJkQZQ0TAykVPqjlwgA8gA+vQRYAOIABIVqqNEClhOF4XKWnyBZcAAEa9DZJTfr0ZTNDwrkblYZRWTAzRAA
7
+ Errors;
8
+ Derives;
9
+ derives = [];
10
+ name;
11
+ errorsDefinitions = {};
12
+ constructor(name) {
13
+ this.name = name;
14
+ }
15
+ /**
16
+ * Register custom class-error in plugin
17
+ **/
18
+ error(kind, error) {
19
+ //@ts-expect-error Set ErrorKind
20
+ error[_errors_1.ErrorKind] = kind;
21
+ this.errorsDefinitions[kind] = error;
22
+ return this;
23
+ }
24
+ derive(handler) {
25
+ this.derives.push(handler);
26
+ return this;
27
+ }
28
+ }
29
+ exports.Plugin = Plugin;
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,16 +24,14 @@ 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>[];
34
34
  }
35
35
  }
36
- export type ErrorDefinitions = Record<string, {
37
- prototype: Error;
38
- }>;
36
+ export type ErrorDefinitions = Record<string, Error>;
39
37
  export {};
package/dist/types.js CHANGED
File without changes
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>, onError: CaughtMiddlewareHandler<Context>);
13
- on<T extends UpdateName>(updateName: T, handler: Handler<InstanceType<(typeof contextsMappings)[T]>>): this;
14
- use(handler: Handler<Context>): 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
@@ -4,17 +4,16 @@ exports.Updates = void 0;
4
4
  const contexts_1 = require("@gramio/contexts");
5
5
  const middleware_io_1 = require("middleware-io");
6
6
  class Updates {
7
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
8
7
  bot;
9
8
  isStarted = false;
10
9
  offset = 0;
11
10
  composer = middleware_io_1.Composer.builder();
12
11
  onError;
13
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
14
12
  constructor(bot, onError) {
15
13
  this.bot = bot;
16
14
  this.onError = onError;
17
15
  }
16
+ //TODO: FIX
18
17
  on(updateName, handler) {
19
18
  return this.use(async (context, next) => {
20
19
  //TODO: fix typings
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gramio",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "description": "WIP",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -14,13 +14,14 @@
14
14
  "license": "ISC",
15
15
  "devDependencies": {
16
16
  "@biomejs/biome": "1.5.3",
17
- "@gramio/types": "^7.1.1",
18
- "@types/node": "^20.11.19"
17
+ "@gramio/types": "^7.1.2",
18
+ "@types/node": "^20.11.19",
19
+ "typescript": "^5.3.3"
19
20
  },
20
21
  "dependencies": {
21
- "@gramio/contexts": "^0.0.2",
22
+ "@gramio/contexts": "^0.0.4",
22
23
  "@gramio/files": "^0.0.3",
23
- "@gramio/format": "^0.0.7",
24
+ "@gramio/format": "^0.0.8",
24
25
  "@gramio/keyboards": "^0.1.6",
25
26
  "form-data-encoder": "^4.0.2",
26
27
  "inspectable": "^2.1.0",
@@ -1,8 +0,0 @@
1
- import { APIMethodParams, APIMethods, TelegramAPIResponseError, TelegramResponseParameters } from "@gramio/types";
2
- export declare class TelegramError<T extends keyof APIMethods> extends Error {
3
- method: T;
4
- params: APIMethodParams<T>;
5
- code: number;
6
- payload?: TelegramResponseParameters;
7
- constructor(error: TelegramAPIResponseError, method: T, params: APIMethodParams<T>);
8
- }
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TelegramError = void 0;
4
- class TelegramError extends Error {
5
- method;
6
- params;
7
- code;
8
- payload;
9
- constructor(error, method, params) {
10
- super(error.description);
11
- this.name = method;
12
- this.method = method;
13
- this.params = params;
14
- this.code = error.error_code;
15
- if (error.parameters)
16
- this.payload = error.parameters;
17
- }
18
- }
19
- exports.TelegramError = TelegramError;