@xtsea/tgcore-ts 0.1.5 → 0.1.7

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.d.mts CHANGED
@@ -1,3 +1,53 @@
1
+ type InlineKeyboardButton = {
2
+ text: string;
3
+ url?: string;
4
+ style?: string;
5
+ callback_data?: string;
6
+ web_app?: {
7
+ url: string;
8
+ };
9
+ login_url?: {
10
+ url: string;
11
+ forward_text?: string;
12
+ bot_username?: string;
13
+ request_write_access?: boolean;
14
+ };
15
+ switch_inline_query?: string;
16
+ switch_inline_query_current_chat?: string;
17
+ switch_inline_query_chosen_chat?: {
18
+ query?: string;
19
+ allow_user_chats?: boolean;
20
+ allow_bot_chats?: boolean;
21
+ allow_group_chats?: boolean;
22
+ allow_channel_chats?: boolean;
23
+ };
24
+ copy_text?: {
25
+ text: string;
26
+ };
27
+ };
28
+ type InlineKeyboardMarkup = {
29
+ inline_keyboard: InlineKeyboardButton[][];
30
+ };
31
+ declare class KeyboardBuilder {
32
+ private keyboard;
33
+ private _maxPerRow;
34
+ static inline(): KeyboardBuilder;
35
+ maxPerRow(n: number): this;
36
+ private currentRow;
37
+ row(): this;
38
+ private push;
39
+ url(text: string, url: string): this;
40
+ style(text: string, style: string, url: string): this;
41
+ callback(text: string, data: string): this;
42
+ webApp(text: string, url: string): this;
43
+ loginUrl(text: string, url: string, opts?: Omit<NonNullable<InlineKeyboardButton["login_url"]>, "url">): this;
44
+ switchInline(text: string, query: string): this;
45
+ switchInlineCurrentChat(text: string, query: string): this;
46
+ switchInlineChosenChat(text: string, opts: InlineKeyboardButton["switch_inline_query_chosen_chat"]): this;
47
+ copyText(text: string, copy: string): this;
48
+ build(): InlineKeyboardMarkup;
49
+ }
50
+
1
51
  type HttpOptions = {
2
52
  api_key: string;
3
53
  base_url: string;
@@ -60,4 +110,4 @@ declare class Client {
60
110
  constructor(opts: ClientOptions);
61
111
  }
62
112
 
63
- export { Client, type ClientOptions };
113
+ export { Client, type ClientOptions, type InlineKeyboardButton, type InlineKeyboardMarkup, KeyboardBuilder };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,53 @@
1
+ type InlineKeyboardButton = {
2
+ text: string;
3
+ url?: string;
4
+ style?: string;
5
+ callback_data?: string;
6
+ web_app?: {
7
+ url: string;
8
+ };
9
+ login_url?: {
10
+ url: string;
11
+ forward_text?: string;
12
+ bot_username?: string;
13
+ request_write_access?: boolean;
14
+ };
15
+ switch_inline_query?: string;
16
+ switch_inline_query_current_chat?: string;
17
+ switch_inline_query_chosen_chat?: {
18
+ query?: string;
19
+ allow_user_chats?: boolean;
20
+ allow_bot_chats?: boolean;
21
+ allow_group_chats?: boolean;
22
+ allow_channel_chats?: boolean;
23
+ };
24
+ copy_text?: {
25
+ text: string;
26
+ };
27
+ };
28
+ type InlineKeyboardMarkup = {
29
+ inline_keyboard: InlineKeyboardButton[][];
30
+ };
31
+ declare class KeyboardBuilder {
32
+ private keyboard;
33
+ private _maxPerRow;
34
+ static inline(): KeyboardBuilder;
35
+ maxPerRow(n: number): this;
36
+ private currentRow;
37
+ row(): this;
38
+ private push;
39
+ url(text: string, url: string): this;
40
+ style(text: string, style: string, url: string): this;
41
+ callback(text: string, data: string): this;
42
+ webApp(text: string, url: string): this;
43
+ loginUrl(text: string, url: string, opts?: Omit<NonNullable<InlineKeyboardButton["login_url"]>, "url">): this;
44
+ switchInline(text: string, query: string): this;
45
+ switchInlineCurrentChat(text: string, query: string): this;
46
+ switchInlineChosenChat(text: string, opts: InlineKeyboardButton["switch_inline_query_chosen_chat"]): this;
47
+ copyText(text: string, copy: string): this;
48
+ build(): InlineKeyboardMarkup;
49
+ }
50
+
1
51
  type HttpOptions = {
2
52
  api_key: string;
3
53
  base_url: string;
@@ -60,4 +110,4 @@ declare class Client {
60
110
  constructor(opts: ClientOptions);
61
111
  }
62
112
 
63
- export { Client, type ClientOptions };
113
+ export { Client, type ClientOptions, type InlineKeyboardButton, type InlineKeyboardMarkup, KeyboardBuilder };
package/dist/index.js CHANGED
@@ -20,10 +20,95 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- Client: () => Client
23
+ Client: () => Client,
24
+ KeyboardBuilder: () => KeyboardBuilder
24
25
  });
