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,199 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requiresFormDataUpload = requiresFormDataUpload;
4
+ exports.createJsonPayload = createJsonPayload;
5
+ exports.createFormDataPayload = createFormDataPayload;
6
+ const platform_node_js_1 = require("../platform.node.js");
7
+ const types_js_1 = require("../types.js");
8
+ // === Payload types (JSON vs. form data)
9
+ /**
10
+ * Determines for a given payload if it may be sent as JSON, or if it has to be
11
+ * uploaded via multipart/form-data. Returns `true` in the latter case and
12
+ * `false` in the former.
13
+ *
14
+ * @param payload The payload to analyze
15
+ */
16
+ function requiresFormDataUpload(payload) {
17
+ return payload instanceof types_js_1.InputFile || (typeof payload === "object" &&
18
+ payload !== null &&
19
+ Object.values(payload).some((v) => Array.isArray(v)
20
+ ? v.some(requiresFormDataUpload)
21
+ : v instanceof types_js_1.InputFile || requiresFormDataUpload(v)));
22
+ }
23
+ /**
24
+ * Calls `JSON.stringify` but removes `null` values from objects before
25
+ * serialization
26
+ *
27
+ * @param value value
28
+ * @returns stringified value
29
+ */
30
+ function str(value) {
31
+ return JSON.stringify(value, (_, v) => v !== null && v !== void 0 ? v : undefined);
32
+ }
33
+ /**
34
+ * Turns a payload into an options object that can be passed to a `fetch` call
35
+ * by setting the necessary headers and method. May only be called for payloads
36
+ * `P` that let `requiresFormDataUpload(P)` return `false`.
37
+ *
38
+ * @param payload The payload to wrap
39
+ */
40
+ function createJsonPayload(payload) {
41
+ return {
42
+ method: "POST",
43
+ headers: {
44
+ "content-type": "application/json",
45
+ connection: "keep-alive",
46
+ },
47
+ body: str(payload),
48
+ };
49
+ }
50
+ async function* protectItr(itr, onError) {
51
+ try {
52
+ yield* itr;
53
+ }
54
+ catch (err) {
55
+ onError(err);
56
+ }
57
+ }
58
+ /**
59
+ * Turns a payload into an options object that can be passed to a `fetch` call
60
+ * by setting the necessary headers and method. Note that this method creates a
61
+ * multipart/form-data stream under the hood. If possible, a JSON payload should
62
+ * be created instead for performance reasons.
63
+ *
64
+ * @param payload The payload to wrap
65
+ */
66
+ function createFormDataPayload(payload, onError) {
67
+ const boundary = createBoundary();
68
+ const itr = payloadToMultipartItr(payload, boundary);
69
+ const safeItr = protectItr(itr, onError);
70
+ const stream = (0, platform_node_js_1.itrToStream)(safeItr);
71
+ return {
72
+ method: "POST",
73
+ headers: {
74
+ "content-type": `multipart/form-data; boundary=${boundary}`,
75
+ connection: "keep-alive",
76
+ },
77
+ body: stream,
78
+ };
79
+ }
80
+ // === Form data creation
81
+ function createBoundary() {
82
+ // Taken from Deno std lib
83
+ return "----------" + randomId(32);
84
+ }
85
+ function randomId(length = 16) {
86
+ return Array.from(Array(length))
87
+ .map(() => Math.random().toString(36)[2] || 0)
88
+ .join("");
89
+ }
90
+ const enc = new TextEncoder();
91
+ /**
92
+ * Takes a payload object and produces a valid multipart/form-data stream. The
93
+ * stream is an iterator of `Uint8Array` objects. You also need to specify the
94
+ * boundary string that was used in the Content-Type header of the HTTP request.
95
+ *
96
+ * @param payload a payload object
97
+ * @param boundary the boundary string to use between the parts
98
+ */
99
+ async function* payloadToMultipartItr(payload, boundary) {
100
+ const files = extractFiles(payload);
101
+ // Start multipart/form-data protocol
102
+ yield enc.encode(`--${boundary}\r\n`);
103
+ // Send all payload fields
104
+ const separator = enc.encode(`\r\n--${boundary}\r\n`);
105
+ let first = true;
106
+ for (const [key, value] of Object.entries(payload)) {
107
+ if (value == null)
108
+ continue;
109
+ if (!first)
110
+ yield separator;
111
+ yield valuePart(key, typeof value === "object" ? str(value) : value);
112
+ first = false;
113
+ }
114
+ // Send all files
115
+ for (const { id, origin, file } of files) {
116
+ if (!first)
117
+ yield separator;
118
+ yield* filePart(id, origin, file);
119
+ first = false;
120
+ }
121
+ // End multipart/form-data protocol
122
+ yield enc.encode(`\r\n--${boundary}--\r\n`);
123
+ }
124
+ /**
125
+ * Replaces all instances of `InputFile` in a given payload by attach://
126
+ * strings. This alters the passed object. After calling this method, the
127
+ * payload object can be stringified.
128
+ *
129
+ * Returns a list of `InputFile` instances along with the random identifiers
130
+ * that were used in the corresponding attach:// strings, as well as the origin
131
+ * keys of the original payload object.
132
+ *
133
+ * @param value a payload object, or a part of it
134
+ * @param key the origin key of the payload object, if a part of it is passed
135
+ * @returns the cleaned payload object
136
+ */
137
+ function extractFiles(value) {
138
+ if (typeof value !== "object" || value === null)
139
+ return [];
140
+ return Object.entries(value).flatMap(([k, v]) => {
141
+ if (Array.isArray(v))
142
+ return v.flatMap((p) => extractFiles(p));
143
+ else if (v instanceof types_js_1.InputFile) {
144
+ const id = randomId();
145
+ // Overwrite `InputFile` instance with attach:// string
146
+ Object.assign(value, { [k]: `attach://${id}` });
147
+ const origin = k === "media" &&
148
+ "type" in value && typeof value.type === "string"
149
+ ? value.type // use `type` for `InputMedia*`
150
+ : k; // use property key otherwise
151
+ return { id, origin, file: v };
152
+ }
153
+ else
154
+ return extractFiles(v);
155
+ });
156
+ }
157
+ /** Turns a regular value into a `Uint8Array` */
158
+ function valuePart(key, value) {
159
+ return enc.encode(`content-disposition:form-data;name="${key}"\r\n\r\n${value}`);
160
+ }
161
+ /** Turns an InputFile into a generator of `Uint8Array`s */
162
+ async function* filePart(id, origin, input) {
163
+ const filename = input.filename || `${origin}.${getExt(origin)}`;
164
+ if (filename.includes("\r") || filename.includes("\n")) {
165
+ throw new Error(`File paths cannot contain carriage-return (\\r) \
166
+ or newline (\\n) characters! Filename for property '${origin}' was:
167
+ """
168
+ ${filename}
169
+ """`);
170
+ }
171
+ yield enc.encode(`content-disposition:form-data;name="${id}";filename=${filename}\r\ncontent-type:application/octet-stream\r\n\r\n`);
172
+ const data = await input.toRaw();
173
+ if (data instanceof Uint8Array)
174
+ yield data;
175
+ else
176
+ yield* data;
177
+ }
178
+ /** Returns the default file extension for an API property name */
179
+ function getExt(key) {
180
+ switch (key) {
181
+ case "certificate":
182
+ return "pem";
183
+ case "photo":
184
+ case "thumbnail":
185
+ return "jpg";
186
+ case "voice":
187
+ return "ogg";
188
+ case "audio":
189
+ return "mp3";
190
+ case "animation":
191
+ case "video":
192
+ case "video_note":
193
+ return "mp4";
194
+ case "sticker":
195
+ return "webp";
196
+ default:
197
+ return "dat";
198
+ }
199
+ }