@skravets/eapi 0.0.1

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,498 @@
1
+ /**
2
+ * @module
3
+ *
4
+ * Сгенерированные TypeScript типы для [API](...).
5
+ */
6
+ /**
7
+ * Сгенерированные из OpenAPI типы для `paths`
8
+ */
9
+ interface paths {
10
+ "/": {
11
+ /** Get Hello World! */
12
+ get: operations["app.getHello"];
13
+ };
14
+ "/channels": {
15
+ /** Get Hello World! */
16
+ get: operations["channels.getHello"];
17
+ };
18
+ "/channels/{channelId}/last-message": {
19
+ /** Get last channel message */
20
+ get: operations["channels.getLastMessage"];
21
+ };
22
+ "/channels/{channelId}/new-messages": {
23
+ /** Get channel messages */
24
+ post: operations["channels.getMessages"];
25
+ };
26
+ "/subscriptions": {
27
+ /** Create subscription */
28
+ post: operations["subscriptions.create"];
29
+ };
30
+ "/subscriptions/{subId}": {
31
+ /** Get subscription by subscription id */
32
+ get: operations["subscriptions.getById"];
33
+ /** Update subscription by subscription id */
34
+ put: operations["subscriptions.update"];
35
+ /** Delete subscription by subscription id */
36
+ delete: operations["subscriptions.delete"];
37
+ };
38
+ }
39
+ /**
40
+ * Сгенерированные из OpenAPI типы для `components`
41
+ */
42
+ interface components {
43
+ schemas: {
44
+ IGetChannelMessages: {
45
+ /**
46
+ * @description Cursor to get messages from id to id
47
+ * @example {
48
+ * "from": 1,
49
+ * "to": 10
50
+ * }
51
+ */
52
+ cursor: Record<string, never>;
53
+ };
54
+ ChatDataDto: {
55
+ /** @example true */
56
+ id?: boolean;
57
+ /** @example true */
58
+ username?: boolean;
59
+ /** @example true */
60
+ title?: boolean;
61
+ /** @example true */
62
+ type?: boolean;
63
+ /** @example true */
64
+ participants_count?: boolean;
65
+ /** @example true */
66
+ last_offset_id?: boolean;
67
+ /** @example true */
68
+ last_parsed_date?: boolean;
69
+ /** @example true */
70
+ is_locked?: boolean;
71
+ /** @example true */
72
+ about?: boolean;
73
+ /** @example true */
74
+ avatar_id?: boolean;
75
+ };
76
+ SubscriptionChatDataDto: {
77
+ /**
78
+ * @description Chat schema
79
+ * @example {
80
+ * "id": true,
81
+ * "username": true,
82
+ * "title": true,
83
+ * "type": true,
84
+ * "participants_count": true,
85
+ * "last_offset_id": true,
86
+ * "last_parsed_date": true,
87
+ * "is_locked": true,
88
+ * "about": true,
89
+ * "avatar_id": true
90
+ * }
91
+ */
92
+ data: components["schemas"]["ChatDataDto"];
93
+ };
94
+ SubscriptionMessageDataDto: {
95
+ /**
96
+ * @description Messages schema
97
+ * @example {
98
+ * "channel_id": true,
99
+ * "created_at": true,
100
+ * "first_name": true,
101
+ * "has_comments": true,
102
+ * "has_reactions": true,
103
+ * "last_name": true,
104
+ * "message_created_at": true,
105
+ * "message_id": true,
106
+ * "reply_message_id": true,
107
+ * "search_text": true,
108
+ * "text": true,
109
+ * "user_id": true,
110
+ * "username": true
111
+ * }
112
+ */
113
+ data: Record<string, never>;
114
+ /** @description Messages limit per request */
115
+ limit?: number;
116
+ };
117
+ SubscriptionDataDto: {
118
+ /**
119
+ * @description Chat meta data
120
+ * @example {
121
+ * "data": {
122
+ * "id": true,
123
+ * "username": true,
124
+ * "title": true,
125
+ * "type": true,
126
+ * "participants_count": true,
127
+ * "last_offset_id": true,
128
+ * "last_parsed_date": true,
129
+ * "is_locked": true,
130
+ * "about": true,
131
+ * "avatar_id": true
132
+ * }
133
+ * }
134
+ */
135
+ chat?: components["schemas"]["SubscriptionChatDataDto"];
136
+ /**
137
+ * @description Messages data
138
+ * @example {
139
+ * "data": {
140
+ * "channel_id": true,
141
+ * "created_at": true,
142
+ * "first_name": true,
143
+ * "has_comments": true,
144
+ * "has_reactions": true,
145
+ * "last_name": true,
146
+ * "message_created_at": true,
147
+ * "message_id": true,
148
+ * "reply_message_id": true,
149
+ * "search_text": true,
150
+ * "text": true,
151
+ * "user_id": true,
152
+ * "username": true
153
+ * },
154
+ * "limit": 100
155
+ * }
156
+ */
157
+ messages?: components["schemas"]["SubscriptionMessageDataDto"];
158
+ };
159
+ SubscribeDto: {
160
+ /**
161
+ * @description Chat Id to subscribe
162
+ * @example 1234567890
163
+ */
164
+ chatId: number;
165
+ /** @example {
166
+ * "chat": {
167
+ * "data": {
168
+ * "id": true,
169
+ * "username": true,
170
+ * "title": true,
171
+ * "type": true,
172
+ * "participants_count": true,
173
+ * "last_offset_id": true,
174
+ * "last_parsed_date": true,
175
+ * "is_locked": true,
176
+ * "about": true,
177
+ * "avatar_id": true
178
+ * }
179
+ * },
180
+ * "messages": {
181
+ * "data": {
182
+ * "channel_id": true,
183
+ * "created_at": true,
184
+ * "first_name": true,
185
+ * "has_comments": true,
186
+ * "has_reactions": true,
187
+ * "last_name": true,
188
+ * "message_created_at": true,
189
+ * "message_id": true,
190
+ * "reply_message_id": true,
191
+ * "search_text": true,
192
+ * "text": true,
193
+ * "user_id": true,
194
+ * "username": true
195
+ * },
196
+ * "limit": 100
197
+ * }
198
+ * } */
199
+ data: components["schemas"]["SubscriptionDataDto"];
200
+ /**
201
+ * @description Webhook url
202
+ * @example https://example.com/webhook
203
+ */
204
+ webhookUrl: string;
205
+ /**
206
+ * @description Short description
207
+ * @example to get messages from chat 123
208
+ */
209
+ description?: string;
210
+ };
211
+ UpdateSubscribeDto: {
212
+ /**
213
+ * @description Scopes to subscribe
214
+ * @example {
215
+ * "chat": {
216
+ * "data": {
217
+ * "username": true
218
+ * }
219
+ * }
220
+ * }
221
+ */
222
+ data?: components["schemas"]["SubscriptionDataDto"];
223
+ /**
224
+ * @description Webhook url
225
+ * @example https://example.com/webhook
226
+ */
227
+ webhookUrl?: string;
228
+ };
229
+ };
230
+ }
231
+ type $defs = Record<string, never>;
232
+ /**
233
+ * Сгенерированные из OpenAPI типы для `operations`
234
+ */
235
+ interface operations {
236
+ "app.getHello": {
237
+ parameters: {
238
+ header: {
239
+ /** @description `Basic token`, where token is `id:secret` base64 encoded */
240
+ Authorization: string;
241
+ };
242
+ };
243
+ responses: {
244
+ /** @description returns Hello World! */
245
+ 200: {
246
+ headers: {
247
+ [name: string]: unknown;
248
+ };
249
+ content: {
250
+ "application/json": unknown;
251
+ };
252
+ };
253
+ };
254
+ };
255
+ "channels.getHello": {
256
+ parameters: {
257
+ header: {
258
+ /** @description `Basic token`, where token is `id:secret` base64 encoded */
259
+ Authorization: string;
260
+ };
261
+ };
262
+ responses: {
263
+ /** @description returns Hello World! */
264
+ 200: {
265
+ headers: {
266
+ [name: string]: unknown;
267
+ };
268
+ content: {
269
+ "application/json": unknown;
270
+ };
271
+ };
272
+ /** @description Unauthorized */
273
+ 401: {
274
+ headers: {
275
+ [name: string]: unknown;
276
+ };
277
+ };
278
+ };
279
+ };
280
+ "channels.getLastMessage": {
281
+ parameters: {
282
+ header: {
283
+ /** @description `Basic token`, where token is `id:secret` base64 encoded */
284
+ Authorization: string;
285
+ };
286
+ path: {
287
+ channelId: number;
288
+ };
289
+ };
290
+ responses: {
291
+ /** @description Returns array with last message by channel id */
292
+ 200: {
293
+ headers: {
294
+ [name: string]: unknown;
295
+ };
296
+ content: {
297
+ "application/json": unknown;
298
+ };
299
+ };
300
+ /** @description Unauthorized */
301
+ 401: {
302
+ headers: {
303
+ [name: string]: unknown;
304
+ };
305
+ };
306
+ };
307
+ };
308
+ "channels.getMessages": {
309
+ parameters: {
310
+ header: {
311
+ /** @description `Basic token`, where token is `id:secret` base64 encoded */
312
+ Authorization: string;
313
+ };
314
+ path: {
315
+ channelId: number;
316
+ };
317
+ };
318
+ requestBody: {
319
+ content: {
320
+ "application/json": components["schemas"]["IGetChannelMessages"];
321
+ };
322
+ };
323
+ responses: {
324
+ /** @description Returns array of messages by channel id and cursors */
325
+ 200: {
326
+ headers: {
327
+ [name: string]: unknown;
328
+ };
329
+ content: {
330
+ "application/json": unknown;
331
+ };
332
+ };
333
+ /** @description Unauthorized */
334
+ 401: {
335
+ headers: {
336
+ [name: string]: unknown;
337
+ };
338
+ };
339
+ };
340
+ };
341
+ "subscriptions.create": {
342
+ parameters: {
343
+ header: {
344
+ /** @description `Basic token`, where token is `id:secret` base64 encoded */
345
+ Authorization: string;
346
+ };
347
+ };
348
+ requestBody: {
349
+ content: {
350
+ "application/json": components["schemas"]["SubscribeDto"];
351
+ };
352
+ };
353
+ responses: {
354
+ /** @description Subscription created, returns created subscription */
355
+ 201: {
356
+ headers: {
357
+ [name: string]: unknown;
358
+ };
359
+ content: {
360
+ "application/json": components["schemas"]["SubscribeDto"];
361
+ };
362
+ };
363
+ /** @description Unauthorized */
364
+ 401: {
365
+ headers: {
366
+ [name: string]: unknown;
367
+ };
368
+ };
369
+ };
370
+ };
371
+ "subscriptions.getById": {
372
+ parameters: {
373
+ header: {
374
+ /** @description `Basic token`, where token is `id:secret` base64 encoded */
375
+ Authorization: string;
376
+ };
377
+ path: {
378
+ subId: number;
379
+ };
380
+ };
381
+ responses: {
382
+ /** @description Subscription found, returns subscription */
383
+ 200: {
384
+ headers: {
385
+ [name: string]: unknown;
386
+ };
387
+ content: {
388
+ "application/json": components["schemas"]["SubscribeDto"];
389
+ };
390
+ };
391
+ /** @description Invalid subscription id */
392
+ 400: {
393
+ headers: {
394
+ [name: string]: unknown;
395
+ };
396
+ };
397
+ /** @description Unauthorized */
398
+ 401: {
399
+ headers: {
400
+ [name: string]: unknown;
401
+ };
402
+ };
403
+ /** @description You have no rights to manipulate subscription */
404
+ 403: {
405
+ headers: {
406
+ [name: string]: unknown;
407
+ };
408
+ };
409
+ };
410
+ };
411
+ "subscriptions.update": {
412
+ parameters: {
413
+ header: {
414
+ /** @description `Basic token`, where token is `id:secret` base64 encoded */
415
+ Authorization: string;
416
+ };
417
+ path: {
418
+ subId: number;
419
+ };
420
+ };
421
+ requestBody: {
422
+ content: {
423
+ "application/json": components["schemas"]["UpdateSubscribeDto"];
424
+ };
425
+ };
426
+ responses: {
427
+ /** @description Subscription updated, returns updated subscription */
428
+ 200: {
429
+ headers: {
430
+ [name: string]: unknown;
431
+ };
432
+ content: {
433
+ "application/json": components["schemas"]["SubscribeDto"];
434
+ };
435
+ };
436
+ /** @description Invalid subscription id */
437
+ 400: {
438
+ headers: {
439
+ [name: string]: unknown;
440
+ };
441
+ };
442
+ /** @description Unauthorized */
443
+ 401: {
444
+ headers: {
445
+ [name: string]: unknown;
446
+ };
447
+ };
448
+ /** @description You have no rights to manipulate subscription */
449
+ 403: {
450
+ headers: {
451
+ [name: string]: unknown;
452
+ };
453
+ };
454
+ };
455
+ };
456
+ "subscriptions.delete": {
457
+ parameters: {
458
+ header: {
459
+ /** @description `Basic token`, where token is `id:secret` base64 encoded */
460
+ Authorization: string;
461
+ };
462
+ path: {
463
+ subId: number;
464
+ };
465
+ };
466
+ responses: {
467
+ /** @description Subscription deleted, returns deleted subscription */
468
+ 200: {
469
+ headers: {
470
+ [name: string]: unknown;
471
+ };
472
+ content: {
473
+ "application/json": components["schemas"]["SubscribeDto"];
474
+ };
475
+ };
476
+ /** @description Invalid subscription id */
477
+ 400: {
478
+ headers: {
479
+ [name: string]: unknown;
480
+ };
481
+ };
482
+ /** @description Unauthorized */
483
+ 401: {
484
+ headers: {
485
+ [name: string]: unknown;
486
+ };
487
+ };
488
+ /** @description You have no rights to manipulate subscription */
489
+ 403: {
490
+ headers: {
491
+ [name: string]: unknown;
492
+ };
493
+ };
494
+ };
495
+ };
496
+ }
497
+
498
+ export type { $defs, components, operations, paths };
package/dist/index.cjs ADDED
@@ -0,0 +1,214 @@
1
+ 'use strict';
2
+
3
+ const frameworks = {
4
+ elysia: ({ body }) => ({
5
+ body,
6
+ response: () => new Response("OK")
7
+ }),
8
+ fastify: (request, reply) => ({
9
+ body: request.body,
10
+ response: () => reply.send("OK")
11
+ }),
12
+ hono: (c) => ({ body: c.req.json(), response: () => c.text("OK") }),
13
+ express: (req, res) => ({ body: req.body, response: () => res.send("OK") }),
14
+ koa: (ctx) => ({
15
+ body: ctx.request.body,
16
+ response: () => {
17
+ ctx.body = "OK";
18
+ }
19
+ }),
20
+ http: (req, res) => ({
21
+ body: new Promise((resolve) => {
22
+ let body = "";
23
+ req.on("data", (chunk) => {
24
+ body += chunk.toString();
25
+ });
26
+ req.on("end", () => resolve(JSON.parse(body)));
27
+ }),
28
+ response: () => res.writeHead(200).end("OK")
29
+ }),
30
+ "std/http": (req) => ({
31
+ body: req.json(),
32
+ response: () => new Response("OK")
33
+ }),
34
+ "Bun.serve": (req) => ({
35
+ body: req.json(),
36
+ response: () => new Response("OK")
37
+ })
38
+ };
39
+ function webhookHandler(eApi, framework) {
40
+ const frameworkAdapter = frameworks[framework];
41
+ return async (...args) => {
42
+ const { body, response } = frameworkAdapter(...args);
43
+ await eApi.emit(await body);
44
+ if (response)
45
+ return response();
46
+ };
47
+ }
48
+
49
+ class EApi {
50
+ options;
51
+ listeners = [];
52
+ constructor(options) {
53
+ this.options = options;
54
+ }
55
+ async request(path, data, options) {
56
+ const requestOptions = {
57
+ method: options?.method || "POST",
58
+ mimeType: "json",
59
+ ...this.options.requestOptions,
60
+ ...options,
61
+ body: data ? JSON.stringify(data) : void 0,
62
+ headers: {
63
+ "user-agent": "EAPI SDK",
64
+ Authorization: `Basic ${btoa(`${this.options.client.id}:${this.options.client.secret}`)}`,
65
+ ...this.options.requestOptions?.headers,
66
+ ...options?.headers
67
+ }
68
+ };
69
+ requestOptions.mimeType = void 0;
70
+ const response = await fetch(this.options.host + path, requestOptions);
71
+ if (!response.ok) {
72
+ throw new Error(`${response.status} ${await response.text()}`);
73
+ }
74
+ return response.json();
75
+ }
76
+ on(type, handler) {
77
+ this.listeners.push({ event: type, handler });
78
+ }
79
+ test = {};
80
+ async emit(data) {
81
+ const listeners = this.listeners.find((x) => x.event === data.type);
82
+ if (listeners)
83
+ await listeners.handler(data);
84
+ }
85
+ /** @generated start-generate-methods */
86
+ /**
87
+ * @tags app
88
+ */
89
+ app = {
90
+ /**
91
+ *
92
+ *
93
+ * @tags App
94
+ * @summary Get Hello World!
95
+ *
96
+ * [Documentation](.../App/operation/app.getHello)
97
+ */
98
+ getHello: (options) => {
99
+ return this.request(`/`, void 0, { method: "GET", ...options });
100
+ }
101
+ };
102
+ /**
103
+ * @tags channels
104
+ */
105
+ channels = {
106
+ /**
107
+ *
108
+ *
109
+ * @tags Channels
110
+ * @summary Get Hello World!
111
+ *
112
+ * [Documentation](.../Channels/operation/channels.getHello)
113
+ */
114
+ getHello: (options) => {
115
+ return this.request(`/channels`, void 0, {
116
+ method: "GET",
117
+ ...options
118
+ });
119
+ },
120
+ /**
121
+ *
122
+ *
123
+ * @tags Channels
124
+ * @summary Get last channel message
125
+ *
126
+ * [Documentation](.../Channels/operation/channels.getLastMessage)
127
+ */
128
+ getLastMessage: (channelId, options) => {
129
+ return this.request(`/channels/${channelId}/last-message`, void 0, {
130
+ method: "GET",
131
+ ...options
132
+ });
133
+ },
134
+ /**
135
+ *
136
+ *
137
+ * @tags Channels
138
+ * @summary Get channel messages
139
+ *
140
+ * [Documentation](.../Channels/operation/channels.getMessages)
141
+ */
142
+ getMessages: (channelId, body, options) => {
143
+ return this.request(`/channels/${channelId}/new-messages`, body, {
144
+ method: "POST",
145
+ ...options
146
+ });
147
+ }
148
+ };
149
+ /**
150
+ * @tags subscriptions
151
+ */
152
+ subscriptions = {
153
+ /**
154
+ *
155
+ *
156
+ * @tags Subscriptions
157
+ * @summary Create subscription
158
+ *
159
+ * [Documentation](.../Subscriptions/operation/subscriptions.create)
160
+ */
161
+ create: (body, options) => {
162
+ return this.request(`/subscriptions`, body, {
163
+ method: "POST",
164
+ ...options
165
+ });
166
+ },
167
+ /**
168
+ *
169
+ *
170
+ * @tags Subscriptions
171
+ * @summary Get subscription by subscription id
172
+ *
173
+ * [Documentation](.../Subscriptions/operation/subscriptions.getById)
174
+ */
175
+ getById: (subId, options) => {
176
+ return this.request(`/subscriptions/${subId}`, void 0, {
177
+ method: "GET",
178
+ ...options
179
+ });
180
+ },
181
+ /**
182
+ *
183
+ *
184
+ * @tags Subscriptions
185
+ * @summary Update subscription by subscription id
186
+ *
187
+ * [Documentation](.../Subscriptions/operation/subscriptions.update)
188
+ */
189
+ update: (subId, body, options) => {
190
+ return this.request(`/subscriptions/${subId}`, body, {
191
+ method: "PUT",
192
+ ...options
193
+ });
194
+ },
195
+ /**
196
+ *
197
+ *
198
+ * @tags Subscriptions
199
+ * @summary Delete subscription by subscription id
200
+ *
201
+ * [Documentation](.../Subscriptions/operation/subscriptions.delete)
202
+ */
203
+ delete: (subId, options) => {
204
+ return this.request(`/subscriptions/${subId}`, void 0, {
205
+ method: "DELETE",
206
+ ...options
207
+ });
208
+ }
209
+ };
210
+ /** @generated stop-generate-methods */
211
+ }
212
+
213
+ exports.EApi = EApi;
214
+ exports.webhookHandler = webhookHandler;