Comparing version 0.0.24 to 0.0.25
@@ -75,4 +75,9 @@ import { CallbackData } from "@gramio/callback-data"; | ||
onStart(handler: Hooks.OnStart): this; | ||
onStop(handler: Hooks.OnStop): this; | ||
preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this; | ||
preRequest(handler: Hooks.PreRequest): this; | ||
onResponse<Methods extends keyof APIMethods, Handler extends Hooks.OnResponse<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this; | ||
onResponse(handler: Hooks.OnResponse): this; | ||
onResponseError<Methods extends keyof APIMethods, Handler extends Hooks.OnResponseError<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this; | ||
onResponseError(handler: Hooks.OnResponseError): this; | ||
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this; | ||
@@ -79,0 +84,0 @@ use(handler: Handler<Context<typeof this> & Derives["global"]>): this; |
@@ -85,2 +85,4 @@ "use strict"; | ||
preRequest: [], | ||
onResponse: [], | ||
onResponseError: [], | ||
onError: [], | ||
@@ -114,7 +116,7 @@ onStart: [], | ||
} | ||
async runImmutableHooks(type, context) { | ||
async runImmutableHooks(type, ...context) { | ||
for await (const hook of this.hooks[type]) { | ||
//TODO: solve that later | ||
//@ts-expect-error | ||
await hook(context); | ||
await hook(...context); | ||
} | ||
@@ -148,4 +150,16 @@ } | ||
const data = (await response.body.json()); | ||
if (!data.ok) | ||
throw new errors_1.TelegramError(data, method, params); | ||
if (!data.ok) { | ||
const err = new errors_1.TelegramError(data, method, params); | ||
// @ts-expect-error | ||
this.runImmutableHooks("onResponseError", err, this.api); | ||
throw err; | ||
} | ||
this.runImmutableHooks("onResponse", | ||
// TODO: fix type error | ||
// @ts-expect-error | ||
{ | ||
method, | ||
params, | ||
response: data.result, | ||
}); | ||
return data.result; | ||
@@ -225,2 +239,6 @@ } | ||
} | ||
onStop(handler) { | ||
this.hooks.onStop.push(handler); | ||
return this; | ||
} | ||
preRequest(methodsOrHandler, handler) { | ||
@@ -249,2 +267,44 @@ if (typeof methodsOrHandler === "string" || | ||
} | ||
onResponse(methodsOrHandler, handler) { | ||
if (typeof methodsOrHandler === "string" || | ||
Array.isArray(methodsOrHandler)) { | ||
// TODO: error | ||
if (!handler) | ||
throw new Error("TODO:"); | ||
const methods = typeof methodsOrHandler === "string" | ||
? [methodsOrHandler] | ||
: methodsOrHandler; | ||
this.hooks.onResponse.push(async (context) => { | ||
// TODO: remove ts-ignore | ||
// @ts-expect-error | ||
if (methods.includes(context.method)) | ||
return handler(context); | ||
return context; | ||
}); | ||
} | ||
else | ||
this.hooks.onResponse.push(methodsOrHandler); | ||
return this; | ||
} | ||
onResponseError(methodsOrHandler, handler) { | ||
if (typeof methodsOrHandler === "string" || | ||
Array.isArray(methodsOrHandler)) { | ||
// TODO: error | ||
if (!handler) | ||
throw new Error("TODO:"); | ||
const methods = typeof methodsOrHandler === "string" | ||
? [methodsOrHandler] | ||
: methodsOrHandler; | ||
this.hooks.onResponseError.push(async (context) => { | ||
// TODO: remove ts-ignore | ||
// @ts-expect-error | ||
if (methods.includes(context.method)) | ||
return handler(context); | ||
return context; | ||
}); | ||
} | ||
else | ||
this.hooks.onResponseError.push(methodsOrHandler); | ||
return this; | ||
} | ||
on(updateName, handler) { | ||
@@ -285,2 +345,16 @@ this.updates.on(updateName, handler); | ||
} | ||
for (const value of plugin.onResponses) { | ||
const [onResponse, updateName] = value; | ||
if (!updateName) | ||
this.onResponse(onResponse); | ||
else | ||
this.onResponse(updateName, onResponse); | ||
} | ||
for (const value of plugin.onResponseErrors) { | ||
const [onResponseError, updateName] = value; | ||
if (!updateName) | ||
this.onResponseError(onResponseError); | ||
else | ||
this.onResponseError(updateName, onResponseError); | ||
} | ||
for (const handler of plugin.groups) { | ||
@@ -287,0 +361,0 @@ this.group(handler); |
@@ -13,2 +13,10 @@ import type { BotLike, Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts"; | ||
][]; | ||
onResponses: [ | ||
Hooks.OnResponse<any>, | ||
MaybeArray<keyof APIMethods> | undefined | ||
][]; | ||
onResponseErrors: [ | ||
Hooks.OnResponseError<any>, | ||
MaybeArray<keyof APIMethods> | undefined | ||
][]; | ||
groups: ((bot: Bot<any, any>) => Bot<any, any>)[]; | ||
@@ -41,2 +49,6 @@ name: string; | ||
preRequest(handler: Hooks.PreRequest): this; | ||
onResponse<Methods extends keyof APIMethods, Handler extends Hooks.OnResponse<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this; | ||
onResponse(handler: Hooks.OnResponse): this; | ||
onResponseError<Methods extends keyof APIMethods, Handler extends Hooks.OnResponseError<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this; | ||
onResponseError(handler: Hooks.OnResponseError): this; | ||
} |
@@ -64,2 +64,4 @@ "use strict"; | ||
preRequests = []; | ||
onResponses = []; | ||
onResponseErrors = []; | ||
groups = []; | ||
@@ -106,2 +108,20 @@ name; | ||
} | ||
onResponse(methodsOrHandler, handler) { | ||
if ((typeof methodsOrHandler === "string" || | ||
Array.isArray(methodsOrHandler)) && | ||
handler) | ||
this.onResponses.push([handler, methodsOrHandler]); | ||
else if (typeof methodsOrHandler === "function") | ||
this.onResponses.push([methodsOrHandler, undefined]); | ||
return this; | ||
} | ||
onResponseError(methodsOrHandler, handler) { | ||
if ((typeof methodsOrHandler === "string" || | ||
Array.isArray(methodsOrHandler)) && | ||
handler) | ||
this.onResponseErrors.push([handler, methodsOrHandler]); | ||
else if (typeof methodsOrHandler === "function") | ||
this.onResponseErrors.push([methodsOrHandler, undefined]); | ||
return this; | ||
} | ||
}; | ||
@@ -108,0 +128,0 @@ return Plugin = _classThis; |
import type { BotLike, Context, UpdateName } from "@gramio/contexts"; | ||
import type { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types"; | ||
import type { APIMethodParams, APIMethodReturn, APIMethods, TelegramUser } from "@gramio/types"; | ||
import type { NextMiddleware } from "middleware-io"; | ||
@@ -17,5 +17,5 @@ import type { TelegramError } from "./errors"; | ||
} | ||
type AnyTelegramError = { | ||
[APIMethod in keyof APIMethods]: TelegramError<APIMethod>; | ||
}[keyof APIMethods]; | ||
type AnyTelegramError<Methods extends keyof APIMethods = keyof APIMethods> = { | ||
[APIMethod in Methods]: TelegramError<APIMethod>; | ||
}[Methods]; | ||
type AnyTelegramMethod<Methods extends keyof APIMethods> = { | ||
@@ -27,2 +27,9 @@ [APIMethod in Methods]: { | ||
}[Methods]; | ||
type AnyTelegramMethodWithReturn<Methods extends keyof APIMethods> = { | ||
[APIMethod in Methods]: { | ||
method: APIMethod; | ||
params: APIMethodParams<APIMethod>; | ||
response: APIMethodReturn<APIMethod>; | ||
}; | ||
}[Methods]; | ||
export type MaybePromise<T> = Promise<T> | T; | ||
@@ -46,4 +53,8 @@ export declare namespace Hooks { | ||
}) => unknown; | ||
type OnResponseError<Methods extends keyof APIMethods = keyof APIMethods> = (context: AnyTelegramError<Methods>, api: BotLike["api"]) => unknown; | ||
type OnResponse<Methods extends keyof APIMethods = keyof APIMethods> = (context: AnyTelegramMethodWithReturn<Methods>) => unknown; | ||
interface Store<T extends ErrorDefinitions> { | ||
preRequest: PreRequest[]; | ||
onResponse: OnResponse[]; | ||
onResponseError: OnResponseError[]; | ||
onError: OnError<T>[]; | ||
@@ -50,0 +61,0 @@ onStart: OnStart[]; |
{ | ||
"name": "gramio", | ||
"version": "0.0.24", | ||
"version": "0.0.25", | ||
"description": "Powerful Telegram Bot API framework", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
48764
1090