@skravets/alert-bot 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Kravets
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Alert Bot SDK
2
+
3
+ Простой клиент для Alert Bot API.
4
+
5
+ ## Пример использования
6
+
7
+ ```ts
8
+ import { AlertBot } from "alert-bot-sdk";
9
+
10
+ const client = new AlertBot({
11
+ host: "https://api.example.com",
12
+ apiKey: process.env.ALERT_BOT_API_KEY!,
13
+ });
14
+
15
+ await client.updateServices({
16
+ services: [
17
+ { type: "healthcheck", endpoint: "https://example.com/health" },
18
+ { type: "openrouter", apiKey: "sk-openrouter" },
19
+ ],
20
+ title: "Project name",
21
+ chatId: "chat-123",
22
+ });
23
+ ```
24
+
25
+ ## Публикация
26
+
27
+ 1. Задеплоить на Staging новые версии контракта. Нужно указывать tags в OpenAPI чтобы они попали в группу.
28
+ 2. Обновить в `package.json` версию.
29
+ 3. Запустить workflow `Publish package`.
@@ -0,0 +1,111 @@
1
+ /**
2
+ * @module
3
+ *
4
+ * Сгенерированные TypeScript типы для [API](...).
5
+ */
6
+ /**
7
+ * Сгенерированные из OpenAPI типы для `paths`
8
+ */
9
+ interface paths {
10
+ "/hello": {
11
+ post: {
12
+ requestBody: {
13
+ content: {
14
+ "application/json": {
15
+ /** @example Alice */
16
+ name: string;
17
+ };
18
+ };
19
+ };
20
+ responses: {
21
+ /** @description Hello response */
22
+ 200: {
23
+ headers: {
24
+ [name: string]: unknown;
25
+ };
26
+ content: {
27
+ "application/json": {
28
+ /** @example Hello, Alice! */
29
+ message: string;
30
+ project: {
31
+ id: string;
32
+ title: string;
33
+ };
34
+ };
35
+ };
36
+ };
37
+ /** @description Unauthorized */
38
+ 401: {
39
+ headers: {
40
+ [name: string]: unknown;
41
+ };
42
+ };
43
+ };
44
+ };
45
+ };
46
+ "/projects/services": {
47
+ put: {
48
+ requestBody: {
49
+ content: {
50
+ "application/json": {
51
+ services: ({
52
+ /** @enum {string} */
53
+ type: "healthcheck";
54
+ /** Format: uri */
55
+ endpoint: string;
56
+ } | {
57
+ /** @enum {string} */
58
+ type: "openrouter";
59
+ apiKey: string;
60
+ })[];
61
+ title?: string;
62
+ chatId?: string;
63
+ threadForumId?: string;
64
+ };
65
+ };
66
+ };
67
+ responses: {
68
+ /** @description Services updated successfully */
69
+ 200: {
70
+ headers: {
71
+ [name: string]: unknown;
72
+ };
73
+ content: {
74
+ "application/json": {
75
+ success: boolean;
76
+ project: {
77
+ id: string;
78
+ title: string;
79
+ services: ({
80
+ /** @enum {string} */
81
+ type: "healthcheck";
82
+ /** Format: uri */
83
+ endpoint: string;
84
+ } | {
85
+ /** @enum {string} */
86
+ type: "openrouter";
87
+ apiKey: string;
88
+ })[];
89
+ updatedAt: null | string;
90
+ };
91
+ };
92
+ };
93
+ };
94
+ /** @description Unauthorized */
95
+ 401: {
96
+ headers: {
97
+ [name: string]: unknown;
98
+ };
99
+ };
100
+ };
101
+ };
102
+ };
103
+ }
104
+ /**
105
+ * Сгенерированные из OpenAPI типы для `components`
106
+ */
107
+ type components = {};
108
+ type $defs = Record<string, never>;
109
+ type operations = Record<string, never>;
110
+
111
+ export type { $defs, components, operations, paths };
@@ -0,0 +1,111 @@
1
+ /**
2
+ * @module
3
+ *
4
+ * Сгенерированные TypeScript типы для [API](...).
5
+ */
6
+ /**
7
+ * Сгенерированные из OpenAPI типы для `paths`
8
+ */
9
+ interface paths {
10
+ "/hello": {
11
+ post: {
12
+ requestBody: {
13
+ content: {
14
+ "application/json": {
15
+ /** @example Alice */
16
+ name: string;
17
+ };
18
+ };
19
+ };
20
+ responses: {
21
+ /** @description Hello response */
22
+ 200: {
23
+ headers: {
24
+ [name: string]: unknown;
25
+ };
26
+ content: {
27
+ "application/json": {
28
+ /** @example Hello, Alice! */
29
+ message: string;
30
+ project: {
31
+ id: string;
32
+ title: string;
33
+ };
34
+ };
35
+ };
36
+ };
37
+ /** @description Unauthorized */
38
+ 401: {
39
+ headers: {
40
+ [name: string]: unknown;
41
+ };
42
+ };
43
+ };
44
+ };
45
+ };
46
+ "/projects/services": {
47
+ put: {
48
+ requestBody: {
49
+ content: {
50
+ "application/json": {
51
+ services: ({
52
+ /** @enum {string} */
53
+ type: "healthcheck";
54
+ /** Format: uri */
55
+ endpoint: string;
56
+ } | {
57
+ /** @enum {string} */
58
+ type: "openrouter";
59
+ apiKey: string;
60
+ })[];
61
+ title?: string;
62
+ chatId?: string;
63
+ threadForumId?: string;
64
+ };
65
+ };
66
+ };
67
+ responses: {
68
+ /** @description Services updated successfully */
69
+ 200: {
70
+ headers: {
71
+ [name: string]: unknown;
72
+ };
73
+ content: {
74
+ "application/json": {
75
+ success: boolean;
76
+ project: {
77
+ id: string;
78
+ title: string;
79
+ services: ({
80
+ /** @enum {string} */
81
+ type: "healthcheck";
82
+ /** Format: uri */
83
+ endpoint: string;
84
+ } | {
85
+ /** @enum {string} */
86
+ type: "openrouter";
87
+ apiKey: string;
88
+ })[];
89
+ updatedAt: null | string;
90
+ };
91
+ };
92
+ };
93
+ };
94
+ /** @description Unauthorized */
95
+ 401: {
96
+ headers: {
97
+ [name: string]: unknown;
98
+ };
99
+ };
100
+ };
101
+ };
102
+ };
103
+ }
104
+ /**
105
+ * Сгенерированные из OpenAPI типы для `components`
106
+ */
107
+ type components = {};
108
+ type $defs = Record<string, never>;
109
+ type operations = Record<string, never>;
110
+
111
+ export type { $defs, components, operations, paths };
package/dist/index.cjs ADDED
@@ -0,0 +1,60 @@
1
+ 'use strict';
2
+
3
+ class AlertBot {
4
+ options;
5
+ constructor(options) {
6
+ this.options = options;
7
+ }
8
+ async request(path, data, options) {
9
+ const requestOptions = {
10
+ method: options?.method || "POST",
11
+ mimeType: "json",
12
+ ...this.options.requestOptions,
13
+ ...options,
14
+ body: data ? JSON.stringify(data) : void 0,
15
+ headers: {
16
+ "user-agent": "ALERT BOT SDK",
17
+ Authorization: `Bearer ${this.options.apiKey}`,
18
+ ...data ? { "content-type": "application/json" } : {},
19
+ ...this.options.requestOptions?.headers,
20
+ ...options?.headers
21
+ }
22
+ };
23
+ requestOptions.mimeType = void 0;
24
+ const response = await fetch(this.options.host + path, requestOptions);
25
+ if (!response.ok) {
26
+ throw new Error(
27
+ `${this.options.host + path} ${options?.method} ${response.status} ${await response.text()}`
28
+ );
29
+ }
30
+ const contentType = response.headers.get("content-type");
31
+ const length = response.headers.get("content-length");
32
+ if (contentType?.includes("application/json")) {
33
+ return response.json();
34
+ }
35
+ if (length) {
36
+ return response.text();
37
+ }
38
+ return void 0;
39
+ }
40
+ // on<T extends WebhookBodyTypes>(
41
+ // type: T,
42
+ // handler: (context: EventByType[T]) => unknown,
43
+ // ) {
44
+ // // @ts-expect-error
45
+ // this.listeners.push({ event: type, handler });
46
+ // }
47
+ // async emit(data: WebhookBody) {
48
+ // const listeners = this.listeners.find((x) => x.event === data.type);
49
+ // if (listeners) await listeners.handler(data);
50
+ // }
51
+ async updateServices(body) {
52
+ return this.request(
53
+ "/projects/services",
54
+ body,
55
+ { method: "PUT" }
56
+ );
57
+ }
58
+ }
59
+
60
+ exports.AlertBot = AlertBot;
@@ -0,0 +1,44 @@
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
+
26
+ /**
27
+ * @module
28
+ *
29
+ * Библиотека для взаимодействия с Alert Bot API.
30
+ */
31
+
32
+ interface AlertBotOptions {
33
+ apiKey: string;
34
+ host: string;
35
+ requestOptions?: RequestOptions;
36
+ }
37
+ declare class AlertBot {
38
+ private options;
39
+ constructor(options?: AlertBotOptions);
40
+ private request;
41
+ updateServices(body: GetRequestBody<"/projects/services", "put">): Promise<GetResponse<"/projects/services", "put">>;
42
+ }
43
+
44
+ export { AlertBot, type AlertBotOptions };
@@ -0,0 +1,44 @@
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
+
26
+ /**
27
+ * @module
28
+ *
29
+ * Библиотека для взаимодействия с Alert Bot API.
30
+ */
31
+
32
+ interface AlertBotOptions {
33
+ apiKey: string;
34
+ host: string;
35
+ requestOptions?: RequestOptions;
36
+ }
37
+ declare class AlertBot {
38
+ private options;
39
+ constructor(options?: AlertBotOptions);
40
+ private request;
41
+ updateServices(body: GetRequestBody<"/projects/services", "put">): Promise<GetResponse<"/projects/services", "put">>;
42
+ }
43
+
44
+ export { AlertBot, type AlertBotOptions };
package/dist/index.js ADDED
@@ -0,0 +1,58 @@
1
+ class AlertBot {
2
+ options;
3
+ constructor(options) {
4
+ this.options = options;
5
+ }
6
+ async request(path, data, options) {
7
+ const requestOptions = {
8
+ method: options?.method || "POST",
9
+ mimeType: "json",
10
+ ...this.options.requestOptions,
11
+ ...options,
12
+ body: data ? JSON.stringify(data) : void 0,
13
+ headers: {
14
+ "user-agent": "ALERT BOT SDK",
15
+ Authorization: `Bearer ${this.options.apiKey}`,
16
+ ...data ? { "content-type": "application/json" } : {},
17
+ ...this.options.requestOptions?.headers,
18
+ ...options?.headers
19
+ }
20
+ };
21
+ requestOptions.mimeType = void 0;
22
+ const response = await fetch(this.options.host + path, requestOptions);
23
+ if (!response.ok) {
24
+ throw new Error(
25
+ `${this.options.host + path} ${options?.method} ${response.status} ${await response.text()}`
26
+ );
27
+ }
28
+ const contentType = response.headers.get("content-type");
29
+ const length = response.headers.get("content-length");
30
+ if (contentType?.includes("application/json")) {
31
+ return response.json();
32
+ }
33
+ if (length) {
34
+ return response.text();
35
+ }
36
+ return void 0;
37
+ }
38
+ // on<T extends WebhookBodyTypes>(
39
+ // type: T,
40
+ // handler: (context: EventByType[T]) => unknown,
41
+ // ) {
42
+ // // @ts-expect-error
43
+ // this.listeners.push({ event: type, handler });
44
+ // }
45
+ // async emit(data: WebhookBody) {
46
+ // const listeners = this.listeners.find((x) => x.event === data.type);
47
+ // if (listeners) await listeners.handler(data);
48
+ // }
49
+ async updateServices(body) {
50
+ return this.request(
51
+ "/projects/services",
52
+ body,
53
+ { method: "PUT" }
54
+ );
55
+ }
56
+ }
57
+
58
+ export { AlertBot };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@skravets/alert-bot",
3
+ "version": "0.0.1",
4
+ "module": "./dist/index.js",
5
+ "main": "./dist/index.cjs",
6
+ "types": "./dist/index.d.ts",
7
+ "scripts": {
8
+ "generate": "bun scripts/generate.ts",
9
+ "build": "bunx pkgroll",
10
+ "prepublishOnly": "bunx pkgroll"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "import": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ },
18
+ "require": {
19
+ "types": "./dist/index.d.cts",
20
+ "default": "./dist/index.cjs"
21
+ }
22
+ },
23
+ "./openapi": {
24
+ "import": {
25
+ "types": "./dist/api-types.d.ts"
26
+ },
27
+ "require": {
28
+ "types": "./dist/api-types.d.cts"
29
+ }
30
+ }
31
+ },
32
+ "type": "module",
33
+ "devDependencies": {
34
+ "@biomejs/biome": "^1.8.3",
35
+ "@scalar/openapi-parser": "^0.7.2",
36
+ "@types/bun": "^1.1.6",
37
+ "openapi-typescript": "^7.0.2",
38
+ "pkgroll": "^2.1.1",
39
+ "ts-dedent": "^2.2.0",
40
+ "typescript": "^5.5.3"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public"
44
+ },
45
+ "license": "MIT",
46
+ "files": ["dist"]
47
+ }