From 138e7fc49b549bd5af72826a3be83acf7f3ec201 Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Sat, 22 Jun 2024 00:31:28 +0200 Subject: [PATCH] fix: unify webhookCallback overloads --- src/convenience/webhook.ts | 48 +++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/src/convenience/webhook.ts b/src/convenience/webhook.ts index aa275547..66ece61a 100644 --- a/src/convenience/webhook.ts +++ b/src/convenience/webhook.ts @@ -32,6 +32,11 @@ export interface WebhookOptions { secretToken?: string; } +type Adapters = typeof adapters; +type AdapterNames = keyof Adapters; +type ResolveName = A extends + AdapterNames ? Adapters[A] : A; + /** * Creates a callback function that you can pass to a web framework (such as * express) if you want to run your bot via webhooks. Use it like this: @@ -48,40 +53,41 @@ export interface WebhookOptions { * * @param bot The bot for which to create a callback * @param adapter An optional string identifying the framework (default: 'express') - * @param onTimeout An optional strategy to handle timeouts (default: 'throw') - * @param timeoutMilliseconds An optional number of timeout milliseconds (default: 10_000) + * @param webhookOptions Further options for the webhook setup */ -export function webhookCallback( - bot: Bot, - adapter?: FrameworkAdapter, - onTimeout?: WebhookOptions["onTimeout"], - timeoutMilliseconds?: WebhookOptions["timeoutMilliseconds"], - secretToken?: WebhookOptions["secretToken"], -): (...args: any[]) => any; -export function webhookCallback( +export function webhookCallback< + C extends Context = Context, + A extends FrameworkAdapter | AdapterNames = FrameworkAdapter | AdapterNames, +>( bot: Bot, - adapter?: FrameworkAdapter, + adapter: A, webhookOptions?: WebhookOptions, -): (...args: any[]) => any; +): ( + ...args: Parameters> +) => ReturnType>["handlerReturn"] extends undefined + ? Promise + : NonNullable>["handlerReturn"]>; export function webhookCallback< C extends Context = Context, - A extends keyof typeof nativeAdapters = keyof typeof nativeAdapters, + A extends FrameworkAdapter | AdapterNames = FrameworkAdapter | AdapterNames, >( bot: Bot, adapter: A, - webhookOptions?: WebhookOptions, + onTimeout?: WebhookOptions["onTimeout"], + timeoutMilliseconds?: WebhookOptions["timeoutMilliseconds"], + secretToken?: WebhookOptions["secretToken"], ): ( - ...args: Parameters -) => ReturnType["handlerReturn"] extends undefined + ...args: Parameters> +) => ReturnType>["handlerReturn"] extends undefined ? Promise - : NonNullable["handlerReturn"]>; + : NonNullable>["handlerReturn"]>; export function webhookCallback( bot: Bot, - adapter: keyof typeof nativeAdapters | FrameworkAdapter = defaultAdapter, - onTimeout: + adapter: FrameworkAdapter | AdapterNames = defaultAdapter, + onTimeout?: | WebhookOptions - | WebhookOptions["onTimeout"] = "throw", - timeoutMilliseconds: WebhookOptions["timeoutMilliseconds"] = 10_000, + | WebhookOptions["onTimeout"], + timeoutMilliseconds?: WebhookOptions["timeoutMilliseconds"], secretToken?: WebhookOptions["secretToken"], ) { const {