@xtsea/tgcore-ts 0.1.10 → 0.1.13

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/README.md CHANGED
@@ -34,16 +34,30 @@ npm install @xtsea/tgcore-ts
34
34
  ## Getting started
35
35
 
36
36
  ```ts
37
- import { tgcore } from "@xtsea/tgcore-ts"
37
+ import { tgcore, KeyboardBuilder } from "@xtsea/tgcore-ts"
38
38
 
39
39
  // old version: 0.1.9 new Client({})
40
40
 
41
- const tg = tgcore({ api_key: "fw_live_xxx" }) // version: 0.1.10
41
+ const tg = tgcore({ api_key: "fw_live_xxx" }) // latest version
42
42
 
43
43
  await tg.raw.sendMessage({
44
44
  chat_id: -1001234567890,
45
45
  text: "Hello from tgcore-ts"
46
46
  })
47
+
48
+
49
+ const keyboard = KeyboardBuilder
50
+ .inline()
51
+ .callback("Yes", "yes")
52
+ .callback("No", "no")
53
+ .build()
54
+
55
+ await tg.calls
56
+ .sendMessage()
57
+ .chatId(123)
58
+ .text("Confirm?")
59
+ .replyMarkup(keyboard)
60
+ .execute()
47
61
  ```
48
62
 
49
63
  ## Why TGCore?
