gramio 0.2.3 → 0.2.5
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.
- package/dist/index.cjs +39 -9
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +40 -10
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -317,6 +317,23 @@ Plugin = __decorateElement$1(_init$1, 0, "Plugin", _Plugin_decorators, Plugin);
|
|
|
317
317
|
__runInitializers$1(_init$1, 1, Plugin);
|
|
318
318
|
|
|
319
319
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
320
|
+
function convertToString(value) {
|
|
321
|
+
const typeOfValue = typeof value;
|
|
322
|
+
if (typeOfValue === "string") return value;
|
|
323
|
+
if (typeOfValue === "object") return JSON.stringify(value);
|
|
324
|
+
return String(value);
|
|
325
|
+
}
|
|
326
|
+
function simplifyObject(obj) {
|
|
327
|
+
const result = {};
|
|
328
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
329
|
+
const typeOfValue = typeof value;
|
|
330
|
+
if (value === void 0 || value === null || typeOfValue === "function")
|
|
331
|
+
continue;
|
|
332
|
+
result[key] = convertToString(value);
|
|
333
|
+
}
|
|
334
|
+
return result;
|
|
335
|
+
}
|
|
336
|
+
const IS_BUN = typeof Bun !== "undefined";
|
|
320
337
|
|
|
321
338
|
class UpdateQueue {
|
|
322
339
|
updateQueue = [];
|
|
@@ -537,7 +554,8 @@ class Bot {
|
|
|
537
554
|
constructor(tokenOrOptions, optionsRaw) {
|
|
538
555
|
const token = typeof tokenOrOptions === "string" ? tokenOrOptions : tokenOrOptions?.token;
|
|
539
556
|
const options = typeof tokenOrOptions === "object" ? tokenOrOptions : optionsRaw;
|
|
540
|
-
if (!token
|
|
557
|
+
if (!token) throw new Error("Token is required!");
|
|
558
|
+
if (typeof token !== "string")
|
|
541
559
|
throw new Error(`Token is ${typeof token} but it should be a string!`);
|
|
542
560
|
this.options = {
|
|
543
561
|
...options,
|
|
@@ -573,7 +591,7 @@ class Bot {
|
|
|
573
591
|
}
|
|
574
592
|
async _callApi(method, params = {}) {
|
|
575
593
|
const debug$api$method = debug$api.extend(method);
|
|
576
|
-
|
|
594
|
+
let url = `${this.options.api.baseURL}${this.options.token}/${this.options.api.useTest ? "test/" : ""}${method}`;
|
|
577
595
|
const reqOptions = {
|
|
578
596
|
method: "POST",
|
|
579
597
|
...this.options.api.fetchOptions,
|
|
@@ -591,8 +609,18 @@ class Bot {
|
|
|
591
609
|
);
|
|
592
610
|
params = context.params;
|
|
593
611
|
if (params && files.isMediaUpload(method, params)) {
|
|
594
|
-
|
|
595
|
-
|
|
612
|
+
if (IS_BUN) {
|
|
613
|
+
const formData = await files.convertJsonToFormData(method, params);
|
|
614
|
+
reqOptions.body = formData;
|
|
615
|
+
} else {
|
|
616
|
+
const [formData, paramsWithoutFiles] = await files.extractFilesToFormData(
|
|
617
|
+
method,
|
|
618
|
+
params
|
|
619
|
+
);
|
|
620
|
+
reqOptions.body = formData;
|
|
621
|
+
const simplifiedParams = simplifyObject(paramsWithoutFiles);
|
|
622
|
+
url += `?${new URLSearchParams(simplifiedParams).toString()}`;
|
|
623
|
+
}
|
|
596
624
|
} else {
|
|
597
625
|
reqOptions.headers.set("Content-Type", "application/json");
|
|
598
626
|
reqOptions.body = JSON.stringify(params);
|
|
@@ -953,12 +981,14 @@ class Bot {
|
|
|
953
981
|
if (!context.data) return next();
|
|
954
982
|
if (typeof trigger === "string" && context.data !== trigger)
|
|
955
983
|
return next();
|
|
956
|
-
if (trigger instanceof
|
|
957
|
-
return next();
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
if (trigger instanceof callbackData.CallbackData)
|
|
984
|
+
if (trigger instanceof RegExp) {
|
|
985
|
+
if (!trigger.test(context.data)) return next();
|
|
986
|
+
context.queryData = context.data.match(trigger);
|
|
987
|
+
}
|
|
988
|
+
if (trigger instanceof callbackData.CallbackData) {
|
|
989
|
+
if (!trigger.regexp().test(context.data)) return next();
|
|
961
990
|
context.queryData = trigger.unpack(context.data);
|
|
991
|
+
}
|
|
962
992
|
return handler(context);
|
|
963
993
|
});
|
|
964
994
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -889,7 +889,7 @@ declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDe
|
|
|
889
889
|
* ```
|
|
890
890
|
*/
|
|
891
891
|
callbackQuery<Trigger extends CallbackData | string | RegExp>(trigger: Trigger, handler: (context: Omit<ContextType<typeof this, "callback_query">, "data"> & Derives["global"] & Derives["callback_query"] & {
|
|
892
|
-
queryData: Trigger extends CallbackData ? ReturnType<Trigger["unpack"]> : RegExpMatchArray
|
|
892
|
+
queryData: Trigger extends CallbackData ? ReturnType<Trigger["unpack"]> : Trigger extends RegExp ? RegExpMatchArray : never;
|
|
893
893
|
}) => unknown): this;
|
|
894
894
|
/** Register handler to `chosen_inline_result` update */
|
|
895
895
|
chosenInlineResult<Ctx = ContextType<typeof this, "chosen_inline_result"> & Derives["global"] & Derives["chosen_inline_result"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
|
|
@@ -944,7 +944,7 @@ declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDe
|
|
|
944
944
|
* if (context.args) await context.send(`Params ${context.args[1]}`);
|
|
945
945
|
* });
|
|
946
946
|
*/
|
|
947
|
-
hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]
|
|
947
|
+
hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"], Trigger extends RegExp | string | ((context: Ctx) => boolean) = RegExp | string | ((context: Ctx) => boolean)>(trigger: Trigger, handler: (context: Ctx & {
|
|
948
948
|
args: RegExpMatchArray | null;
|
|
949
949
|
}) => unknown): this;
|
|
950
950
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -889,7 +889,7 @@ declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDe
|
|
|
889
889
|
* ```
|
|
890
890
|
*/
|
|
891
891
|
callbackQuery<Trigger extends CallbackData | string | RegExp>(trigger: Trigger, handler: (context: Omit<ContextType<typeof this, "callback_query">, "data"> & Derives["global"] & Derives["callback_query"] & {
|
|
892
|
-
queryData: Trigger extends CallbackData ? ReturnType<Trigger["unpack"]> : RegExpMatchArray
|
|
892
|
+
queryData: Trigger extends CallbackData ? ReturnType<Trigger["unpack"]> : Trigger extends RegExp ? RegExpMatchArray : never;
|
|
893
893
|
}) => unknown): this;
|
|
894
894
|
/** Register handler to `chosen_inline_result` update */
|
|
895
895
|
chosenInlineResult<Ctx = ContextType<typeof this, "chosen_inline_result"> & Derives["global"] & Derives["chosen_inline_result"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
|
|
@@ -944,7 +944,7 @@ declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDe
|
|
|
944
944
|
* if (context.args) await context.send(`Params ${context.args[1]}`);
|
|
945
945
|
* });
|
|
946
946
|
*/
|
|
947
|
-
hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]
|
|
947
|
+
hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"], Trigger extends RegExp | string | ((context: Ctx) => boolean) = RegExp | string | ((context: Ctx) => boolean)>(trigger: Trigger, handler: (context: Ctx & {
|
|
948
948
|
args: RegExpMatchArray | null;
|
|
949
949
|
}) => unknown): this;
|
|
950
950
|
/**
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import { CallbackData } from '@gramio/callback-data';
|
|
|
4
4
|
export * from '@gramio/callback-data';
|
|
5
5
|
import { contextsMappings, PhotoAttachment } from '@gramio/contexts';
|
|
6
6
|
export * from '@gramio/contexts';
|
|
7
|
-
import { isMediaUpload, convertJsonToFormData } from '@gramio/files';
|
|
7
|
+
import { isMediaUpload, convertJsonToFormData, extractFilesToFormData } from '@gramio/files';
|
|
8
8
|
export * from '@gramio/files';
|
|
9
9
|
import { FormattableMap } from '@gramio/format';
|
|
10
10
|
export * from '@gramio/format';
|
|
@@ -319,6 +319,23 @@ Plugin = __decorateElement$1(_init$1, 0, "Plugin", _Plugin_decorators, Plugin);
|
|
|
319
319
|
__runInitializers$1(_init$1, 1, Plugin);
|
|
320
320
|
|
|
321
321
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
322
|
+
function convertToString(value) {
|
|
323
|
+
const typeOfValue = typeof value;
|
|
324
|
+
if (typeOfValue === "string") return value;
|
|
325
|
+
if (typeOfValue === "object") return JSON.stringify(value);
|
|
326
|
+
return String(value);
|
|
327
|
+
}
|
|
328
|
+
function simplifyObject(obj) {
|
|
329
|
+
const result = {};
|
|
330
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
331
|
+
const typeOfValue = typeof value;
|
|
332
|
+
if (value === void 0 || value === null || typeOfValue === "function")
|
|
333
|
+
continue;
|
|
334
|
+
result[key] = convertToString(value);
|
|
335
|
+
}
|
|
336
|
+
return result;
|
|
337
|
+
}
|
|
338
|
+
const IS_BUN = typeof Bun !== "undefined";
|
|
322
339
|
|
|
323
340
|
class UpdateQueue {
|
|
324
341
|
updateQueue = [];
|
|
@@ -539,7 +556,8 @@ class Bot {
|
|
|
539
556
|
constructor(tokenOrOptions, optionsRaw) {
|
|
540
557
|
const token = typeof tokenOrOptions === "string" ? tokenOrOptions : tokenOrOptions?.token;
|
|
541
558
|
const options = typeof tokenOrOptions === "object" ? tokenOrOptions : optionsRaw;
|
|
542
|
-
if (!token
|
|
559
|
+
if (!token) throw new Error("Token is required!");
|
|
560
|
+
if (typeof token !== "string")
|
|
543
561
|
throw new Error(`Token is ${typeof token} but it should be a string!`);
|
|
544
562
|
this.options = {
|
|
545
563
|
...options,
|
|
@@ -575,7 +593,7 @@ class Bot {
|
|
|
575
593
|
}
|
|
576
594
|
async _callApi(method, params = {}) {
|
|
577
595
|
const debug$api$method = debug$api.extend(method);
|
|
578
|
-
|
|
596
|
+
let url = `${this.options.api.baseURL}${this.options.token}/${this.options.api.useTest ? "test/" : ""}${method}`;
|
|
579
597
|
const reqOptions = {
|
|
580
598
|
method: "POST",
|
|
581
599
|
...this.options.api.fetchOptions,
|
|
@@ -593,8 +611,18 @@ class Bot {
|
|
|
593
611
|
);
|
|
594
612
|
params = context.params;
|
|
595
613
|
if (params && isMediaUpload(method, params)) {
|
|
596
|
-
|
|
597
|
-
|
|
614
|
+
if (IS_BUN) {
|
|
615
|
+
const formData = await convertJsonToFormData(method, params);
|
|
616
|
+
reqOptions.body = formData;
|
|
617
|
+
} else {
|
|
618
|
+
const [formData, paramsWithoutFiles] = await extractFilesToFormData(
|
|
619
|
+
method,
|
|
620
|
+
params
|
|
621
|
+
);
|
|
622
|
+
reqOptions.body = formData;
|
|
623
|
+
const simplifiedParams = simplifyObject(paramsWithoutFiles);
|
|
624
|
+
url += `?${new URLSearchParams(simplifiedParams).toString()}`;
|
|
625
|
+
}
|
|
598
626
|
} else {
|
|
599
627
|
reqOptions.headers.set("Content-Type", "application/json");
|
|
600
628
|
reqOptions.body = JSON.stringify(params);
|
|
@@ -955,12 +983,14 @@ class Bot {
|
|
|
955
983
|
if (!context.data) return next();
|
|
956
984
|
if (typeof trigger === "string" && context.data !== trigger)
|
|
957
985
|
return next();
|
|
958
|
-
if (trigger instanceof
|
|
959
|
-
return next();
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
if (trigger instanceof CallbackData)
|
|
986
|
+
if (trigger instanceof RegExp) {
|
|
987
|
+
if (!trigger.test(context.data)) return next();
|
|
988
|
+
context.queryData = context.data.match(trigger);
|
|
989
|
+
}
|
|
990
|
+
if (trigger instanceof CallbackData) {
|
|
991
|
+
if (!trigger.regexp().test(context.data)) return next();
|
|
963
992
|
context.queryData = trigger.unpack(context.data);
|
|
993
|
+
}
|
|
964
994
|
return handler(context);
|
|
965
995
|
});
|
|
966
996
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gramio",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.5",
|
|
5
5
|
"description": "Powerful, extensible and really type-safe Telegram Bot API framework",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.js",
|
|
@@ -41,18 +41,18 @@
|
|
|
41
41
|
"license": "MIT",
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@biomejs/biome": "1.9.4",
|
|
44
|
-
"@types/bun": "^1.2.
|
|
44
|
+
"@types/bun": "^1.2.5",
|
|
45
45
|
"@types/debug": "^4.1.12",
|
|
46
|
-
"pkgroll": "^2.
|
|
47
|
-
"typescript": "^5.
|
|
46
|
+
"pkgroll": "^2.11.2",
|
|
47
|
+
"typescript": "^5.8.2"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@gramio/callback-data": "^0.0.3",
|
|
51
|
-
"@gramio/contexts": "^0.1.
|
|
52
|
-
"@gramio/files": "^0.
|
|
51
|
+
"@gramio/contexts": "^0.1.4",
|
|
52
|
+
"@gramio/files": "^0.2.0",
|
|
53
53
|
"@gramio/format": "^0.1.5",
|
|
54
54
|
"@gramio/keyboards": "^1.1.0",
|
|
55
|
-
"@gramio/types": "^8.3.
|
|
55
|
+
"@gramio/types": "^8.3.3",
|
|
56
56
|
"debug": "^4.4.0",
|
|
57
57
|
"inspectable": "^3.0.2",
|
|
58
58
|
"middleware-io": "^2.8.1"
|