statocysts 0.1.0 → 0.2.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/dist/browser.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { a as DEFAULT_EXTRACTOR, c as ServiceProvider, i as slackProvider, l as defineProvider, n as SlackData, o as DefineProviderContext, r as SlackOptions, s as DefineProviderOptions, t as jsonProvider } from "./index-pwxPZaBo.js";
1
+ import { a as SenderUrl, c as DefineProviderContext, d as defineProvider, i as SenderRegistry, l as DefineProviderOptions, n as slack, o as buildSenderRegistry, r as Sender, s as DEFAULT_EXTRACTOR, t as json, u as ServiceProvider } from "./index-CK1eOwNP.js";
2
2
  import { FetchOptions } from "ofetch";
3
3
 
4
4
  //#region src/browser.d.ts
5
- type Protocol = 'json:' | 'slack:';
6
- declare const SUPPORTED_PROTOCOLS: readonly ["json:", "slack:"];
5
+ declare const senderRegistry: SenderRegistry;
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 { DEFAULT_EXTRACTOR, DefineProviderContext, DefineProviderOptions, Protocol, SUPPORTED_PROTOCOLS, ServiceProvider, SlackData, SlackOptions, defineProvider, jsonProvider, send, slackProvider };
9
+ export { DEFAULT_EXTRACTOR, DefineProviderContext, DefineProviderOptions, Sender, SenderRegistry, SenderUrl, ServiceProvider, buildSenderRegistry, createSender, defineProvider, json, send, senderRegistry, slack };
package/dist/browser.js CHANGED
@@ -1,19 +1,17 @@
1
- import { a as assert, i as defineProvider, n as slackProvider, r as DEFAULT_EXTRACTOR, t as jsonProvider } from "./shared-DLVVDcdQ.js";
1
+ import { a as defineProvider, i as DEFAULT_EXTRACTOR, n as json, o as assert, r as slack, t as buildSenderRegistry } from "./shared-BiiZsNzb.js";
2
2
  import { ofetch } from "ofetch";
3
3
 
4
4
  //#region src/browser.ts
5
- const SUPPORTED_PROTOCOLS = ["json:", "slack:"];
6
- const providers = {
7
- "json:": jsonProvider,
8
- "slack:": slackProvider
9
- };
5
+ const senderRegistry = buildSenderRegistry([json, slack]);
6
+ function createSender(urls) {
7
+ return senderRegistry(urls);
8
+ }
10
9
  async function send(url, message, options) {
11
10
  const _url = typeof url === "string" ? new URL(url) : url;
12
- assert(SUPPORTED_PROTOCOLS.includes(_url.protocol), `Unsupported protocol ${_url.protocol}`);
13
- const provider = providers[_url.protocol];
14
- if (!provider) throw new Error(`Unsupported protocol ${_url.protocol}`);
15
- await ofetch(await provider.buildRequest(_url.toString(), message), options);
11
+ const provider = senderRegistry.resolveProvider(_url);
12
+ assert(provider, `Unsupported protocol ${_url.protocol}`);
13
+ await ofetch(provider.buildRequest(_url.toString(), message, options), options);
16
14
  }
17
15
 
18
16
  //#endregion
19
- export { DEFAULT_EXTRACTOR, SUPPORTED_PROTOCOLS, defineProvider, jsonProvider, send, slackProvider };
17
+ export { DEFAULT_EXTRACTOR, buildSenderRegistry, createSender, defineProvider, json, send, senderRegistry, slack };
@@ -1,9 +1,11 @@
1
+ import { FetchOptions } from "ofetch";
2
+
1
3
  //#region src/core/provider.d.ts
2
4
  interface ServiceProvider<Protocol extends string, Data = unknown, Options = unknown> {
3
5
  readonly protocol: Protocol;
4
6
  $infer: Data;
5
7
  defaultOptions: Options | undefined;
6
- buildRequest: (url: string, message: string, options?: Options) => Promise<Request>;
8
+ buildRequest: (url: string, message: string, options?: Options) => Request;
7
9
  }
