gramio 0.0.2 → 0.0.3

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.
@@ -0,0 +1,12 @@
1
+ import { TelegramAPIResponseError } from "@gramio/types";
2
+ export interface APIErrorDetails {
3
+ method: string;
4
+ params: Record<string, any>;
5
+ }
6
+ export declare class APIError extends Error {
7
+ method: string;
8
+ params: Record<string, any>;
9
+ code: number;
10
+ payload?: TelegramAPIResponseError["parameters"];
11
+ constructor({ method, params }: APIErrorDetails, error: TelegramAPIResponseError);
12
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.APIError = void 0;
4
+ //TODO: more elegant
5
+ class APIError extends Error {
6
+ method;
7
+ params;
8
+ code;
9
+ payload;
10
+ constructor({ method, params }, error) {
11
+ super(error.description);
12
+ this.name = method;
13
+ this.method = method;
14
+ this.params = params;
15
+ this.code = error.error_code;
16
+ //TODO: delete when undefined
17
+ if (error.parameters)
18
+ this.payload = error.parameters;
19
+ }
20
+ }
21
+ exports.APIError = APIError;
@@ -0,0 +1,12 @@
1
+ import type { ApiMethods } from "@gramio/types";
2
+ import "reflect-metadata";
3
+ import { BotOptions } from "./types";
4
+ import { Updates } from "./updates";
5
+ export declare class Bot {
6
+ readonly options: BotOptions;
7
+ readonly api: ApiMethods;
8
+ updates: Updates;
9
+ private _callApi;
10
+ constructor(token: string, options?: Omit<BotOptions, "token">);
11
+ }
12
+ export * from "@gramio/types";
package/dist/index.js ADDED
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
14
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
15
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
16
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
17
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
18
+ };
19
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
20
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.Bot = void 0;
24
+ const files_1 = require("@gramio/files");
25
+ const form_data_encoder_1 = require("form-data-encoder");
26
+ const inspectable_1 = require("inspectable");
27
+ require("reflect-metadata");
28
+ const undici_1 = require("undici");
29
+ const apiErrors_1 = require("./apiErrors");
30
+ const updates_1 = require("./updates");
31
+ let Bot = class Bot {
32
+ options = {};
33
+ api = new Proxy({}, {
34
+ get: (_target, method) => (args) => this._callApi(method, args),
35
+ });
36
+ updates = new updates_1.Updates(this);
37
+ async _callApi(method, params = {}) {
38
+ const url = `https://api.telegram.org/bot${this.options.token}/${method}`;
39
+ const reqOptions = {
40
+ method: "POST",
41
+ duplex: "half",
42
+ };
43
+ if ((0, files_1.isMediaUpload)(method, params)) {
44
+ const formData = await (0, files_1.convertJsonToFormData)(method, params);
45
+ const encoder = new form_data_encoder_1.FormDataEncoder(formData);
46
+ reqOptions.body = encoder.encode();
47
+ reqOptions.headers = encoder.headers;
48
+ }
49
+ else {
50
+ reqOptions.headers = {
51
+ "Content-Type": "application/json",
52
+ };
53
+ reqOptions.body = JSON.stringify(params);
54
+ }
55
+ const response = await (0, undici_1.fetch)(url, reqOptions);
56
+ const data = (await response.json());
57
+ if (!data.ok)
58
+ throw new apiErrors_1.APIError({ method, params }, data);
59
+ return data.result;
60
+ }
61
+ constructor(token, options) {
62
+ this.options = { ...options, token };
63
+ }
64
+ };
65
+ exports.Bot = Bot;
66
+ exports.Bot = Bot = __decorate([
67
+ (0, inspectable_1.Inspectable)({
68
+ serialize: () => ({}),
69
+ })
70
+ ], Bot);
71
+ __exportStar(require("@gramio/types"), exports);
@@ -0,0 +1,7 @@
1
+ import { contextsMappings } from "@gramio/contexts";
2
+ import { NextMiddleware } from "middleware-io";
3
+ export interface BotOptions {
4
+ token?: string;
5
+ }
6
+ export type THandler<T> = (context: T, next: NextMiddleware) => unknown;
7
+ export type UpdateNames = keyof typeof contextsMappings;
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,17 @@
1
+ import { Context, contextsMappings } from "@gramio/contexts";
2
+ import type { TelegramUpdate } from "@gramio/types";
3
+ import { Bot } from ".";
4
+ import { THandler, UpdateNames } from "./types";
5
+ export declare class Updates {
6
+ private readonly bot;
7
+ private isStarted;
8
+ private offset;
9
+ private composer;
10
+ constructor(bot: Bot);
11
+ on<T extends UpdateNames>(updateName: T, handler: THandler<InstanceType<(typeof contextsMappings)[T]>>): this;
12
+ use(handler: THandler<Context>): this;
13
+ handleUpdate(data: TelegramUpdate): Promise<void>;
14
+ startPolling(): Promise<null>;
15
+ startFetchLoop(): Promise<void>;
16
+ stopPolling(): void;
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gramio",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "WIP",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",