statocysts 0.0.4 → 0.0.5

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/index.d.ts CHANGED
@@ -1 +1,31 @@
1
- export { };
1
+ import { FetchOptions } from "ofetch";
2
+ import { z } from "zod";
3
+
4
+ //#region src/sender.d.ts
5
+ type Protocol = 'generic:' | 'slack:';
6
+ declare const SUPPORTED_PROTOCOLS: string[];
7
+ declare function send(url: string | URL, message: string, options?: FetchOptions): Promise<void>;
8
+ //#endregion
9
+ //#region src/services/chat/slack.d.ts
10
+ declare const slackOptionsSchema: z.ZodObject<{
11
+ botname: z.ZodOptional<z.ZodString>;
12
+ icon: z.ZodOptional<z.ZodString>;
13
+ color: z.ZodOptional<z.ZodString>;
14
+ title: z.ZodOptional<z.ZodString>;
15
+ thread_ts: z.ZodOptional<z.ZodString>;
16
+ }, z.core.$strip>;
17
+ type SlackOptions = z.infer<typeof slackOptionsSchema>;
18
+ declare function buildSlackRequest(url: URL, message: string): Request;
19
+ //#endregion
20
+ //#region src/services/specialized/generic.d.ts
21
+ declare const genericOptionsSchema: z.ZodObject<{
22
+ template: z.ZodDefault<z.ZodOptional<z.ZodEnum<{
23
+ json: "json";
24
+ plaintext: "plaintext";
25
+ }>>>;
26
+ method: z.ZodDefault<z.ZodOptional<z.ZodString>>;
27
+ }, z.core.$strip>;
28
+ type GenericOptions = z.infer<typeof genericOptionsSchema>;
29
+ declare function buildGenericRequest(url: URL, message: string): Request;
30
+ //#endregion
31
+ export { GenericOptions, Protocol, SUPPORTED_PROTOCOLS, SlackOptions, buildGenericRequest, buildSlackRequest, send };
package/dist/index.js CHANGED
@@ -0,0 +1,145 @@
1
+ import { ofetch } from "ofetch";
2
+ import { z } from "zod";
3
+
4
+ //#region src/utils/assert.ts
5
+ function assert(condition, message) {
6
+ if (!condition) throw typeof message === "string" ? new Error(message) : message;
7
+ }
8
+
9
+ //#endregion
10
+ //#region src/services/chat/slack.ts
11
+ const slackOptionsSchema = z.object({
12
+ botname: z.string().optional(),
13
+ icon: z.string().optional(),
14
+ color: z.string().optional(),
15
+ title: z.string().optional(),
16
+ thread_ts: z.string().optional()
17
+ });
18
+ function isBotApiFormat(url) {
19
+ return url.username === "xoxb";
20
+ }
21
+ function isWebhookFormat(url) {
22
+ return url.username === "hook";
23
+ }
24
+ function parseBotToken(url) {
25
+ assert(url.password, "Bot token is required");
26
+ return url.password;
27
+ }
28
+ function parseWebhookToken(url) {
29
+ assert(url.password, "Webhook token is required");
30
+ const parts = url.password.split("-");
31
+ assert(parts.length === 3, "Invalid webhook token format");
32
+ return {
33
+ id: parts[0],
34
+ token: parts[1],
35
+ secret: parts[2]
36
+ };
37
+ }
38
+ function buildBotApiRequest(url, message, options) {
39
+ const token = parseBotToken(url);
40
+ const body = {
41
+ channel: url.hostname,
42
+ text: message
43
+ };
44
+ if (options.botname) body.username = options.botname;
45
+ if (options.icon) if (options.icon.startsWith(":") && options.icon.endsWith(":")) body.icon_emoji = options.icon;
46
+ else body.icon_url = options.icon;
47
+ if (options.thread_ts) body.thread_ts = options.thread_ts;
48
+ const attachments = [];
49
+ if (options.color || options.title) {
50
+ const attachment = {};
51
+ if (options.color) attachment.color = options.color.startsWith("%23") ? decodeURIComponent(options.color) : options.color;
52
+ if (options.title) attachment.title = options.title;
53
+ attachments.push(attachment);
54
+ }
55
+ if (attachments.length > 0) body.attachments = attachments;
56
+ return new Request("https://slack.com/api/chat.postMessage", {
57
+ method: "POST",
58
+ headers: {
59
+ "Authorization": `Bearer xoxb-${token}`,
60
+ "Content-Type": "application/json"
61
+ },
62
+ body: JSON.stringify(body)
63
+ });
64
+ }
65
+ function buildWebhookRequest(url, message, options) {
66
+ const { id, token, secret } = parseWebhookToken(url);
67
+ const body = { text: message };
68
+ if (options.botname) body.username = options.botname;
69
+ if (options.icon) if (options.icon.startsWith(":") && options.icon.endsWith(":")) body.icon_emoji = options.icon;
70
+ else body.icon_url = options.icon;
71
+ const attachments = [];
72
+ if (options.color || options.title) {
73
+ const attachment = {};
74
+ if (options.color) attachment.color = options.color.startsWith("%23") ? decodeURIComponent(options.color) : options.color;
75
+ if (options.title) attachment.title = options.title;
76
+ attachments.push(attachment);
77
+ }
78
+ if (attachments.length > 0) body.attachments = attachments;
79
+ return new Request(`https://hooks.slack.com/services/${id}/${token}/${secret}`, {
80
+ method: "POST",
81
+ headers: { "Content-Type": "application/json" },
82
+ body: JSON.stringify(body)
83
+ });
84
+ }
85
+ function buildSlackRequest(url, message) {
86
+ assert(url.protocol === "slack:", `Unexpected protocol ${url.protocol}`);
87
+ const params = new URLSearchParams(url.search);
88
+ const options = slackOptionsSchema.parse(Object.fromEntries(params.entries()));
89
+ if (isBotApiFormat(url)) return buildBotApiRequest(url, message, options);
90
+ if (isWebhookFormat(url)) return buildWebhookRequest(url, message, options);
91
+ throw new Error(`Unsupported Slack URL format: ${url.toString()}`);
92
+ }
93
+
94
+ //#endregion
95
+ //#region src/services/specialized/generic.ts
96
+ const genericOptionsSchema = z.object({
97
+ template: z.enum(["json", "plaintext"]).optional().default("json"),
98
+ method: z.string().optional().default("POST")
99
+ });
100
+ function isOptionParam(key) {
101
+ return !key.startsWith("@") && !key.startsWith("$");
102
+ }
103
+ function buildGenericRequest(url, message) {
104
+ assert(url.protocol === "generic:", `Unexpected protocol ${url.protocol}`);
105
+ const params = new URLSearchParams(url.hash.slice(1));
106
+ const allParamsEntries = Array.from(params.entries());
107
+ const optionsParams = Object.fromEntries(allParamsEntries.filter(([key]) => isOptionParam(key)));
108
+ const options = genericOptionsSchema.parse(optionsParams);
109
+ const headers = Object.fromEntries(allParamsEntries.filter(([key]) => key.startsWith("@")).map(([key, value]) => [key.slice(1), value]));
110
+ const dataProperties = Object.fromEntries(allParamsEntries.filter(([key]) => key.startsWith("$")).map(([key, value]) => [key.slice(1), value]));
111
+ const targetUrl = new URL(url.host + url.pathname);
112
+ return new Request(targetUrl.toString(), {
113
+ method: options.method,
114
+ headers: {
115
+ "Content-Type": options.template === "json" ? "application/json" : "text/plain",
116
+ ...headers
117
+ },
118
+ body: options.template === "json" ? JSON.stringify({
119
+ ...dataProperties,
120
+ message
121
+ }) : message
122
+ });
123
+ }
124
+
125
+ //#endregion
126
+ //#region src/sender.ts
127
+ const SUPPORTED_PROTOCOLS = ["generic:", "slack:"];
128
+ async function send(url, message, options) {
129
+ const _url = typeof url === "string" ? new URL(url) : url;
130
+ assert(SUPPORTED_PROTOCOLS.includes(_url.protocol), `Unsupported protocol ${_url.protocol}`);
131
+ let req;
132
+ switch (_url.protocol) {
133
+ case "generic:":
134
+ req = buildGenericRequest(_url, message);
135
+ break;
136
+ case "slack:":
137
+ req = buildSlackRequest(_url, message);
138
+ break;
139
+ default: throw new Error(`Unsupported protocol ${_url.protocol}`);
140
+ }
141
+ await ofetch(req, options);
142
+ }
143
+
144
+ //#endregion
145
+ export { SUPPORTED_PROTOCOLS, buildGenericRequest, buildSlackRequest, send };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "statocysts",
3
3
  "type": "module",
4
- "version": "0.0.4",
4
+ "version": "0.0.5",
5
5
  "description": "Notification library for JavaScript",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/octoplorer/statocysts",
@@ -32,8 +32,13 @@
32
32
  "engines": {
33
33
  "node": ">=24.12.0"
34
34
  },
35
+ "dependencies": {
36
+ "ofetch": "^1.5.1",
37
+ "zod": "^4.2.1"
38
+ },
35
39
  "devDependencies": {
36
40
  "@antfu/eslint-config": "^6.7.1",
41
+ "@types/node": "^25.0.2",
37
42
  "bumpp": "^10.3.2",
38
43
  "eslint": "^9.39.2",
39
44
  "eslint-plugin-format": "^1.1.0",