@xtsea/tgcore-ts 0.1.11 → 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 +16 -2
- package/dist/index.cjs +33 -12
- package/dist/index.d.cts +24 -5
- package/dist/index.d.ts +24 -5
- package/dist/index.js +33 -12
- package/package.json +1 -1
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
|
|
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(
|
|
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(
|
|
64
|
+
return this.http.post({
|
|
65
|
+
path: "/api/v2/sendMessage",
|
|
66
|
+
body: params
|
|
67
|
+
});
|
|
65
68
|
}
|
|
66
69
|
getMe() {
|
|
67
|
-
return this.http.post(
|
|
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(
|
|
74
|
-
this.
|
|
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.
|
|
92
|
+
return this.client.request(this.path, this.params);
|
|
86
93
|
}
|
|
87
94
|
async throw() {
|
|
88
95
|
const res = await this.execute();
|
|
@@ -148,18 +155,18 @@ var SendPhotoBuilder = class extends BaseCallBuilder {
|
|
|
148
155
|
|
|
149
156
|
// src/calls/index.ts
|
|
150
157
|
var CallMethods = class {
|
|
151
|
-
constructor(
|
|
152
|
-
this.
|
|
158
|
+
constructor(client) {
|
|
159
|
+
this.client = client;
|
|
153
160
|
}
|
|
154
161
|
sendMessage() {
|
|
155
162
|
return new SendMessageBuilder(
|
|
156
|
-
this.
|
|
163
|
+
this.client,
|
|
157
164
|
"/api/v2/sendMessage"
|
|
158
165
|
);
|
|
159
166
|
}
|
|
160
167
|
sendPhoto() {
|
|
161
168
|
return new SendPhotoBuilder(
|
|
162
|
-
this.
|
|
169
|
+
this.client,
|
|
163
170
|
"/api/v2/sendPhoto"
|
|
164
171
|
);
|
|
165
172
|
}
|
|
@@ -171,6 +178,7 @@ function tgcore(options) {
|
|
|
171
178
|
}
|
|
172
179
|
var Client = class {
|
|
173
180
|
constructor(opts) {
|
|
181
|
+
this.middlewares = [];
|
|
174
182
|
if (!opts?.api_key) {
|
|
175
183
|
throw new Error("tgcore-ts: api_key is required");
|
|
176
184
|
}
|
|
@@ -182,6 +190,13 @@ var Client = class {
|
|
|
182
190
|
this.calls = new CallMethods(this.http);
|
|
183
191
|
this.raw = new RawMethods(this.http);
|
|
184
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
|
+
}
|
|
185
200
|
};
|
|
186
201
|
|
|
187
202
|
// src/builders/keyboard.ts
|
|
@@ -232,6 +247,12 @@ var KeyboardBuilder = class _KeyboardBuilder {
|
|
|
232
247
|
this.currentRow().push(btn);
|
|
233
248
|
return this;
|
|
234
249
|
}
|
|
250
|
+
callbackJson(text, obj) {
|
|
251
|
+
return this.callback(text, JSON.stringify(obj));
|
|
252
|
+
}
|
|
253
|
+
button(btn) {
|
|
254
|
+
return this.push(btn);
|
|
255
|
+
}
|
|
235
256
|
url(text, url) {
|
|
236
257
|
return this.push({ text, url });
|
|
237
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(
|
|
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
|
|
29
|
+
protected client: any;
|
|
26
30
|
protected path: string;
|
|
31
|
+
protected method: "POST" | "GET";
|
|
27
32
|
protected params: Record<string, any>;
|
|
28
|
-
constructor(
|
|
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>;
|
|
@@ -53,8 +58,8 @@ declare class SendPhotoBuilder extends BaseCallBuilder {
|
|
|
53
58
|
}
|
|
54
59
|
|
|
55
60
|
declare class CallMethods {
|
|
56
|
-
private
|
|
57
|
-
constructor(
|
|
61
|
+
private client;
|
|
62
|
+
constructor(client: any);
|
|
58
63
|
sendMessage(): SendMessageBuilder;
|
|
59
64
|
sendPhoto(): SendPhotoBuilder;
|
|
60
65
|
}
|
|
@@ -64,11 +69,21 @@ type ClientOptions = {
|
|
|
64
69
|
base_url?: string;
|
|
65
70
|
timeout_ms?: number;
|
|
66
71
|
};
|
|
72
|
+
|
|
73
|
+
type MiddlewareContext = {
|
|
74
|
+
method: string;
|
|
75
|
+
payload?: any;
|
|
76
|
+
};
|
|
77
|
+
type Middleware = (ctx: MiddlewareContext, next: () => Promise<any>) => Promise<any>;
|
|
78
|
+
|
|
67
79
|
declare function tgcore(options: ClientOptions): Client;
|
|
68
80
|
declare class Client {
|
|
69
81
|
private http;
|
|
82
|
+
private middlewares;
|
|
70
83
|
raw: RawMethods;
|
|
71
84
|
calls: CallMethods;
|
|
85
|
+
use(mw: Middleware): this;
|
|
86
|
+
request(path: string, body?: any): Promise<any>;
|
|
72
87
|
constructor(opts: ClientOptions);
|
|
73
88
|
}
|
|
74
89
|
|
|
@@ -99,9 +114,11 @@ type InlineKeyboardButton = {
|
|
|
99
114
|
text: string;
|
|
100
115
|
};
|
|
101
116
|
};
|
|
117
|
+
|
|
102
118
|
type InlineKeyboardMarkup = {
|
|
103
119
|
inline_keyboard: InlineKeyboardButton[][];
|
|
104
120
|
};
|
|
121
|
+
|
|
105
122
|
declare class KeyboardBuilder {
|
|
106
123
|
private keyboard;
|
|
107
124
|
private _maxPerRow;
|
|
@@ -110,6 +127,8 @@ declare class KeyboardBuilder {
|
|
|
110
127
|
private currentRow;
|
|
111
128
|
row(): this;
|
|
112
129
|
private push;
|
|
130
|
+
callbackJson(text: string, obj: any): this;
|
|
131
|
+
button(btn: InlineKeyboardButton): this;
|
|
113
132
|
url(text: string, url: string): this;
|
|
114
133
|
style(text: string, style: string, url: string): this;
|
|
115
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(
|
|
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
|
|
29
|
+
protected client: any;
|
|
26
30
|
protected path: string;
|
|
31
|
+
protected method: "POST" | "GET";
|
|
27
32
|
protected params: Record<string, any>;
|
|
28
|
-
constructor(
|
|
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>;
|
|
@@ -53,8 +58,8 @@ declare class SendPhotoBuilder extends BaseCallBuilder {
|
|
|
53
58
|
}
|
|
54
59
|
|
|
55
60
|
declare class CallMethods {
|
|
56
|
-
private
|
|
57
|
-
constructor(
|
|
61
|
+
private client;
|
|
62
|
+
constructor(client: any);
|
|
58
63
|
sendMessage(): SendMessageBuilder;
|
|
59
64
|
sendPhoto(): SendPhotoBuilder;
|
|
60
65
|
}
|
|
@@ -64,11 +69,21 @@ type ClientOptions = {
|
|
|
64
69
|
base_url?: string;
|
|
65
70
|
timeout_ms?: number;
|
|
66
71
|
};
|
|
72
|
+
|
|
73
|
+
type MiddlewareContext = {
|
|
74
|
+
method: string;
|
|
75
|
+
payload?: any;
|
|
76
|
+
};
|
|
77
|
+
type Middleware = (ctx: MiddlewareContext, next: () => Promise<any>) => Promise<any>;
|
|
78
|
+
|
|
67
79
|
declare function tgcore(options: ClientOptions): Client;
|
|
68
80
|
declare class Client {
|
|
69
81
|
private http;
|
|
82
|
+
private middlewares;
|
|
70
83
|
raw: RawMethods;
|
|
71
84
|
calls: CallMethods;
|
|
85
|
+
use(mw: Middleware): this;
|
|
86
|
+
request(path: string, body?: any): Promise<any>;
|
|
72
87
|
constructor(opts: ClientOptions);
|
|
73
88
|
}
|
|
74
89
|
|
|
@@ -99,9 +114,11 @@ type InlineKeyboardButton = {
|
|
|
99
114
|
text: string;
|
|
100
115
|
};
|
|
101
116
|
};
|
|
117
|
+
|
|
102
118
|
type InlineKeyboardMarkup = {
|
|
103
119
|
inline_keyboard: InlineKeyboardButton[][];
|
|
104
120
|
};
|
|
121
|
+
|
|
105
122
|
declare class KeyboardBuilder {
|
|
106
123
|
private keyboard;
|
|
107
124
|
private _maxPerRow;
|
|
@@ -110,6 +127,8 @@ declare class KeyboardBuilder {
|
|
|
110
127
|
private currentRow;
|
|
111
128
|
row(): this;
|
|
112
129
|
private push;
|
|
130
|
+
callbackJson(text: string, obj: any): this;
|
|
131
|
+
button(btn: InlineKeyboardButton): this;
|
|
113
132
|
url(text: string, url: string): this;
|
|
114
133
|
style(text: string, style: string, url: string): this;
|
|
115
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(
|
|
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(
|
|
36
|
+
return this.http.post({
|
|
37
|
+
path: "/api/v2/sendMessage",
|
|
38
|
+
body: params
|
|
39
|
+
});
|
|
37
40
|
}
|
|
38
41
|
getMe() {
|
|
39
|
-
return this.http.post(
|
|
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(
|
|
46
|
-
this.
|
|
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.
|
|
64
|
+
return this.client.request(this.path, this.params);
|
|
58
65
|
}
|
|
59
66
|
async throw() {
|
|
60
67
|
const res = await this.execute();
|
|
@@ -120,18 +127,18 @@ var SendPhotoBuilder = class extends BaseCallBuilder {
|
|
|
120
127
|
|
|
121
128
|
// src/calls/index.ts
|
|
122
129
|
var CallMethods = class {
|
|
123
|
-
constructor(
|
|
124
|
-
this.
|
|
130
|
+
constructor(client) {
|
|
131
|
+
this.client = client;
|
|
125
132
|
}
|
|
126
133
|
sendMessage() {
|
|
127
134
|
return new SendMessageBuilder(
|
|
128
|
-
this.
|
|
135
|
+
this.client,
|
|
129
136
|
"/api/v2/sendMessage"
|
|
130
137
|
);
|
|
131
138
|
}
|
|
132
139
|
sendPhoto() {
|
|
133
140
|
return new SendPhotoBuilder(
|
|
134
|
-
this.
|
|
141
|
+
this.client,
|
|
135
142
|
"/api/v2/sendPhoto"
|
|
136
143
|
);
|
|
137
144
|
}
|
|
@@ -143,6 +150,7 @@ function tgcore(options) {
|
|
|
143
150
|
}
|
|
144
151
|
var Client = class {
|
|
145
152
|
constructor(opts) {
|
|
153
|
+
this.middlewares = [];
|
|
146
154
|
if (!opts?.api_key) {
|
|
147
155
|
throw new Error("tgcore-ts: api_key is required");
|
|
148
156
|
}
|
|
@@ -154,6 +162,13 @@ var Client = class {
|
|
|
154
162
|
this.calls = new CallMethods(this.http);
|
|
155
163
|
this.raw = new RawMethods(this.http);
|
|
156
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
|
+
}
|
|
157
172
|
};
|
|
158
173
|
|
|
159
174
|
// src/builders/keyboard.ts
|
|
@@ -204,6 +219,12 @@ var KeyboardBuilder = class _KeyboardBuilder {
|
|
|
204
219
|
this.currentRow().push(btn);
|
|
205
220
|
return this;
|
|
206
221
|
}
|
|
222
|
+
callbackJson(text, obj) {
|
|
223
|
+
return this.callback(text, JSON.stringify(obj));
|
|
224
|
+
}
|
|
225
|
+
button(btn) {
|
|
226
|
+
return this.push(btn);
|
|
227
|
+
}
|
|
207
228
|
url(text, url) {
|
|
208
229
|
return this.push({ text, url });
|
|
209
230
|
}
|