Comparing version 0.0.16 to 0.0.17
import { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
import type { APIMethods } from "@gramio/types"; | ||
import type { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types"; | ||
import "reflect-metadata"; | ||
@@ -9,3 +9,5 @@ import { Plugin } from "./plugin"; | ||
readonly options: BotOptions; | ||
info: TelegramUser | undefined; | ||
readonly api: APIMethods; | ||
private dependencies; | ||
private errorsDefinitions; | ||
@@ -70,5 +72,11 @@ private errorHandler; | ||
}>; | ||
onStart(handler: Hooks.OnStart): this; | ||
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this; | ||
use(handler: Handler<Context<typeof this> & Derives["global"]>): this; | ||
extend<NewPlugin extends Plugin>(plugin: NewPlugin): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>; | ||
start({ webhook, dropPendingUpdates, allowedUpdates, }?: { | ||
webhook?: Omit<APIMethodParams<"setWebhook">, "drop_pending_updates" | "allowed_updates">; | ||
dropPendingUpdates?: boolean; | ||
allowedUpdates?: NonNullable<APIMethodParams<"getUpdates">>["allowed_updates"]; | ||
}): Promise<TelegramUser>; | ||
} |
@@ -20,5 +20,7 @@ "use strict"; | ||
options = {}; | ||
info; | ||
api = new Proxy({}, { | ||
get: (_target, method) => (args) => this._callApi(method, args), | ||
}); | ||
dependencies = []; | ||
errorsDefinitions = { | ||
@@ -49,2 +51,3 @@ TELEGRAM: errors_1.TelegramError, | ||
onError: [], | ||
onStart: [], | ||
}; | ||
@@ -63,2 +66,4 @@ constructor(token, options) { | ||
for await (const hook of this.hooks[type]) { | ||
//TODO: solve that later | ||
//@ts-expect-error | ||
await hook(context); | ||
@@ -168,2 +173,6 @@ } | ||
} | ||
onStart(handler) { | ||
this.hooks.onStart.push(handler); | ||
return this; | ||
} | ||
on(updateName, handler) { | ||
@@ -178,2 +187,6 @@ this.updates.on(updateName, handler); | ||
extend(plugin) { | ||
if (plugin.dependencies.some((dep) => !this.dependencies.includes(dep))) | ||
throw new Error(`The «${plugin.name}» plugin needs dependencies registered before: ${plugin.dependencies | ||
.filter((dep) => !this.dependencies.includes(dep)) | ||
.join(", ")}`); | ||
for (const [key, value] of Object.entries(plugin.errorsDefinitions)) { | ||
@@ -190,4 +203,36 @@ if (this.errorsDefinitions[key]) | ||
} | ||
this.dependencies.push(plugin.name); | ||
return this; | ||
} | ||
async start({ webhook, dropPendingUpdates, allowedUpdates, } = {}) { | ||
//TODO: maybe it useless?? | ||
this.info = await this.api.getMe(); | ||
if (!webhook) { | ||
await this.api.deleteWebhook({ | ||
drop_pending_updates: dropPendingUpdates, | ||
}); | ||
await this.updates.startPolling({ | ||
allowed_updates: allowedUpdates, | ||
}); | ||
this.runImmutableHooks("onStart", { | ||
plugins: this.dependencies, | ||
info: this.info, | ||
updatesFrom: "long-polling", | ||
}); | ||
return this.info; | ||
} | ||
if (this.updates.isStarted) | ||
this.updates.stopPolling(); | ||
await this.api.setWebhook({ | ||
...webhook, | ||
drop_pending_updates: dropPendingUpdates, | ||
allowed_updates: allowedUpdates, | ||
}); | ||
this.runImmutableHooks("onStart", { | ||
plugins: this.dependencies, | ||
info: this.info, | ||
updatesFrom: "long-polling", | ||
}); | ||
return this.info; | ||
} | ||
}; | ||
@@ -194,0 +239,0 @@ exports.Bot = Bot; |
@@ -5,2 +5,3 @@ export * from "./bot"; | ||
export * from "./plugin"; | ||
export * from "./webhook"; | ||
export * from "@gramio/contexts"; | ||
@@ -7,0 +8,0 @@ export * from "@gramio/files"; |
@@ -21,2 +21,3 @@ "use strict"; | ||
__exportStar(require("./plugin"), exports); | ||
__exportStar(require("./webhook"), exports); | ||
__exportStar(require("@gramio/contexts"), exports); | ||
@@ -23,0 +24,0 @@ __exportStar(require("@gramio/files"), exports); |
@@ -12,3 +12,6 @@ import { BotLike, Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
}>; | ||
constructor(name: string); | ||
dependencies: string[]; | ||
constructor(name: string, { dependencies }?: { | ||
dependencies?: string[]; | ||
}); | ||
/** | ||
@@ -15,0 +18,0 @@ * Register custom class-error in plugin |
@@ -12,4 +12,7 @@ "use strict"; | ||
errorsDefinitions = {}; | ||
constructor(name) { | ||
dependencies = []; | ||
constructor(name, { dependencies } = {}) { | ||
this.name = name; | ||
if (dependencies) | ||
this.dependencies = dependencies; | ||
} | ||
@@ -16,0 +19,0 @@ /** |
import { BotLike, Context, UpdateName } from "@gramio/contexts"; | ||
import { APIMethodParams, APIMethods } from "@gramio/types"; | ||
import { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types"; | ||
import { NextMiddleware } from "middleware-io"; | ||
@@ -32,5 +32,11 @@ import { TelegramError } from "./errors"; | ||
type OnError<T extends ErrorDefinitions, Ctx extends Context<BotLike> = Context<BotLike>> = (options: OnErrorContext<Ctx, T>) => unknown; | ||
type OnStart = (context: { | ||
plugins: string[]; | ||
info: TelegramUser; | ||
updatesFrom: "webhook" | "long-polling"; | ||
}) => unknown; | ||
interface Store<T extends ErrorDefinitions> { | ||
preRequest: PreRequest[]; | ||
onError: OnError<T>[]; | ||
onStart: OnStart[]; | ||
} | ||
@@ -37,0 +43,0 @@ } |
import { Context, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
import type { TelegramUpdate } from "@gramio/types"; | ||
import type { APIMethodParams, TelegramUpdate } from "@gramio/types"; | ||
import { CaughtMiddlewareHandler } from "middleware-io"; | ||
@@ -8,3 +8,3 @@ import type { Bot } from "./bot"; | ||
private readonly bot; | ||
private isStarted; | ||
isStarted: boolean; | ||
private offset; | ||
@@ -17,5 +17,6 @@ private composer; | ||
handleUpdate(data: TelegramUpdate): Promise<void>; | ||
startPolling(): Promise<void>; | ||
startFetchLoop(): Promise<void>; | ||
/**@deprecated use bot.start instead */ | ||
startPolling(params?: APIMethodParams<"getUpdates">): Promise<void>; | ||
startFetchLoop(params?: APIMethodParams<"getUpdates">): Promise<void>; | ||
stopPolling(): void; | ||
} |
@@ -66,13 +66,15 @@ "use strict"; | ||
} | ||
async startPolling() { | ||
/**@deprecated use bot.start instead */ | ||
async startPolling(params = {}) { | ||
if (this.isStarted) | ||
throw new Error("[UPDATES] Polling already started!"); | ||
this.isStarted = true; | ||
this.startFetchLoop(); | ||
this.startFetchLoop(params); | ||
return; | ||
} | ||
async startFetchLoop() { | ||
async startFetchLoop(params = {}) { | ||
while (this.isStarted) { | ||
const updates = await this.bot.api.getUpdates({ | ||
offset: this.offset, | ||
...params, | ||
}); | ||
@@ -79,0 +81,0 @@ for await (const update of updates) { |
{ | ||
"name": "gramio", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "Powerful Telegram Bot API framework", | ||
@@ -28,3 +28,3 @@ "main": "./dist/index.js", | ||
"@biomejs/biome": "1.5.3", | ||
"@gramio/types": "^7.1.3", | ||
"@gramio/types": "^7.1.4", | ||
"@types/node": "^20.11.20", | ||
@@ -31,0 +31,0 @@ "typescript": "^5.3.3" |
26622
18
654