25
26
  module.exports = __toCommonJS(index_exports);
26
27
 
28
+ // src/builders/keyboard.ts
29
+ var KeyboardBuilder = class _KeyboardBuilder {
30
+ constructor() {
31
+ this.keyboard = [[]];
32
+ this._maxPerRow = null;
33
+ }
34
+ static inline() {
35
+ return new _KeyboardBuilder();
36
+ }
37
+ maxPerRow(n) {
38
+ if (!Number.isInteger(n) || n <= 0) throw new Error("maxPerRow must be a positive integer");
39
+ this._maxPerRow = n;
40
+ return this;
41
+ }
42
+ currentRow() {
43
+ return this.keyboard[this.keyboard.length - 1];
44
+ }
45
+ row() {
46
+ if (this.currentRow().length === 0) return this;
47
+ this.keyboard.push([]);
48
+ return this;
49
+ }
50
+ push(btn) {
51
+ var _a;
52
+ if (!((_a = btn.text) == null ? void 0 : _a.trim())) throw new Error("Button text is required");
53
+ if (btn.text.length > 64) throw new Error("Button text too long (max 64 chars)");
54
+ const actionKeys = [
55
+ "url",
56
+ "callback_data",
57
+ "web_app",
58
+ "login_url",
59
+ "switch_inline_query",
60
+ "switch_inline_query_current_chat",
61
+ "switch_inline_query_chosen_chat",
62
+ "copy_text"
63
+ ];
64
+ const used = actionKeys.filter((k) => btn[k] != null);
65
+ if (used.length !== 1) {
66
+ throw new Error(`InlineKeyboardButton must have exactly 1 action field, got: ${used.join(", ") || "none"}`);
67
+ }
68
+ if (btn.callback_data && btn.callback_data.length > 64) {
69
+ throw new Error("callback_data too long (max 64 chars)");
70
+ }
71
+ if (this._maxPerRow && this.currentRow().length >= this._maxPerRow) {
72
+ this.row();
73
+ }
74
+ this.currentRow().push(btn);
75
+ return this;
76
+ }
77
+ url(text, url) {
78
+ return this.push({ text, url });
79
+ }
80
+ style(text, style, url) {
81
+ return this.push({ text, style, url });
82
+ }
83
+ callback(text, data) {
84
+ return this.push({ text, callback_data: data });
85
+ }
86
+ webApp(text, url) {
87
+ return this.push({ text, web_app: { url } });
88
+ }
89
+ loginUrl(text, url, opts) {
90
+ return this.push({ text, login_url: { url, ...opts != null ? opts : {} } });
91
+ }
92
+ switchInline(text, query) {
93
+ return this.push({ text, switch_inline_query: query });
94
+ }
95
+ switchInlineCurrentChat(text, query) {
96
+ return this.push({ text, switch_inline_query_current_chat: query });
97
+ }
98
+ switchInlineChosenChat(text, opts) {
99
+ if (!opts) throw new Error("switch_inline_query_chosen_chat options required");
100
+ return this.push({ text, switch_inline_query_chosen_chat: opts });
101
+ }
102
+ copyText(text, copy) {
103
+ return this.push({ text, copy_text: { text: copy } });
104
+ }
105
+ build() {
106
+ const cleaned = this.keyboard.filter((r) => r.length > 0);
107
+ if (cleaned.length === 0) throw new Error("Keyboard is empty");
108
+ return { inline_keyboard: cleaned };
109
+ }
110
+ };
111
+
27
112
  // src/http.ts
