Comparing version 1.23.0 to 1.23.1
@@ -287,15 +287,20 @@ "use strict"; | ||
} | ||
else { | ||
this.pollingRunning = true; | ||
this.pollingAbortController = new shim_node_js_1.AbortController(); | ||
this.pollingRunning = true; | ||
this.pollingAbortController = new shim_node_js_1.AbortController(); | ||
try { | ||
setup.push(withRetries(async () => { | ||
var _a; | ||
await this.api.deleteWebhook({ | ||
drop_pending_updates: options === null || options === void 0 ? void 0 : options.drop_pending_updates, | ||
}, (_a = this.pollingAbortController) === null || _a === void 0 ? void 0 : _a.signal); | ||
}, (_b = this.pollingAbortController) === null || _b === void 0 ? void 0 : _b.signal)); | ||
await Promise.all(setup); | ||
// All async ops of setup complete, run callback | ||
await ((_c = options === null || options === void 0 ? void 0 : options.onStart) === null || _c === void 0 ? void 0 : _c.call(options, this.botInfo)); | ||
} | ||
setup.push(withRetries(async () => { | ||
var _a; | ||
await this.api.deleteWebhook({ | ||
drop_pending_updates: options === null || options === void 0 ? void 0 : options.drop_pending_updates, | ||
}, (_a = this.pollingAbortController) === null || _a === void 0 ? void 0 : _a.signal); | ||
}, (_b = this.pollingAbortController) === null || _b === void 0 ? void 0 : _b.signal)); | ||
await Promise.all(setup); | ||
// All async ops of setup complete, run callback | ||
await ((_c = options === null || options === void 0 ? void 0 : options.onStart) === null || _c === void 0 ? void 0 : _c.call(options, this.botInfo)); | ||
catch (err) { | ||
this.pollingRunning = false; | ||
this.pollingAbortController = undefined; | ||
throw err; | ||
} | ||
// Bot was stopped during `onStart` | ||
@@ -302,0 +307,0 @@ if (!this.pollingRunning) |
@@ -27,3 +27,4 @@ import { DEFAULT_UPDATE_TYPES } from "../bot.js"; | ||
* List of update types a bot receives by default. Useful if you want to | ||
* receive all update types but `chat_member`. | ||
* receive all update types but `chat_member`, `message_reaction`, and | ||
* `message_reaction_count`. | ||
* | ||
@@ -48,4 +49,5 @@ * ```ts | ||
* | ||
* The main use case for this is when you want to receive `chat_member` | ||
* updates, as they need to be enabled first. Use it like so: | ||
* The main use case for this is when you want to receive `chat_member`, | ||
* `message_reaction`, and `message_reaction_count` updates, as they need to | ||
* be enabled first. Use it like so: | ||
* | ||
@@ -52,0 +54,0 @@ * ```ts |
@@ -0,12 +1,8 @@ | ||
/// <reference types="node" /> | ||
import { type Update } from "../types.js"; | ||
/** | ||
* HTTP Web frameworks for which grammY provides compatible callback out of the | ||
* box. | ||
*/ | ||
export type SupportedFrameworks = keyof typeof adapters; | ||
/** | ||
* Abstraction over a request-response cycle, providing access to the update, as | ||
* well as a mechanism for responding to the request and to end it. | ||
*/ | ||
export interface ReqResHandler { | ||
export interface ReqResHandler<T = void> { | ||
/** | ||
@@ -41,3 +37,3 @@ * The update object sent from Telegram, usually resolves the request's JSON | ||
*/ | ||
handlerReturn?: any; | ||
handlerReturn?: Promise<T>; | ||
} | ||
@@ -48,22 +44,156 @@ /** | ||
*/ | ||
export type FrameworkAdapter = (...args: any[]) => ReqResHandler; | ||
export type FrameworkAdapter = (...args: any[]) => ReqResHandler<any>; | ||
export type LambdaAdapter = (event: { | ||
body?: string; | ||
headers: Record<string, string | undefined>; | ||
}, _context: unknown, callback: (arg0: unknown, arg1: Record<string, unknown>) => Promise<unknown>) => ReqResHandler; | ||
export type LambdaAsyncAdapter = (event: { | ||
body?: string; | ||
headers: Record<string, string | undefined>; | ||
}, _context: unknown) => ReqResHandler; | ||
export type AzureAdapter = (request: { | ||
body?: unknown; | ||
}, context: { | ||
res?: { | ||
status: number; | ||
body: string; | ||
headers?: Record<string, string>; | ||
set?: (key: string, value: string) => void; | ||
send?: { | ||
(body: unknown): void; | ||
(status: number, body: unknown): void; | ||
}; | ||
}; | ||
}) => ReqResHandler; | ||
export type CloudflareAdapter = (event: { | ||
request: Request; | ||
respondWith: (response: Promise<Response>) => void; | ||
}) => ReqResHandler; | ||
export type CloudflareModuleAdapter = (request: Request) => ReqResHandler<Response>; | ||
export type ExpressAdapter = (req: { | ||
body: Update; | ||
header: (header: string) => string | undefined; | ||
}, res: { | ||
end: (cb?: () => void) => typeof res; | ||
set: (field: string, value?: string | string[]) => typeof res; | ||
send: (json: string) => typeof res; | ||
status: (code: number) => typeof res; | ||
}) => ReqResHandler; | ||
export type FastifyAdapter = (request: { | ||
body: unknown; | ||
headers: any; | ||
}, reply: { | ||
status: (code: number) => typeof reply; | ||
headers: (headers: Record<string, string>) => typeof reply; | ||
code: (code: number) => typeof reply; | ||
send: { | ||
(): typeof reply; | ||
(json: string): typeof reply; | ||
}; | ||
}) => ReqResHandler; | ||
export type HonoAdapter = (c: { | ||
req: { | ||
json: <T>() => Promise<T>; | ||
header: (header: string) => string | undefined; | ||
}; | ||
body: (data: string | ArrayBuffer | ReadableStream | null, arg?: any, headers?: Record<string, string | string[]>) => Response; | ||
status: (status: any) => void; | ||
json: (json: string) => Response; | ||
}) => ReqResHandler<Response>; | ||
export type HttpAdapter = (req: { | ||
headers: NodeJS.Dict<string | string[]>; | ||
on: (event: string, listener: (chunk: unknown) => void) => typeof req; | ||
once: (event: string, listener: () => void) => typeof req; | ||
}, res: { | ||
writeHead: { | ||
(status: number): typeof res; | ||
(status: number, headers: Record<string, string>): typeof res; | ||
}; | ||
end: (json?: string) => void; | ||
}) => ReqResHandler; | ||
export type KoaAdapter = (ctx: { | ||
get: (header: string) => string | undefined; | ||
set: (key: string, value: string) => void; | ||
status: number; | ||
body: string; | ||
request: { | ||
body?: unknown; | ||
}; | ||
response: { | ||
body: unknown; | ||
status: number; | ||
}; | ||
}) => ReqResHandler; | ||
export type NextAdapter = (req: { | ||
body: Update; | ||
headers: NodeJS.Dict<string | string[]>; | ||
}, res: { | ||
end: (cb?: () => void) => typeof res; | ||
status: (code: number) => typeof res; | ||
json: (json: string) => any; | ||
send: (json: string) => any; | ||
}) => ReqResHandler; | ||
export type NHttpAdapter = (rev: { | ||
body: unknown; | ||
headers: { | ||
get: (header: string) => string | null; | ||
}; | ||
response: { | ||
sendStatus: (status: number) => void; | ||
status: (status: number) => { | ||
send: (json: string) => void; | ||
}; | ||
}; | ||
}) => ReqResHandler; | ||
export type OakAdapter = (ctx: { | ||
request: { | ||
body: { | ||
json: () => Promise<Update>; | ||
}; | ||
headers: { | ||
get: (header: string) => string | null; | ||
}; | ||
}; | ||
response: { | ||
status: number; | ||
type: string | undefined; | ||
body: unknown; | ||
}; | ||
}) => ReqResHandler; | ||
export type ServeHttpAdapter = (requestEvent: { | ||
request: Request; | ||
respondWith: (response: Response) => void; | ||
}) => ReqResHandler; | ||
export type StdHttpAdapter = (req: Request) => ReqResHandler<Response>; | ||
export type SveltekitAdapter = ({ request }: { | ||
request: Request; | ||
}) => ReqResHandler<unknown>; | ||
export type WorktopAdapter = (req: { | ||
json: () => Promise<Update>; | ||
headers: { | ||
get: (header: string) => string | null; | ||
}; | ||
}, res: { | ||
end: (data: BodyInit | null) => void; | ||
send: (status: number, json: string) => void; | ||
}) => ReqResHandler; | ||
export declare const adapters: { | ||
"aws-lambda": FrameworkAdapter; | ||
"aws-lambda-async": FrameworkAdapter; | ||
azure: FrameworkAdapter; | ||
cloudflare: FrameworkAdapter; | ||
"cloudflare-mod": FrameworkAdapter; | ||
express: FrameworkAdapter; | ||
fastify: FrameworkAdapter; | ||
hono: FrameworkAdapter; | ||
http: FrameworkAdapter; | ||
https: FrameworkAdapter; | ||
koa: FrameworkAdapter; | ||
"next-js": FrameworkAdapter; | ||
nhttp: FrameworkAdapter; | ||
oak: FrameworkAdapter; | ||
serveHttp: FrameworkAdapter; | ||
"std/http": FrameworkAdapter; | ||
sveltekit: FrameworkAdapter; | ||
worktop: FrameworkAdapter; | ||
"aws-lambda": LambdaAdapter; | ||
"aws-lambda-async": LambdaAsyncAdapter; | ||
azure: AzureAdapter; | ||
cloudflare: CloudflareAdapter; | ||
"cloudflare-mod": CloudflareModuleAdapter; | ||
express: ExpressAdapter; | ||
fastify: FastifyAdapter; | ||
hono: HonoAdapter; | ||
http: HttpAdapter; | ||
https: HttpAdapter; | ||
koa: KoaAdapter; | ||
"next-js": NextAdapter; | ||
nhttp: NHttpAdapter; | ||
oak: OakAdapter; | ||
serveHttp: ServeHttpAdapter; | ||
"std/http": StdHttpAdapter; | ||
sveltekit: SveltekitAdapter; | ||
worktop: WorktopAdapter; | ||
}; |
@@ -17,18 +17,23 @@ "use strict"; | ||
/** AWS lambda serverless functions */ | ||
const awsLambda = (event, _context, callback) => ({ | ||
update: JSON.parse(event.body), | ||
header: event.headers[SECRET_HEADER], | ||
end: () => callback(null, { statusCode: 200 }), | ||
respond: (json) => callback(null, { | ||
statusCode: 200, | ||
headers: { "Content-Type": "application/json" }, | ||
body: json, | ||
}), | ||
unauthorized: () => callback(null, { statusCode: 401 }), | ||
}); | ||
const awsLambda = (event, _context, callback) => { | ||
var _a; | ||
return ({ | ||
update: JSON.parse((_a = event.body) !== null && _a !== void 0 ? _a : "{}"), | ||
header: event.headers[SECRET_HEADER], | ||
end: () => callback(null, { statusCode: 200 }), | ||
respond: (json) => callback(null, { | ||
statusCode: 200, | ||
headers: { "Content-Type": "application/json" }, | ||
body: json, | ||
}), | ||
unauthorized: () => callback(null, { statusCode: 401 }), | ||
}); | ||
}; | ||
/** AWS lambda async/await serverless functions */ | ||
const awsLambdaAsync = (event, _context) => { | ||
var _a; | ||
// deno-lint-ignore no-explicit-any | ||
let resolveResponse; | ||
return { | ||
update: JSON.parse(event.body), | ||
update: JSON.parse((_a = event.body) !== null && _a !== void 0 ? _a : "{}"), | ||
header: event.headers[SECRET_HEADER], | ||
@@ -48,17 +53,22 @@ end: () => resolveResponse({ statusCode: 200 }), | ||
/** Azure Functions */ | ||
const azure = (context, req) => ({ | ||
update: Promise.resolve(req.body), | ||
header: context.res.headers[SECRET_HEADER], | ||
end: () => (context.res = { | ||
status: 200, | ||
body: "", | ||
}), | ||
respond: (json) => { | ||
context.res.set("Content-Type", "application/json"); | ||
context.res.send(json); | ||
}, | ||
unauthorized: () => { | ||
context.res.send(401, WRONG_TOKEN_ERROR); | ||
}, | ||
}); | ||
const azure = (request, context) => { | ||
var _a, _b; | ||
return ({ | ||
update: Promise.resolve(request.body), | ||
header: (_b = (_a = context.res) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b[SECRET_HEADER], | ||
end: () => (context.res = { | ||
status: 200, | ||
body: "", | ||
}), | ||
respond: (json) => { | ||
var _a, _b, _c, _d; | ||
(_b = (_a = context.res) === null || _a === void 0 ? void 0 : _a.set) === null || _b === void 0 ? void 0 : _b.call(_a, "Content-Type", "application/json"); | ||
(_d = (_c = context.res) === null || _c === void 0 ? void 0 : _c.send) === null || _d === void 0 ? void 0 : _d.call(_c, json); | ||
}, | ||
unauthorized: () => { | ||
var _a, _b; | ||
(_b = (_a = context.res) === null || _a === void 0 ? void 0 : _a.send) === null || _b === void 0 ? void 0 : _b.call(_a, 401, WRONG_TOKEN_ERROR); | ||
}, | ||
}); | ||
}; | ||
/** Native CloudFlare workers (service worker) */ | ||
@@ -118,5 +128,5 @@ const cloudflare = (event) => { | ||
/** fastify web framework */ | ||
const fastify = (req, reply) => ({ | ||
update: Promise.resolve(req.body), | ||
header: req.headers[SECRET_HEADER_LOWERCASE], | ||
const fastify = (request, reply) => ({ | ||
update: Promise.resolve(request.body), | ||
header: request.headers[SECRET_HEADER_LOWERCASE], | ||
end: () => reply.status(200).send(), | ||
@@ -127,17 +137,16 @@ respond: (json) => reply.headers({ "Content-Type": "application/json" }).send(json), | ||
/** hono web framework */ | ||
const hono = (ctx) => { | ||
const hono = (c) => { | ||
let resolveResponse; | ||
return { | ||
update: ctx.req.json(), | ||
header: ctx.req.header(SECRET_HEADER), | ||
update: c.req.json(), | ||
header: c.req.header(SECRET_HEADER), | ||
end: () => { | ||
resolveResponse(ctx.body()); | ||
resolveResponse(c.body(null)); | ||
}, | ||
respond: (json) => { | ||
resolveResponse(ctx.json(json)); | ||
resolveResponse(c.json(json)); | ||
}, | ||
unauthorized: () => { | ||
ctx.status(401); | ||
ctx.statusText(WRONG_TOKEN_ERROR); | ||
resolveResponse(ctx.body()); | ||
c.status(401); | ||
resolveResponse(c.body(null)); | ||
}, | ||
@@ -176,3 +185,3 @@ handlerReturn: new Promise((resolve) => { | ||
update: Promise.resolve(ctx.request.body), | ||
header: ctx.get(SECRET_HEADER), | ||
header: ctx.get(SECRET_HEADER) || undefined, | ||
end: () => { | ||
@@ -190,12 +199,12 @@ ctx.body = ""; | ||
/** Next.js Serverless Functions */ | ||
const nextJs = (req, res) => ({ | ||
update: Promise.resolve(req.body), | ||
header: req.headers[SECRET_HEADER_LOWERCASE], | ||
end: () => res.end(), | ||
respond: (json) => res.status(200).json(json), | ||
unauthorized: () => res.status(401).send(WRONG_TOKEN_ERROR), | ||
const nextJs = (request, response) => ({ | ||
update: Promise.resolve(request.body), | ||
header: request.headers[SECRET_HEADER_LOWERCASE], | ||
end: () => response.end(), | ||
respond: (json) => response.status(200).json(json), | ||
unauthorized: () => response.status(401).send(WRONG_TOKEN_ERROR), | ||
}); | ||
/** nhttp web framework */ | ||
const nhttp = (rev) => ({ | ||
update: rev.body, | ||
update: Promise.resolve(rev.body), | ||
header: rev.headers.get(SECRET_HEADER) || undefined, | ||
@@ -275,10 +284,13 @@ end: () => rev.response.sendStatus(200), | ||
}; | ||
/** worktop CloudFlare workers framework */ | ||
const worktop = (req, res) => ({ | ||
update: Promise.resolve(req.body.json()), | ||
header: req.headers.get(SECRET_HEADER), | ||
end: () => res.end(), | ||
respond: (json) => res.send(200, json), | ||
unauthorized: () => res.send(401, WRONG_TOKEN_ERROR), | ||
}); | ||
/** worktop Cloudflare workers framework */ | ||
const worktop = (req, res) => { | ||
var _a; | ||
return ({ | ||
update: Promise.resolve(req.json()), | ||
header: (_a = req.headers.get(SECRET_HEADER)) !== null && _a !== void 0 ? _a : undefined, | ||
end: () => res.end(null), | ||
respond: (json) => res.send(200, json), | ||
unauthorized: () => res.send(401, WRONG_TOKEN_ERROR), | ||
}); | ||
}; | ||
// Please open a pull request if you want to add another adapter | ||
@@ -285,0 +297,0 @@ exports.adapters = { |
import { type Bot } from "../bot.js"; | ||
import { type Context } from "../context.js"; | ||
import { type FrameworkAdapter, type SupportedFrameworks } from "./frameworks.js"; | ||
import { adapters as nativeAdapters, type FrameworkAdapter } from "./frameworks.js"; | ||
export interface WebhookOptions { | ||
@@ -31,3 +31,4 @@ /** An optional strategy to handle timeouts (default: 'throw') */ | ||
*/ | ||
export declare function webhookCallback<C extends Context = Context>(bot: Bot<C>, adapter?: SupportedFrameworks | FrameworkAdapter, onTimeout?: WebhookOptions["onTimeout"], timeoutMilliseconds?: WebhookOptions["timeoutMilliseconds"], secretToken?: WebhookOptions["secretToken"]): (...args: any[]) => any; | ||
export declare function webhookCallback<C extends Context = Context>(bot: Bot<C>, adapter?: SupportedFrameworks | FrameworkAdapter, webhookOptions?: WebhookOptions): (...args: any[]) => any; | ||
export declare function webhookCallback<C extends Context = Context>(bot: Bot<C>, adapter?: FrameworkAdapter, onTimeout?: WebhookOptions["onTimeout"], timeoutMilliseconds?: WebhookOptions["timeoutMilliseconds"], secretToken?: WebhookOptions["secretToken"]): (...args: any[]) => any; | ||
export declare function webhookCallback<C extends Context = Context>(bot: Bot<C>, adapter?: FrameworkAdapter, webhookOptions?: WebhookOptions): (...args: any[]) => any; | ||
export declare function webhookCallback<C extends Context = Context, A extends keyof typeof nativeAdapters = keyof typeof nativeAdapters>(bot: Bot<C>, adapter: A, webhookOptions?: WebhookOptions): (...args: Parameters<typeof nativeAdapters[A]>) => ReturnType<typeof nativeAdapters[A]>["handlerReturn"] extends undefined ? Promise<void> : NonNullable<ReturnType<typeof nativeAdapters[A]>["handlerReturn"]>; |
@@ -785,3 +785,3 @@ import { type Context } from "./context.js"; | ||
message: [U["message"]] extends [object] ? U["message"] : undefined; | ||
edited_message: [U["edited_message"]] extends [object] ? U["edited_message"] : undefined; | ||
editedMessage: [U["edited_message"]] extends [object] ? U["edited_message"] : undefined; | ||
channelPost: [U["channel_post"]] extends [object] ? U["channel_post"] : undefined; | ||
@@ -788,0 +788,0 @@ editedChannelPost: [U["edited_channel_post"]] extends [object] ? U["edited_channel_post"] : undefined; |
{ | ||
"name": "grammy", | ||
"description": "The Telegram Bot Framework.", | ||
"version": "1.23.0", | ||
"version": "1.23.1", | ||
"author": "KnorpelSenf", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -13,3 +13,3 @@ <div align="center"><a href="https://grammy.dev"><img src="https://raw.githubusercontent.com/grammyjs/website/main/logos/grammY.png" alt="grammY"></a></h1></div> | ||
[![Bot API](https://img.shields.io/badge/Bot%20API-7.2-blue?logo=telegram&style=flat&labelColor=000&color=3b82f6)](https://core.telegram.org/bots/api) | ||
[![Bot API](https://img.shields.io/badge/Bot%20API-7.3-blue?logo=telegram&style=flat&labelColor=000&color=3b82f6)](https://core.telegram.org/bots/api) | ||
[![Deno](https://shield.deno.dev/x/grammy)](https://deno.land/x/grammy) | ||
@@ -296,2 +296,4 @@ [![npm](https://img.shields.io/npm/v/grammy?logo=npm&style=flat&labelColor=000&color=3b82f6)](https://www.npmjs.org/package/grammy) | ||
<td align="center" valign="top" width="11.11%"><a href="https://github.com/mordv"><img src="https://avatars.githubusercontent.com/u/32086218?v=4?s=100" width="100px;" alt="Alexander Mordvinov"/><br /><sub><b>Alexander Mordvinov</b></sub></a><br /><a href="#ideas-mordv" title="Ideas, Planning, & Feedback">π€</a></td> | ||
<td align="center" valign="top" width="11.11%"><a href="http://zi.vc/"><img src="https://avatars.githubusercontent.com/u/4239224?v=4?s=100" width="100px;" alt="Ash"/><br /><sub><b>Ash</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/pulls?q=is%3Apr+reviewed-by%3Azivc" title="Reviewed Pull Requests">π</a></td> | ||
<td align="center" valign="top" width="11.11%"><a href="https://github.com/winstxnhdw"><img src="https://avatars.githubusercontent.com/u/56998716?v=4?s=100" width="100px;" alt="Winston H."/><br /><sub><b>Winston H.</b></sub></a><br /><a href="#ideas-winstxnhdw" title="Ideas, Planning, & Feedback">π€</a> <a href="https://github.com/grammyjs/grammY/commits?author=winstxnhdw" title="Code">π»</a> <a href="https://github.com/grammyjs/grammY/pulls?q=is%3Apr+reviewed-by%3Awinstxnhdw" title="Reviewed Pull Requests">π</a> <a href="https://github.com/grammyjs/grammY/commits?author=winstxnhdw" title="Tests">β οΈ</a></td> | ||
</tr> | ||
@@ -298,0 +300,0 @@ </tbody> |
Sorry, the diff of this file is not supported yet
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
1009368
20132
308