gramio 0.0.50 → 0.1.0
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 +1 -1
- package/dist/index.cjs +1191 -0
- package/dist/index.d.cts +1012 -0
- package/dist/index.d.ts +1009 -13
- package/dist/index.js +1157 -31
- package/package.json +24 -11
- package/dist/bot.d.ts +0 -404
- package/dist/bot.js +0 -824
- package/dist/composer.d.ts +0 -34
- package/dist/composer.js +0 -71
- package/dist/errors.d.ts +0 -17
- package/dist/errors.js +0 -29
- package/dist/filters.d.ts +0 -20
- package/dist/filters.js +0 -23
- package/dist/plugin.d.ts +0 -218
- package/dist/plugin.js +0 -269
- package/dist/types.d.ts +0 -242
- package/dist/types.js +0 -2
- package/dist/updates.d.ts +0 -17
- package/dist/updates.js +0 -87
- package/dist/webhook/adapters.d.ts +0 -53
- package/dist/webhook/adapters.js +0 -58
- package/dist/webhook/index.d.ts +0 -35
- package/dist/webhook/index.js +0 -46
package/dist/composer.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
2
|
-
import { type CaughtMiddlewareHandler, type Middleware, Composer as MiddlewareComposer } from "middleware-io";
|
|
3
|
-
import type { AnyBot, Handler, Hooks } from "./types";
|
|
4
|
-
/** Base-composer without chainable type-safety */
|
|
5
|
-
export declare class Composer {
|
|
6
|
-
protected composer: MiddlewareComposer<Context<AnyBot> & {
|
|
7
|
-
[key: string]: unknown;
|
|
8
|
-
}, Context<AnyBot> & {
|
|
9
|
-
[key: string]: unknown;
|
|
10
|
-
}>;
|
|
11
|
-
length: number;
|
|
12
|
-
composed: Middleware<Context<AnyBot>>;
|
|
13
|
-
protected onError: CaughtMiddlewareHandler<Context<AnyBot>>;
|
|
14
|
-
constructor(onError?: CaughtMiddlewareHandler<Context<any>>);
|
|
15
|
-
/** Register handler to one or many Updates */
|
|
16
|
-
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<any>): this;
|
|
17
|
-
/** Register handler to any Update */
|
|
18
|
-
use(handler: Handler<any>): this;
|
|
19
|
-
/**
|
|
20
|
-
* Derive some data to handlers
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```ts
|
|
24
|
-
* new Bot("token").derive((context) => {
|
|
25
|
-
* return {
|
|
26
|
-
* superSend: () => context.send("Derived method")
|
|
27
|
-
* }
|
|
28
|
-
* })
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<AnyBot, Update>>>(updateNameOrHandler: MaybeArray<Update> | Handler, handler?: Handler): this;
|
|
32
|
-
protected recompose(): this;
|
|
33
|
-
compose(context: Context<AnyBot>, next?: import("middleware-io").NextMiddleware): void;
|
|
34
|
-
}
|
package/dist/composer.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Composer = void 0;
|
|
4
|
-
const middleware_io_1 = require("middleware-io");
|
|
5
|
-
/** Base-composer without chainable type-safety */
|
|
6
|
-
class Composer {
|
|
7
|
-
composer = middleware_io_1.Composer.builder();
|
|
8
|
-
length = 0;
|
|
9
|
-
composed;
|
|
10
|
-
onError;
|
|
11
|
-
constructor(onError) {
|
|
12
|
-
this.onError =
|
|
13
|
-
onError ||
|
|
14
|
-
((_, error) => {
|
|
15
|
-
throw error;
|
|
16
|
-
});
|
|
17
|
-
this.recompose();
|
|
18
|
-
}
|
|
19
|
-
/** Register handler to one or many Updates */
|
|
20
|
-
on(updateName, handler) {
|
|
21
|
-
return this.use(async (context, next) => {
|
|
22
|
-
if (context.is(updateName))
|
|
23
|
-
return await handler(context, next);
|
|
24
|
-
return await next();
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
/** Register handler to any Update */
|
|
28
|
-
use(handler) {
|
|
29
|
-
this.composer.caught(this.onError).use(handler);
|
|
30
|
-
return this.recompose();
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Derive some data to handlers
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```ts
|
|
37
|
-
* new Bot("token").derive((context) => {
|
|
38
|
-
* return {
|
|
39
|
-
* superSend: () => context.send("Derived method")
|
|
40
|
-
* }
|
|
41
|
-
* })
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
derive(updateNameOrHandler, handler) {
|
|
45
|
-
if (typeof updateNameOrHandler === "function")
|
|
46
|
-
this.use(async (context, next) => {
|
|
47
|
-
for (const [key, value] of Object.entries(await updateNameOrHandler(context))) {
|
|
48
|
-
context[key] = value;
|
|
49
|
-
}
|
|
50
|
-
next();
|
|
51
|
-
});
|
|
52
|
-
else if (handler)
|
|
53
|
-
this.on(updateNameOrHandler, async (context, next) => {
|
|
54
|
-
for (const [key, value] of Object.entries(await handler(context))) {
|
|
55
|
-
context[key] = value;
|
|
56
|
-
}
|
|
57
|
-
next();
|
|
58
|
-
});
|
|
59
|
-
return this;
|
|
60
|
-
}
|
|
61
|
-
recompose() {
|
|
62
|
-
// @ts-expect-error middleware-io moment
|
|
63
|
-
this.composed = this.composer.compose();
|
|
64
|
-
this.length = this.composer.length;
|
|
65
|
-
return this;
|
|
66
|
-
}
|
|
67
|
-
compose(context, next = middleware_io_1.noopNext) {
|
|
68
|
-
this.composed(context, next);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
exports.Composer = Composer;
|
package/dist/errors.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { APIMethods, TelegramAPIResponseError, TelegramResponseParameters } from "@gramio/types";
|
|
2
|
-
import type { MaybeSuppressedParams } from "./types";
|
|
3
|
-
/** Symbol to determine which error kind is it */
|
|
4
|
-
export declare const ErrorKind: symbol;
|
|
5
|
-
/** Represent {@link TelegramAPIResponseError} and thrown in API calls */
|
|
6
|
-
export declare class TelegramError<T extends keyof APIMethods> extends Error {
|
|
7
|
-
/** Name of the API Method */
|
|
8
|
-
method: T;
|
|
9
|
-
/** Params that were sent */
|
|
10
|
-
params: MaybeSuppressedParams<T>;
|
|
11
|
-
/** See {@link TelegramAPIResponseError.error_code}*/
|
|
12
|
-
code: number;
|
|
13
|
-
/** Describes why a request was unsuccessful. */
|
|
14
|
-
payload?: TelegramResponseParameters;
|
|
15
|
-
/** Construct new TelegramError */
|
|
16
|
-
constructor(error: TelegramAPIResponseError, method: T, params: MaybeSuppressedParams<T>);
|
|
17
|
-
}
|
package/dist/errors.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TelegramError = exports.ErrorKind = void 0;
|
|
4
|
-
/** Symbol to determine which error kind is it */
|
|
5
|
-
exports.ErrorKind = Symbol("ErrorKind");
|
|
6
|
-
/** Represent {@link TelegramAPIResponseError} and thrown in API calls */
|
|
7
|
-
class TelegramError extends Error {
|
|
8
|
-
/** Name of the API Method */
|
|
9
|
-
method;
|
|
10
|
-
/** Params that were sent */
|
|
11
|
-
params;
|
|
12
|
-
/** See {@link TelegramAPIResponseError.error_code}*/
|
|
13
|
-
code;
|
|
14
|
-
/** Describes why a request was unsuccessful. */
|
|
15
|
-
payload;
|
|
16
|
-
/** Construct new TelegramError */
|
|
17
|
-
constructor(error, method, params) {
|
|
18
|
-
super(error.description);
|
|
19
|
-
this.name = method;
|
|
20
|
-
this.method = method;
|
|
21
|
-
this.params = params;
|
|
22
|
-
this.code = error.error_code;
|
|
23
|
-
if (error.parameters)
|
|
24
|
-
this.payload = error.parameters;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
exports.TelegramError = TelegramError;
|
|
28
|
-
//@ts-expect-error
|
|
29
|
-
TelegramError.constructor[exports.ErrorKind] = "TELEGRAM";
|
package/dist/filters.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
2
|
-
import type { Bot } from "./bot";
|
|
3
|
-
export interface AdditionDefinitions {
|
|
4
|
-
equal: any;
|
|
5
|
-
addition: Record<string, any>;
|
|
6
|
-
}
|
|
7
|
-
type ReturnIfNonNever<T> = [T] extends [never] ? {} : T;
|
|
8
|
-
export type Filters<BotType extends Bot = Bot, Base = Context<BotType>, ConditionalAdditions extends AdditionDefinitions[] = []> = {
|
|
9
|
-
_s: Base;
|
|
10
|
-
_ad: ConditionalAdditions;
|
|
11
|
-
__filters: ((context: Context<BotType>) => boolean)[];
|
|
12
|
-
context<T extends UpdateName>(updateName: MaybeArray<T>): Filters<BotType, ContextType<BotType, T>, ConditionalAdditions>;
|
|
13
|
-
is2(): Filters<BotType, 2, ConditionalAdditions>;
|
|
14
|
-
} & ReturnIfNonNever<{
|
|
15
|
-
[K in keyof ConditionalAdditions & number]: ConditionalAdditions[K] extends {
|
|
16
|
-
equal: infer E;
|
|
17
|
-
addition: infer T;
|
|
18
|
-
} ? Base extends E ? T : {} : {};
|
|
19
|
-
}[number]>;
|
|
20
|
-
export {};
|
package/dist/filters.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
// type filter = Filters<
|
|
4
|
-
// Context<Bot> & {prop: 2},
|
|
5
|
-
// [{ equal: { prop: 2 }; addition: { some: 2 } }]
|
|
6
|
-
// >;
|
|
7
|
-
// const a = {} as filter;
|
|
8
|
-
// // a.s;
|
|
9
|
-
// type S = [{ equal: { prop: 2 }; addition: { some: 2 } }];
|
|
10
|
-
// type C = {[K in keyof S & number]: S[K]};
|
|
11
|
-
// type SA = {
|
|
12
|
-
// [K in keyof S & number]: S[K] extends {
|
|
13
|
-
// equal: infer E;
|
|
14
|
-
// addition: infer T;
|
|
15
|
-
// } ? Context<Bot> & {prop: 2} extends E ? T : {} : {}}[number];
|
|
16
|
-
// type A = Context<Bot> & {prop: 2} extends SA ? true : false;
|
|
17
|
-
// export const filters: Filters = {
|
|
18
|
-
// __filters: [],
|
|
19
|
-
// context(updateName) {
|
|
20
|
-
// this.__filters.push((context) => context.is(updateName));
|
|
21
|
-
// return this;
|
|
22
|
-
// },
|
|
23
|
-
// };
|
package/dist/plugin.d.ts
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
import type { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
2
|
-
import type { APIMethods } from "@gramio/types";
|
|
3
|
-
import type { Bot } from "./bot";
|
|
4
|
-
import { Composer } from "./composer";
|
|
5
|
-
import type { AnyBot, DeriveDefinitions, ErrorDefinitions, Handler, Hooks } from "./types";
|
|
6
|
-
/**
|
|
7
|
-
* `Plugin` is an object from which you can extends in Bot instance and adopt types
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* import { Plugin, Bot } from "gramio";
|
|
12
|
-
*
|
|
13
|
-
* export class PluginError extends Error {
|
|
14
|
-
* wow: "type" | "safe" = "type";
|
|
15
|
-
* }
|
|
16
|
-
*
|
|
17
|
-
* const plugin = new Plugin("gramio-example")
|
|
18
|
-
* .error("PLUGIN", PluginError)
|
|
19
|
-
* .derive(() => {
|
|
20
|
-
* return {
|
|
21
|
-
* some: ["derived", "props"] as const,
|
|
22
|
-
* };
|
|
23
|
-
* });
|
|
24
|
-
*
|
|
25
|
-
* const bot = new Bot(process.env.TOKEN!)
|
|
26
|
-
* .extend(plugin)
|
|
27
|
-
* .onError(({ context, kind, error }) => {
|
|
28
|
-
* if (context.is("message") && kind === "PLUGIN") {
|
|
29
|
-
* console.log(error.wow);
|
|
30
|
-
* }
|
|
31
|
-
* })
|
|
32
|
-
* .use((context) => {
|
|
33
|
-
* console.log(context.some);
|
|
34
|
-
* });
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
|
|
38
|
-
/**
|
|
39
|
-
* @internal
|
|
40
|
-
* Set of Plugin data
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*/
|
|
44
|
-
_: {
|
|
45
|
-
/** Name of plugin */
|
|
46
|
-
name: string;
|
|
47
|
-
/** List of plugin dependencies. If user does't extend from listed there dependencies it throw a error */
|
|
48
|
-
dependencies: string[];
|
|
49
|
-
/** remap generic type. {} in runtime */
|
|
50
|
-
Errors: Errors;
|
|
51
|
-
/** remap generic type. {} in runtime */
|
|
52
|
-
Derives: Derives;
|
|
53
|
-
/** Composer */
|
|
54
|
-
composer: Composer;
|
|
55
|
-
/** Store plugin preRequests hooks */
|
|
56
|
-
preRequests: [Hooks.PreRequest<any>, MaybeArray<keyof APIMethods> | undefined][];
|
|
57
|
-
/** Store plugin onResponses hooks */
|
|
58
|
-
onResponses: [Hooks.OnResponse<any>, MaybeArray<keyof APIMethods> | undefined][];
|
|
59
|
-
/** Store plugin onResponseErrors hooks */
|
|
60
|
-
onResponseErrors: [Hooks.OnResponseError<any>, MaybeArray<keyof APIMethods> | undefined][];
|
|
61
|
-
/**
|
|
62
|
-
* Store plugin groups
|
|
63
|
-
*
|
|
64
|
-
* If you use `on` or `use` in group and on plugin-level groups handlers are registered after plugin-level handlers
|
|
65
|
-
* */
|
|
66
|
-
groups: ((bot: AnyBot) => AnyBot)[];
|
|
67
|
-
/** Store plugin onStarts hooks */
|
|
68
|
-
onStarts: Hooks.OnStart[];
|
|
69
|
-
/** Store plugin onStops hooks */
|
|
70
|
-
onStops: Hooks.OnStop[];
|
|
71
|
-
/** Store plugin onErrors hooks */
|
|
72
|
-
onErrors: Hooks.OnError<any, any>[];
|
|
73
|
-
/** Map of plugin errors */
|
|
74
|
-
errorsDefinitions: Record<string, {
|
|
75
|
-
new (...args: any): any;
|
|
76
|
-
prototype: Error;
|
|
77
|
-
}>;
|
|
78
|
-
decorators: Record<string, unknown>;
|
|
79
|
-
};
|
|
80
|
-
/** Create new Plugin. Please provide `name` */
|
|
81
|
-
constructor(name: string, { dependencies }?: {
|
|
82
|
-
dependencies?: string[];
|
|
83
|
-
});
|
|
84
|
-
/** Currently not isolated!!!
|
|
85
|
-
*
|
|
86
|
-
* > [!WARNING]
|
|
87
|
-
* > If you use `on` or `use` in a `group` and at the plugin level, the group handlers are registered **after** the handlers at the plugin level
|
|
88
|
-
*/
|
|
89
|
-
group(grouped: (bot: Bot<Errors, Derives>) => AnyBot): this;
|
|
90
|
-
/**
|
|
91
|
-
* Register custom class-error in plugin
|
|
92
|
-
**/
|
|
93
|
-
error<Name extends string, NewError extends {
|
|
94
|
-
new (...args: any): any;
|
|
95
|
-
prototype: Error;
|
|
96
|
-
}>(kind: Name, error: NewError): Plugin<Errors & { [name in Name]: InstanceType<NewError>; }, Derives>;
|
|
97
|
-
/**
|
|
98
|
-
* Derive some data to handlers
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```ts
|
|
102
|
-
* new Bot("token").derive((context) => {
|
|
103
|
-
* return {
|
|
104
|
-
* superSend: () => context.send("Derived method")
|
|
105
|
-
* }
|
|
106
|
-
* })
|
|
107
|
-
* ```
|
|
108
|
-
*/
|
|
109
|
-
derive<Handler extends Hooks.Derive<Context<AnyBot>>>(handler: Handler): Plugin<Errors, Derives & {
|
|
110
|
-
global: Awaited<ReturnType<Handler>>;
|
|
111
|
-
}>;
|
|
112
|
-
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<AnyBot, Update>>>(updateName: MaybeArray<Update>, handler: Handler): Plugin<Errors, Derives & {
|
|
113
|
-
[K in Update]: Awaited<ReturnType<Handler>>;
|
|
114
|
-
}>;
|
|
115
|
-
decorate<Value extends Record<string, any>>(value: Value): Plugin<Errors, Derives & {
|
|
116
|
-
global: {
|
|
117
|
-
[K in keyof Value]: Value[K];
|
|
118
|
-
};
|
|
119
|
-
}>;
|
|
120
|
-
decorate<Name extends string, Value>(name: Name, value: Value): Plugin<Errors, Derives & {
|
|
121
|
-
global: {
|
|
122
|
-
[K in Name]: Value;
|
|
123
|
-
};
|
|
124
|
-
}>;
|
|
125
|
-
/** Register handler to one or many Updates */
|
|
126
|
-
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<AnyBot, T> & Derives["global"] & Derives[T]>): this;
|
|
127
|
-
/** Register handler to any Updates */
|
|
128
|
-
use(handler: Handler<Context<AnyBot> & Derives["global"]>): this;
|
|
129
|
-
/**
|
|
130
|
-
* This hook called before sending a request to Telegram Bot API (allows us to impact the sent parameters).
|
|
131
|
-
*
|
|
132
|
-
* @example
|
|
133
|
-
* ```typescript
|
|
134
|
-
* import { Bot } from "gramio";
|
|
135
|
-
*
|
|
136
|
-
* const bot = new Bot(process.env.TOKEN!).preRequest((context) => {
|
|
137
|
-
* if (context.method === "sendMessage") {
|
|
138
|
-
* context.params.text = "mutate params";
|
|
139
|
-
* }
|
|
140
|
-
*
|
|
141
|
-
* return context;
|
|
142
|
-
* });
|
|
143
|
-
*
|
|
144
|
-
* bot.start();
|
|
145
|
-
* ```
|
|
146
|
-
*
|
|
147
|
-
* [Documentation](https://gramio.dev/hooks/pre-request.html)
|
|
148
|
-
* */
|
|
149
|
-
preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
150
|
-
preRequest(handler: Hooks.PreRequest): this;
|
|
151
|
-
/**
|
|
152
|
-
* This hook called when API return successful response
|
|
153
|
-
*
|
|
154
|
-
* [Documentation](https://gramio.dev/hooks/on-response.html)
|
|
155
|
-
* */
|
|
156
|
-
onResponse<Methods extends keyof APIMethods, Handler extends Hooks.OnResponse<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
157
|
-
onResponse(handler: Hooks.OnResponse): this;
|
|
158
|
-
/**
|
|
159
|
-
* This hook called when API return an error
|
|
160
|
-
*
|
|
161
|
-
* [Documentation](https://gramio.dev/hooks/on-response-error.html)
|
|
162
|
-
* */
|
|
163
|
-
onResponseError<Methods extends keyof APIMethods, Handler extends Hooks.OnResponseError<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
164
|
-
onResponseError(handler: Hooks.OnResponseError): this;
|
|
165
|
-
/**
|
|
166
|
-
* This hook called when the bot is `started`.
|
|
167
|
-
*
|
|
168
|
-
* @example
|
|
169
|
-
* ```typescript
|
|
170
|
-
* import { Bot } from "gramio";
|
|
171
|
-
*
|
|
172
|
-
* const bot = new Bot(process.env.TOKEN!).onStart(
|
|
173
|
-
* ({ plugins, info, updatesFrom }) => {
|
|
174
|
-
* console.log(`plugin list - ${plugins.join(", ")}`);
|
|
175
|
-
* console.log(`bot username is @${info.username}`);
|
|
176
|
-
* console.log(`updates from ${updatesFrom}`);
|
|
177
|
-
* }
|
|
178
|
-
* );
|
|
179
|
-
*
|
|
180
|
-
* bot.start();
|
|
181
|
-
* ```
|
|
182
|
-
*
|
|
183
|
-
* [Documentation](https://gramio.dev/hooks/on-start.html)
|
|
184
|
-
* */
|
|
185
|
-
onStart(handler: Hooks.OnStart): this;
|
|
186
|
-
/**
|
|
187
|
-
* This hook called when the bot stops.
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* ```typescript
|
|
191
|
-
* import { Bot } from "gramio";
|
|
192
|
-
*
|
|
193
|
-
* const bot = new Bot(process.env.TOKEN!).onStop(
|
|
194
|
-
* ({ plugins, info, updatesFrom }) => {
|
|
195
|
-
* console.log(`plugin list - ${plugins.join(", ")}`);
|
|
196
|
-
* console.log(`bot username is @${info.username}`);
|
|
197
|
-
* }
|
|
198
|
-
* );
|
|
199
|
-
*
|
|
200
|
-
* bot.start();
|
|
201
|
-
* bot.stop();
|
|
202
|
-
* ```
|
|
203
|
-
*
|
|
204
|
-
* [Documentation](https://gramio.dev/hooks/on-stop.html)
|
|
205
|
-
* */
|
|
206
|
-
onStop(handler: Hooks.OnStop): this;
|
|
207
|
-
/**
|
|
208
|
-
* Set error handler.
|
|
209
|
-
* @example
|
|
210
|
-
* ```ts
|
|
211
|
-
* bot.onError("message", ({ context, kind, error }) => {
|
|
212
|
-
* return context.send(`${kind}: ${error.message}`);
|
|
213
|
-
* })
|
|
214
|
-
* ```
|
|
215
|
-
*/
|
|
216
|
-
onError<T extends UpdateName>(updateName: MaybeArray<T>, handler: Hooks.OnError<Errors, ContextType<Bot, T> & Derives["global"] & Derives[T]>): this;
|
|
217
|
-
onError(handler: Hooks.OnError<Errors, Context<AnyBot> & Derives["global"]>): this;
|
|
218
|
-
}
|
package/dist/plugin.js
DELETED
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
-
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
-
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
-
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
-
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
-
var _, done = false;
|
|
8
|
-
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
-
var context = {};
|
|
10
|
-
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
-
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
-
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
-
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
-
if (kind === "accessor") {
|
|
15
|
-
if (result === void 0) continue;
|
|
16
|
-
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
-
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
-
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
-
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
-
}
|
|
21
|
-
else if (_ = accept(result)) {
|
|
22
|
-
if (kind === "field") initializers.unshift(_);
|
|
23
|
-
else descriptor[key] = _;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
-
done = true;
|
|
28
|
-
};
|
|
29
|
-
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
-
var useValue = arguments.length > 2;
|
|
31
|
-
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
-
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
-
}
|
|
34
|
-
return useValue ? value : void 0;
|
|
35
|
-
};
|
|
36
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
-
exports.Plugin = void 0;
|
|
38
|
-
const inspectable_1 = require("inspectable");
|
|
39
|
-
const composer_1 = require("./composer");
|
|
40
|
-
const errors_1 = require("./errors");
|
|
41
|
-
/**
|
|
42
|
-
* `Plugin` is an object from which you can extends in Bot instance and adopt types
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* ```ts
|
|
46
|
-
* import { Plugin, Bot } from "gramio";
|
|
47
|
-
*
|
|
48
|
-
* export class PluginError extends Error {
|
|
49
|
-
* wow: "type" | "safe" = "type";
|
|
50
|
-
* }
|
|
51
|
-
*
|
|
52
|
-
* const plugin = new Plugin("gramio-example")
|
|
53
|
-
* .error("PLUGIN", PluginError)
|
|
54
|
-
* .derive(() => {
|
|
55
|
-
* return {
|
|
56
|
-
* some: ["derived", "props"] as const,
|
|
57
|
-
* };
|
|
58
|
-
* });
|
|
59
|
-
*
|
|
60
|
-
* const bot = new Bot(process.env.TOKEN!)
|
|
61
|
-
* .extend(plugin)
|
|
62
|
-
* .onError(({ context, kind, error }) => {
|
|
63
|
-
* if (context.is("message") && kind === "PLUGIN") {
|
|
64
|
-
* console.log(error.wow);
|
|
65
|
-
* }
|
|
66
|
-
* })
|
|
67
|
-
* .use((context) => {
|
|
68
|
-
* console.log(context.some);
|
|
69
|
-
* });
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
let Plugin = (() => {
|
|
73
|
-
let _classDecorators = [(0, inspectable_1.Inspectable)({
|
|
74
|
-
serialize: (plugin) => ({
|
|
75
|
-
name: plugin._.name,
|
|
76
|
-
dependencies: plugin._.dependencies,
|
|
77
|
-
}),
|
|
78
|
-
})];
|
|
79
|
-
let _classDescriptor;
|
|
80
|
-
let _classExtraInitializers = [];
|
|
81
|
-
let _classThis;
|
|
82
|
-
var Plugin = class {
|
|
83
|
-
static { _classThis = this; }
|
|
84
|
-
static {
|
|
85
|
-
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
86
|
-
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
87
|
-
Plugin = _classThis = _classDescriptor.value;
|
|
88
|
-
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
89
|
-
__runInitializers(_classThis, _classExtraInitializers);
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* @internal
|
|
93
|
-
* Set of Plugin data
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*/
|
|
97
|
-
_ = {
|
|
98
|
-
/** Name of plugin */
|
|
99
|
-
name: "",
|
|
100
|
-
/** List of plugin dependencies. If user does't extend from listed there dependencies it throw a error */
|
|
101
|
-
dependencies: [],
|
|
102
|
-
/** remap generic type. {} in runtime */
|
|
103
|
-
Errors: {},
|
|
104
|
-
/** remap generic type. {} in runtime */
|
|
105
|
-
Derives: {},
|
|
106
|
-
/** Composer */
|
|
107
|
-
composer: new composer_1.Composer(),
|
|
108
|
-
/** Store plugin preRequests hooks */
|
|
109
|
-
preRequests: [],
|
|
110
|
-
/** Store plugin onResponses hooks */
|
|
111
|
-
onResponses: [],
|
|
112
|
-
/** Store plugin onResponseErrors hooks */
|
|
113
|
-
onResponseErrors: [],
|
|
114
|
-
/**
|
|
115
|
-
* Store plugin groups
|
|
116
|
-
*
|
|
117
|
-
* If you use `on` or `use` in group and on plugin-level groups handlers are registered after plugin-level handlers
|
|
118
|
-
* */
|
|
119
|
-
groups: [],
|
|
120
|
-
/** Store plugin onStarts hooks */
|
|
121
|
-
onStarts: [],
|
|
122
|
-
/** Store plugin onStops hooks */
|
|
123
|
-
onStops: [],
|
|
124
|
-
/** Store plugin onErrors hooks */
|
|
125
|
-
onErrors: [],
|
|
126
|
-
/** Map of plugin errors */
|
|
127
|
-
errorsDefinitions: {},
|
|
128
|
-
decorators: {},
|
|
129
|
-
};
|
|
130
|
-
/** Create new Plugin. Please provide `name` */
|
|
131
|
-
constructor(name, { dependencies } = {}) {
|
|
132
|
-
this._.name = name;
|
|
133
|
-
if (dependencies)
|
|
134
|
-
this._.dependencies = dependencies;
|
|
135
|
-
}
|
|
136
|
-
/** Currently not isolated!!!
|
|
137
|
-
*
|
|
138
|
-
* > [!WARNING]
|
|
139
|
-
* > If you use `on` or `use` in a `group` and at the plugin level, the group handlers are registered **after** the handlers at the plugin level
|
|
140
|
-
*/
|
|
141
|
-
group(grouped) {
|
|
142
|
-
this._.groups.push(grouped);
|
|
143
|
-
return this;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Register custom class-error in plugin
|
|
147
|
-
**/
|
|
148
|
-
error(kind, error) {
|
|
149
|
-
//@ts-expect-error Set ErrorKind
|
|
150
|
-
error[errors_1.ErrorKind] = kind;
|
|
151
|
-
this._.errorsDefinitions[kind] = error;
|
|
152
|
-
return this;
|
|
153
|
-
}
|
|
154
|
-
derive(updateNameOrHandler, handler) {
|
|
155
|
-
this._.composer.derive(updateNameOrHandler, handler);
|
|
156
|
-
return this;
|
|
157
|
-
}
|
|
158
|
-
decorate(nameOrValue, value) {
|
|
159
|
-
if (typeof nameOrValue === "string")
|
|
160
|
-
this._.decorators[nameOrValue] = value;
|
|
161
|
-
else {
|
|
162
|
-
for (const [name, value] of Object.entries(nameOrValue)) {
|
|
163
|
-
this._.decorators[name] = value;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return this;
|
|
167
|
-
}
|
|
168
|
-
/** Register handler to one or many Updates */
|
|
169
|
-
on(updateName, handler) {
|
|
170
|
-
this._.composer.on(updateName, handler);
|
|
171
|
-
return this;
|
|
172
|
-
}
|
|
173
|
-
/** Register handler to any Updates */
|
|
174
|
-
use(handler) {
|
|
175
|
-
this._.composer.use(handler);
|
|
176
|
-
return this;
|
|
177
|
-
}
|
|
178
|
-
preRequest(methodsOrHandler, handler) {
|
|
179
|
-
if ((typeof methodsOrHandler === "string" ||
|
|
180
|
-
Array.isArray(methodsOrHandler)) &&
|
|
181
|
-
handler)
|
|
182
|
-
this._.preRequests.push([handler, methodsOrHandler]);
|
|
183
|
-
else if (typeof methodsOrHandler === "function")
|
|
184
|
-
this._.preRequests.push([methodsOrHandler, undefined]);
|
|
185
|
-
return this;
|
|
186
|
-
}
|
|
187
|
-
onResponse(methodsOrHandler, handler) {
|
|
188
|
-
if ((typeof methodsOrHandler === "string" ||
|
|
189
|
-
Array.isArray(methodsOrHandler)) &&
|
|
190
|
-
handler)
|
|
191
|
-
this._.onResponses.push([handler, methodsOrHandler]);
|
|
192
|
-
else if (typeof methodsOrHandler === "function")
|
|
193
|
-
this._.onResponses.push([methodsOrHandler, undefined]);
|
|
194
|
-
return this;
|
|
195
|
-
}
|
|
196
|
-
onResponseError(methodsOrHandler, handler) {
|
|
197
|
-
if ((typeof methodsOrHandler === "string" ||
|
|
198
|
-
Array.isArray(methodsOrHandler)) &&
|
|
199
|
-
handler)
|
|
200
|
-
this._.onResponseErrors.push([handler, methodsOrHandler]);
|
|
201
|
-
else if (typeof methodsOrHandler === "function")
|
|
202
|
-
this._.onResponseErrors.push([methodsOrHandler, undefined]);
|
|
203
|
-
return this;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* This hook called when the bot is `started`.
|
|
207
|
-
*
|
|
208
|
-
* @example
|
|
209
|
-
* ```typescript
|
|
210
|
-
* import { Bot } from "gramio";
|
|
211
|
-
*
|
|
212
|
-
* const bot = new Bot(process.env.TOKEN!).onStart(
|
|
213
|
-
* ({ plugins, info, updatesFrom }) => {
|
|
214
|
-
* console.log(`plugin list - ${plugins.join(", ")}`);
|
|
215
|
-
* console.log(`bot username is @${info.username}`);
|
|
216
|
-
* console.log(`updates from ${updatesFrom}`);
|
|
217
|
-
* }
|
|
218
|
-
* );
|
|
219
|
-
*
|
|
220
|
-
* bot.start();
|
|
221
|
-
* ```
|
|
222
|
-
*
|
|
223
|
-
* [Documentation](https://gramio.dev/hooks/on-start.html)
|
|
224
|
-
* */
|
|
225
|
-
onStart(handler) {
|
|
226
|
-
this._.onStarts.push(handler);
|
|
227
|
-
return this;
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* This hook called when the bot stops.
|
|
231
|
-
*
|
|
232
|
-
* @example
|
|
233
|
-
* ```typescript
|
|
234
|
-
* import { Bot } from "gramio";
|
|
235
|
-
*
|
|
236
|
-
* const bot = new Bot(process.env.TOKEN!).onStop(
|
|
237
|
-
* ({ plugins, info, updatesFrom }) => {
|
|
238
|
-
* console.log(`plugin list - ${plugins.join(", ")}`);
|
|
239
|
-
* console.log(`bot username is @${info.username}`);
|
|
240
|
-
* }
|
|
241
|
-
* );
|
|
242
|
-
*
|
|
243
|
-
* bot.start();
|
|
244
|
-
* bot.stop();
|
|
245
|
-
* ```
|
|
246
|
-
*
|
|
247
|
-
* [Documentation](https://gramio.dev/hooks/on-stop.html)
|
|
248
|
-
* */
|
|
249
|
-
onStop(handler) {
|
|
250
|
-
this._.onStops.push(handler);
|
|
251
|
-
return this;
|
|
252
|
-
}
|
|
253
|
-
onError(updateNameOrHandler, handler) {
|
|
254
|
-
if (typeof updateNameOrHandler === "function") {
|
|
255
|
-
this._.onErrors.push(updateNameOrHandler);
|
|
256
|
-
return this;
|
|
257
|
-
}
|
|
258
|
-
if (handler) {
|
|
259
|
-
this._.onErrors.push(async (errContext) => {
|
|
260
|
-
if (errContext.context.is(updateNameOrHandler))
|
|
261
|
-
await handler(errContext);
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
return this;
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
return Plugin = _classThis;
|
|
268
|
-
})();
|
|
269
|
-
exports.Plugin = Plugin;
|