Comparing version 0.0.11 to 0.0.12
@@ -0,6 +1,8 @@ | ||
import { Context } from "@gramio/contexts"; | ||
import type { APIMethods } from "@gramio/types"; | ||
import "reflect-metadata"; | ||
import { BotOptions, ErrorDefinitions, Hooks } from "./types"; | ||
import { Plugin } from "#plugin"; | ||
import { BotOptions, ErrorDefinitions, Handler, Hooks } from "./types"; | ||
import { Updates } from "./updates"; | ||
export declare class Bot<Errors extends ErrorDefinitions = {}> { | ||
export declare class Bot<Errors extends ErrorDefinitions = {}, Derives = {}> { | ||
readonly options: BotOptions; | ||
@@ -49,3 +51,3 @@ readonly api: APIMethods; | ||
prototype: Error; | ||
}>(kind: Name, error: NewError): Bot<Errors & { [name in Name]: InstanceType<NewError>; }>; | ||
}>(kind: Name, error: NewError): Bot<Errors & { [name in Name]: InstanceType<NewError>; }, {}>; | ||
/** | ||
@@ -61,2 +63,5 @@ * Set error handler. | ||
onError(handler: Hooks.OnError<Errors>): this; | ||
derive<Handler extends (context: Context) => object>(handler: Handler): Bot<Errors, Derives & ReturnType<Handler>>; | ||
use(handler: Handler<Context & Derives>): this; | ||
extend<NewPlugin extends Plugin>(plugin: NewPlugin): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>; | ||
} |
@@ -41,2 +41,3 @@ "use strict"; | ||
const formattable = format_1.FormattableMap[ctx.method]; | ||
// @ts-ignore add AnyTelegramMethod to @gramio/format | ||
if (formattable) | ||
@@ -146,2 +147,25 @@ ctx.params = formattable(ctx.params); | ||
} | ||
derive(handler) { | ||
this.updates.use((context, next) => { | ||
for (const [key, value] of Object.entries(handler(context))) { | ||
context[key] = value; | ||
} | ||
next(); | ||
}); | ||
return this; | ||
} | ||
use(handler) { | ||
this.updates.use(handler); | ||
return this; | ||
} | ||
extend(plugin) { | ||
for (const [key, value] of Object.entries(plugin.errorsDefinitions)) { | ||
if (this.errorsDefinitions[key]) | ||
this.errorsDefinitions[key] = value; | ||
} | ||
for (const derive of plugin.derives) { | ||
this.derive(derive); | ||
} | ||
return this; | ||
} | ||
}; | ||
@@ -153,3 +177,2 @@ exports.Bot = Bot; | ||
}) | ||
// biome-ignore lint/complexity/noBannedTypes: <explanation> | ||
], Bot); |
export * from "./bot"; | ||
export * from "./errors"; | ||
export * from "./types"; | ||
export * from "./plugin"; | ||
export * from "@gramio/contexts"; | ||
@@ -5,0 +6,0 @@ export * from "@gramio/files"; |
@@ -20,2 +20,3 @@ "use strict"; | ||
__exportStar(require("./types"), exports); | ||
__exportStar(require("./plugin"), exports); | ||
__exportStar(require("@gramio/contexts"), exports); | ||
@@ -22,0 +23,0 @@ __exportStar(require("@gramio/files"), exports); |
@@ -36,5 +36,3 @@ import { Context } from "@gramio/contexts"; | ||
} | ||
export type ErrorDefinitions = Record<string, { | ||
prototype: Error; | ||
}>; | ||
export type ErrorDefinitions = Record<string, Error>; | ||
export {}; |
@@ -12,5 +12,5 @@ import { Context, UpdateName, contextsMappings } from "@gramio/contexts"; | ||
private onError; | ||
constructor(bot: Bot<any>, onError: CaughtMiddlewareHandler<Context>); | ||
constructor(bot: Bot<any, any>, onError: CaughtMiddlewareHandler<Context>); | ||
on<T extends UpdateName>(updateName: T, handler: Handler<InstanceType<(typeof contextsMappings)[T]>>): this; | ||
use(handler: Handler<Context>): this; | ||
use(handler: Handler<Context & any>): this; | ||
handleUpdate(data: TelegramUpdate): Promise<void>; | ||
@@ -17,0 +17,0 @@ startPolling(): Promise<void>; |
@@ -7,3 +7,2 @@ "use strict"; | ||
class Updates { | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
bot; | ||
@@ -14,3 +13,2 @@ isStarted = false; | ||
onError; | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
constructor(bot, onError) { | ||
@@ -41,2 +39,3 @@ this.bot = bot; | ||
let context = new UpdateContext({ | ||
//@ts-expect-error | ||
bot: this.bot, | ||
@@ -51,2 +50,3 @@ update: data, | ||
context = new contexts_1.contextsMappings[context.eventType]({ | ||
//@ts-expect-error | ||
bot: this.bot, | ||
@@ -53,0 +53,0 @@ update: data, |
{ | ||
"name": "gramio", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "WIP", | ||
@@ -17,4 +17,5 @@ "main": "./dist/index.js", | ||
"@biomejs/biome": "1.5.3", | ||
"@gramio/types": "^7.1.1", | ||
"@types/node": "^20.11.19" | ||
"@gramio/types": "^7.1.2", | ||
"@types/node": "^20.11.19", | ||
"typescript": "^5.3.3" | ||
}, | ||
@@ -24,3 +25,3 @@ "dependencies": { | ||
"@gramio/files": "^0.0.3", | ||
"@gramio/format": "^0.0.7", | ||
"@gramio/format": "^0.0.8", | ||
"@gramio/keyboards": "^0.1.6", | ||
@@ -27,0 +28,0 @@ "form-data-encoder": "^4.0.2", |
@@ -0,0 +0,0 @@ # GramIO |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19646
505
4
+ Added@gramio/format@0.0.8(transitive)
Updated@gramio/format@^0.0.8