grammy-tools 1.36.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +339 -0
  3. package/out/bot.d.ts +314 -0
  4. package/out/bot.js +669 -0
  5. package/out/composer.d.ts +764 -0
  6. package/out/composer.js +704 -0
  7. package/out/context.d.ts +1618 -0
  8. package/out/context.js +1976 -0
  9. package/out/convenience/constants.d.ts +98 -0
  10. package/out/convenience/constants.js +37 -0
  11. package/out/convenience/frameworks.d.ts +216 -0
  12. package/out/convenience/frameworks.js +370 -0
  13. package/out/convenience/inline_query.d.ts +401 -0
  14. package/out/convenience/inline_query.js +461 -0
  15. package/out/convenience/input_media.d.ts +72 -0
  16. package/out/convenience/input_media.js +82 -0
  17. package/out/convenience/keyboard.d.ts +763 -0
  18. package/out/convenience/keyboard.js +935 -0
  19. package/out/convenience/session.d.ts +345 -0
  20. package/out/convenience/session.js +455 -0
  21. package/out/convenience/webhook.d.ts +59 -0
  22. package/out/convenience/webhook.js +80 -0
  23. package/out/core/api.d.ts +1628 -0
  24. package/out/core/api.js +1907 -0
  25. package/out/core/client.d.ts +180 -0
  26. package/out/core/client.js +226 -0
  27. package/out/core/error.d.ts +53 -0
  28. package/out/core/error.js +87 -0
  29. package/out/core/payload.d.ts +39 -0
  30. package/out/core/payload.js +199 -0
  31. package/out/filter.d.ts +959 -0
  32. package/out/filter.js +378 -0
  33. package/out/mod.d.ts +14 -0
  34. package/out/mod.js +43 -0
  35. package/out/platform.node.d.ts +16 -0
  36. package/out/platform.node.js +43 -0
  37. package/out/shim.node.d.ts +2 -0
  38. package/out/shim.node.js +7 -0
  39. package/out/types.d.ts +1 -0
  40. package/out/types.js +17 -0
  41. package/out/types.node.d.ts +78 -0
  42. package/out/types.node.js +107 -0
  43. package/out/web.d.ts +1 -0
  44. package/out/web.mjs +5172 -0
  45. package/package.json +71 -0
