Comparing version 0.0.20 to 0.0.21
@@ -1,7 +0,8 @@ | ||
import { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
import type { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types"; | ||
import type { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
import type { APIMethodParams, APIMethods, SetMyCommandsParams, TelegramBotCommand, TelegramUser } from "@gramio/types"; | ||
import { Plugin } from "./plugin"; | ||
import { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks, MaybePromise } from "./types"; | ||
import type { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks, MaybePromise } from "./types"; | ||
import { Updates } from "./updates"; | ||
export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> { | ||
/** @internal */ | ||
__Derives: Derives; | ||
@@ -78,4 +79,8 @@ readonly options: BotOptions; | ||
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; | ||
hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & { | ||
args: RegExpMatchArray | null; | ||
}) => unknown): this; | ||
command(command: string, handler: (context: ContextType<typeof this, "message"> & Derives["global"] & Derives["message"] & { | ||
args: string | null; | ||
}) => unknown, options?: Omit<SetMyCommandsParams, "commands"> & Omit<TelegramBotCommand, "command">): this; | ||
start({ webhook, dropPendingUpdates, allowedUpdates, }?: { | ||
@@ -82,0 +87,0 @@ webhook?: Omit<APIMethodParams<"setWebhook">, "drop_pending_updates" | "allowed_updates">; |
@@ -42,2 +42,3 @@ "use strict"; | ||
const undici_1 = require("undici"); | ||
const _plugin_1 = require("./plugin"); | ||
const errors_1 = require("./errors"); | ||
@@ -61,2 +62,3 @@ const updates_1 = require("./updates"); | ||
} | ||
/** @internal */ | ||
__Derives; | ||
@@ -83,4 +85,14 @@ options = {}; | ||
hooks = { | ||
preRequest: [ | ||
(context) => { | ||
preRequest: [], | ||
onError: [], | ||
onStart: [], | ||
}; | ||
constructor(token, options) { | ||
if (!token || typeof token !== "string") | ||
throw new Error(`Token is ${typeof token} but it should be a string!`); | ||
this.options = { ...options, token }; | ||
if (!(options?.plugins && | ||
"format" in options.plugins && | ||
!options.plugins.format)) | ||
this.extend(new _plugin_1.Plugin("@gramio/format").preRequest((context) => { | ||
if (!context.params) | ||
@@ -93,11 +105,3 @@ return context; | ||
return context; | ||
}, | ||
], | ||
onError: [], | ||
onStart: [], | ||
}; | ||
constructor(token, options) { | ||
if (!token || typeof token !== "string") | ||
throw new Error(`Token is ${typeof token} but it should be a string!`); | ||
this.options = { ...options, token }; | ||
})); | ||
} | ||
@@ -275,3 +279,3 @@ async runHooks(type, context) { | ||
hears(trigger, handler) { | ||
this.on("message", (context, next) => { | ||
return this.on("message", (context, next) => { | ||
if ((typeof trigger === "string" && context.text === trigger) || | ||
@@ -283,2 +287,5 @@ // @ts-expect-error | ||
trigger.test(context.text))) { | ||
//@ts-expect-error | ||
context.args = | ||
trigger instanceof RegExp ? context.text?.match(trigger) : null; | ||
// TODO: remove | ||
@@ -291,4 +298,7 @@ //@ts-expect-error | ||
} | ||
command(command, handler) { | ||
this.on("message", (context, next) => { | ||
command(command, handler, options) { | ||
if (command.startsWith("/")) | ||
throw new Error("Do not use / in command name"); | ||
return this.on("message", (context, next) => { | ||
// TODO: change to find | ||
if (context.entities?.some((entity) => { | ||
@@ -301,4 +311,7 @@ if (entity.type !== "bot_command" || entity.offset > 0) | ||
?.replace(`@${this.info.username}`, ""); | ||
return cmd && cmd === command; | ||
// @ts-expect-error | ||
context.args = context.text?.slice(entity.length).trim() || null; | ||
return cmd?.startsWith(command); | ||
})) | ||
// @ts-expect-error | ||
return handler(context); | ||
@@ -305,0 +318,0 @@ return next(); |
@@ -1,2 +0,2 @@ | ||
import { APIMethodParams, APIMethods, TelegramAPIResponseError, TelegramResponseParameters } from "@gramio/types"; | ||
import type { APIMethodParams, APIMethods, TelegramAPIResponseError, TelegramResponseParameters } from "@gramio/types"; | ||
export declare const ErrorKind: unique symbol; | ||
@@ -3,0 +3,0 @@ export declare class TelegramError<T extends keyof APIMethods> extends Error { |
@@ -1,7 +0,12 @@ | ||
import { BotLike, Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
import { DeriveDefinitions, ErrorDefinitions, Hooks } from "./types"; | ||
import type { BotLike, Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
import type { APIMethods } from "@gramio/types"; | ||
import type { DeriveDefinitions, ErrorDefinitions, Hooks } from "./types"; | ||
export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> { | ||
Errors: Errors; | ||
Derives: Derives; | ||
derives: [Hooks.Derive<any>, UpdateName | undefined][]; | ||
derives: [Hooks.Derive<any>, MaybeArray<UpdateName> | undefined][]; | ||
preRequests: [ | ||
Hooks.PreRequest<any>, | ||
MaybeArray<keyof APIMethods> | undefined | ||
][]; | ||
name: string; | ||
@@ -29,2 +34,4 @@ errorsDefinitions: Record<string, { | ||
}>; | ||
preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this; | ||
preRequest(handler: Hooks.PreRequest): this; | ||
} |
@@ -63,2 +63,3 @@ "use strict"; | ||
derives = []; | ||
preRequests = []; | ||
name; | ||
@@ -82,3 +83,5 @@ errorsDefinitions = {}; | ||
derive(updateNameOrHandler, handler) { | ||
if (typeof updateNameOrHandler === "string" && handler) | ||
if ((typeof updateNameOrHandler === "string" || | ||
Array.isArray(updateNameOrHandler)) && | ||
handler) | ||
this.derives.push([handler, updateNameOrHandler]); | ||
@@ -89,2 +92,11 @@ else if (typeof updateNameOrHandler === "function") | ||
} | ||
preRequest(methodsOrHandler, handler) { | ||
if ((typeof methodsOrHandler === "string" || | ||
Array.isArray(methodsOrHandler)) && | ||
handler) | ||
this.preRequests.push([handler, methodsOrHandler]); | ||
else if (typeof methodsOrHandler === "function") | ||
this.preRequests.push([methodsOrHandler, undefined]); | ||
return this; | ||
} | ||
}; | ||
@@ -91,0 +103,0 @@ return Plugin = _classThis; |
@@ -1,7 +0,10 @@ | ||
import { BotLike, Context, UpdateName } from "@gramio/contexts"; | ||
import { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types"; | ||
import { NextMiddleware } from "middleware-io"; | ||
import { TelegramError } from "./errors"; | ||
import type { BotLike, Context, UpdateName } from "@gramio/contexts"; | ||
import type { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types"; | ||
import type { NextMiddleware } from "middleware-io"; | ||
import type { TelegramError } from "./errors"; | ||
export interface BotOptions { | ||
token?: string; | ||
plugins?: { | ||
format?: boolean; | ||
}; | ||
} | ||
@@ -8,0 +11,0 @@ export type Handler<T> = (context: T, next: NextMiddleware) => unknown; |
@@ -1,6 +0,6 @@ | ||
import { Context, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
import { type Context, type MaybeArray, type UpdateName } from "@gramio/contexts"; | ||
import type { APIMethodParams, TelegramUpdate } from "@gramio/types"; | ||
import { CaughtMiddlewareHandler } from "middleware-io"; | ||
import { type CaughtMiddlewareHandler } from "middleware-io"; | ||
import type { Bot } from "./bot"; | ||
import { Handler } from "./types"; | ||
import type { Handler } from "./types"; | ||
export declare class Updates { | ||
@@ -7,0 +7,0 @@ private readonly bot; |
@@ -1,3 +0,3 @@ | ||
import { TelegramUpdate } from "@gramio/types"; | ||
import { MaybePromise } from "../types"; | ||
import type { TelegramUpdate } from "@gramio/types"; | ||
import type { MaybePromise } from "../types"; | ||
export interface FrameworkHandler { | ||
@@ -4,0 +4,0 @@ update: MaybePromise<TelegramUpdate>; |
@@ -16,8 +16,8 @@ "use strict"; | ||
update: c.req.json(), | ||
header: c.req.header(SECRET_TOKEN_HEADER) | ||
header: c.req.header(SECRET_TOKEN_HEADER), | ||
}), | ||
express: (req) => ({ | ||
update: req.body, | ||
header: req.header(SECRET_TOKEN_HEADER) | ||
}) | ||
header: req.header(SECRET_TOKEN_HEADER), | ||
}), | ||
}; |
@@ -1,3 +0,3 @@ | ||
import { Bot } from "../bot"; | ||
import type { Bot } from "../bot"; | ||
import { frameworks } from "./adapters"; | ||
export declare function webhookHandler(bot: Bot, framework: keyof typeof frameworks): (...args: any[]) => Promise<void>; |
{ | ||
"name": "gramio", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"description": "Powerful Telegram Bot API framework", | ||
@@ -27,4 +27,4 @@ "main": "./dist/index.js", | ||
"devDependencies": { | ||
"@biomejs/biome": "1.5.3", | ||
"@gramio/types": "^7.1.6", | ||
"@biomejs/biome": "1.6.0", | ||
"@gramio/types": "^7.1.7", | ||
"@types/node": "^20.11.25", | ||
@@ -31,0 +31,0 @@ "typescript": "^5.4.2" |
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
38579
869