@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,229 @@
1
+ import { paths } from './api-types.cjs';
2
+
3
+ type GetRequestBody<Path extends keyof paths, Method extends "get" | "post" | "put" | "delete", ContentType extends string = "application/json"> = paths[Path] extends {
4
+ [K in Method]: any;
5
+ } ? paths[Path][Method] extends {
6
+ requestBody?: {
7
+ content: any;
8
+ };
9
+ } ? NonNullable<paths[Path][Method]["requestBody"]>["content"][ContentType] : never : never;
10
+ type GetResponse<Path extends keyof paths, Method extends "get" | "post" | "put" | "delete"> = paths[Path] extends {
11
+ [K in Method]: any;
12
+ } ? paths[Path][Method] extends {
13
+ responses: {
14
+ 200: {
15
+ content: {
16
+ "application/json": any;
17
+ };
18
+ };
19
+ };
20
+ } ? paths[Path][Method]["responses"][200]["content"]["application/json"] : never : never;
21
+ type RequestOptions = Omit<NonNullable<Parameters<typeof fetch>[1]>, "headers"> & {
22
+ mimeType?: "json" | "x-www-form-urlencoded";
23
+ headers?: Record<string, string>;
24
+ };
25
+ interface ChatStored {
26
+ id: number;
27
+ username: string;
28
+ title: string;
29
+ about: string;
30
+ type: "group" | "channel";
31
+ participants_count: number;
32
+ avatar_id: string | null;
33
+ last_offset_id: number;
34
+ last_parsed_date: string;
35
+ is_locked: boolean;
36
+ }
37
+ interface MessageStored {
38
+ channel_id: number;
39
+ message_id: number;
40
+ message_created_at: string;
41
+ created_at: string;
42
+ text: string;
43
+ user_id: number;
44
+ username: string;
45
+ first_name: string;
46
+ last_name: string;
47
+ reply_message_id: number;
48
+ has_reactions: number;
49
+ has_comments: number;
50
+ }
51
+ interface PublishersStatusQueueInputChatParsed {
52
+ chat: ChatStored;
53
+ }
54
+ interface ChatParsedTyped extends PublishersStatusQueueInputChatParsed {
55
+ type: "chat_parsed";
56
+ }
57
+ interface PublishersStatusQueueInputMessagesParsed {
58
+ chat: {
59
+ id: number;
60
+ username: string;
61
+ };
62
+ offset: {
63
+ start: number;
64
+ end: number;
65
+ plannedEnd: number;
66
+ };
67
+ messages: MessageStored[];
68
+ }
69
+ interface MessagesParsedTyped extends PublishersStatusQueueInputMessagesParsed {
70
+ type: "messages_parsed";
71
+ }
72
+ interface EventByType {
73
+ chat_parsed: ChatParsedTyped;
74
+ messages_parsed: MessagesParsedTyped;
75
+ }
76
+ type WebhookBody = ChatParsedTyped | MessagesParsedTyped;
77
+ type WebhookBodyTypes = keyof EventByType;
78
+
79
+ declare const frameworks: {
80
+ elysia: ({ body }: any) => {
81
+ body: any;
82
+ response: () => Response;
83
+ };
84
+ fastify: (request: any, reply: any) => {
85
+ body: any;
86
+ response: () => any;
87
+ };
88
+ hono: (c: any) => {
89
+ body: any;
90
+ response: () => any;
91
+ };
92
+ express: (req: any, res: any) => {
93
+ body: any;
94
+ response: () => any;
95
+ };
96
+ koa: (ctx: any) => {
97
+ body: any;
98
+ response: () => void;
99
+ };
100
+ http: (req: any, res: any) => {
101
+ body: Promise<WebhookBody>;
102
+ response: () => any;
103
+ };
104
+ "std/http": (req: any) => {
105
+ body: any;
106
+ response: () => Response;
107
+ };
108
+ "Bun.serve": (req: any) => {
109
+ body: any;
110
+ response: () => Response;
111
+ };
112
+ };
113
+ declare function webhookHandler<Framework extends keyof typeof frameworks>(eApi: EApi, framework: Framework): ReturnType<(typeof frameworks)[Framework]> extends {
114
+ response: () => any;
115
+ } ? (...args: Parameters<(typeof frameworks)[Framework]>) => ReturnType<ReturnType<(typeof frameworks)[Framework]>["response"]> : (...args: Parameters<(typeof frameworks)[Framework]>) => void;
116
+
117
+ /**
118
+ * @module
119
+ *
120
+ * Библиотека для взаимодействия с [API](...).
121
+ */
122
+
123
+ interface EApiOptions {
124
+ client: {
125
+ id: string;
126
+ secret: string;
127
+ };
128
+ host: string;
129
+ requestOptions?: RequestOptions;
130
+ }
131
+ declare class EApi {
132
+ private options;
133
+ private listeners;
134
+ constructor(options?: EApiOptions);
135
+ private request;
136
+ on<T extends WebhookBodyTypes>(type: T, handler: (context: EventByType[T]) => unknown): void;
137
+ test: {};
138
+ emit(data: WebhookBody): Promise<void>;
139
+ /** @generated start-generate-methods */
140
+ /**
141
+ * @tags app
142
+ */
143
+ app: {
144
+ /**
145
+ *
146
+ *
147
+ * @tags App
148
+ * @summary Get Hello World!
149
+ *
150
+ * [Documentation](.../App/operation/app.getHello)
151
+ */
152
+ getHello: (options?: RequestOptions) => Promise<GetResponse<"/", "get">>;
153
+ };
154
+ /**
155
+ * @tags channels
156
+ */
157
+ channels: {
158
+ /**
159
+ *
160
+ *
161
+ * @tags Channels
162
+ * @summary Get Hello World!
163
+ *
164
+ * [Documentation](.../Channels/operation/channels.getHello)
165
+ */
166
+ getHello: (options?: RequestOptions) => Promise<GetResponse<"/channels", "get">>;
167
+ /**
168
+ *
169
+ *
170
+ * @tags Channels
171
+ * @summary Get last channel message
172
+ *
173
+ * [Documentation](.../Channels/operation/channels.getLastMessage)
174
+ */
175
+ getLastMessage: (channelId: paths["/channels/{channelId}/last-message"]["get"]["parameters"]["path"]["channelId"], options?: RequestOptions) => Promise<GetResponse<"/channels/{channelId}/last-message", "get">>;
176
+ /**
177
+ *
178
+ *
179
+ * @tags Channels
180
+ * @summary Get channel messages
181
+ *
182
+ * [Documentation](.../Channels/operation/channels.getMessages)
183
+ */
184
+ getMessages: (channelId: paths["/channels/{channelId}/new-messages"]["post"]["parameters"]["path"]["channelId"], body: GetRequestBody<"/channels/{channelId}/new-messages", "post">, options?: RequestOptions) => Promise<GetResponse<"/channels/{channelId}/new-messages", "post">>;
185
+ };
186
+ /**
187
+ * @tags subscriptions
188
+ */
189
+ subscriptions: {
190
+ /**
191
+ *
192
+ *
193
+ * @tags Subscriptions
194
+ * @summary Create subscription
195
+ *
196
+ * [Documentation](.../Subscriptions/operation/subscriptions.create)
197
+ */
198
+ create: (body: GetRequestBody<"/subscriptions", "post">, options?: RequestOptions) => Promise<GetResponse<"/subscriptions", "post">>;
199
+ /**
200
+ *
201
+ *
202
+ * @tags Subscriptions
203
+ * @summary Get subscription by subscription id
204
+ *
205
+ * [Documentation](.../Subscriptions/operation/subscriptions.getById)
206
+ */
207
+ getById: (subId: paths["/subscriptions/{subId}"]["get"]["parameters"]["path"]["subId"], options?: RequestOptions) => Promise<GetResponse<"/subscriptions/{subId}", "get">>;
208
+ /**
209
+ *
210
+ *
211
+ * @tags Subscriptions
212
+ * @summary Update subscription by subscription id
213
+ *
214
+ * [Documentation](.../Subscriptions/operation/subscriptions.update)
215
+ */
216
+ update: (subId: paths["/subscriptions/{subId}"]["put"]["parameters"]["path"]["subId"], body: GetRequestBody<"/subscriptions/{subId}", "put">, options?: RequestOptions) => Promise<GetResponse<"/subscriptions/{subId}", "put">>;
217
+ /**
218
+ *
219
+ *
220
+ * @tags Subscriptions
221
+ * @summary Delete subscription by subscription id
222
+ *
223
+ * [Documentation](.../Subscriptions/operation/subscriptions.delete)
224
+ */
225
+ delete: (subId: paths["/subscriptions/{subId}"]["delete"]["parameters"]["path"]["subId"], options?: RequestOptions) => Promise<GetResponse<"/subscriptions/{subId}", "delete">>;
226
+ };
227
+ }
228
+
229
+ export { EApi, type EApiOptions, webhookHandler };
@@ -0,0 +1,229 @@
1
+ import { paths } from './api-types.js';
2
+
3
+ type GetRequestBody<Path extends keyof paths, Method extends "get" | "post" | "put" | "delete", ContentType extends string = "application/json"> = paths[Path] extends {
4
+ [K in Method]: any;
5
+ } ? paths[Path][Method] extends {
6
+ requestBody?: {
7
+ content: any;
8
+ };
9
+ } ? NonNullable<paths[Path][Method]["requestBody"]>["content"][ContentType] : never : never;
10
+ type GetResponse<Path extends keyof paths, Method extends "get" | "post" | "put" | "delete"> = paths[Path] extends {
11
+ [K in Method]: any;
12
+ } ? paths[Path][Method] extends {
13
+ responses: {
14
+ 200: {
15
+ content: {
16
+ "application/json": any;
17
+ };
18
+ };
19
+ };
20
+ } ? paths[Path][Method]["responses"][200]["content"]["application/json"] : never : never;
21
+ type RequestOptions = Omit<NonNullable<Parameters<typeof fetch>[1]>, "headers"> & {
22
+ mimeType?: "json" | "x-www-form-urlencoded";
23
+ headers?: Record<string, string>;
24
+ };
25
+ interface ChatStored {
26
+ id: number;
27
+ username: string;
28
+ title: string;
29
+ about: string;
30
+ type: "group" | "channel";
31
+ participants_count: number;
32
+ avatar_id: string | null;
33
+ last_offset_id: number;
34
+ last_parsed_date: string;
35
+ is_locked: boolean;
36
+ }
37
+ interface MessageStored {
38
+ channel_id: number;
39
+ message_id: number;
40
+ message_created_at: string;
41
+ created_at: string;
42
+ text: string;
43
+ user_id: number;
44
+ username: string;
45
+ first_name: string;
46
+ last_name: string;
47
+ reply_message_id: number;
48
+ has_reactions: number;
49
+ has_comments: number;
50
+ }
51
+ interface PublishersStatusQueueInputChatParsed {
52
+ chat: ChatStored;
53
+ }
54
+ interface ChatParsedTyped extends PublishersStatusQueueInputChatParsed {
55
+ type: "chat_parsed";
56
+ }
57
+ interface PublishersStatusQueueInputMessagesParsed {
58
+ chat: {
59
+ id: number;
60
+ username: string;
61
+ };
62
+ offset: {
63
+ start: number;
64
+ end: number;
65
+ plannedEnd: number;
66
+ };
67
+ messages: MessageStored[];
68
+ }
69
+ interface MessagesParsedTyped extends PublishersStatusQueueInputMessagesParsed {
70
+ type: "messages_parsed";
71
+ }
72
+ interface EventByType {
73
+ chat_parsed: ChatParsedTyped;
74
+ messages_parsed: MessagesParsedTyped;
75
+ }
76
+ type WebhookBody = ChatParsedTyped | MessagesParsedTyped;
77
+ type WebhookBodyTypes = keyof EventByType;
78
+
79
+ declare const frameworks: {
80
+ elysia: ({ body }: any) => {
81
+ body: any;
82
+ response: () => Response;
83
+ };
84
+ fastify: (request: any, reply: any) => {
85
+ body: any;
86
+ response: () => any;
87
+ };
88
+ hono: (c: any) => {
89
+ body: any;
90
+ response: () => any;
91
+ };
92
+ express: (req: any, res: any) => {
93
+ body: any;
94
+ response: () => any;
95
+ };
96
+ koa: (ctx: any) => {
97
+ body: any;
98
+ response: () => void;
99
+ };
100
+ http: (req: any, res: any) => {
101
+ body: Promise<WebhookBody>;
102
+ response: () => any;
103
+ };
104
+ "std/http": (req: any) => {
105
+ body: any;
106
+ response: () => Response;
107
+ };
108
+ "Bun.serve": (req: any) => {
109
+ body: any;
110
+ response: () => Response;
111
+ };
112
+ };
113
+ declare function webhookHandler<Framework extends keyof typeof frameworks>(eApi: EApi, framework: Framework): ReturnType<(typeof frameworks)[Framework]> extends {
114
+ response: () => any;
115
+ } ? (...args: Parameters<(typeof frameworks)[Framework]>) => ReturnType<ReturnType<(typeof frameworks)[Framework]>["response"]> : (...args: Parameters<(typeof frameworks)[Framework]>) => void;
116
+
117
+ /**
118
+ * @module
119
+ *
120
+ * Библиотека для взаимодействия с [API](...).
121
+ */
122
+
123
+ interface EApiOptions {
124
+ client: {
125
+ id: string;
126
+ secret: string;
127
+ };
128
+ host: string;
129
+ requestOptions?: RequestOptions;
130
+ }
131
+ declare class EApi {
132
+ private options;
133
+ private listeners;
134
+ constructor(options?: EApiOptions);
135
+ private request;
136
+ on<T extends WebhookBodyTypes>(type: T, handler: (context: EventByType[T]) => unknown): void;
137
+ test: {};
138
+ emit(data: WebhookBody): Promise<void>;
139
+ /** @generated start-generate-methods */
140
+ /**
141
+ * @tags app
142
+ */
143
+ app: {
144
+ /**
145
+ *
146
+ *
147
+ * @tags App
148
+ * @summary Get Hello World!
149
+ *
150
+ * [Documentation](.../App/operation/app.getHello)
151
+ */
152
+ getHello: (options?: RequestOptions) => Promise<GetResponse<"/", "get">>;
153
+ };
154
+ /**
155
+ * @tags channels
156
+ */
157
+ channels: {
158
+ /**
159
+ *
160
+ *
161
+ * @tags Channels
162
+ * @summary Get Hello World!
163
+ *
164
+ * [Documentation](.../Channels/operation/channels.getHello)
165
+ */
166
+ getHello: (options?: RequestOptions) => Promise<GetResponse<"/channels", "get">>;
167
+ /**
168
+ *
169
+ *
170
+ * @tags Channels
171
+ * @summary Get last channel message
172
+ *
173
+ * [Documentation](.../Channels/operation/channels.getLastMessage)
174
+ */
175
+ getLastMessage: (channelId: paths["/channels/{channelId}/last-message"]["get"]["parameters"]["path"]["channelId"], options?: RequestOptions) => Promise<GetResponse<"/channels/{channelId}/last-message", "get">>;
176
+ /**
177
+ *
178
+ *
179
+ * @tags Channels
180
+ * @summary Get channel messages
181
+ *
182
+ * [Documentation](.../Channels/operation/channels.getMessages)
183
+ */
184
+ getMessages: (channelId: paths["/channels/{channelId}/new-messages"]["post"]["parameters"]["path"]["channelId"], body: GetRequestBody<"/channels/{channelId}/new-messages", "post">, options?: RequestOptions) => Promise<GetResponse<"/channels/{channelId}/new-messages", "post">>;
185
+ };
186
+ /**
187
+ * @tags subscriptions
188
+ */
189
+ subscriptions: {
190
+ /**
191
+ *
192
+ *
193
+ * @tags Subscriptions
194
+ * @summary Create subscription
195
+ *
196
+ * [Documentation](.../Subscriptions/operation/subscriptions.create)
197
+ */
198
+ create: (body: GetRequestBody<"/subscriptions", "post">, options?: RequestOptions) => Promise<GetResponse<"/subscriptions", "post">>;
199
+ /**
200
+ *
201
+ *
202
+ * @tags Subscriptions
203
+ * @summary Get subscription by subscription id
204
+ *
205
+ * [Documentation](.../Subscriptions/operation/subscriptions.getById)
206
+ */
207
+ getById: (subId: paths["/subscriptions/{subId}"]["get"]["parameters"]["path"]["subId"], options?: RequestOptions) => Promise<GetResponse<"/subscriptions/{subId}", "get">>;
208
+ /**
209
+ *
210
+ *
211
+ * @tags Subscriptions
212
+ * @summary Update subscription by subscription id
213
+ *
214
+ * [Documentation](.../Subscriptions/operation/subscriptions.update)
215
+ */
216
+ update: (subId: paths["/subscriptions/{subId}"]["put"]["parameters"]["path"]["subId"], body: GetRequestBody<"/subscriptions/{subId}", "put">, options?: RequestOptions) => Promise<GetResponse<"/subscriptions/{subId}", "put">>;
217
+ /**
218
+ *
219
+ *
220
+ * @tags Subscriptions
221
+ * @summary Delete subscription by subscription id
222
+ *
223
+ * [Documentation](.../Subscriptions/operation/subscriptions.delete)
224
+ */
225
+ delete: (subId: paths["/subscriptions/{subId}"]["delete"]["parameters"]["path"]["subId"], options?: RequestOptions) => Promise<GetResponse<"/subscriptions/{subId}", "delete">>;
226
+ };
227
+ }
228
+
229
+ export { EApi, type EApiOptions, webhookHandler };
package/dist/index.js ADDED
@@ -0,0 +1,211 @@
1
+ const frameworks = {
2
+ elysia: ({ body }) => ({
3
+ body,
4
+ response: () => new Response("OK")
5
+ }),
6
+ fastify: (request, reply) => ({
7
+ body: request.body,
8
+ response: () => reply.send("OK")
9
+ }),
10
+ hono: (c) => ({ body: c.req.json(), response: () => c.text("OK") }),
11
+ express: (req, res) => ({ body: req.body, response: () => res.send("OK") }),
12
+ koa: (ctx) => ({
13
+ body: ctx.request.body,
14
+ response: () => {
15
+ ctx.body = "OK";
16
+ }
17
+ }),
18
+ http: (req, res) => ({
19
+ body: new Promise((resolve) => {
20
+ let body = "";
21
+ req.on("data", (chunk) => {
22
+ body += chunk.toString();
23
+ });
24
+ req.on("end", () => resolve(JSON.parse(body)));
25
+ }),
26
+ response: () => res.writeHead(200).end("OK")
27
+ }),
28
+ "std/http": (req) => ({
29
+ body: req.json(),
30
+ response: () => new Response("OK")
31
+ }),
32
+ "Bun.serve": (req) => ({
33
+ body: req.json(),
34
+ response: () => new Response("OK")
35
+ })
36
+ };
37
+ function webhookHandler(eApi, framework) {
38
+ const frameworkAdapter = frameworks[framework];
39
+ return async (...args) => {
40
+ const { body, response } = frameworkAdapter(...args);
41
+ await eApi.emit(await body);
42
+ if (response)
43
+ return response();
44
+ };
45
+ }
46
+
47
+ class EApi {
48
+ options;
49
+ listeners = [];
50
+ constructor(options) {
51
+ this.options = options;
52
+ }
53
+ async request(path, data, options) {
54
+ const requestOptions = {
55
+ method: options?.method || "POST",
56
+ mimeType: "json",
57
+ ...this.options.requestOptions,
58
+ ...options,
59
+ body: data ? JSON.stringify(data) : void 0,
60
+ headers: {
61
+ "user-agent": "EAPI SDK",
62
+ Authorization: `Basic ${btoa(`${this.options.client.id}:${this.options.client.secret}`)}`,
63
+ ...this.options.requestOptions?.headers,
64
+ ...options?.headers
65
+ }
66
+ };
67
+ requestOptions.mimeType = void 0;
68
+ const response = await fetch(this.options.host + path, requestOptions);
69
+ if (!response.ok) {
70
+ throw new Error(`${response.status} ${await response.text()}`);
71
+ }
72
+ return response.json();
73
+ }
74
+ on(type, handler) {
75
+ this.listeners.push({ event: type, handler });
76
+ }
77
+ test = {};
78
+ async emit(data) {
79
+ const listeners = this.listeners.find((x) => x.event === data.type);
80
+ if (listeners)
81
+ await listeners.handler(data);
82
+ }
83
+ /** @generated start-generate-methods */
84
+ /**
85
+ * @tags app
86
+ */
87
+ app = {
88
+ /**
89
+ *
90
+ *
91
+ * @tags App
92
+ * @summary Get Hello World!
93
+ *
94
+ * [Documentation](.../App/operation/app.getHello)
95
+ */
96
+ getHello: (options) => {
97
+ return this.request(`/`, void 0, { method: "GET", ...options });
98
+ }
99
+ };
100
+ /**
101
+ * @tags channels
102
+ */
103
+ channels = {
104
+ /**
105
+ *
106
+ *
107
+ * @tags Channels
108
+ * @summary Get Hello World!
109
+ *
110
+ * [Documentation](.../Channels/operation/channels.getHello)
111
+ */
112
+ getHello: (options) => {
113
+ return this.request(`/channels`, void 0, {
114
+ method: "GET",
115
+ ...options
116
+ });
117
+ },
118
+ /**
119
+ *
120
+ *
121
+ * @tags Channels
122
+ * @summary Get last channel message
123
+ *
124
+ * [Documentation](.../Channels/operation/channels.getLastMessage)
125
+ */
126
+ getLastMessage: (channelId, options) => {
127
+ return this.request(`/channels/${channelId}/last-message`, void 0, {
128
+ method: "GET",
129
+ ...options
130
+ });
131
+ },
132
+ /**
133
+ *
134
+ *
135
+ * @tags Channels
136
+ * @summary Get channel messages
137
+ *
138
+ * [Documentation](.../Channels/operation/channels.getMessages)
139
+ */
140
+ getMessages: (channelId, body, options) => {
141
+ return this.request(`/channels/${channelId}/new-messages`, body, {
142
+ method: "POST",
143
+ ...options
144
+ });
145
+ }
146
+ };
147
+ /**
148
+ * @tags subscriptions
149
+ */
150
+ subscriptions = {
151
+ /**
152
+ *
153
+ *
154
+ * @tags Subscriptions
155
+ * @summary Create subscription
156
+ *
157
+ * [Documentation](.../Subscriptions/operation/subscriptions.create)
158
+ */
159
+ create: (body, options) => {
160
+ return this.request(`/subscriptions`, body, {
161
+ method: "POST",
162
+ ...options
163
+ });
164
+ },
165
+ /**
166
+ *
167
+ *
168
+ * @tags Subscriptions
169
+ * @summary Get subscription by subscription id
170
+ *
171
+ * [Documentation](.../Subscriptions/operation/subscriptions.getById)
172
+ */
173
+ getById: (subId, options) => {
174
+ return this.request(`/subscriptions/${subId}`, void 0, {
175
+ method: "GET",
176
+ ...options
177
+ });
178
+ },
179
+ /**
180
+ *
181
+ *
182
+ * @tags Subscriptions
183
+ * @summary Update subscription by subscription id
184
+ *
185
+ * [Documentation](.../Subscriptions/operation/subscriptions.update)
186
+ */
187
+ update: (subId, body, options) => {
188
+ return this.request(`/subscriptions/${subId}`, body, {
189
+ method: "PUT",
190
+ ...options
191
+ });
192
+ },
193
+ /**
194
+ *
195
+ *
196
+ * @tags Subscriptions
197
+ * @summary Delete subscription by subscription id
198
+ *
199
+ * [Documentation](.../Subscriptions/operation/subscriptions.delete)
200
+ */
201
+ delete: (subId, options) => {
202
+ return this.request(`/subscriptions/${subId}`, void 0, {
203
+ method: "DELETE",
204
+ ...options
205
+ });
206
+ }
207
+ };
208
+ /** @generated stop-generate-methods */
209
+ }
210
+
211
+ export { EApi, webhookHandler };