8
10
  interface DefineProviderContext<Data = unknown> {
9
11
  url: URL;
@@ -18,6 +20,17 @@ interface DefineProviderOptions<Data = unknown, Options = unknown> {
18
20
  declare const DEFAULT_EXTRACTOR: (url: URL) => unknown;
19
21
  declare function defineProvider<const Protocol extends string, Data = unknown, Options = unknown>(protocol: Protocol, createOptions: DefineProviderOptions<Data, Options>): ServiceProvider<Protocol, Data, Options>;
20
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
21
34
  //#region src/services/chat/slack/index.d.ts
22
35
  interface SlackData {
23
36
  type: 'bot' | 'webhook';
@@ -42,9 +55,9 @@ interface SlackOptions {
42
55
  */
43
56
  body?: Record<string, any>;
44
57
  }
45
- declare const slackProvider: ServiceProvider<"slack:", SlackData, SlackOptions>;
58
+ declare const slack: ServiceProvider<"slack:", SlackData, SlackOptions>;
46
59
  //#endregion
47
60
  //#region src/services/specialized/json/index.d.ts
48
- declare const jsonProvider: ServiceProvider<"json:", unknown, unknown>;
61
+ declare const json: ServiceProvider<"json:", unknown, unknown>;
49
62
  //#endregion
50
- export { DEFAULT_EXTRACTOR as a, ServiceProvider as c, slackProvider as i, defineProvider as l, SlackData as n, DefineProviderContext as o, SlackOptions as r, DefineProviderOptions as s, jsonProvider as t };
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/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { a as DEFAULT_EXTRACTOR, c as ServiceProvider, i as slackProvider, l as defineProvider, n as SlackData, o as DefineProviderContext, r as SlackOptions, s as DefineProviderOptions, t as jsonProvider } from "./index-pwxPZaBo.js";
1
+ import { a as SenderUrl, c as DefineProviderContext, d as defineProvider, i as SenderRegistry, l as DefineProviderOptions, n as slack, o as buildSenderRegistry, r as Sender, s as DEFAULT_EXTRACTOR, t as json, u as ServiceProvider } from "./index-CK1eOwNP.js";
2
2
  import { FetchOptions } from "ofetch";
3
3
 
4
4
  //#region src/index.d.ts
5
- type Protocol = 'json:' | 'slack:';
6
- declare const SUPPORTED_PROTOCOLS: readonly ["json:", "slack:"];
5
+ declare const senderRegistry: SenderRegistry;
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 { DEFAULT_EXTRACTOR, DefineProviderContext, DefineProviderOptions, Protocol, SUPPORTED_PROTOCOLS, ServiceProvider, SlackData, SlackOptions, defineProvider, jsonProvider, send, slackProvider };
9
+ export { DEFAULT_EXTRACTOR, DefineProviderContext, DefineProviderOptions, Sender, SenderRegistry, SenderUrl, ServiceProvider, buildSenderRegistry, createSender, defineProvider, json, send, senderRegistry, slack };
package/dist/index.js CHANGED
@@ -1,19 +1,17 @@
1
- import { a as assert, i as defineProvider, n as slackProvider, r as DEFAULT_EXTRACTOR, t as jsonProvider } from "./shared-DLVVDcdQ.js";
1
+ import { a as defineProvider, i as DEFAULT_EXTRACTOR, n as json, o as assert, r as slack, t as buildSenderRegistry } from "./shared-BiiZsNzb.js";
2
2
  import { ofetch } from "ofetch";
3
3
 
4
4
  //#region src/index.ts
5
- const SUPPORTED_PROTOCOLS = ["json:", "slack:"];
6
- const providers = {
7
- "json:": jsonProvider,
8
- "slack:": slackProvider
9
- };
5
+ const senderRegistry = buildSenderRegistry([json, slack]);
6
+ function createSender(urls) {
7
+ return senderRegistry(urls);
8
+ }
10
9
  async function send(url, message, options) {
11
10
  const _url = typeof url === "string" ? new URL(url) : url;
12
- assert(SUPPORTED_PROTOCOLS.includes(_url.protocol), `Unsupported protocol ${_url.protocol}`);
13
- const provider = providers[_url.protocol];
14
- if (!provider) throw new Error(`Unsupported protocol ${_url.protocol}`);
15
- await ofetch(await provider.buildRequest(_url.toString(), message), options);
11
+ const provider = senderRegistry.resolveProvider(_url);
12
+ assert(provider, `Unsupported protocol ${_url.protocol}`);
13
+ await ofetch(provider.buildRequest(_url.toString(), message, options), options);
16
14
  }
17
15
 
18
16
  //#endregion
19
- export { DEFAULT_EXTRACTOR, SUPPORTED_PROTOCOLS, defineProvider, jsonProvider, send, slackProvider };
17
+ export { DEFAULT_EXTRACTOR, buildSenderRegistry, createSender, defineProvider, json, send, senderRegistry, slack };
@@ -1,5 +1,6 @@
1
1
  import defu from "defu";
2
2
  import { withProtocol, withoutLeadingSlash } from "ufo";
3
+ import { ofetch } from "ofetch";
3
4
 
4
5
  //#region src/utils/assert.ts
5
6
  function assert(condition, message) {
@@ -11,7 +12,7 @@ function assert(condition, message) {
11
12
  const DEFAULT_EXTRACTOR = (url) => Object.fromEntries(url.searchParams.entries());
12
13
  function defineProvider(protocol, createOptions) {
13
14
  const createOpt = defu(createOptions, { extractor: DEFAULT_EXTRACTOR });
14
- const buildRequest = async (protocolUrl, message, options) => {
15
+ const buildRequest = (protocolUrl, message, options) => {
15
16
  const url = new URL(protocolUrl);
16
17
  assert(url.protocol === protocol, `Unexpected protocol "${url.protocol}"`);
17
18
  const ctx = {
@@ -36,7 +37,7 @@ function defineProvider(protocol, createOptions) {
36
37
 
37
38
  //#endregion
38
39
  //#region src/services/chat/slack/index.ts
39
- const slackProvider = defineProvider("slack:", {
40
+ const slack = defineProvider("slack:", {
40
41
  extractor: (url) => {
41
42
  assert(url.hostname === "bot" || url.hostname === "webhook", `Invalid slack URL: ${url.toString()}`);
42
43
  if (url.hostname === "bot") {
@@ -79,7 +80,7 @@ const slackProvider = defineProvider("slack:", {
79
80
 
80
81
  //#endregion
81
82
  //#region src/services/specialized/json/index.ts
82
- const jsonProvider = defineProvider("json:", { createRequest() {
83
+ const json = defineProvider("json:", { createRequest() {
83
84
  const url = new URL(this.url);
84
85
  const headers = new Headers([["Content-Type", "application/json"]]);
85
86
  const body = { message: this.message };
@@ -104,4 +105,31 @@ const jsonProvider = defineProvider("json:", { createRequest() {
104
105
  } });
105
106
 
106
107
  //#endregion
107
- export { assert as a, defineProvider as i, slackProvider as n, DEFAULT_EXTRACTOR as r, jsonProvider as t };
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 };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "statocysts",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.2.1",
5
5
  "description": "Notification library for JavaScript",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/octoplorer/statocysts",
@@ -14,8 +14,12 @@
14
14
  },
15
15
  "keywords": [
16
16
  "notification",
17
- "shoutrrr"
17
+ "shoutrrr",
18
+ "apprise"
18
19
  ],
20
+ "imports": {
21
+ "#/*": "./src/*"
22
+ },
19
23
  "exports": {
20
24
  ".": {
21
25
  "types": "./dist/index.d.ts",