Comparing version 0.0.19 to 0.0.20
@@ -73,5 +73,9 @@ import { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
onStart(handler: Hooks.OnStart): this; | ||
preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this; | ||
preRequest(handler: Hooks.PreRequest): 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: MaybePromise<NewPlugin>): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>; | ||
hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx) => unknown): void; | ||
command(command: string, handler: (context: ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]) => unknown): void; | ||
start({ webhook, dropPendingUpdates, allowedUpdates, }?: { | ||
@@ -78,0 +82,0 @@ webhook?: Omit<APIMethodParams<"setWebhook">, "drop_pending_updates" | "allowed_updates">; |
@@ -82,10 +82,10 @@ "use strict"; | ||
preRequest: [ | ||
(ctx) => { | ||
if (!ctx.params) | ||
return ctx; | ||
const formattable = format_1.FormattableMap[ctx.method]; | ||
(context) => { | ||
if (!context.params) | ||
return context; | ||
const formattable = format_1.FormattableMap[context.method]; | ||
// @ts-ignore add AnyTelegramMethod to @gramio/format | ||
if (formattable) | ||
ctx.params = formattable(ctx.params); | ||
return ctx; | ||
context.params = formattable(context.params); | ||
return context; | ||
}, | ||
@@ -217,2 +217,25 @@ ], | ||
} | ||
preRequest(methodsOrHandler, handler) { | ||
if (typeof methodsOrHandler === "string" || | ||
Array.isArray(methodsOrHandler)) { | ||
// TODO: error | ||
if (!handler) | ||
throw new Error("TODO:"); | ||
const methods = typeof methodsOrHandler === "string" | ||
? [methodsOrHandler] | ||
: methodsOrHandler; | ||
// TODO: remove error | ||
// @ts-expect-error | ||
this.hooks.preRequest.push(async (context) => { | ||
// TODO: remove ts-ignore | ||
// @ts-expect-error | ||
if (methods.includes(context.method)) | ||
return handler(context); | ||
return context; | ||
}); | ||
} | ||
else | ||
this.hooks.preRequest.push(methodsOrHandler); | ||
return this; | ||
} | ||
on(updateName, handler) { | ||
@@ -249,2 +272,32 @@ this.updates.on(updateName, handler); | ||
} | ||
hears(trigger, handler) { | ||
this.on("message", (context, next) => { | ||
if ((typeof trigger === "string" && context.text === trigger) || | ||
// @ts-expect-error | ||
(typeof trigger === "function" && trigger(context)) || | ||
(trigger instanceof RegExp && | ||
context.text && | ||
trigger.test(context.text))) { | ||
// TODO: remove | ||
//@ts-expect-error | ||
return handler(context); | ||
} | ||
return next(); | ||
}); | ||
} | ||
command(command, handler) { | ||
this.on("message", (context, next) => { | ||
if (context.entities?.some((entity) => { | ||
if (entity.type !== "bot_command" || entity.offset > 0) | ||
return false; | ||
const cmd = context.text | ||
?.slice(1, entity.length) | ||
// biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
?.replace(`@${this.info.username}`, ""); | ||
return cmd && cmd === command; | ||
})) | ||
return handler(context); | ||
return next(); | ||
}); | ||
} | ||
async start({ webhook, dropPendingUpdates, allowedUpdates, } = {}) { | ||
@@ -251,0 +304,0 @@ await Promise.all(this.lazyloadPlugins.map(async (plugin) => this.extend(await plugin))); |
@@ -17,13 +17,13 @@ import { BotLike, Context, UpdateName } from "@gramio/contexts"; | ||
}[keyof APIMethods]; | ||
type AnyTelegramMethod = { | ||
[APIMethod in keyof APIMethods]: { | ||
type AnyTelegramMethod<Methods extends keyof APIMethods> = { | ||
[APIMethod in Methods]: { | ||
method: APIMethod; | ||
params: APIMethodParams<APIMethod>; | ||
}; | ||
}[keyof APIMethods]; | ||
}[Methods]; | ||
export type MaybePromise<T> = Promise<T> | T; | ||
export declare namespace Hooks { | ||
type Derive<Ctx> = (context: Ctx) => MaybePromise<Record<string, unknown>>; | ||
type PreRequestContext = AnyTelegramMethod; | ||
type PreRequest = (ctx: PreRequestContext) => MaybePromise<PreRequestContext>; | ||
type PreRequestContext<Methods extends keyof APIMethods> = AnyTelegramMethod<Methods>; | ||
type PreRequest<Methods extends keyof APIMethods = keyof APIMethods> = (ctx: PreRequestContext<Methods>) => MaybePromise<PreRequestContext<Methods>>; | ||
type OnErrorContext<Ctx extends Context<BotLike>, T extends ErrorDefinitions> = ErrorHandlerParams<Ctx, "TELEGRAM", AnyTelegramError> | ErrorHandlerParams<Ctx, "UNKNOWN", Error> | { | ||
@@ -30,0 +30,0 @@ [K in keyof T]: ErrorHandlerParams<Ctx, K & string, T[K & string]>; |
@@ -35,3 +35,3 @@ "use strict"; | ||
if (!UpdateContext) | ||
return; | ||
throw new Error(updateType); | ||
try { | ||
@@ -78,4 +78,4 @@ let context = new UpdateContext({ | ||
const updates = await this.bot.api.getUpdates({ | ||
...params, | ||
offset: this.offset, | ||
...params, | ||
}); | ||
@@ -82,0 +82,0 @@ for await (const update of updates) { |
{ | ||
"name": "gramio", | ||
"version": "0.0.19", | ||
"version": "0.0.20", | ||
"description": "Powerful Telegram Bot API framework", | ||
@@ -29,4 +29,4 @@ "main": "./dist/index.js", | ||
"@gramio/types": "^7.1.6", | ||
"@types/node": "^20.11.24", | ||
"typescript": "^5.3.3" | ||
"@types/node": "^20.11.25", | ||
"typescript": "^5.4.2" | ||
}, | ||
@@ -40,3 +40,3 @@ "dependencies": { | ||
"middleware-io": "^2.8.1", | ||
"undici": "^6.6.2" | ||
"undici": "^6.7.1" | ||
}, | ||
@@ -43,0 +43,0 @@ "files": [ |
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
36584
829
Updatedundici@^6.7.1