28
113
  var Http = class {
29
114
  constructor(opts) {
@@ -39,7 +124,7 @@ var Http = class {
39
124
  "content-type": "application/json",
40
125
  "x-api-key": this.opts.api_key
41
126
  },
42
- body: JSON.stringify(body ?? {}),
127
+ body: JSON.stringify(body != null ? body : {}),
43
128
  signal: controller.signal
44
129
  });
45
130
  const json = await res.json().catch(() => null);
@@ -84,7 +169,7 @@ var BaseCallBuilder = class {
84
169
  }
85
170
  async throw() {
86
171
  const res = await this.execute();
87
- if (!res?.ok) {
172
+ if (!(res == null ? void 0 : res.ok)) {
88
173
  throw new Error(JSON.stringify(res));
89
174
  }
90
175
  return res;
@@ -135,13 +220,14 @@ var CallMethods = class {
135
220
  // src/client.ts
136
221
  var Client = class {
137
222
  constructor(opts) {
138
- if (!opts?.api_key) {
223
+ var _a, _b;
224
+ if (!(opts == null ? void 0 : opts.api_key)) {
139
225
  throw new Error("tgcore-ts: api_key is required");
140
226
  }
141
227
  this.http = new Http({
142
228
  api_key: opts.api_key,
143
- base_url: opts.base_url ?? "https://services-pro.ryzenths.dpdns.org",
144
- timeout_ms: opts.timeout_ms ?? 3e4
229
+ base_url: (_a = opts.base_url) != null ? _a : "https://services-pro.ryzenths.dpdns.org",
230
+ timeout_ms: (_b = opts.timeout_ms) != null ? _b : 3e4
145
231
  });
146
232
  this.calls = new CallMethods(this.http);
147
233
  this.raw = new RawMethods(this.http);
@@ -149,5 +235,6 @@ var Client = class {
149
235
  };
150
236
  // Annotate the CommonJS export names for ESM import in node:
151
237
  0 && (module.exports = {
152
- Client
238
+ Client,
239
+ KeyboardBuilder
153
240
  });
package/dist/index.mjs CHANGED
@@ -1,3 +1,87 @@
1
+ // src/builders/keyboard.ts
2
+ var KeyboardBuilder = class _KeyboardBuilder {
3
+ constructor() {
4
+ this.keyboard = [[]];
5
+ this._maxPerRow = null;
6
+ }
7
+ static inline() {
8
+ return new _KeyboardBuilder();
9
+ }
10
+ maxPerRow(n) {
11
+ if (!Number.isInteger(n) || n <= 0) throw new Error("maxPerRow must be a positive integer");
12
+ this._maxPerRow = n;
13
+ return this;
14
+ }
15
+ currentRow() {
16
+ return this.keyboard[this.keyboard.length - 1];
17
+ }
18
+ row() {
19
+ if (this.currentRow().length === 0) return this;
20
+ this.keyboard.push([]);
21
+ return this;
22
+ }
23
+ push(btn) {
24
+ var _a;
25
+ if (!((_a = btn.text) == null ? void 0 : _a.trim())) throw new Error("Button text is required");
26
+ if (btn.text.length > 64) throw new Error("Button text too long (max 64 chars)");
27
+ const actionKeys = [
28
+ "url",
29
+ "callback_data",
30
+ "web_app",
31
+ "login_url",
32
+ "switch_inline_query",
33
+ "switch_inline_query_current_chat",
34
+ "switch_inline_query_chosen_chat",
35
+ "copy_text"
36
+ ];
37
+ const used = actionKeys.filter((k) => btn[k] != null);
38
+ if (used.length !== 1) {
39
+ throw new Error(`InlineKeyboardButton must have exactly 1 action field, got: ${used.join(", ") || "none"}`);
40
+ }
41
+ if (btn.callback_data && btn.callback_data.length > 64) {
42
+ throw new Error("callback_data too long (max 64 chars)");
43
+ }
44
+ if (this._maxPerRow && this.currentRow().length >= this._maxPerRow) {
45
+ this.row();
46
+ }
47
+ this.currentRow().push(btn);
48
+ return this;
49
+ }
50
+ url(text, url) {
51
+ return this.push({ text, url });
52
+ }
53
+ style(text, style, url) {
54
+ return this.push({ text, style, url });
55
+ }
56
+ callback(text, data) {
57
+ return this.push({ text, callback_data: data });
58
+ }
59
+ webApp(text, url) {
60
+ return this.push({ text, web_app: { url } });
61
+ }
62
+ loginUrl(text, url, opts) {
63
+ return this.push({ text, login_url: { url, ...opts != null ? opts : {} } });
64
+ }
65
+ switchInline(text, query) {
66
+ return this.push({ text, switch_inline_query: query });
67
+ }
68
+ switchInlineCurrentChat(text, query) {
69
+ return this.push({ text, switch_inline_query_current_chat: query });
70
+ }
71
+ switchInlineChosenChat(text, opts) {
72
+ if (!opts) throw new Error("switch_inline_query_chosen_chat options required");
73
+ return this.push({ text, switch_inline_query_chosen_chat: opts });
74
+ }
75
+ copyText(text, copy) {
76
+ return this.push({ text, copy_text: { text: copy } });
77
+ }
78
+ build() {
79
+ const cleaned = this.keyboard.filter((r) => r.length > 0);
80
+ if (cleaned.length === 0) throw new Error("Keyboard is empty");
81
+ return { inline_keyboard: cleaned };
82
+ }
83
+ };
84
+
1
85
  // src/http.ts
2
86
  var Http = class {
3
87
  constructor(opts) {
@@ -13,7 +97,7 @@ var Http = class {
13
97
  "content-type": "application/json",
14
98
  "x-api-key": this.opts.api_key
15
99
  },
16
- body: JSON.stringify(body ?? {}),
100
+ body: JSON.stringify(body != null ? body : {}),
17
101
  signal: controller.signal
18
102
  });
19
103
  const json = await res.json().catch(() => null);
@@ -58,7 +142,7 @@ var BaseCallBuilder = class {
58
142
  }
59
143
  async throw() {
60
144
  const res = await this.execute();
61
- if (!res?.ok) {
145
+ if (!(res == null ? void 0 : res.ok)) {
62
146
  throw new Error(JSON.stringify(res));
63
147
  }
64
148
  return res;
@@ -109,18 +193,20 @@ var CallMethods = class {
109
193
  // src/client.ts
110
194
  var Client = class {
111
195
  constructor(opts) {
112
- if (!opts?.api_key) {
196
+ var _a, _b;
197
+ if (!(opts == null ? void 0 : opts.api_key)) {
113
198
  throw new Error("tgcore-ts: api_key is required");
114
199
  }
115
200
  this.http = new Http({
116
201
  api_key: opts.api_key,
117
- base_url: opts.base_url ?? "https://services-pro.ryzenths.dpdns.org",
118
- timeout_ms: opts.timeout_ms ?? 3e4
202
+ base_url: (_a = opts.base_url) != null ? _a : "https://services-pro.ryzenths.dpdns.org",
203
+ timeout_ms: (_b = opts.timeout_ms) != null ? _b : 3e4
119
204
  });
120
205
  this.calls = new CallMethods(this.http);
121
206
  this.raw = new RawMethods(this.http);
122
207
  }
123
208
  };
124
209
  export {
125
- Client
210
+ Client,
211
+ KeyboardBuilder
126
212
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xtsea/tgcore-ts",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "tgcore TypeScript SDK • Telegram Engine Client",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",