statocysts 0.6.0 → 0.7.0
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/browser.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as http, c as SenderUrl, d as DefineProviderOptions, f as ServiceProvider, h as defineTransport, i as HttpPayload, l as buildSenderRegistry, m as Transport, n as telegram, o as Sender, p as defineProvider, r as slack, s as SenderRegistry, t as json, u as DefineProviderContext } from "./index-DIqKYuP3.js";
|
|
2
2
|
|
|
3
3
|
//#region src/browser.d.ts
|
|
4
|
+
|
|
4
5
|
declare const senderRegistry: SenderRegistry;
|
|
5
6
|
declare function createSender(urls: SenderUrl[]): Sender;
|
|
6
7
|
declare function send(url: string | URL, title: string, body?: string): Promise<void>;
|
|
7
8
|
//#endregion
|
|
8
|
-
export { DefineProviderContext, DefineProviderOptions, HttpPayload, Sender, SenderRegistry, SenderUrl, ServiceProvider, Transport, buildSenderRegistry, createSender, defineProvider, defineTransport, http, json, send, senderRegistry, slack };
|
|
9
|
+
export { DefineProviderContext, DefineProviderOptions, HttpPayload, Sender, SenderRegistry, SenderUrl, ServiceProvider, Transport, buildSenderRegistry, createSender, defineProvider, defineTransport, http, json, send, senderRegistry, slack, telegram };
|
package/dist/browser.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as http, c as assert, i as slack, n as json, o as defineTransport, r as telegram, s as defineProvider, t as buildSenderRegistry } from "./shared-DBiCYY_-.js";
|
|
2
2
|
|
|
3
3
|
//#region src/browser.ts
|
|
4
|
-
const senderRegistry = buildSenderRegistry([
|
|
4
|
+
const senderRegistry = buildSenderRegistry([
|
|
5
|
+
json,
|
|
6
|
+
slack,
|
|
7
|
+
telegram
|
|
8
|
+
]);
|
|
5
9
|
function createSender(urls) {
|
|
6
10
|
return senderRegistry(urls);
|
|
7
11
|
}
|
|
@@ -17,4 +21,4 @@ async function send(url, title, body) {
|
|
|
17
21
|
}
|
|
18
22
|
|
|
19
23
|
//#endregion
|
|
20
|
-
export { buildSenderRegistry, createSender, defineProvider, defineTransport, http, json, send, senderRegistry, slack };
|
|
24
|
+
export { buildSenderRegistry, createSender, defineProvider, defineTransport, http, json, send, senderRegistry, slack, telegram };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as ofetch0 from "ofetch";
|
|
2
2
|
import { FetchOptions } from "ofetch";
|
|
3
|
+
import z from "zod";
|
|
3
4
|
|
|
4
5
|
//#region src/core/transport.d.ts
|
|
5
6
|
/**
|
|
@@ -89,7 +90,19 @@ interface SlackOptions {
|
|
|
89
90
|
}
|
|
90
91
|
declare const slack: ServiceProvider<"slack:", HttpPayload, SlackOptions>;
|
|
91
92
|
//#endregion
|
|
93
|
+
//#region src/services/chat/telegram/index.d.ts
|
|
94
|
+
interface TelegramOptions {
|
|
95
|
+
/**
|
|
96
|
+
* The base URL for Telegram Bot API
|
|
97
|
+
*
|
|
98
|
+
* @default `https://api.telegram.org`
|
|
99
|
+
*/
|
|
100
|
+
apiBaseUrl?: string;
|
|
101
|
+
fetchOptions?: FetchOptions;
|
|
102
|
+
}
|
|
103
|
+
declare const telegram: ServiceProvider<"telegram:", HttpPayload, TelegramOptions>;
|
|
104
|
+
//#endregion
|
|
92
105
|
//#region src/services/specialized/json/index.d.ts
|
|
93
106
|
declare const json: ServiceProvider<"json:", HttpPayload, FetchOptions<ofetch0.ResponseType, any>>;
|
|
94
107
|
//#endregion
|
|
95
|
-
export {
|
|
108
|
+
export { http as a, SenderUrl as c, DefineProviderOptions as d, ServiceProvider as f, defineTransport as h, HttpPayload as i, buildSenderRegistry as l, Transport as m, telegram as n, Sender as o, defineProvider as p, slack as r, SenderRegistry as s, json as t, DefineProviderContext as u };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as http, c as SenderUrl, d as DefineProviderOptions, f as ServiceProvider, h as defineTransport, i as HttpPayload, l as buildSenderRegistry, m as Transport, n as telegram, o as Sender, p as defineProvider, r as slack, s as SenderRegistry, t as json, u as DefineProviderContext } from "./index-DIqKYuP3.js";
|
|
2
2
|
|
|
3
3
|
//#region src/index.d.ts
|
|
4
|
+
|
|
4
5
|
declare const senderRegistry: SenderRegistry;
|
|
5
6
|
declare function createSender(urls: SenderUrl[]): Sender;
|
|
6
7
|
declare function send(url: string | URL, title: string, body?: string): Promise<void>;
|
|
7
8
|
//#endregion
|
|
8
|
-
export { DefineProviderContext, DefineProviderOptions, HttpPayload, Sender, SenderRegistry, SenderUrl, ServiceProvider, Transport, buildSenderRegistry, createSender, defineProvider, defineTransport, http, json, send, senderRegistry, slack };
|
|
9
|
+
export { DefineProviderContext, DefineProviderOptions, HttpPayload, Sender, SenderRegistry, SenderUrl, ServiceProvider, Transport, buildSenderRegistry, createSender, defineProvider, defineTransport, http, json, send, senderRegistry, slack, telegram };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as http, c as assert, i as slack, n as json, o as defineTransport, r as telegram, s as defineProvider, t as buildSenderRegistry } from "./shared-DBiCYY_-.js";
|
|
2
2
|
|
|
3
3
|
//#region src/index.ts
|
|
4
|
-
const senderRegistry = buildSenderRegistry([
|
|
4
|
+
const senderRegistry = buildSenderRegistry([
|
|
5
|
+
json,
|
|
6
|
+
slack,
|
|
7
|
+
telegram
|
|
8
|
+
]);
|
|
5
9
|
function createSender(urls) {
|
|
6
10
|
return senderRegistry(urls);
|
|
7
11
|
}
|
|
@@ -17,4 +21,4 @@ async function send(url, title, body) {
|
|
|
17
21
|
}
|
|
18
22
|
|
|
19
23
|
//#endregion
|
|
20
|
-
export { buildSenderRegistry, createSender, defineProvider, defineTransport, http, json, send, senderRegistry, slack };
|
|
24
|
+
export { buildSenderRegistry, createSender, defineProvider, defineTransport, http, json, send, senderRegistry, slack, telegram };
|
|
@@ -1,12 +1,28 @@
|
|
|
1
|
+
import { getQuery, withProtocol, withoutLeadingSlash } from "ufo";
|
|
1
2
|
import defu from "defu";
|
|
2
3
|
import { ofetch } from "ofetch";
|
|
3
|
-
import
|
|
4
|
+
import z from "zod";
|
|
4
5
|
|
|
5
6
|
//#region src/utils/assert.ts
|
|
6
7
|
function assert(condition, message) {
|
|
7
8
|
if (!condition) throw typeof message === "string" ? new Error(message) : message;
|
|
8
9
|
}
|
|
9
10
|
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/utils/markdown.ts
|
|
13
|
+
/**
|
|
14
|
+
* Escape special characters: _*[]()~`>#+-=|{}.!
|
|
15
|
+
*/
|
|
16
|
+
function escapeMarkdown(text) {
|
|
17
|
+
return text.replace(/([_*[\]()~`>#+\-=|{}.!])/g, "\\$1");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/utils/url.ts
|
|
22
|
+
function getValidateQuery(url, parser) {
|
|
23
|
+
return parser(getQuery(url.toString()));
|
|
24
|
+
}
|
|
25
|
+
|
|
10
26
|
//#endregion
|
|
11
27
|
//#region src/core/provider.ts
|
|
12
28
|
function defineProvider(protocol, createOptions) {
|
|
@@ -17,7 +33,7 @@ function defineProvider(protocol, createOptions) {
|
|
|
17
33
|
url,
|
|
18
34
|
message
|
|
19
35
|
};
|
|
20
|
-
const opts = defu(
|
|
36
|
+
const opts = defu(options ?? {}, createOptions.defaultOptions ?? {});
|
|
21
37
|
const payload = await createOptions.prepare.call(ctx, ctx, opts);
|
|
22
38
|
await createOptions.transport.send(payload);
|
|
23
39
|
};
|
|
@@ -112,6 +128,57 @@ const slack = defineProvider("slack:", {
|
|
|
112
128
|
}
|
|
113
129
|
});
|
|
114
130
|
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/services/chat/telegram/index.ts
|
|
133
|
+
const telegramQuerySchema = z.object({ parse_mode: z.enum([
|
|
134
|
+
"Markdown",
|
|
135
|
+
"MarkdownV2",
|
|
136
|
+
"HTML"
|
|
137
|
+
]).optional() });
|
|
138
|
+
const telegram = defineProvider("telegram:", {
|
|
139
|
+
transport: http,
|
|
140
|
+
defaultOptions: { apiBaseUrl: "https://api.telegram.org" },
|
|
141
|
+
async prepare(_, options) {
|
|
142
|
+
const { url } = this;
|
|
143
|
+
assert(url.hostname === "bot", `Invalid telegram URL: ${url.toString()}`);
|
|
144
|
+
assert(url.username, "Bot token is required");
|
|
145
|
+
const pathSegments = url.pathname.split("/").filter(Boolean);
|
|
146
|
+
assert(pathSegments.length > 0, "At least one chat ID is required");
|
|
147
|
+
const queryResult = getValidateQuery(url, telegramQuerySchema.safeParse);
|
|
148
|
+
if (!queryResult.success) throw new Error("Invalid telegram query");
|
|
149
|
+
const query = queryResult.data;
|
|
150
|
+
const botToken = `${url.username}:${url.password}`;
|
|
151
|
+
const chatPart = decodeURIComponent(pathSegments[0]);
|
|
152
|
+
const [chatId, messageThreadId] = chatPart.includes(":") ? chatPart.split(":", 2) : [chatPart, void 0];
|
|
153
|
+
const requestUrl = new URL(`/bot${botToken}/sendMessage`, options.apiBaseUrl);
|
|
154
|
+
const headers = new Headers([["Content-Type", "application/json"]]);
|
|
155
|
+
let text;
|
|
156
|
+
if (this.message.body) {
|
|
157
|
+
const parseMode = query.parse_mode;
|
|
158
|
+
let titleFormatted;
|
|
159
|
+
if (parseMode === "HTML") titleFormatted = `<b>${this.message.title}</b>`;
|
|
160
|
+
else if (parseMode === "MarkdownV2") titleFormatted = `*${escapeMarkdown(this.message.title)}*`;
|
|
161
|
+
else titleFormatted = `*${this.message.title}*`;
|
|
162
|
+
text = `${titleFormatted}\n\n${this.message.body}`;
|
|
163
|
+
} else if (query.parse_mode === "MarkdownV2") text = escapeMarkdown(this.message.title);
|
|
164
|
+
else text = this.message.title;
|
|
165
|
+
const body = {
|
|
166
|
+
chat_id: chatId,
|
|
167
|
+
text,
|
|
168
|
+
parse_mode: query.parse_mode
|
|
169
|
+
};
|
|
170
|
+
if (messageThreadId) body.message_thread_id = Number.parseInt(messageThreadId, 10);
|
|
171
|
+
return {
|
|
172
|
+
request: new Request(requestUrl, {
|
|
173
|
+
method: "POST",
|
|
174
|
+
headers,
|
|
175
|
+
body: JSON.stringify(body)
|
|
176
|
+
}),
|
|
177
|
+
fetchOptions: options.fetchOptions
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
|
|
115
182
|
//#endregion
|
|
116
183
|
//#region src/services/specialized/json/index.ts
|
|
117
184
|
const json = defineProvider("json:", {
|
|
@@ -177,4 +244,4 @@ function buildSenderRegistry(providers) {
|
|
|
177
244
|
}
|
|
178
245
|
|
|
179
246
|
//#endregion
|
|
180
|
-
export {
|
|
247
|
+
export { http as a, assert as c, slack as i, json as n, defineTransport as o, telegram as r, defineProvider as s, buildSenderRegistry as t };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "statocysts",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"description": "Notification library for JavaScript",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/octoplorer/statocysts",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"zod": "^4.2.1"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@types/node": "^25.0.
|
|
55
|
+
"@types/node": "^25.0.3",
|
|
56
56
|
"@vitest/coverage-v8": "4.0.16",
|
|
57
57
|
"tsdown": "^0.18.1",
|
|
58
58
|
"typescript": "^5.9.3",
|