@civet/common 2.0.0 → 2.1.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.
@@ -1,6 +1,7 @@
1
1
  import { GenericDataProvider, Notifier, ResourceContextValue } from '@civet/core';
2
2
  import { EventReceiver } from '@civet/events';
3
3
  export type SSEReceiverOptions<Resource extends ResourceContextValue<GenericDataProvider>, EventType = MessageEvent> = {
4
+ events?: string[];
4
5
  getEvents?: (resource: Resource | undefined, type: string, event: MessageEvent) => Promise<EventType[]> | EventType[];
5
6
  };
6
7
  export type SSEOptions<Resource extends ResourceContextValue<GenericDataProvider>, EventType = MessageEvent> = {
package/dist/main.js CHANGED
@@ -1,88 +1,90 @@
1
1
  import { DataProvider as d, Meta as l } from "@civet/core";
2
- import { eventPlugin as p, EventReceiver as v } from "@civet/events";
3
- class f extends d {
2
+ import { eventPlugin as f, EventReceiver as p } from "@civet/events";
3
+ class v extends d {
4
4
  options;
5
- constructor(r = {}) {
6
- super(), this.options = r;
5
+ constructor(s = {}) {
6
+ super(), this.options = s;
7
7
  }
8
- handleGet(r, s, t, e, u) {
9
- return this.request(r, s, t, e, u);
8
+ handleGet(s, n, t, e, u) {
9
+ return this.request(s, n, t, e, u);
10
10
  }
11
- normalizeResource(r) {
12
- return new URL(r, this.options.baseURL).toString();
11
+ normalizeResource(s) {
12
+ return new URL(s, this.options.baseURL).toString();
13
13
  }
14
- async request(r, s, t, e, u) {
14
+ async request(s, n, t, e, u) {
15
15
  e = e instanceof l ? e : new l(e);
16
- const i = new AbortController();
17
- u?.listen(i.abort.bind(i));
18
- const a = new URL(r), c = new Headers(s?.headers), n = { ...s, headers: c };
19
- await this.options.modifyRequest?.(a, n, e);
20
- const o = await fetch(a.toString(), {
21
- ...n,
16
+ const a = new AbortController();
17
+ u?.listen(a.abort.bind(a));
18
+ const r = new URL(s), c = new Headers(n?.headers), o = { ...n, headers: c };
19
+ await this.options.modifyRequest?.(r, o, e);
20
+ const i = await fetch(r.toString(), {
21
+ ...o,
22
22
  signal: AbortSignal.any(
23
- [n.signal, i.signal].filter((g) => g != null)
23
+ [o.signal, a.signal].filter((g) => g != null)
24
24
  )
25
25
  });
26
- if (!o.ok) {
26
+ if (!i.ok) {
27
27
  if (t?.handleError)
28
28
  return t.handleError(
29
- a,
30
- n,
29
+ r,
31
30
  o,
31
+ i,
32
32
  e
33
33
  );
34
34
  if (this.options.handleError)
35
35
  return this.options.handleError(
36
- a,
37
- n,
36
+ r,
38
37
  o,
38
+ i,
39
39
  e
40
40
  );
41
- throw new Error(o.statusText);
41
+ throw new Error(i.statusText);
42
42
  }
43
43
  if (t?.getResponse)
44
44
  return t.getResponse(
45
- a,
46
- n,
45
+ r,
47
46
  o,
47
+ i,
48
48
  e
49
49
  );
50
50
  if (this.options.getResponse)
51
51
  return this.options.getResponse(
52
- a,
53
- n,
52
+ r,
54
53
  o,
54
+ i,
55
55
  e
56
56
  );
57
57
  if (t?.json || !t?.noJson && /^application\/[^+]*[+]?(json);?.*$/.test(
58
- o.headers.get("Content-Type") ?? ""
58
+ i.headers.get("Content-Type") ?? ""
59
59
  ))
60
- return o.json();
61
- if (!t?.noText) return o.text();
60
+ return i.json();
61
+ if (!t?.noText) return i.text();
62
62
  throw new Error("unprocessable response");
63
63
  }
64
64
  }
65
- const w = p(f);
66
- class R extends v {
65
+ const w = f(v);
66
+ class R extends p {
67
67
  eventSource;
68
68
  options;
69
- constructor(r, s = {}) {
70
- super(), this.eventSource = r, this.options = s;
69
+ constructor(s, n = {}) {
70
+ super(), this.eventSource = s, this.options = n;
71
71
  }
72
- handleSubscribe(r, s, t) {
72
+ handleSubscribe(s, n, t) {
73
73
  let e;
74
- const u = r.subscribe(
74
+ const u = s.subscribe(
75
75
  (c) => {
76
76
  e = c;
77
77
  }
78
- ), i = new AbortController();
79
- return i.signal.addEventListener("abort", u), (s?.events?.length ? s.events : ["message"]).forEach((c) => {
78
+ ), a = new AbortController();
79
+ a.signal.addEventListener("abort", u);
80
+ let r = [];
81
+ return n?.events ? r = n.events : this.options.events && (r = this.options.events), r.length === 0 && (r = ["message"]), r.forEach((c) => {
80
82
  this.eventSource.addEventListener(
81
83
  c,
82
- async (n) => s?.getEvents ? t(await s.getEvents(e, c, n)) : this.options.getEvents ? t(await this.options.getEvents(e, c, n)) : t([n]),
83
- { signal: i.signal }
84
+ async (o) => n?.getEvents ? t(await n.getEvents(e, c, o)) : this.options.getEvents ? t(await this.options.getEvents(e, c, o)) : t([o]),
85
+ { signal: a.signal }
84
86
  );
85
- }), i.abort.bind(i);
87
+ }), a.abort.bind(a);
86
88
  }
87
89
  }
88
90
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@civet/common",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Civet",
5
5
  "author": {
6
6
  "name": "Aaron Burmeister"
@@ -51,8 +51,8 @@
51
51
  "react-dom": ">=18.0"
52
52
  },
53
53
  "devDependencies": {
54
- "@civet/core": "^6.1.1",
55
- "@civet/events": "^4.0.1",
54
+ "@civet/core": "^6.2.0",
55
+ "@civet/events": "^4.1.0",
56
56
  "@eslint/js": "^9.35.0",
57
57
  "@types/react": "^19.1.12",
58
58
  "@types/react-dom": "^19.1.9",