@blujosi/rivetkit-svelte 2.3.8 → 2.3.10

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.
@@ -33,26 +33,26 @@ export interface CrudTransformOptions<T> {
33
33
  * Property name (or accessor) used to uniquely identify items.
34
34
  * Defaults to `"id"`.
35
35
  */
36
- key?: keyof T | ((item: T) => unknown);
36
+ key?: keyof T | ((item: T) => string) | string | number;
37
37
  }
38
38
  /**
39
39
  * Transform for a **create** event.
40
40
  * - **Array:** appends the incoming item; duplicates (same key) are ignored.
41
41
  * - **Single item:** replaces the current value with the incoming item.
42
42
  */
43
- export declare function createTransform<T>(opts?: CrudTransformOptions<T>): <C extends T[] | T>(current: C, incoming: unknown) => C;
43
+ export declare function createTransform<T>(opts?: CrudTransformOptions<T>): <C extends T[] | T>(current: C, incoming: CrudEvent<T>) => C;
44
44
  /**
45
45
  * Transform for an **update** event.
46
46
  * - **Array:** replaces the matching item in-place; returns unchanged if no match.
47
47
  * - **Single item:** replaces the current value with the incoming item.
48
48
  */
49
- export declare function updateTransform<T>(opts?: CrudTransformOptions<T>): <C extends T[] | T>(current: C, incoming: unknown) => C;
49
+ export declare function updateTransform<T>(opts?: CrudTransformOptions<T>): <C extends T[] | T>(current: C, incoming: CrudEvent<T>) => C;
50
50
  /**
51
51
  * Transform for a **delete** event.
52
52
  * - **Array:** removes the matching item. `incoming` can be the full item or just the key value.
53
53
  * - **Single item:** returns the current value unchanged (cannot delete a scalar).
54
54
  */
55
- export declare function deleteTransform<T>(opts?: CrudTransformOptions<T>): <C extends T[] | T>(current: C, incoming: unknown) => C;
55
+ export declare function deleteTransform<T>(opts?: CrudTransformOptions<T>): <C extends T[] | T>(current: C, incoming: CrudEvent<T>) => C;
56
56
  /**
57
57
  * A single transform that handles create, update, and delete events.
58
58
  *
@@ -19,7 +19,15 @@
19
19
  // Helpers
20
20
  // ---------------------------------------------------------------------------
21
21
  function getKey(item, key) {
22
- return typeof key === "function" ? key(item) : item[key];
22
+ if (typeof key === "function") {
23
+ return key(item);
24
+ }
25
+ else if (typeof key === "string" || typeof key === "number") {
26
+ return item[key];
27
+ }
28
+ else {
29
+ return item[key];
30
+ }
23
31
  }
24
32
  // ---------------------------------------------------------------------------
25
33
  // Individual transforms
@@ -32,7 +40,7 @@ function getKey(item, key) {
32
40
  export function createTransform(opts = {}) {
33
41
  const keyProp = opts.key ?? "id";
34
42
  return ((current, incoming) => {
35
- const item = incoming;
43
+ const item = incoming.data;
36
44
  if (Array.isArray(current)) {
37
45
  const id = getKey(item, keyProp);
38
46
  if (current.some((c) => getKey(c, keyProp) === id))
@@ -50,7 +58,7 @@ export function createTransform(opts = {}) {
50
58
  export function updateTransform(opts = {}) {
51
59
  const keyProp = opts.key ?? "id";
52
60
  return ((current, incoming) => {
53
- const item = incoming;
61
+ const item = incoming.data;
54
62
  if (Array.isArray(current)) {
55
63
  const id = getKey(item, keyProp);
56
64
  const idx = current.findIndex((c) => getKey(c, keyProp) === id);
@@ -72,9 +80,9 @@ export function deleteTransform(opts = {}) {
72
80
  const keyProp = opts.key ?? "id";
73
81
  return ((current, incoming) => {
74
82
  if (Array.isArray(current)) {
75
- const id = typeof incoming === "object" && incoming !== null
76
- ? getKey(incoming, keyProp)
77
- : incoming;
83
+ const id = typeof incoming.data === "object" && incoming.data !== null
84
+ ? getKey(incoming.data, keyProp)
85
+ : incoming.data;
78
86
  return current.filter((c) => getKey(c, keyProp) !== id);
79
87
  }
80
88
  return current;
@@ -115,11 +123,11 @@ export function crudTransform(opts = {}) {
115
123
  return ((current, incoming) => {
116
124
  switch (incoming.type) {
117
125
  case "created":
118
- return create(current, incoming.data);
126
+ return create(current, incoming);
119
127
  case "updated":
120
- return update(current, incoming.data);
128
+ return update(current, incoming);
121
129
  case "deleted":
122
- return del(current, incoming.data);
130
+ return del(current, incoming);
123
131
  default:
124
132
  return current;
125
133
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blujosi/rivetkit-svelte",
3
- "version": "2.3.8",
3
+ "version": "2.3.10",
4
4
  "scripts": {
5
5
  "build": "vite build && npm run prepack",
6
6
  "preview": "vite preview",