Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unify webhookCallback overloads #604

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions src/convenience/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export interface WebhookOptions {
secretToken?: string;
}

type Adapters = typeof adapters;
type AdapterNames = keyof Adapters;
type ResolveName<A extends FrameworkAdapter | AdapterNames> = 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:
Expand All @@ -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<C extends Context = Context>(
bot: Bot<C>,
adapter?: FrameworkAdapter,
onTimeout?: WebhookOptions["onTimeout"],
timeoutMilliseconds?: WebhookOptions["timeoutMilliseconds"],
secretToken?: WebhookOptions["secretToken"],
): (...args: any[]) => any;
export function webhookCallback<C extends Context = Context>(
export function webhookCallback<
C extends Context = Context,
A extends FrameworkAdapter | AdapterNames = FrameworkAdapter | AdapterNames,
>(
bot: Bot<C>,
adapter?: FrameworkAdapter,
adapter: A,
webhookOptions?: WebhookOptions,
): (...args: any[]) => any;
): (
...args: Parameters<ResolveName<A>>
) => ReturnType<ResolveName<A>>["handlerReturn"] extends undefined
? Promise<void>
: NonNullable<ReturnType<ResolveName<A>>["handlerReturn"]>;
export function webhookCallback<
C extends Context = Context,
A extends keyof typeof nativeAdapters = keyof typeof nativeAdapters,
A extends FrameworkAdapter | AdapterNames = FrameworkAdapter | AdapterNames,
>(
bot: Bot<C>,
adapter: A,
webhookOptions?: WebhookOptions,
onTimeout?: WebhookOptions["onTimeout"],
timeoutMilliseconds?: WebhookOptions["timeoutMilliseconds"],
secretToken?: WebhookOptions["secretToken"],
): (
...args: Parameters<typeof nativeAdapters[A]>
) => ReturnType<typeof nativeAdapters[A]>["handlerReturn"] extends undefined
...args: Parameters<ResolveName<A>>
) => ReturnType<ResolveName<A>>["handlerReturn"] extends undefined
? Promise<void>
: NonNullable<ReturnType<typeof nativeAdapters[A]>["handlerReturn"]>;
: NonNullable<ReturnType<ResolveName<A>>["handlerReturn"]>;
export function webhookCallback<C extends Context = Context>(
bot: Bot<C>,
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 {
Expand Down
Loading