Comparing version 0.0.26 to 0.0.27
@@ -5,3 +5,3 @@ import { CallbackData } from "@gramio/callback-data"; | ||
import { Plugin } from "./plugin"; | ||
import type { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks, MaybePromise } from "./types"; | ||
import type { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks, MaybePromise, SuppressedAPIMethods } from "./types"; | ||
import { Updates } from "./updates"; | ||
@@ -13,3 +13,3 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> { | ||
info: TelegramUser | undefined; | ||
readonly api: APIMethods; | ||
readonly api: SuppressedAPIMethods; | ||
private lazyloadPlugins; | ||
@@ -91,2 +91,12 @@ private dependencies; | ||
}) => unknown): this; | ||
chosenInlineResult<Ctx = ContextType<typeof this, "chosen_inline_result"> & Derives["global"] & Derives["chosen_inline_result"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & { | ||
args: RegExpMatchArray | null; | ||
}) => unknown): this; | ||
inlineQuery<Ctx = ContextType<typeof this, "inline_query"> & Derives["global"] & Derives["inline_query"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & { | ||
args: RegExpMatchArray | null; | ||
}) => unknown, options?: { | ||
onResult?: (context: ContextType<Bot, "chosen_inline_result"> & Derives["global"] & Derives["chosen_inline_result"] & { | ||
args: RegExpMatchArray | null; | ||
}) => unknown; | ||
}): this; | ||
hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & { | ||
@@ -93,0 +103,0 @@ args: RegExpMatchArray | null; |
@@ -152,3 +152,5 @@ "use strict"; | ||
this.runImmutableHooks("onResponseError", err, this.api); | ||
throw err; | ||
if (!params?.suppress) | ||
throw err; | ||
return err; | ||
} | ||
@@ -400,4 +402,43 @@ this.runImmutableHooks("onResponse", | ||
} | ||
chosenInlineResult(trigger, handler) { | ||
return this.on("chosen_inline_result", (context, next) => { | ||
if ((typeof trigger === "string" && context.query === trigger) || | ||
// @ts-expect-error | ||
(typeof trigger === "function" && trigger(context)) || | ||
(trigger instanceof RegExp && | ||
context.query && | ||
trigger.test(context.query))) { | ||
//@ts-expect-error | ||
context.args = | ||
trigger instanceof RegExp ? context.query?.match(trigger) : null; | ||
// TODO: remove | ||
//@ts-expect-error | ||
return handler(context); | ||
} | ||
return next(); | ||
}); | ||
} | ||
inlineQuery(trigger, handler, options = {}) { | ||
// @ts-expect-error fix later... | ||
if (options.onResult) | ||
this.chosenInlineResult(trigger, options.onResult); | ||
return this.on("inline_query", (context, next) => { | ||
if ((typeof trigger === "string" && context.query === trigger) || | ||
// @ts-expect-error | ||
(typeof trigger === "function" && trigger(context)) || | ||
(trigger instanceof RegExp && | ||
context.query && | ||
trigger.test(context.query))) { | ||
//@ts-expect-error | ||
context.args = | ||
trigger instanceof RegExp ? context.query?.match(trigger) : null; | ||
// TODO: remove | ||
//@ts-expect-error | ||
return handler(context); | ||
} | ||
return next(); | ||
}); | ||
} | ||
hears(trigger, handler) { | ||
return this.on(["message", "business_message"], (context, next) => { | ||
return this.on("message", (context, next) => { | ||
if ((typeof trigger === "string" && context.text === trigger) || | ||
@@ -404,0 +445,0 @@ // @ts-expect-error |
@@ -1,9 +0,10 @@ | ||
import type { APIMethodParams, APIMethods, TelegramAPIResponseError, TelegramResponseParameters } from "@gramio/types"; | ||
import type { APIMethods, TelegramAPIResponseError, TelegramResponseParameters } from "@gramio/types"; | ||
import type { MaybeSuppressedParams } from "./types"; | ||
export declare const ErrorKind: unique symbol; | ||
export declare class TelegramError<T extends keyof APIMethods> extends Error { | ||
method: T; | ||
params: APIMethodParams<T>; | ||
params: MaybeSuppressedParams<T>; | ||
code: number; | ||
payload?: TelegramResponseParameters; | ||
constructor(error: TelegramAPIResponseError, method: T, params: APIMethodParams<T>); | ||
constructor(error: TelegramAPIResponseError, method: T, params: MaybeSuppressedParams<T>); | ||
} |
@@ -1,2 +0,2 @@ | ||
import type { BotLike, Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
import type { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
import type { APIMethods } from "@gramio/types"; | ||
@@ -40,6 +40,6 @@ import type { DeriveDefinitions, ErrorDefinitions, Hooks } from "./types"; | ||
}>(kind: Name, error: NewError): Plugin<Errors & { [name in Name]: InstanceType<NewError>; }, Derives>; | ||
derive<Handler extends Hooks.Derive<Context<BotLike>>>(handler: Handler): Plugin<Errors, Derives & { | ||
derive<Handler extends Hooks.Derive<Context<Bot>>>(handler: Handler): Plugin<Errors, Derives & { | ||
global: Awaited<ReturnType<Handler>>; | ||
}>; | ||
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<BotLike, Update>>>(updateName: MaybeArray<Update>, handler: Handler): Plugin<Errors, Derives & { | ||
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<Bot, Update>>>(updateName: MaybeArray<Update>, handler: Handler): Plugin<Errors, Derives & { | ||
[K in Update]: Awaited<ReturnType<Handler>>; | ||
@@ -46,0 +46,0 @@ }>; |
@@ -1,4 +0,5 @@ | ||
import type { BotLike, Context, UpdateName } from "@gramio/contexts"; | ||
import type { Context, UpdateName } from "@gramio/contexts"; | ||
import type { APIMethodParams, APIMethodReturn, APIMethods, TelegramUser } from "@gramio/types"; | ||
import type { NextMiddleware } from "middleware-io"; | ||
import type { Bot } from "./bot"; | ||
import type { TelegramError } from "./errors"; | ||
@@ -12,3 +13,3 @@ export interface BotOptions { | ||
export type Handler<T> = (context: T, next: NextMiddleware) => unknown; | ||
interface ErrorHandlerParams<Ctx extends Context<BotLike>, Kind extends string, Err> { | ||
interface ErrorHandlerParams<Ctx extends Context<Bot>, Kind extends string, Err> { | ||
context: Ctx; | ||
@@ -24,5 +25,33 @@ kind: Kind; | ||
method: APIMethod; | ||
params: APIMethodParams<APIMethod>; | ||
params: MaybeSuppressedParams<APIMethod>; | ||
}; | ||
}[Methods]; | ||
export interface Suppress<IsSuppressed extends boolean | undefined = undefined> { | ||
/** | ||
* Pass `true` if you want to suppress throwing errors of this method. | ||
* | ||
* **But this does not undo getting into the `onResponseError` hook**. | ||
* | ||
* @example | ||
* ```ts | ||
* const response = await bot.api.sendMessage({ | ||
* suppress: true, | ||
* chat_id: "@not_found", | ||
* text: "Suppressed method" | ||
* }); | ||
* | ||
* if(response instanceof TelegramError) console.error("sendMessage returns an error...") | ||
* else console.log("Message has been sent successfully"); | ||
* ``` | ||
* | ||
* */ | ||
suppress?: IsSuppressed; | ||
} | ||
export type MaybeSuppressedParams<Method extends keyof APIMethods, IsSuppressed extends boolean | undefined = undefined> = APIMethodParams<Method> & Suppress<IsSuppressed>; | ||
export type SuppressedAPIMethodParams<Method extends keyof APIMethods> = undefined extends APIMethodParams<Method> ? Suppress<true> : MaybeSuppressedParams<Method, true>; | ||
type MaybeSuppressedReturn<Method extends keyof APIMethods, IsSuppressed extends boolean | undefined = undefined> = true extends IsSuppressed ? TelegramError<Method> | APIMethodReturn<Method> : APIMethodReturn<Method>; | ||
export type SuppressedAPIMethodReturn<Method extends keyof APIMethods> = MaybeSuppressedReturn<Method, true>; | ||
export type SuppressedAPIMethods<Methods extends keyof APIMethods = keyof APIMethods> = { | ||
[APIMethod in Methods]: APIMethodParams<APIMethod> extends undefined ? <IsSuppressed extends boolean | undefined = undefined>(params?: Suppress<IsSuppressed>) => Promise<MaybeSuppressedReturn<APIMethod, IsSuppressed>> : undefined extends APIMethodParams<APIMethod> ? <IsSuppressed extends boolean | undefined = undefined>(params?: MaybeSuppressedParams<APIMethod, IsSuppressed>) => Promise<MaybeSuppressedReturn<APIMethod, IsSuppressed>> : <IsSuppressed extends boolean | undefined = undefined>(params: MaybeSuppressedParams<APIMethod, IsSuppressed>) => Promise<MaybeSuppressedReturn<APIMethod, IsSuppressed>>; | ||
}; | ||
type AnyTelegramMethodWithReturn<Methods extends keyof APIMethods> = { | ||
@@ -40,6 +69,6 @@ [APIMethod in 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> | { | ||
type OnErrorContext<Ctx extends Context<Bot>, T extends ErrorDefinitions> = ErrorHandlerParams<Ctx, "TELEGRAM", AnyTelegramError> | ErrorHandlerParams<Ctx, "UNKNOWN", Error> | { | ||
[K in keyof T]: ErrorHandlerParams<Ctx, K & string, T[K & string]>; | ||
}[keyof T]; | ||
type OnError<T extends ErrorDefinitions, Ctx extends Context<BotLike> = Context<BotLike>> = (options: OnErrorContext<Ctx, T>) => unknown; | ||
type OnError<T extends ErrorDefinitions, Ctx extends Context<any> = Context<Bot>> = (options: OnErrorContext<Ctx, T>) => unknown; | ||
type OnStart = (context: { | ||
@@ -54,3 +83,3 @@ plugins: string[]; | ||
}) => unknown; | ||
type OnResponseError<Methods extends keyof APIMethods = keyof APIMethods> = (context: AnyTelegramError<Methods>, api: BotLike["api"]) => unknown; | ||
type OnResponseError<Methods extends keyof APIMethods = keyof APIMethods> = (context: AnyTelegramError<Methods>, api: Bot["api"]) => unknown; | ||
type OnResponse<Methods extends keyof APIMethods = keyof APIMethods> = (context: AnyTelegramMethodWithReturn<Methods>) => unknown; | ||
@@ -57,0 +86,0 @@ interface Store<T extends ErrorDefinitions> { |
@@ -36,2 +36,5 @@ "use strict"; | ||
throw new Error(updateType); | ||
const updatePayload = data[updateType]; | ||
if (!updatePayload) | ||
throw new Error(""); | ||
try { | ||
@@ -41,3 +44,4 @@ let context = new UpdateContext({ | ||
update: data, | ||
payload: data[updateType], | ||
// @ts-expect-error | ||
payload: updatePayload, | ||
type: updateType, | ||
@@ -47,11 +51,14 @@ updateId: data.update_id, | ||
if ("isEvent" in context && context.isEvent() && context.eventType) { | ||
// @ts-expect-error contextsMappings is any | ||
const payload = data.message ?? | ||
data.edited_message ?? | ||
data.channel_post ?? | ||
data.edited_channel_post ?? | ||
data.business_message; | ||
if (!payload) | ||
throw new Error("Unsupported event??"); | ||
context = new contexts_1.contextsMappings[context.eventType]({ | ||
bot: this.bot, | ||
update: data, | ||
payload: data.message ?? | ||
data.edited_message ?? | ||
data.channel_post ?? | ||
data.edited_channel_post ?? | ||
data.business_message, | ||
payload, | ||
// @ts-expect-error | ||
type: context.eventType, | ||
@@ -58,0 +65,0 @@ updateId: data.update_id, |
{ | ||
"name": "gramio", | ||
"version": "0.0.26", | ||
"version": "0.0.27", | ||
"description": "Powerful Telegram Bot API framework", | ||
@@ -28,11 +28,11 @@ "main": "./dist/index.js", | ||
"@biomejs/biome": "1.6.4", | ||
"@types/node": "^20.12.4", | ||
"typescript": "^5.4.3" | ||
"@types/node": "^20.12.5", | ||
"typescript": "^5.4.4" | ||
}, | ||
"dependencies": { | ||
"@gramio/callback-data": "^0.0.2", | ||
"@gramio/contexts": "^0.0.8", | ||
"@gramio/contexts": "^0.0.9", | ||
"@gramio/files": "^0.0.4", | ||
"@gramio/format": "^0.0.8", | ||
"@gramio/keyboards": "^0.2.3", | ||
"@gramio/keyboards": "^0.3.0", | ||
"@gramio/types": "^7.2.1", | ||
@@ -39,0 +39,0 @@ "inspectable": "^3.0.0", |
53779
1180
+ Added@gramio/contexts@0.0.9(transitive)
+ Added@gramio/keyboards@0.3.3(transitive)
- Removed@gramio/contexts@0.0.8(transitive)
- Removed@gramio/keyboards@0.2.3(transitive)
Updated@gramio/contexts@^0.0.9
Updated@gramio/keyboards@^0.3.0