package/dist/index.cjs CHANGED
@@ -31,17 +31,17 @@ var Http = class {
31
31
  constructor(opts) {
32
32
  this.opts = opts;
33
33
  }
34
- async post(path, body) {
34
+ async post(ctx) {
35
35
  const controller = new AbortController();
36
36
  const timeout = setTimeout(() => controller.abort(), this.opts.timeout_ms);
37
37
  try {
38
- const res = await fetch(this.opts.base_url + path, {
38
+ const res = await fetch(this.opts.base_url + ctx.path, {
39
39
  method: "POST",
40
40
  headers: {
41
41
  "content-type": "application/json",
42
42
  "x-api-key": this.opts.api_key
43
43
  },
44
- body: JSON.stringify(body ?? {}),
44
+ body: JSON.stringify(ctx.body ?? {}),
45
45
  signal: controller.signal
46
46
  });
47
47
  const json = await res.json().catch(() => null);
@@ -61,18 +61,25 @@ var RawMethods = class {
61
61
  this.http = http;
62
62
  }
63
63
  sendMessage(params) {
64
- return this.http.post("/api/v2/sendMessage", params);
64
+ return this.http.post({
65
+ path: "/api/v2/sendMessage",
66
+ body: params
67
+ });
65
68
  }
66
69
  getMe() {
67
- return this.http.post("/api/v2/getMe", {});
70
+ return this.http.post({
71
+ path: "/api/v2/getMe",
72
+ body: {}
73
+ });
68
74
  }
69
75
  };
70
76
 
71
77
  // src/calls/base.ts
72
78
  var BaseCallBuilder = class {
73
- constructor(http, path) {
74
- this.http = http;
79
+ constructor(client, path, method = "POST") {
80
+ this.client = client;
75
81
  this.path = path;
82
+ this.method = method;
76
83
  this.params = {};
77
84
  }
78
85
  set(key, value) {
@@ -82,7 +89,7 @@ var BaseCallBuilder = class {
82
89
  return this;
83
90
  }
84
91
  async execute() {
85
- return this.http.post(this.path, this.params);
92
+ return this.client.request(this.path, this.params);
86
93
  }
87
94
  async throw() {
88
95
  const res = await this.execute();
@@ -121,17 +128,48 @@ var SendMessageBuilder = class extends BaseCallBuilder {
121
128
  }
122
129
  };
123
130
 
131
+ // src/calls/sendPhoto.ts
132
+ var SendPhotoBuilder = class extends BaseCallBuilder {
133
+ chatId(id) {
134
+ return this.set("chat_id", id);
135
+ }
136
+ photo(url) {
137
+ return this.set("photo", url);
138
+ }
139
+ caption(caption) {
140
+ return this.set("caption", caption);
141
+ }
142
+ parseMode(mode) {
143
+ return this.set("parse_mode", mode);
144
+ }
145
+ disableNotification(value) {
146
+ return this.set("disable_notification", value);
147
+ }
148
+ protectContent(value) {
149
+ return this.set("protect_content", value);
150
+ }
151
+ replyMarkup(markup) {
152
+ return this.set("reply_markup", markup);
153
+ }
154
+ };
155
+
124
156
  // src/calls/index.ts
125
157
  var CallMethods = class {
126
- constructor(http) {
127
- this.http = http;
158
+ constructor(client) {
159
+ this.client = client;
128
160
  }
129
161
  sendMessage() {
130
162
  return new SendMessageBuilder(
131
- this.http,
163
+ this.client,
132
164
  "/api/v2/sendMessage"
133
165
  );
134
166
  }
167
+ sendPhoto() {
168
+ return new SendPhotoBuilder(
169
+ this.client,
170
+ "/api/v2/sendPhoto"
171
+ );
172
+ }
135
173
  };
136
174
 
137
175
  // src/client.ts
@@ -140,6 +178,7 @@ function tgcore(options) {
140
178
  }
141
179
  var Client = class {
142
180
  constructor(opts) {
181
+ this.middlewares = [];
143
182
  if (!opts?.api_key) {
144
183
  throw new Error("tgcore-ts: api_key is required");
145
184
  }
@@ -151,6 +190,13 @@ var Client = class {
151
190
  this.calls = new CallMethods(this.http);
152
191
  this.raw = new RawMethods(this.http);
153
192
  }
193
+ use(mw) {
194
+ this.middlewares.push(mw);
195
+ return this;
196
+ }
197
+ async request(path, body) {
198
+ return this.http.post({ path, body });
199
+ }
154
200
  };
155
201
 
156
202
  // src/builders/keyboard.ts
@@ -201,6 +247,12 @@ var KeyboardBuilder = class _KeyboardBuilder {
201
247
  this.currentRow().push(btn);
202
248
  return this;
203
249
  }
250
+ callbackJson(text, obj) {
251
+ return this.callback(text, JSON.stringify(obj));
252
+ }
253
+ button(btn) {
254
+ return this.push(btn);
255
+ }
204
256
  url(text, url) {
205
257
  return this.push({ text, url });
206
258
  }
package/dist/index.d.cts CHANGED
@@ -3,10 +3,14 @@ type HttpOptions = {
3
3
  base_url: string;
4
4
  timeout_ms: number;
5
5
  };
6
+ type HttpContext = {
7
+ path: string;
8
+ body?: any;
9
+ };
6
10
  declare class Http {
7
11
  private opts;
8
12
  constructor(opts: HttpOptions);
9
- post(path: string, body: any): Promise<any>;
13
+ post(ctx: HttpContext): Promise<any>;
10
14
  }
11
15
 
12
16
  declare class RawMethods {
@@ -22,10 +26,11 @@ declare class RawMethods {
22
26
  }
23
27
 
24
28
  declare abstract class BaseCallBuilder<T = any> {
25
- protected http: any;
29
+ protected client: any;
26
30
  protected path: string;
31
+ protected method: "POST" | "GET";
27
32
  protected params: Record<string, any>;
28
- constructor(http: any, path: string);
33
+ constructor(client: any, path: string, method?: "POST" | "GET");
29
34
  protected set(key: string, value: any): this;
30
35
  execute(): Promise<T>;
31
36
  throw(): Promise<T>;
@@ -42,10 +47,21 @@ declare class SendMessageBuilder extends BaseCallBuilder {
42
47
  linkPreviewOptions(options: any): this;
43
48
  }
44
49
 
50
+ declare class SendPhotoBuilder extends BaseCallBuilder {
51
+ chatId(id: string | number): this;
52
+ photo(url: string): this;
53
+ caption(caption: string): this;
54
+ parseMode(mode: "HTML" | "Markdown" | "MarkdownV2"): this;
55
+ disableNotification(value: boolean): this;
56
+ protectContent(value: boolean): this;
57
+ replyMarkup(markup: any): this;
58
+ }
59
+
45
60
  declare class CallMethods {
46
- private http;
47
- constructor(http: any);
61
+ private client;
62
+ constructor(client: any);
48
63
  sendMessage(): SendMessageBuilder;
64
+ sendPhoto(): SendPhotoBuilder;
49
65
  }
50
66
 
51
67
  type ClientOptions = {
@@ -53,11 +69,21 @@ type ClientOptions = {
53
69
  base_url?: string;
54
70
  timeout_ms?: number;
55
71
  };
72
+
73
+ type MiddlewareContext = {
74
+ method: string;
75
+ payload?: any;
76
+ };
77
+ type Middleware = (ctx: MiddlewareContext, next: () => Promise<any>) => Promise<any>;
78
+
56
79
  declare function tgcore(options: ClientOptions): Client;
57
80
  declare class Client {
58
81
  private http;
82
+ private middlewares;
59
83
  raw: RawMethods;
60
84
  calls: CallMethods;
85
+ use(mw: Middleware): this;
86
+ request(path: string, body?: any): Promise<any>;
61
87
  constructor(opts: ClientOptions);
62
88
  }
63
89
 
@@ -88,9 +114,11 @@ type InlineKeyboardButton = {
88
114
  text: string;
89
115
  };
90
116
  };
117
+
91
118
  type InlineKeyboardMarkup = {
92
119
  inline_keyboard: InlineKeyboardButton[][];
93
120
  };
121
+
94
122
  declare class KeyboardBuilder {
95
123
  private keyboard;
96
124
  private _maxPerRow;
@@ -99,6 +127,8 @@ declare class KeyboardBuilder {
99
127
  private currentRow;
100
128
  row(): this;
101
129
  private push;
130
+ callbackJson(text: string, obj: any): this;
131
+ button(btn: InlineKeyboardButton): this;
102
132
  url(text: string, url: string): this;
103
133
  style(text: string, style: string, url: string): this;
104
134
  callback(text: string, data: string): this;
package/dist/index.d.ts CHANGED
@@ -3,10 +3,14 @@ type HttpOptions = {
3
3
  base_url: string;
4
4
  timeout_ms: number;
5
5
  };
6
+ type HttpContext = {
7
+ path: string;
8
+ body?: any;
9
+ };
6
10
  declare class Http {
7
11
  private opts;
8
12
  constructor(opts: HttpOptions);
9
- post(path: string, body: any): Promise<any>;
13
+ post(ctx: HttpContext): Promise<any>;
10
14
  }
11
15
 
12
16
  declare class RawMethods {
@@ -22,10 +26,11 @@ declare class RawMethods {
22
26
  }
23
27
 
24
28
  declare abstract class BaseCallBuilder<T = any> {
25
- protected http: any;
29
+ protected client: any;
26
30
  protected path: string;
31
+ protected method: "POST" | "GET";
27
32
  protected params: Record<string, any>;
28
- constructor(http: any, path: string);
33
+ constructor(client: any, path: string, method?: "POST" | "GET");
29
34
  protected set(key: string, value: any): this;
30
35
  execute(): Promise<T>;
31
36
  throw(): Promise<T>;
@@ -42,10 +47,21 @@ declare class SendMessageBuilder extends BaseCallBuilder {
42
47
  linkPreviewOptions(options: any): this;
43
48
  }
44
49
 
50
+ declare class SendPhotoBuilder extends BaseCallBuilder {
51
+ chatId(id: string | number): this;
52
+ photo(url: string): this;
53
+ caption(caption: string): this;
54
+ parseMode(mode: "HTML" | "Markdown" | "MarkdownV2"): this;
55
+ disableNotification(value: boolean): this;
56
+ protectContent(value: boolean): this;
57
+ replyMarkup(markup: any): this;
58
+ }
59
+
45
60
  declare class CallMethods {
46
- private http;
47
- constructor(http: any);
61
+ private client;
62
+ constructor(client: any);
48
63
  sendMessage(): SendMessageBuilder;
64
+ sendPhoto(): SendPhotoBuilder;
49
65
  }
50
66
 
51
67
  type ClientOptions = {
@@ -53,11 +69,21 @@ type ClientOptions = {
53
69
  base_url?: string;
54
70
  timeout_ms?: number;
55
71
  };
72
+
73
+ type MiddlewareContext = {
74
+ method: string;
75
+ payload?: any;
76
+ };
77
+ type Middleware = (ctx: MiddlewareContext, next: () => Promise<any>) => Promise<any>;
78
+
56
79
  declare function tgcore(options: ClientOptions): Client;
57
80
  declare class Client {
58
81
  private http;
82
+ private middlewares;
59
83
  raw: RawMethods;
60
84
  calls: CallMethods;
85
+ use(mw: Middleware): this;
86
+ request(path: string, body?: any): Promise<any>;
61
87
  constructor(opts: ClientOptions);
62
88
  }
63
89
 
@@ -88,9 +114,11 @@ type InlineKeyboardButton = {
88
114
  text: string;
89
115
  };
90
116
  };
117
+
91
118
  type InlineKeyboardMarkup = {
92
119
  inline_keyboard: InlineKeyboardButton[][];
93
120
  };
121
+
94
122
  declare class KeyboardBuilder {
95
123
  private keyboard;
96
124
  private _maxPerRow;
@@ -99,6 +127,8 @@ declare class KeyboardBuilder {
99
127
  private currentRow;
100
128
  row(): this;
101
129
  private push;
130
+ callbackJson(text: string, obj: any): this;
131
+ button(btn: InlineKeyboardButton): this;
102
132
  url(text: string, url: string): this;
103
133
  style(text: string, style: string, url: string): this;
104
134
  callback(text: string, data: string): this;
package/dist/index.js CHANGED
@@ -3,17 +3,17 @@ var Http = class {
3
3
  constructor(opts) {
4
4
  this.opts = opts;
5
5
  }
6
- async post(path, body) {
6
+ async post(ctx) {
7
7
  const controller = new AbortController();
8
8
  const timeout = setTimeout(() => controller.abort(), this.opts.timeout_ms);
9
9
  try {
10
- const res = await fetch(this.opts.base_url + path, {
10
+ const res = await fetch(this.opts.base_url + ctx.path, {
11
11
  method: "POST",
12
12
  headers: {
13
13
  "content-type": "application/json",
14
14
  "x-api-key": this.opts.api_key
15
15
  },
16
- body: JSON.stringify(body ?? {}),
16
+ body: JSON.stringify(ctx.body ?? {}),
17
17
  signal: controller.signal
18
18
  });
19
19
  const json = await res.json().catch(() => null);
@@ -33,18 +33,25 @@ var RawMethods = class {
33
33
  this.http = http;
34
34
  }
35
35
  sendMessage(params) {
36
- return this.http.post("/api/v2/sendMessage", params);
36
+ return this.http.post({
37
+ path: "/api/v2/sendMessage",
38
+ body: params
39
+ });
37
40
  }
38
41
  getMe() {
39
- return this.http.post("/api/v2/getMe", {});
42
+ return this.http.post({
43
+ path: "/api/v2/getMe",
44
+ body: {}
45
+ });
40
46
  }
41
47
  };
42
48
 
43
49
  // src/calls/base.ts
44
50
  var BaseCallBuilder = class {
45
- constructor(http, path) {
46
- this.http = http;
51
+ constructor(client, path, method = "POST") {
52
+ this.client = client;
47
53
  this.path = path;
54
+ this.method = method;
48
55
  this.params = {};
49
56
  }
50
57
  set(key, value) {
@@ -54,7 +61,7 @@ var BaseCallBuilder = class {
54
61
  return this;
55
62
  }
56
63
  async execute() {
57
- return this.http.post(this.path, this.params);
64
+ return this.client.request(this.path, this.params);
58
65
  }
59
66
  async throw() {
60
67
  const res = await this.execute();
@@ -93,17 +100,48 @@ var SendMessageBuilder = class extends BaseCallBuilder {
93
100
  }
94
101
  };
95
102
 
103
+ // src/calls/sendPhoto.ts
104
+ var SendPhotoBuilder = class extends BaseCallBuilder {
105
+ chatId(id) {
106
+ return this.set("chat_id", id);
107
+ }
108
+ photo(url) {
109
+ return this.set("photo", url);
110
+ }
111
+ caption(caption) {
112
+ return this.set("caption", caption);
113
+ }
114
+ parseMode(mode) {
115
+ return this.set("parse_mode", mode);
116
+ }
117
+ disableNotification(value) {
118
+ return this.set("disable_notification", value);
119
+ }
120
+ protectContent(value) {
121
+ return this.set("protect_content", value);
122
+ }
123
+ replyMarkup(markup) {
124
+ return this.set("reply_markup", markup);
125
+ }
126
+ };
127
+
96
128
  // src/calls/index.ts
97
129
  var CallMethods = class {
98
- constructor(http) {
99
- this.http = http;
130
+ constructor(client) {
131
+ this.client = client;
100
132
  }
101
133
  sendMessage() {
102
134
  return new SendMessageBuilder(
103
- this.http,
135
+ this.client,
104
136
  "/api/v2/sendMessage"
105
137
  );
106
138
  }
139
+ sendPhoto() {
140
+ return new SendPhotoBuilder(
141
+ this.client,
142
+ "/api/v2/sendPhoto"
143
+ );
144
+ }
107
145
  };
108
146
 
109
147
  // src/client.ts
@@ -112,6 +150,7 @@ function tgcore(options) {
112
150
  }
113
151
  var Client = class {
114
152
  constructor(opts) {
153
+ this.middlewares = [];
115
154
  if (!opts?.api_key) {
116
155
  throw new Error("tgcore-ts: api_key is required");
117
156
  }
@@ -123,6 +162,13 @@ var Client = class {
123
162
  this.calls = new CallMethods(this.http);
124
163
  this.raw = new RawMethods(this.http);
125
164
  }
165
+ use(mw) {
166
+ this.middlewares.push(mw);
167
+ return this;
168
+ }
169
+ async request(path, body) {
170
+ return this.http.post({ path, body });
171
+ }
126
172
  };
127
173
 
128
174
  // src/builders/keyboard.ts
@@ -173,6 +219,12 @@ var KeyboardBuilder = class _KeyboardBuilder {
173
219
  this.currentRow().push(btn);
174
220
  return this;
175
221
  }
222
+ callbackJson(text, obj) {
223
+ return this.callback(text, JSON.stringify(obj));
224
+ }
225
+ button(btn) {
226
+ return this.push(btn);
227
+ }
176
228
  url(text, url) {
177
229
  return this.push({ text, url });
178
230
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xtsea/tgcore-ts",
3
- "version": "0.1.10",
3
+ "version": "0.1.13",
4
4
  "description": "tgcore TypeScript SDK • Telegram Engine Client",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",