statocysts 0.3.0 → 0.4.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/LICENSE +21 -0
- package/dist/browser.d.ts +2 -2
- package/dist/browser.js +2 -2
- package/dist/index-I_frtEQ-.d.ts +95 -0
- package/dist/index.d.ts +3 -4
- package/dist/index.js +5 -5
- package/dist/shared-DsBK2dP_.js +164 -0
- package/package.json +2 -2
- package/dist/index-CK1eOwNP.d.ts +0 -63
- package/dist/shared-BiiZsNzb.js +0 -135
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-PRESENT Ming Lin <https://github.com/enpitsuLin>
|
|
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/dist/browser.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as Sender, c as buildSenderRegistry, d as ServiceProvider, f as defineProvider, i as http, l as DefineProviderContext, m as defineTransport, n as slack, o as SenderRegistry, p as Transport, r as HttpPayload, s as SenderUrl, t as json, u as DefineProviderOptions } from "./index-I_frtEQ-.js";
|
|
2
2
|
import { FetchOptions } from "ofetch";
|
|
3
3
|
|
|
4
4
|
//#region src/browser.d.ts
|
|
@@ -6,4 +6,4 @@ declare const senderRegistry: SenderRegistry;
|
|
|
6
6
|
declare function createSender(urls: SenderUrl[]): Sender;
|
|
7
7
|
declare function send(url: string | URL, message: string, options?: FetchOptions): Promise<void>;
|
|
8
8
|
//#endregion
|
|
9
|
-
export {
|
|
9
|
+
export { DefineProviderContext, DefineProviderOptions, HttpPayload, Sender, SenderRegistry, SenderUrl, ServiceProvider, Transport, buildSenderRegistry, createSender, defineProvider, defineTransport, http, json, send, senderRegistry, slack };
|
package/dist/browser.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as defineTransport, i as http, n as json, o as defineProvider, r as slack, s as assert, t as buildSenderRegistry } from "./shared-DsBK2dP_.js";
|
|
2
2
|
import { ofetch } from "ofetch";
|
|
3
3
|
|
|
4
4
|
//#region src/browser.ts
|
|
@@ -14,4 +14,4 @@ async function send(url, message, options) {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
//#endregion
|
|
17
|
-
export {
|
|
17
|
+
export { buildSenderRegistry, createSender, defineProvider, defineTransport, http, json, send, senderRegistry, slack };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import * as ofetch0 from "ofetch";
|
|
2
|
+
import { FetchOptions } from "ofetch";
|
|
3
|
+
|
|
4
|
+
//#region src/core/transport.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Transport interface for protocol-specific network communication
|
|
7
|
+
* Each transport handles the actual sending of data over a specific protocol
|
|
8
|
+
*/
|
|
9
|
+
interface Transport<Payload = unknown> {
|
|
10
|
+
/**
|
|
11
|
+
* Send data using the transport's protocol
|
|
12
|
+
* @param payload Protocol-specific payload to send
|
|
13
|
+
*/
|
|
14
|
+
send: (payload: Payload) => Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
declare function defineTransport<Payload>(transport: Transport<Payload>): Transport<Payload>;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/core/provider.d.ts
|
|
19
|
+
type InferTransportPayload<T> = T extends Transport<infer P> ? P : never;
|
|
20
|
+
interface ServiceProvider<Protocol extends string, Payload, Options = void> {
|
|
21
|
+
readonly $transport: Transport<Payload>;
|
|
22
|
+
readonly protocol: Protocol;
|
|
23
|
+
defaultOptions: Options | undefined;
|
|
24
|
+
send: (url: string, message: {
|
|
25
|
+
title: string;
|
|
26
|
+
body?: string;
|
|
27
|
+
}, options?: Options) => Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
interface DefineProviderContext {
|
|
30
|
+
url: URL;
|
|
31
|
+
message: {
|
|
32
|
+
title: string;
|
|
33
|
+
body?: string;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
interface DefineProviderOptions<T extends Transport, Options> {
|
|
37
|
+
defaultOptions?: Options;
|
|
38
|
+
transport: T;
|
|
39
|
+
prepare: (this: DefineProviderContext, ctx: DefineProviderContext, options: Options) => Promise<InferTransportPayload<T>>;
|
|
40
|
+
}
|
|
41
|
+
declare function defineProvider<const Protocol extends string, T extends Transport<any>, Options = void>(protocol: Protocol, createOptions: DefineProviderOptions<T, Options>): ServiceProvider<Protocol, InferTransportPayload<T>, Options>;
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/core/sender.d.ts
|
|
44
|
+
interface Sender {
|
|
45
|
+
send: (message: string, options?: FetchOptions) => Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
type SenderUrl = string | URL;
|
|
48
|
+
interface SenderRegistry {
|
|
49
|
+
(urls: SenderUrl[]): Sender;
|
|
50
|
+
resolveProvider: (url: string | URL) => ServiceProvider<string, any, any> | undefined;
|
|
51
|
+
}
|
|
52
|
+
declare function buildSenderRegistry(providers: ServiceProvider<string, any, any>[]): SenderRegistry;
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/core/transports/http.d.ts
|
|
55
|
+
/**
|
|
56
|
+
* HTTP payload for HTTP transport
|
|
57
|
+
*/
|
|
58
|
+
interface HttpPayload {
|
|
59
|
+
request: Request;
|
|
60
|
+
fetchOptions?: FetchOptions;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* HTTP transport implementation
|
|
64
|
+
* Handles sending data over HTTP/HTTPS protocols
|
|
65
|
+
*/
|
|
66
|
+
declare const http: Transport<HttpPayload>;
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/services/chat/slack/index.d.ts
|
|
69
|
+
interface SlackOptions {
|
|
70
|
+
/**
|
|
71
|
+
* The base URL for webhook services
|
|
72
|
+
*
|
|
73
|
+
* @default `https://hooks.slack.com/services`
|
|
74
|
+
*/
|
|
75
|
+
hookBaseUrl?: string;
|
|
76
|
+
/**
|
|
77
|
+
* The base URL for bot API services
|
|
78
|
+
*
|
|
79
|
+
* @default `https://slack.com/api`
|
|
80
|
+
*/
|
|
81
|
+
botApiBaseUrl?: string;
|
|
82
|
+
/**
|
|
83
|
+
* The body of the request
|
|
84
|
+
*
|
|
85
|
+
* NOTICE*: This will override the body of the request. You should known what you are doing.
|
|
86
|
+
*/
|
|
87
|
+
body?: Record<string, any>;
|
|
88
|
+
fetchOptions?: FetchOptions;
|
|
89
|
+
}
|
|
90
|
+
declare const slack: ServiceProvider<"slack:", HttpPayload, SlackOptions>;
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/services/specialized/json/index.d.ts
|
|
93
|
+
declare const json: ServiceProvider<"json:", HttpPayload, FetchOptions<ofetch0.ResponseType, any>>;
|
|
94
|
+
//#endregion
|
|
95
|
+
export { Sender as a, buildSenderRegistry as c, ServiceProvider as d, defineProvider as f, http as i, DefineProviderContext as l, defineTransport as m, slack as n, SenderRegistry as o, Transport as p, HttpPayload as r, SenderUrl as s, json as t, DefineProviderOptions as u };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
import { FetchOptions } from "ofetch";
|
|
1
|
+
import { a as Sender, c as buildSenderRegistry, d as ServiceProvider, f as defineProvider, i as http, l as DefineProviderContext, m as defineTransport, n as slack, o as SenderRegistry, p as Transport, r as HttpPayload, s as SenderUrl, t as json, u as DefineProviderOptions } from "./index-I_frtEQ-.js";
|
|
3
2
|
|
|
4
3
|
//#region src/index.d.ts
|
|
5
4
|
declare const senderRegistry: SenderRegistry;
|
|
6
5
|
declare function createSender(urls: SenderUrl[]): Sender;
|
|
7
|
-
declare function send(url: string | URL, message: string
|
|
6
|
+
declare function send(url: string | URL, message: string): Promise<void>;
|
|
8
7
|
//#endregion
|
|
9
|
-
export {
|
|
8
|
+
export { DefineProviderContext, DefineProviderOptions, HttpPayload, Sender, SenderRegistry, SenderUrl, ServiceProvider, Transport, buildSenderRegistry, createSender, defineProvider, defineTransport, http, json, send, senderRegistry, slack };
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
import { ofetch } from "ofetch";
|
|
1
|
+
import { a as defineTransport, i as http, n as json, o as defineProvider, r as slack, s as assert, t as buildSenderRegistry } from "./shared-DsBK2dP_.js";
|
|
3
2
|
|
|
4
3
|
//#region src/index.ts
|
|
5
4
|
const senderRegistry = buildSenderRegistry([json, slack]);
|
|
6
5
|
function createSender(urls) {
|
|
7
6
|
return senderRegistry(urls);
|
|
8
7
|
}
|
|
9
|
-
async function send(url, message
|
|
8
|
+
async function send(url, message) {
|
|
10
9
|
const _url = typeof url === "string" ? new URL(url) : url;
|
|
11
10
|
const provider = senderRegistry.resolveProvider(_url);
|
|
12
11
|
assert(provider, `Unsupported protocol ${_url.protocol}`);
|
|
13
|
-
|
|
12
|
+
const messageObj = { title: message };
|
|
13
|
+
await provider.send(_url.toString(), messageObj);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
//#endregion
|
|
17
|
-
export {
|
|
17
|
+
export { buildSenderRegistry, createSender, defineProvider, defineTransport, http, json, send, senderRegistry, slack };
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import defu from "defu";
|
|
2
|
+
import { ofetch } from "ofetch";
|
|
3
|
+
import { withProtocol, withoutLeadingSlash } from "ufo";
|
|
4
|
+
|
|
5
|
+
//#region src/utils/assert.ts
|
|
6
|
+
function assert(condition, message) {
|
|
7
|
+
if (!condition) throw typeof message === "string" ? new Error(message) : message;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/core/provider.ts
|
|
12
|
+
function defineProvider(protocol, createOptions) {
|
|
13
|
+
const send = async (protocolUrl, message, options) => {
|
|
14
|
+
const url = new URL(protocolUrl);
|
|
15
|
+
assert(url.protocol === protocol, `Unexpected protocol "${url.protocol}"`);
|
|
16
|
+
const ctx = {
|
|
17
|
+
url,
|
|
18
|
+
message
|
|
19
|
+
};
|
|
20
|
+
const opts = defu(createOptions.defaultOptions ?? {}, options ?? {});
|
|
21
|
+
const payload = await createOptions.prepare.call(ctx, ctx, opts);
|
|
22
|
+
await createOptions.transport.send(payload);
|
|
23
|
+
};
|
|
24
|
+
return {
|
|
25
|
+
get protocol() {
|
|
26
|
+
return protocol;
|
|
27
|
+
},
|
|
28
|
+
get defaultOptions() {
|
|
29
|
+
return createOptions.defaultOptions;
|
|
30
|
+
},
|
|
31
|
+
get $transport() {
|
|
32
|
+
return createOptions.transport;
|
|
33
|
+
},
|
|
34
|
+
send
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/core/transport.ts
|
|
40
|
+
function defineTransport(transport) {
|
|
41
|
+
return transport;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/core/transports/http.ts
|
|
46
|
+
/**
|
|
47
|
+
* HTTP transport implementation
|
|
48
|
+
* Handles sending data over HTTP/HTTPS protocols
|
|
49
|
+
*/
|
|
50
|
+
const http = defineTransport({ async send(payload) {
|
|
51
|
+
await ofetch(payload.request, payload.fetchOptions);
|
|
52
|
+
} });
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/services/chat/slack/index.ts
|
|
56
|
+
const slack = defineProvider("slack:", {
|
|
57
|
+
transport: http,
|
|
58
|
+
defaultOptions: {
|
|
59
|
+
hookBaseUrl: "https://hooks.slack.com/",
|
|
60
|
+
botApiBaseUrl: "https://slack.com/"
|
|
61
|
+
},
|
|
62
|
+
async prepare(_, options) {
|
|
63
|
+
const { url } = this;
|
|
64
|
+
assert(url.hostname === "bot" || url.hostname === "webhook", `Invalid slack URL: ${url.toString()}`);
|
|
65
|
+
const type = url.hostname;
|
|
66
|
+
if (type === "bot") {
|
|
67
|
+
assert(url.username, "Channel ID is required");
|
|
68
|
+
assert(url.password, "Bot token is required");
|
|
69
|
+
} else assert(url.pathname.split("/").filter(Boolean).length === 3, "Webhook URL is invalid");
|
|
70
|
+
let requestUrl;
|
|
71
|
+
const headers = new Headers([["Content-Type", "application/json"]]);
|
|
72
|
+
const body = { text: this.message.title };
|
|
73
|
+
if (type === "bot") {
|
|
74
|
+
const { username: channel, password: token, searchParams } = url;
|
|
75
|
+
requestUrl = new URL("/api/chat.postMessage", options.botApiBaseUrl);
|
|
76
|
+
searchParams.forEach((value, key) => {
|
|
77
|
+
requestUrl.searchParams.set(key, value);
|
|
78
|
+
});
|
|
79
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
80
|
+
body.channel = channel;
|
|
81
|
+
} else {
|
|
82
|
+
const { searchParams } = url;
|
|
83
|
+
requestUrl = new URL(`/services/${withoutLeadingSlash(url.pathname)}`, options.hookBaseUrl);
|
|
84
|
+
searchParams.forEach((value, key) => {
|
|
85
|
+
requestUrl.searchParams.set(key, value);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
request: new Request(requestUrl, {
|
|
90
|
+
method: "POST",
|
|
91
|
+
headers,
|
|
92
|
+
body: JSON.stringify(options.body ?? body)
|
|
93
|
+
}),
|
|
94
|
+
fetchOptions: options.fetchOptions
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/services/specialized/json/index.ts
|
|
101
|
+
const json = defineProvider("json:", {
|
|
102
|
+
transport: http,
|
|
103
|
+
defaultOptions: {},
|
|
104
|
+
async prepare(_, options) {
|
|
105
|
+
const url = new URL(this.url);
|
|
106
|
+
const headers = new Headers([["Content-Type", "application/json"]]);
|
|
107
|
+
const body = { title: this.message.title };
|
|
108
|
+
if (this.message.body) body.body = this.message.body;
|
|
109
|
+
Array.from(url.searchParams.entries()).forEach(([key, value]) => {
|
|
110
|
+
if (key.startsWith(" ")) {
|
|
111
|
+
url.searchParams.delete(key);
|
|
112
|
+
const headerKey = key.slice(1);
|
|
113
|
+
if (headers.has(headerKey)) headers.set(headerKey, value);
|
|
114
|
+
else headers.append(headerKey, value);
|
|
115
|
+
} else if (key.startsWith(":")) {
|
|
116
|
+
url.searchParams.delete(key);
|
|
117
|
+
const propertyKey = key.slice(1);
|
|
118
|
+
body[propertyKey] = value;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
const requestUrl = withProtocol(url.toString(), "https:");
|
|
122
|
+
return {
|
|
123
|
+
request: new Request(requestUrl, {
|
|
124
|
+
method: "POST",
|
|
125
|
+
body: JSON.stringify(body),
|
|
126
|
+
headers
|
|
127
|
+
}),
|
|
128
|
+
fetchOptions: options
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region src/core/sender.ts
|
|
135
|
+
function buildSenderRegistry(providers) {
|
|
136
|
+
const providersRegistry = /* @__PURE__ */ new Map();
|
|
137
|
+
providers.forEach((provider) => {
|
|
138
|
+
providersRegistry.set(provider.protocol, provider);
|
|
139
|
+
});
|
|
140
|
+
function resolveProvider(url) {
|
|
141
|
+
const _url = typeof url === "string" ? new URL(url) : url;
|
|
142
|
+
return providersRegistry.get(_url.protocol);
|
|
143
|
+
}
|
|
144
|
+
function createSender(urls) {
|
|
145
|
+
const registries = urls.map((url) => {
|
|
146
|
+
const provider = resolveProvider(url);
|
|
147
|
+
if (!provider) return;
|
|
148
|
+
return {
|
|
149
|
+
provider,
|
|
150
|
+
url
|
|
151
|
+
};
|
|
152
|
+
}).filter((p) => !!p);
|
|
153
|
+
return { async send(message, options) {
|
|
154
|
+
for (const registry of registries) {
|
|
155
|
+
const messageObj = { title: message };
|
|
156
|
+
await registry.provider.send(registry.url.toString(), messageObj, options);
|
|
157
|
+
}
|
|
158
|
+
} };
|
|
159
|
+
}
|
|
160
|
+
return Object.assign(createSender, { resolveProvider });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
//#endregion
|
|
164
|
+
export { defineTransport as a, http as i, json as n, defineProvider as o, slack as r, assert 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.4.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
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@types/node": "^25.0.2",
|
|
55
|
-
"@vitest/coverage-v8": "4.0.
|
|
55
|
+
"@vitest/coverage-v8": "4.0.16",
|
|
56
56
|
"tsdown": "^0.18.0",
|
|
57
57
|
"typescript": "^5.9.3",
|
|
58
58
|
"vitest": "^4.0.15"
|
package/dist/index-CK1eOwNP.d.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { FetchOptions } from "ofetch";
|
|
2
|
-
|
|
3
|
-
//#region src/core/provider.d.ts
|
|
4
|
-
interface ServiceProvider<Protocol extends string, Data = unknown, Options = unknown> {
|
|
5
|
-
readonly protocol: Protocol;
|
|
6
|
-
$infer: Data;
|
|
7
|
-
defaultOptions: Options | undefined;
|
|
8
|
-
buildRequest: (url: string, message: string, options?: Options) => Request;
|
|
9
|
-
}
|
|
10
|
-
interface DefineProviderContext<Data = unknown> {
|
|
11
|
-
url: URL;
|
|
12
|
-
message: string;
|
|
13
|
-
data: Data;
|
|
14
|
-
}
|
|
15
|
-
interface DefineProviderOptions<Data = unknown, Options = unknown> {
|
|
16
|
-
defaultOptions?: Options;
|
|
17
|
-
extractor?: (url: URL) => Data;
|
|
18
|
-
createRequest: (this: DefineProviderContext<Data>, ctx: DefineProviderContext<Data>, Options: Options) => Request;
|
|
19
|
-
}
|
|
20
|
-
declare const DEFAULT_EXTRACTOR: (url: URL) => unknown;
|
|
21
|
-
declare function defineProvider<const Protocol extends string, Data = unknown, Options = unknown>(protocol: Protocol, createOptions: DefineProviderOptions<Data, Options>): ServiceProvider<Protocol, Data, Options>;
|
|
22
|
-
//#endregion
|
|
23
|
-
//#region src/core/sender.d.ts
|
|
24
|
-
interface Sender {
|
|
25
|
-
send: (message: string, options?: FetchOptions) => Promise<void>;
|
|
26
|
-
}
|
|
27
|
-
type SenderUrl = string | URL;
|
|
28
|
-
interface SenderRegistry {
|
|
29
|
-
(urls: SenderUrl[]): Sender;
|
|
30
|
-
resolveProvider: (url: string | URL) => ServiceProvider<string> | undefined;
|
|
31
|
-
}
|
|
32
|
-
declare function buildSenderRegistry(providers: ServiceProvider<string, any, any>[]): SenderRegistry;
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region src/services/chat/slack/index.d.ts
|
|
35
|
-
interface SlackData {
|
|
36
|
-
type: 'bot' | 'webhook';
|
|
37
|
-
}
|
|
38
|
-
interface SlackOptions {
|
|
39
|
-
/**
|
|
40
|
-
* The base URL for webhook services
|
|
41
|
-
*
|
|
42
|
-
* @default `https://hooks.slack.com/services`
|
|
43
|
-
*/
|
|
44
|
-
hookBaseUrl?: string;
|
|
45
|
-
/**
|
|
46
|
-
* The base URL for bot API services
|
|
47
|
-
*
|
|
48
|
-
* @default `https://slack.com/api`
|
|
49
|
-
*/
|
|
50
|
-
botApiBaseUrl?: string;
|
|
51
|
-
/**
|
|
52
|
-
* The body of the request
|
|
53
|
-
*
|
|
54
|
-
* NOTICE*: This will override the body of the request. You should known what you are doing.
|
|
55
|
-
*/
|
|
56
|
-
body?: Record<string, any>;
|
|
57
|
-
}
|
|
58
|
-
declare const slack: ServiceProvider<"slack:", SlackData, SlackOptions>;
|
|
59
|
-
//#endregion
|
|
60
|
-
//#region src/services/specialized/json/index.d.ts
|
|
61
|
-
declare const json: ServiceProvider<"json:", unknown, unknown>;
|
|
62
|
-
//#endregion
|
|
63
|
-
export { SenderUrl as a, DefineProviderContext as c, defineProvider as d, SenderRegistry as i, DefineProviderOptions as l, slack as n, buildSenderRegistry as o, Sender as r, DEFAULT_EXTRACTOR as s, json as t, ServiceProvider as u };
|
package/dist/shared-BiiZsNzb.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import defu from "defu";
|
|
2
|
-
import { withProtocol, withoutLeadingSlash } from "ufo";
|
|
3
|
-
import { ofetch } from "ofetch";
|
|
4
|
-
|
|
5
|
-
//#region src/utils/assert.ts
|
|
6
|
-
function assert(condition, message) {
|
|
7
|
-
if (!condition) throw typeof message === "string" ? new Error(message) : message;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
//#endregion
|
|
11
|
-
//#region src/core/provider.ts
|
|
12
|
-
const DEFAULT_EXTRACTOR = (url) => Object.fromEntries(url.searchParams.entries());
|
|
13
|
-
function defineProvider(protocol, createOptions) {
|
|
14
|
-
const createOpt = defu(createOptions, { extractor: DEFAULT_EXTRACTOR });
|
|
15
|
-
const buildRequest = (protocolUrl, message, options) => {
|
|
16
|
-
const url = new URL(protocolUrl);
|
|
17
|
-
assert(url.protocol === protocol, `Unexpected protocol "${url.protocol}"`);
|
|
18
|
-
const ctx = {
|
|
19
|
-
data: createOpt.extractor(url),
|
|
20
|
-
url,
|
|
21
|
-
message
|
|
22
|
-
};
|
|
23
|
-
const opts = defu(createOptions.defaultOptions ?? {}, options ?? {});
|
|
24
|
-
return createOptions.createRequest.call(ctx, ctx, opts);
|
|
25
|
-
};
|
|
26
|
-
return {
|
|
27
|
-
get protocol() {
|
|
28
|
-
return protocol;
|
|
29
|
-
},
|
|
30
|
-
buildRequest,
|
|
31
|
-
get defaultOptions() {
|
|
32
|
-
return createOptions.defaultOptions;
|
|
33
|
-
},
|
|
34
|
-
$infer: {}
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
//#endregion
|
|
39
|
-
//#region src/services/chat/slack/index.ts
|
|
40
|
-
const slack = defineProvider("slack:", {
|
|
41
|
-
extractor: (url) => {
|
|
42
|
-
assert(url.hostname === "bot" || url.hostname === "webhook", `Invalid slack URL: ${url.toString()}`);
|
|
43
|
-
if (url.hostname === "bot") {
|
|
44
|
-
assert(url.username, "Channel ID is required");
|
|
45
|
-
assert(url.password, "Bot token is required");
|
|
46
|
-
} else assert(url.pathname.split("/").filter(Boolean).length === 3, "Webhook URL is invalid");
|
|
47
|
-
return { type: url.hostname };
|
|
48
|
-
},
|
|
49
|
-
defaultOptions: {
|
|
50
|
-
hookBaseUrl: "https://hooks.slack.com/",
|
|
51
|
-
botApiBaseUrl: "https://slack.com/"
|
|
52
|
-
},
|
|
53
|
-
createRequest(_, options) {
|
|
54
|
-
const { type } = this.data;
|
|
55
|
-
let url;
|
|
56
|
-
const headers = new Headers([["Content-Type", "application/json"]]);
|
|
57
|
-
const body = { text: this.message };
|
|
58
|
-
if (type === "bot") {
|
|
59
|
-
const { username: channel, password: token, searchParams } = this.url;
|
|
60
|
-
url = new URL("/api/chat.postMessage", options.botApiBaseUrl);
|
|
61
|
-
searchParams.forEach((value, key) => {
|
|
62
|
-
url.searchParams.set(key, value);
|
|
63
|
-
});
|
|
64
|
-
headers.set("Authorization", `Bearer ${token}`);
|
|
65
|
-
body.channel = channel;
|
|
66
|
-
} else {
|
|
67
|
-
const { searchParams } = this.url;
|
|
68
|
-
url = new URL(`/services/${withoutLeadingSlash(this.url.pathname)}`, options.hookBaseUrl);
|
|
69
|
-
searchParams.forEach((value, key) => {
|
|
70
|
-
url.searchParams.set(key, value);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
return new Request(url, {
|
|
74
|
-
method: "POST",
|
|
75
|
-
headers,
|
|
76
|
-
body: JSON.stringify(options.body ?? body)
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
//#endregion
|
|
82
|
-
//#region src/services/specialized/json/index.ts
|
|
83
|
-
const json = defineProvider("json:", { createRequest() {
|
|
84
|
-
const url = new URL(this.url);
|
|
85
|
-
const headers = new Headers([["Content-Type", "application/json"]]);
|
|
86
|
-
const body = { message: this.message };
|
|
87
|
-
Array.from(url.searchParams.entries()).forEach(([key, value]) => {
|
|
88
|
-
if (key.startsWith(" ")) {
|
|
89
|
-
url.searchParams.delete(key);
|
|
90
|
-
const headerKey = key.slice(1);
|
|
91
|
-
if (headers.has(headerKey)) headers.set(headerKey, value);
|
|
92
|
-
else headers.append(headerKey, value);
|
|
93
|
-
} else if (key.startsWith(":")) {
|
|
94
|
-
url.searchParams.delete(key);
|
|
95
|
-
const propertyKey = key.slice(1);
|
|
96
|
-
body[propertyKey] = value;
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
const requestUrl = withProtocol(url.toString(), "https:");
|
|
100
|
-
return new Request(requestUrl, {
|
|
101
|
-
method: "POST",
|
|
102
|
-
body: JSON.stringify(body),
|
|
103
|
-
headers
|
|
104
|
-
});
|
|
105
|
-
} });
|
|
106
|
-
|
|
107
|
-
//#endregion
|
|
108
|
-
//#region src/core/sender.ts
|
|
109
|
-
function buildSenderRegistry(providers) {
|
|
110
|
-
const providersRegistry = /* @__PURE__ */ new Map();
|
|
111
|
-
providers.forEach((provider) => {
|
|
112
|
-
providersRegistry.set(provider.protocol, provider);
|
|
113
|
-
});
|
|
114
|
-
function resolveProvider(url) {
|
|
115
|
-
const _url = typeof url === "string" ? new URL(url) : url;
|
|
116
|
-
return providersRegistry.get(_url.protocol);
|
|
117
|
-
}
|
|
118
|
-
function createSender(urls) {
|
|
119
|
-
const registries = urls.map((url) => {
|
|
120
|
-
const provider = resolveProvider(url);
|
|
121
|
-
if (!provider) return;
|
|
122
|
-
return {
|
|
123
|
-
provider,
|
|
124
|
-
url
|
|
125
|
-
};
|
|
126
|
-
}).filter((p) => !!p);
|
|
127
|
-
return { async send(message, options) {
|
|
128
|
-
for (const registry of registries) await ofetch(registry.provider.buildRequest(registry.url.toString(), message), options);
|
|
129
|
-
} };
|
|
130
|
-
}
|
|
131
|
-
return Object.assign(createSender, { resolveProvider });
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
//#endregion
|
|
135
|
-
export { defineProvider as a, DEFAULT_EXTRACTOR as i, json as n, assert as o, slack as r, buildSenderRegistry as t };
|