statocysts 0.1.0 → 0.2.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,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 DEFAULT_EXTRACTOR, c as ServiceProvider, i as buildSenderRegistry, l as defineProvider, n as SenderRegistry, o as DefineProviderContext, r as SenderUrl, s as DefineProviderOptions, t as Sender } from "./shared-CR_VTatP.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, send, senderRegistry };
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, send, senderRegistry };
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 DEFAULT_EXTRACTOR, c as ServiceProvider, i as buildSenderRegistry, l as defineProvider, n as SenderRegistry, o as DefineProviderContext, r as SenderUrl, s as DefineProviderOptions, t as Sender } from "./shared-CR_VTatP.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, send, senderRegistry };
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, send, senderRegistry };
@@ -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 };
@@ -0,0 +1,34 @@
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
+ export { DEFAULT_EXTRACTOR as a, ServiceProvider as c, buildSenderRegistry as i, defineProvider as l, SenderRegistry as n, DefineProviderContext as o, SenderUrl as r, DefineProviderOptions as s, Sender 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.0",
5
5
  "description": "Notification library for JavaScript",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/octoplorer/statocysts",
@@ -1,50 +0,0 @@
1
- //#region src/core/provider.d.ts
2
- interface ServiceProvider<Protocol extends string, Data = unknown, Options = unknown> {
3
- readonly protocol: Protocol;
4
- $infer: Data;
5
- defaultOptions: Options | undefined;
6
- buildRequest: (url: string, message: string, options?: Options) => Promise<Request>;
7
- }
8
- interface DefineProviderContext<Data = unknown> {
9
- url: URL;
10
- message: string;
11
- data: Data;
12
- }
13
- interface DefineProviderOptions<Data = unknown, Options = unknown> {
14
- defaultOptions?: Options;
15
- extractor?: (url: URL) => Data;
16
- createRequest: (this: DefineProviderContext<Data>, ctx: DefineProviderContext<Data>, Options: Options) => Request;
17
- }
18
- declare const DEFAULT_EXTRACTOR: (url: URL) => unknown;
19
- declare function defineProvider<const Protocol extends string, Data = unknown, Options = unknown>(protocol: Protocol, createOptions: DefineProviderOptions<Data, Options>): ServiceProvider<Protocol, Data, Options>;
20
- //#endregion
21
- //#region src/services/chat/slack/index.d.ts
22
- interface SlackData {
23
- type: 'bot' | 'webhook';
24
- }
25
- interface SlackOptions {
26
- /**
27
- * The base URL for webhook services
28
- *
29
- * @default `https://hooks.slack.com/services`
30
- */
31
- hookBaseUrl?: string;
32
- /**
33
- * The base URL for bot API services
34
- *
35
- * @default `https://slack.com/api`
36
- */
37
- botApiBaseUrl?: string;
38
- /**
39
- * The body of the request
40
- *
41
- * NOTICE*: This will override the body of the request. You should known what you are doing.
42
- */
43
- body?: Record<string, any>;
44
- }
45
- declare const slackProvider: ServiceProvider<"slack:", SlackData, SlackOptions>;
46
- //#endregion
47
- //#region src/services/specialized/json/index.d.ts
48
- declare const jsonProvider: ServiceProvider<"json:", unknown, unknown>;
49
- //#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 };