@@ -0,0 +1,370 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.adapters = void 0;
4
+ const SECRET_HEADER = "X-Telegram-Bot-Api-Secret-Token";
5
+ const SECRET_HEADER_LOWERCASE = SECRET_HEADER.toLowerCase();
6
+ const WRONG_TOKEN_ERROR = "secret token is wrong";
7
+ const ok = () => new Response(null, { status: 200 });
8
+ const okJson = (json) => new Response(json, {
9
+ status: 200,
10
+ headers: { "Content-Type": "application/json" },
11
+ });
12
+ const unauthorized = () => new Response('"unauthorized"', {
13
+ status: 401,
14
+ statusText: WRONG_TOKEN_ERROR,
15
+ });
16
+ /** AWS lambda serverless functions */
17
+ const awsLambda = (event, _context, callback) => {
18
+ var _a;
19
+ return ({
20
+ update: JSON.parse((_a = event.body) !== null && _a !== void 0 ? _a : "{}"),
21
+ header: event.headers[SECRET_HEADER],
22
+ end: () => callback(null, { statusCode: 200 }),
23
+ respond: (json) => callback(null, {
24
+ statusCode: 200,
25
+ headers: { "Content-Type": "application/json" },
26
+ body: json,
27
+ }),
28
+ unauthorized: () => callback(null, { statusCode: 401 }),
29
+ });
30
+ };
31
+ /** AWS lambda async/await serverless functions */
32
+ const awsLambdaAsync = (event, _context) => {
33
+ var _a;
34
+ // deno-lint-ignore no-explicit-any
35
+ let resolveResponse;
36
+ return {
37
+ update: JSON.parse((_a = event.body) !== null && _a !== void 0 ? _a : "{}"),
38
+ header: event.headers[SECRET_HEADER],
39
+ end: () => resolveResponse({ statusCode: 200 }),
40
+ respond: (json) => resolveResponse({
41
+ statusCode: 200,
42
+ headers: { "Content-Type": "application/json" },
43
+ body: json,
44
+ }),
45
+ unauthorized: () => resolveResponse({ statusCode: 401 }),
46
+ handlerReturn: new Promise((resolve) => {
47
+ resolveResponse = resolve;
48
+ }),
49
+ };
50
+ };
51
+ /** Azure Functions v3 and v4 */
52
+ const azure = (context, request) => {
53
+ var _a, _b;
54
+ return ({
55
+ update: Promise.resolve(request.body),
56
+ header: (_b = (_a = context.res) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b[SECRET_HEADER],
57
+ end: () => (context.res = {
58
+ status: 200,
59
+ body: "",
60
+ }),
61
+ respond: (json) => {
62
+ var _a, _b, _c, _d;
63
+ (_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");
64
+ (_d = (_c = context.res) === null || _c === void 0 ? void 0 : _c.send) === null || _d === void 0 ? void 0 : _d.call(_c, json);
65
+ },
66
+ unauthorized: () => {
67
+ var _a, _b;
68
+ (_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);
69
+ },
70
+ });
71
+ };
72
+ const azureV4 = (request) => {
73
+ let resolveResponse;
74
+ return {
75
+ update: Promise.resolve(request.json()),
76
+ header: request.headers.get(SECRET_HEADER) || undefined,
77
+ end: () => resolveResponse({ status: 204 }),
78
+ respond: (json) => resolveResponse({ jsonBody: json }),
79
+ unauthorized: () => resolveResponse({ status: 401, body: WRONG_TOKEN_ERROR }),
80
+ handlerReturn: new Promise((resolve) => {
81
+ resolveResponse = resolve;
82
+ }),
83
+ };
84
+ };
85
+ /** Bun.serve */
86
+ const bun = (request) => {
87
+ let resolveResponse;
88
+ return {
89
+ update: request.json(),
90
+ header: request.headers.get(SECRET_HEADER) || undefined,
91
+ end: () => {
92
+ resolveResponse(ok());
93
+ },
94
+ respond: (json) => {
95
+ resolveResponse(okJson(json));
96
+ },
97
+ unauthorized: () => {
98
+ resolveResponse(unauthorized());
99
+ },
100
+ handlerReturn: new Promise((resolve) => {
101
+ resolveResponse = resolve;
102
+ }),
103
+ };
104
+ };
105
+ /** Native CloudFlare workers (service worker) */
106
+ const cloudflare = (event) => {
107
+ let resolveResponse;
108
+ event.respondWith(new Promise((resolve) => {
109
+ resolveResponse = resolve;
110
+ }));
111
+ return {
112
+ update: event.request.json(),
113
+ header: event.request.headers.get(SECRET_HEADER) || undefined,
114
+ end: () => {
115
+ resolveResponse(ok());
116
+ },
117
+ respond: (json) => {
118
+ resolveResponse(okJson(json));
119
+ },
120
+ unauthorized: () => {
121
+ resolveResponse(unauthorized());
122
+ },
123
+ };
124
+ };
125
+ /** Native CloudFlare workers (module worker) */
126
+ const cloudflareModule = (request) => {
127
+ let resolveResponse;
128
+ return {
129
+ update: request.json(),
130
+ header: request.headers.get(SECRET_HEADER) || undefined,
131
+ end: () => {
132
+ resolveResponse(ok());
133
+ },
134
+ respond: (json) => {
135
+ resolveResponse(okJson(json));
136
+ },
137
+ unauthorized: () => {
138
+ resolveResponse(unauthorized());
139
+ },
140
+ handlerReturn: new Promise((resolve) => {
141
+ resolveResponse = resolve;
142
+ }),
143
+ };
144
+ };
145
+ /** express web framework */
146
+ const express = (req, res) => ({
147
+ update: Promise.resolve(req.body),
148
+ header: req.header(SECRET_HEADER),
149
+ end: () => res.end(),
150
+ respond: (json) => {
151
+ res.set("Content-Type", "application/json");
152
+ res.send(json);
153
+ },
154
+ unauthorized: () => {
155
+ res.status(401).send(WRONG_TOKEN_ERROR);
156
+ },
157
+ });
158
+ /** fastify web framework */
159
+ const fastify = (request, reply) => ({
160
+ update: Promise.resolve(request.body),
161
+ header: request.headers[SECRET_HEADER_LOWERCASE],
162
+ end: () => reply.status(200).send(),
163
+ respond: (json) => reply.headers({ "Content-Type": "application/json" }).send(json),
164
+ unauthorized: () => reply.code(401).send(WRONG_TOKEN_ERROR),
165
+ });
166
+ /** hono web framework */
167
+ const hono = (c) => {
168
+ let resolveResponse;
169
+ return {
170
+ update: c.req.json(),
171
+ header: c.req.header(SECRET_HEADER),
172
+ end: () => {
173
+ resolveResponse(c.body(""));
174
+ },
175
+ respond: (json) => {
176
+ resolveResponse(c.json(json));
177
+ },
178
+ unauthorized: () => {
179
+ c.status(401);
180
+ resolveResponse(c.body(""));
181
+ },
182
+ handlerReturn: new Promise((resolve) => {
183
+ resolveResponse = resolve;
184
+ }),
185
+ };
186
+ };
187
+ /** Node.js native 'http' and 'https' modules */
188
+ const http = (req, res) => {
189
+ const secretHeaderFromRequest = req.headers[SECRET_HEADER_LOWERCASE];
190
+ return {
191
+ update: new Promise((resolve, reject) => {
192
+ const chunks = [];
193
+ req.on("data", (chunk) => chunks.push(chunk))
194
+ .once("end", () => {
195
+ // @ts-ignore `Buffer` is Node-only
196
+ // deno-lint-ignore no-node-globals
197
+ const raw = Buffer.concat(chunks).toString("utf-8");
198
+ resolve(JSON.parse(raw));
199
+ })
200
+ .once("error", reject);
201
+ }),
202
+ header: Array.isArray(secretHeaderFromRequest)
203
+ ? secretHeaderFromRequest[0]
204
+ : secretHeaderFromRequest,
205
+ end: () => res.end(),
206
+ respond: (json) => res
207
+ .writeHead(200, { "Content-Type": "application/json" })
208
+ .end(json),
209
+ unauthorized: () => res.writeHead(401).end(WRONG_TOKEN_ERROR),
210
+ };
211
+ };
212
+ /** koa web framework */
213
+ const koa = (ctx) => ({
214
+ update: Promise.resolve(ctx.request.body),
215
+ header: ctx.get(SECRET_HEADER) || undefined,
216
+ end: () => {
217
+ ctx.body = "";
218
+ },
219
+ respond: (json) => {
220
+ ctx.set("Content-Type", "application/json");
221
+ ctx.response.body = json;
222
+ },
223
+ unauthorized: () => {
224
+ ctx.status = 401;
225
+ },
226
+ });
227
+ /** Next.js Serverless Functions */
228
+ const nextJs = (request, response) => ({
229
+ update: Promise.resolve(request.body),
230
+ header: request.headers[SECRET_HEADER_LOWERCASE],
231
+ end: () => response.end(),
232
+ respond: (json) => response.status(200).json(json),
233
+ unauthorized: () => response.status(401).send(WRONG_TOKEN_ERROR),
234
+ });
235
+ /** nhttp web framework */
236
+ const nhttp = (rev) => ({
237
+ update: Promise.resolve(rev.body),
238
+ header: rev.headers.get(SECRET_HEADER) || undefined,
239
+ end: () => rev.response.sendStatus(200),
240
+ respond: (json) => rev.response.status(200).send(json),
241
+ unauthorized: () => rev.response.status(401).send(WRONG_TOKEN_ERROR),
242
+ });
243
+ /** oak web framework */
244
+ const oak = (ctx) => ({
245
+ update: ctx.request.body.json(),
246
+ header: ctx.request.headers.get(SECRET_HEADER) || undefined,
247
+ end: () => {
248
+ ctx.response.status = 200;
249
+ },
250
+ respond: (json) => {
251
+ ctx.response.type = "json";
252
+ ctx.response.body = json;
253
+ },
254
+ unauthorized: () => {
255
+ ctx.response.status = 401;
256
+ },
257
+ });
258
+ /** Deno.serve */
259
+ const serveHttp = (requestEvent) => ({
260
+ update: requestEvent.request.json(),
261
+ header: requestEvent.request.headers.get(SECRET_HEADER) || undefined,
262
+ end: () => requestEvent.respondWith(ok()),
263
+ respond: (json) => requestEvent.respondWith(okJson(json)),
264
+ unauthorized: () => requestEvent.respondWith(unauthorized()),
265
+ });
266
+ /** std/http web server */
267
+ const stdHttp = (req) => {
268
+ let resolveResponse;
269
+ return {
270
+ update: req.json(),
271
+ header: req.headers.get(SECRET_HEADER) || undefined,
272
+ end: () => {
273
+ if (resolveResponse)
274
+ resolveResponse(ok());
275
+ },
276
+ respond: (json) => {
277
+ if (resolveResponse)
278
+ resolveResponse(okJson(json));
279
+ },
280
+ unauthorized: () => {
281
+ if (resolveResponse)
282
+ resolveResponse(unauthorized());
283
+ },
284
+ handlerReturn: new Promise((resolve) => {
285
+ resolveResponse = resolve;
286
+ }),
287
+ };
288
+ };
289
+ /** Sveltekit Serverless Functions */
290
+ const sveltekit = ({ request }) => {
291
+ let resolveResponse;
292
+ return {
293
+ update: Promise.resolve(request.json()),
294
+ header: request.headers.get(SECRET_HEADER) || undefined,
295
+ end: () => {
296
+ if (resolveResponse)
297
+ resolveResponse(ok());
298
+ },
299
+ respond: (json) => {
300
+ if (resolveResponse)
301
+ resolveResponse(okJson(json));
302
+ },
303
+ unauthorized: () => {
304
+ if (resolveResponse)
305
+ resolveResponse(unauthorized());
306
+ },
307
+ handlerReturn: new Promise((resolve) => {
308
+ resolveResponse = resolve;
309
+ }),
310
+ };
311
+ };
312
+ /** worktop Cloudflare workers framework */
313
+ const worktop = (req, res) => {
314
+ var _a;
315
+ return ({
316
+ update: Promise.resolve(req.json()),
317
+ header: (_a = req.headers.get(SECRET_HEADER)) !== null && _a !== void 0 ? _a : undefined,
318
+ end: () => res.end(null),
319
+ respond: (json) => res.send(200, json),
320
+ unauthorized: () => res.send(401, WRONG_TOKEN_ERROR),
321
+ });
322
+ };
323
+ const elysia = (ctx) => {
324
+ // @note upgrade target to use modern code?
325
+ // const { promise, resolve } = Promise.withResolvers<string>();
326
+ let resolve;
327
+ const handlerReturn = new Promise((res) => resolve = res);
328
+ return {
329
+ // @note technically the type shouldn't be limited to Promise, because it's fine to await plain values as well
330
+ update: Promise.resolve(ctx.body),
331
+ header: ctx.headers[SECRET_HEADER_LOWERCASE],
332
+ end() {
333
+ resolve("");
334
+ },
335
+ respond(json) {
336
+ // @note since json is passed as string here, we gotta define proper content-type
337
+ ctx.set.headers["content-type"] = "application/json";
338
+ resolve(json);
339
+ },
340
+ unauthorized() {
341
+ ctx.set.status = 401;
342
+ resolve("");
343
+ },
344
+ handlerReturn,
345
+ };
346
+ };
347
+ // Please open a pull request if you want to add another adapter
348
+ exports.adapters = {
349
+ "aws-lambda": awsLambda,
350
+ "aws-lambda-async": awsLambdaAsync,
351
+ azure,
352
+ "azure-v4": azureV4,
353
+ bun,
354
+ cloudflare,
355
+ "cloudflare-mod": cloudflareModule,
356
+ elysia,
357
+ express,
358
+ fastify,
359
+ hono,
360
+ http,
361
+ https: http,
362
+ koa,
363
+ "next-js": nextJs,
364
+ nhttp,
365
+ oak,
366
+ serveHttp,
367
+ "std/http": stdHttp,
368
+ sveltekit,
369
+ worktop,
370
+ };