Comparing version 0.0.13 to 0.0.14
@@ -5,5 +5,5 @@ import { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
import { Plugin } from "#plugin"; | ||
import { BotOptions, ErrorDefinitions, Handler, Hooks } from "./types"; | ||
import { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks } from "./types"; | ||
import { Updates } from "./updates"; | ||
export declare class Bot<Errors extends ErrorDefinitions = {}, Derives = {}> { | ||
export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> { | ||
readonly options: BotOptions; | ||
@@ -52,3 +52,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>; }, Derives>; | ||
/** | ||
@@ -63,8 +63,13 @@ * Set error handler. | ||
*/ | ||
onError<T extends UpdateName>(updateName: MaybeArray<T>, handler: Hooks.OnError<Errors, ContextType<typeof this, T>>): this; | ||
onError(handler: Hooks.OnError<Errors>): this; | ||
derive<Handler extends (context: Context<typeof this>) => object>(handler: Handler): Bot<Errors, Derives & ReturnType<Handler>>; | ||
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives>): this; | ||
use(handler: Handler<Context<typeof this> & Derives>): this; | ||
onError<T extends UpdateName>(updateName: MaybeArray<T>, handler: Hooks.OnError<Errors, ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this; | ||
onError(handler: Hooks.OnError<Errors, Context<typeof this> & Derives["global"]>): this; | ||
derive<Handler extends Hooks.Derive<Context<typeof this>>>(handler: Handler): Bot<Errors, Derives & { | ||
global: ReturnType<Handler>; | ||
}>; | ||
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<typeof this, Update>>>(updateName: Update, handler: Handler): Bot<Errors, Derives & { | ||
[K in Update]: ReturnType<Handler>; | ||
}>; | ||
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"]>; | ||
} |
@@ -148,9 +148,17 @@ "use strict"; | ||
} | ||
derive(handler) { | ||
this.updates.use((context, next) => { | ||
for (const [key, value] of Object.entries(handler(context))) { | ||
context[key] = value; | ||
} | ||
next(); | ||
}); | ||
derive(updateNameOrHandler, handler) { | ||
if (typeof updateNameOrHandler === "function") | ||
this.updates.use((context, next) => { | ||
for (const [key, value] of Object.entries(updateNameOrHandler(context))) { | ||
context[key] = value; | ||
} | ||
next(); | ||
}); | ||
else if (handler) | ||
this.updates.on(updateNameOrHandler, (context, next) => { | ||
for (const [key, value] of Object.entries(handler(context))) { | ||
context[key] = value; | ||
} | ||
next(); | ||
}); | ||
return this; | ||
@@ -171,4 +179,8 @@ } | ||
} | ||
for (const derive of plugin.derives) { | ||
this.derive(derive); | ||
for (const value of plugin.derives) { | ||
const [derive, updateName] = value; | ||
if (!updateName) | ||
this.derive(derive); | ||
else | ||
this.derive(updateName, derive); | ||
} | ||
@@ -175,0 +187,0 @@ return this; |
@@ -1,7 +0,7 @@ | ||
import { BotLike, Context } from "@gramio/contexts"; | ||
import { ErrorDefinitions } from "types"; | ||
export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives = {}> { | ||
import { BotLike, Context, ContextType, UpdateName } from "@gramio/contexts"; | ||
import { DeriveDefinitions, ErrorDefinitions, Hooks } from "types"; | ||
export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> { | ||
Errors: Errors; | ||
Derives: Derives; | ||
derives: ((context: Context<BotLike>) => object)[]; | ||
derives: [Hooks.Derive<any>, UpdateName | undefined][]; | ||
name: string; | ||
@@ -19,4 +19,9 @@ errorsDefinitions: Record<string, { | ||
prototype: Error; | ||
}>(kind: Name, error: NewError): Plugin<Errors & { [name in Name]: InstanceType<NewError>; }, {}>; | ||
derive<Handler extends (context: Context<BotLike>) => object>(handler: Handler): Plugin<Errors, Derives & ReturnType<Handler>>; | ||
}>(kind: Name, error: NewError): Plugin<Errors & { [name in Name]: InstanceType<NewError>; }, DeriveDefinitions>; | ||
derive<Handler extends Hooks.Derive<Context<BotLike>>>(handler: Handler): Plugin<Errors, Derives & { | ||
global: ReturnType<Handler>; | ||
}>; | ||
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<BotLike, Update>>>(updateName: Update, handler: Handler): Plugin<Errors, Derives & { | ||
[K in Update]: ReturnType<Handler>; | ||
}>; | ||
} |
@@ -24,4 +24,7 @@ "use strict"; | ||
} | ||
derive(handler) { | ||
this.derives.push(handler); | ||
derive(updateNameOrHandler, handler) { | ||
if (typeof updateNameOrHandler === "string" && handler) | ||
this.derives.push([handler, updateNameOrHandler]); | ||
else if (typeof updateNameOrHandler === "function") | ||
this.derives.push([updateNameOrHandler, undefined]); | ||
return this; | ||
@@ -28,0 +31,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { BotLike, Context } from "@gramio/contexts"; | ||
import { BotLike, Context, UpdateName } from "@gramio/contexts"; | ||
import { APIMethodParams, APIMethods } from "@gramio/types"; | ||
@@ -25,2 +25,3 @@ import { NextMiddleware } from "middleware-io"; | ||
export declare namespace Hooks { | ||
type Derive<Ctx> = (context: Ctx) => object; | ||
type PreRequestContext = AnyTelegramMethod; | ||
@@ -38,2 +39,3 @@ type PreRequest = (ctx: PreRequestContext) => MaybePromise<PreRequestContext>; | ||
export type ErrorDefinitions = Record<string, Error>; | ||
export type DeriveDefinitions = Record<UpdateName | "global", {}>; | ||
export {}; |
{ | ||
"name": "gramio", | ||
"version": "0.0.13", | ||
"description": "WIP", | ||
"version": "0.0.14", | ||
"description": "Powerful Telegram Bot API framework", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"keywords": [ | ||
"telegram", | ||
"telegram-bot", | ||
"telegram-bot-api", | ||
"bot", | ||
"framework", | ||
"types", | ||
"client", | ||
"webhook", | ||
"long-polling" | ||
], | ||
"scripts": { | ||
@@ -8,0 +19,0 @@ "type": "tsc --noEmit", |
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
22198
539