@legendapp/state 3.0.0-beta.4 → 3.0.0-beta.40

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.
Files changed (79) hide show
  1. package/.DS_Store +0 -0
  2. package/README.md +2 -2
  3. package/config/enableReactComponents.js +3 -1
  4. package/config/enableReactComponents.mjs +3 -1
  5. package/config/enableReactTracking.d.mts +2 -1
  6. package/config/enableReactTracking.d.ts +2 -1
  7. package/config/enableReactTracking.js +32 -13
  8. package/config/enableReactTracking.mjs +32 -13
  9. package/index.d.mts +46 -8
  10. package/index.d.ts +46 -8
  11. package/index.js +267 -75
  12. package/index.mjs +267 -75
  13. package/package.json +35 -1
  14. package/persist-plugins/async-storage.js +17 -9
  15. package/persist-plugins/async-storage.mjs +17 -9
  16. package/persist-plugins/expo-sqlite.d.mts +19 -0
  17. package/persist-plugins/expo-sqlite.d.ts +19 -0
  18. package/persist-plugins/expo-sqlite.js +72 -0
  19. package/persist-plugins/expo-sqlite.mjs +69 -0
  20. package/persist-plugins/indexeddb.js +13 -3
  21. package/persist-plugins/indexeddb.mjs +13 -3
  22. package/react-native.d.mts +4 -0
  23. package/react-native.d.ts +4 -0
  24. package/react-native.js +53 -0
  25. package/react-native.mjs +40 -0
  26. package/react-reactive/Components.d.mts +19 -0
  27. package/react-reactive/Components.d.ts +19 -0
  28. package/react-reactive/Components.js +53 -0
  29. package/react-reactive/Components.mjs +40 -0
  30. package/react-reactive/enableReactComponents.d.mts +3 -2
  31. package/react-reactive/enableReactComponents.d.ts +3 -2
  32. package/react-reactive/enableReactComponents.js +10 -3
  33. package/react-reactive/enableReactComponents.mjs +10 -3
  34. package/react-reactive/enableReactNativeComponents.d.mts +3 -20
  35. package/react-reactive/enableReactNativeComponents.d.ts +3 -20
  36. package/react-reactive/enableReactNativeComponents.js +8 -3
  37. package/react-reactive/enableReactNativeComponents.mjs +8 -3
  38. package/react-reactive/enableReactive.js +10 -3
  39. package/react-reactive/enableReactive.mjs +10 -3
  40. package/react-reactive/enableReactive.native.js +8 -3
  41. package/react-reactive/enableReactive.native.mjs +8 -3
  42. package/react-reactive/enableReactive.web.js +8 -3
  43. package/react-reactive/enableReactive.web.mjs +8 -3
  44. package/react-web.d.mts +7 -0
  45. package/react-web.d.ts +7 -0
  46. package/react-web.js +39 -0
  47. package/react-web.mjs +37 -0
  48. package/react.d.mts +59 -26
  49. package/react.d.ts +59 -26
  50. package/react.js +136 -87
  51. package/react.mjs +135 -89
  52. package/sync-plugins/crud.d.mts +24 -9
  53. package/sync-plugins/crud.d.ts +24 -9
  54. package/sync-plugins/crud.js +267 -123
  55. package/sync-plugins/crud.mjs +268 -124
  56. package/sync-plugins/firebase.d.mts +7 -3
  57. package/sync-plugins/firebase.d.ts +7 -3
  58. package/sync-plugins/firebase.js +214 -64
  59. package/sync-plugins/firebase.mjs +215 -65
  60. package/sync-plugins/keel.d.mts +12 -13
  61. package/sync-plugins/keel.d.ts +12 -13
  62. package/sync-plugins/keel.js +60 -52
  63. package/sync-plugins/keel.mjs +61 -48
  64. package/sync-plugins/supabase.d.mts +10 -5
  65. package/sync-plugins/supabase.d.ts +10 -5
  66. package/sync-plugins/supabase.js +90 -33
  67. package/sync-plugins/supabase.mjs +91 -34
  68. package/sync-plugins/tanstack-query.d.mts +3 -3
  69. package/sync-plugins/tanstack-query.d.ts +3 -3
  70. package/sync-plugins/tanstack-query.js +1 -1
  71. package/sync-plugins/tanstack-query.mjs +1 -1
  72. package/sync.d.mts +17 -8
  73. package/sync.d.ts +17 -8
  74. package/sync.js +448 -307
  75. package/sync.mjs +446 -307
  76. package/trace.js +5 -6
  77. package/trace.mjs +5 -6
  78. package/types/reactive-native.d.ts +19 -0
  79. package/types/reactive-web.d.ts +7 -0
@@ -1,4 +1,4 @@
1
- import { observable, symbolDelete, isString, isArray, isObject, computeSelector, isFunction, isNullOrUndefined, isPromise, isNumber, when } from '@legendapp/state';
1
+ import { observable, symbolDelete, isString, isArray, isObject, computeSelector, isFunction, isNullOrUndefined, isPromise, isNumber } from '@legendapp/state';
2
2
  import { syncedCrud } from '@legendapp/state/sync-plugins/crud';
3
3
  import { getAuth } from 'firebase/auth';
4
4
  import { ref, getDatabase, query, orderByChild, startAt, update, onValue, onChildAdded, onChildChanged, onChildRemoved, serverTimestamp, remove, push } from 'firebase/database';
@@ -101,6 +101,61 @@ if (process.env.NODE_ENV === "development") {
101
101
  };
102
102
  }
103
103
 
104
+ // src/is.ts
105
+ function isMap(obj) {
106
+ return obj instanceof Map || obj instanceof WeakMap;
107
+ }
108
+ var globalState = {
109
+ pendingNodes: /* @__PURE__ */ new Map(),
110
+ dirtyNodes: /* @__PURE__ */ new Set()
111
+ };
112
+ function replacer(key, value) {
113
+ if (isMap(value)) {
114
+ return {
115
+ __LSType: "Map",
116
+ value: Array.from(value.entries())
117
+ // or with spread: value: [...value]
118
+ };
119
+ } else if (value instanceof Set) {
120
+ return {
121
+ __LSType: "Set",
122
+ value: Array.from(value)
123
+ // or with spread: value: [...value]
124
+ };
125
+ } else if (globalState.replacer) {
126
+ value = globalState.replacer(key, value);
127
+ }
128
+ return value;
129
+ }
130
+ var ISO8601 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/;
131
+ function reviver(key, value) {
132
+ if (value) {
133
+ if (typeof value === "string" && ISO8601.test(value)) {
134
+ return new Date(value);
135
+ }
136
+ if (typeof value === "object") {
137
+ if (value.__LSType === "Map") {
138
+ return new Map(value.value);
139
+ } else if (value.__LSType === "Set") {
140
+ return new Set(value.value);
141
+ }
142
+ }
143
+ if (globalState.reviver) {
144
+ value = globalState.reviver(key, value);
145
+ }
146
+ }
147
+ return value;
148
+ }
149
+ function safeStringify(value) {
150
+ return value ? JSON.stringify(value, replacer) : value;
151
+ }
152
+ function safeParse(value) {
153
+ return value ? JSON.parse(value, reviver) : value;
154
+ }
155
+ function clone(value) {
156
+ return safeParse(safeStringify(value));
157
+ }
158
+
104
159
  // src/sync-plugins/firebase.ts
105
160
  var isEnabled$ = observable(true);
106
161
  var firebaseConfig = {};
@@ -152,9 +207,6 @@ var fns = {
152
207
  };
153
208
  function syncedFirebase(props) {
154
209
  props = { ...firebaseConfig, ...props };
155
- const saving$ = observable({});
156
- const pendingOutgoing$ = observable({});
157
- const pendingIncoming$ = observable({});
158
210
  let didList = false;
159
211
  const {
160
212
  refPath,
@@ -172,6 +224,102 @@ function syncedFirebase(props) {
172
224
  const { fieldCreatedAt, changesSince } = props;
173
225
  const asType = props.as || "value";
174
226
  const fieldUpdatedAt = props.fieldUpdatedAt || "@";
227
+ const isRealtime = realtime !== false;
228
+ const pendingWrites = /* @__PURE__ */ new Map();
229
+ const enqueuePendingWrite = (key) => {
230
+ let resolveFn;
231
+ let rejectFn;
232
+ const promise = new Promise((resolve, reject) => {
233
+ resolveFn = resolve;
234
+ rejectFn = reject;
235
+ });
236
+ const entry = {
237
+ resolve: resolveFn,
238
+ reject: rejectFn
239
+ };
240
+ const state = pendingWrites.get(key);
241
+ if (state) {
242
+ state.waiting.push(entry);
243
+ state.pendingCount += 1;
244
+ } else {
245
+ pendingWrites.set(key, {
246
+ waiting: [entry],
247
+ ready: [],
248
+ pendingCount: 1
249
+ });
250
+ }
251
+ return { promise, entry };
252
+ };
253
+ const flushPending = (key) => {
254
+ const state = pendingWrites.get(key);
255
+ if (!state) {
256
+ return;
257
+ }
258
+ if (state.pendingCount === 0 && state.staged) {
259
+ const { value, apply } = state.staged;
260
+ state.staged = void 0;
261
+ while (state.ready.length) {
262
+ const entry = state.ready.shift();
263
+ entry.resolve(value);
264
+ }
265
+ if (!state.waiting.length && !state.ready.length) {
266
+ pendingWrites.delete(key);
267
+ }
268
+ apply == null ? void 0 : apply(value);
269
+ }
270
+ };
271
+ const resolvePendingWrite = (key, entry) => {
272
+ const state = pendingWrites.get(key);
273
+ if (!state) {
274
+ return;
275
+ }
276
+ const waitingIndex = state.waiting.indexOf(entry);
277
+ if (waitingIndex >= 0) {
278
+ state.waiting.splice(waitingIndex, 1);
279
+ state.pendingCount = Math.max(0, state.pendingCount - 1);
280
+ }
281
+ state.ready.push(entry);
282
+ flushPending(key);
283
+ };
284
+ const rejectPendingWrite = (key, entry, error) => {
285
+ const state = pendingWrites.get(key);
286
+ if (state) {
287
+ const waitingIndex = state.waiting.indexOf(entry);
288
+ if (waitingIndex >= 0) {
289
+ state.waiting.splice(waitingIndex, 1);
290
+ state.pendingCount = Math.max(0, state.pendingCount - 1);
291
+ } else {
292
+ const readyIndex = state.ready.indexOf(entry);
293
+ if (readyIndex >= 0) {
294
+ state.ready.splice(readyIndex, 1);
295
+ }
296
+ }
297
+ if (!state.waiting.length && !state.ready.length) {
298
+ pendingWrites.delete(key);
299
+ }
300
+ }
301
+ entry.reject(error);
302
+ };
303
+ const handleServerValue = (key, value, apply) => {
304
+ var _a;
305
+ const state = pendingWrites.get(key);
306
+ if (!state || !state.waiting.length && !state.ready.length) {
307
+ pendingWrites.delete(key);
308
+ apply == null ? void 0 : apply(value);
309
+ } else {
310
+ state.staged = {
311
+ value: value && typeof value === "object" ? clone(value) : value,
312
+ apply: apply != null ? apply : (_a = state.staged) == null ? void 0 : _a.apply
313
+ };
314
+ flushPending(key);
315
+ }
316
+ };
317
+ const ensureFieldId = (key, value) => {
318
+ if (fieldId && key && value && typeof value === "object" && !value[fieldId]) {
319
+ value[fieldId] = key;
320
+ }
321
+ return value;
322
+ };
175
323
  const computeRef = (lastSync) => {
176
324
  const pathFirebase = refPath(fns.getCurrentUser());
177
325
  let ref = fns.ref(pathFirebase);
@@ -183,7 +331,8 @@ function syncedFirebase(props) {
183
331
  }
184
332
  return ref;
185
333
  };
186
- const list = async ({ lastSync, onError }) => {
334
+ const list = async (getParams) => {
335
+ const { lastSync, onError } = getParams;
187
336
  const ref = computeRef(lastSync);
188
337
  return new Promise((resolve) => {
189
338
  fns.once(
@@ -193,20 +342,17 @@ function syncedFirebase(props) {
193
342
  let values = [];
194
343
  if (!isNullOrUndefined(val)) {
195
344
  values = asType === "value" ? [val] : Object.entries(val).map(([key, value]) => {
196
- if (fieldId && !value[fieldId]) {
197
- value[fieldId] = key;
198
- }
199
- return value;
345
+ return ensureFieldId(key, value);
200
346
  });
201
347
  }
202
348
  didList = true;
203
349
  resolve(values);
204
350
  },
205
- onError
351
+ (error) => onError(error, { source: "list", type: "get", retry: getParams })
206
352
  );
207
353
  });
208
354
  };
209
- const subscribe = realtime ? ({ lastSync, update: update2, onError }) => {
355
+ const subscribe = isRealtime ? ({ lastSync, update: update2, onError }) => {
210
356
  const ref = computeRef(lastSync);
211
357
  let unsubscribes;
212
358
  if (asType === "value") {
@@ -214,14 +360,12 @@ function syncedFirebase(props) {
214
360
  if (!didList)
215
361
  return;
216
362
  const val = snap.val();
217
- if (saving$[""].get()) {
218
- pendingIncoming$[""].set(val);
219
- } else {
363
+ handleServerValue("", val, (resolvedValue) => {
220
364
  update2({
221
- value: [val],
365
+ value: [resolvedValue],
222
366
  mode: "set"
223
367
  });
224
- }
368
+ });
225
369
  };
226
370
  unsubscribes = [fns.onValue(ref, onValue2, onError)];
227
371
  } else {
@@ -229,31 +373,26 @@ function syncedFirebase(props) {
229
373
  if (!didList)
230
374
  return;
231
375
  const key = snap.key;
232
- const val = snap.val();
233
- if (fieldId && !val[fieldId]) {
234
- val[fieldId] = key;
235
- }
236
- if (saving$[key].get()) {
237
- pendingIncoming$[key].set(val);
238
- } else {
376
+ const val = ensureFieldId(key, snap.val());
377
+ handleServerValue(key, val, (resolvedValue) => {
239
378
  update2({
240
- value: [val],
241
- mode: "assign"
379
+ value: [resolvedValue],
380
+ mode: "merge"
242
381
  });
243
- }
382
+ });
244
383
  };
245
384
  const onChildDelete = (snap) => {
246
385
  if (!didList)
247
386
  return;
248
387
  const key = snap.key;
249
- const val = snap.val();
250
- if (fieldId && !val[fieldId]) {
251
- val[fieldId] = key;
252
- }
253
- val[symbolDelete] = true;
254
- update2({
255
- value: [val],
256
- mode: "assign"
388
+ const valueRaw = snap.val();
389
+ const valueWithId = ensureFieldId(key, isNullOrUndefined(valueRaw) ? {} : valueRaw);
390
+ valueWithId[symbolDelete] = true;
391
+ handleServerValue(key, valueWithId, (resolvedValue) => {
392
+ update2({
393
+ value: [resolvedValue],
394
+ mode: "merge"
395
+ });
257
396
  });
258
397
  };
259
398
  unsubscribes = [
@@ -277,42 +416,52 @@ function syncedFirebase(props) {
277
416
  }
278
417
  return addUpdatedAt(input);
279
418
  };
280
- const upsert = async (input) => {
281
- const id = fieldId ? input[fieldId] : "";
282
- if (saving$[id].get()) {
283
- pendingOutgoing$[id].set(input);
284
- } else {
285
- saving$[id].set(true);
286
- const path = joinPaths(refPath(fns.getCurrentUser()), fieldId ? id : "");
287
- await fns.update(fns.ref(path), input);
288
- saving$[id].set(false);
289
- flushAfterSave();
290
- }
291
- return when(
292
- () => !pendingOutgoing$[id].get(),
293
- () => {
294
- const value = pendingIncoming$[id].get();
295
- if (value) {
296
- pendingIncoming$[id].delete();
297
- return value;
298
- }
299
- }
300
- );
301
- };
302
- const flushAfterSave = () => {
303
- const outgoing = pendingOutgoing$.get();
304
- Object.values(outgoing).forEach((value) => {
305
- upsert(value);
419
+ const upsert = (input, params) => {
420
+ const id = fieldId && asType !== "value" ? input[fieldId] : "";
421
+ const pendingKey = fieldId && asType !== "value" ? String(id != null ? id : "") : "";
422
+ const { promise, entry } = enqueuePendingWrite(pendingKey);
423
+ const userId = fns.getCurrentUser();
424
+ const basePath = refPath(userId);
425
+ const childPath = fieldId && asType !== "value" ? pendingKey : "";
426
+ const path = joinPaths(basePath, childPath);
427
+ const ref = fns.ref(path);
428
+ const updatePromise = fns.update(ref, input);
429
+ updatePromise.then(() => {
430
+ resolvePendingWrite(pendingKey, entry);
431
+ }).catch((error) => {
432
+ rejectPendingWrite(pendingKey, entry, error);
306
433
  });
307
- pendingOutgoing$.set({});
434
+ if (!isRealtime) {
435
+ updatePromise.then(() => {
436
+ const onceRef = fieldId && asType !== "value" ? ref : fns.ref(basePath);
437
+ fns.once(
438
+ onceRef,
439
+ (snap) => {
440
+ const rawValue = snap.val();
441
+ const value = fieldId && asType !== "value" ? ensureFieldId(pendingKey, isNullOrUndefined(rawValue) ? {} : rawValue) : rawValue;
442
+ handleServerValue(pendingKey, value, (resolvedValue) => {
443
+ params.update({
444
+ value: resolvedValue,
445
+ mode: "merge"
446
+ });
447
+ });
448
+ },
449
+ (error) => {
450
+ rejectPendingWrite(pendingKey, entry, error);
451
+ }
452
+ );
453
+ }).catch(() => {
454
+ });
455
+ }
456
+ return promise;
308
457
  };
309
- const create = readonly ? void 0 : (input) => {
458
+ const create = readonly ? void 0 : (input, params) => {
310
459
  addCreatedAt(input);
311
- return upsert(input);
460
+ return upsert(input, params);
312
461
  };
313
- const update = readonly ? void 0 : (input) => {
462
+ const update = readonly ? void 0 : (input, params) => {
314
463
  addUpdatedAt(input);
315
- return upsert(input);
464
+ return upsert(input, params);
316
465
  };
317
466
  const deleteFn = readonly ? void 0 : (input) => {
318
467
  const path = joinPaths(
@@ -352,6 +501,7 @@ function syncedFirebase(props) {
352
501
  }
353
502
  return syncedCrud({
354
503
  ...rest,
504
+ // Workaround for type errors
355
505
  list,
356
506
  subscribe,
357
507
  create,
@@ -1,5 +1,5 @@
1
- import { SyncedGetSetSubscribeBaseParams, OnErrorRetryParams, SyncedSetParams } from '@legendapp/state/sync';
2
- import { SyncedCrudPropsBase, SyncedCrudReturnType, CrudAsOption, SyncedCrudPropsSingle, CrudResult, SyncedCrudPropsMany } from '@legendapp/state/sync-plugins/crud';
1
+ import { SyncedGetSetSubscribeBaseParams } from '@legendapp/state/sync';
2
+ import { CrudErrorParams, SyncedCrudPropsBase, SyncedCrudReturnType, CrudAsOption, SyncedCrudPropsSingle, CrudResult, SyncedCrudPropsMany } from '@legendapp/state/sync-plugins/crud';
3
3
 
4
4
  interface KeelObjectBase {
5
5
  id: string;
@@ -13,6 +13,7 @@ type APIError = {
13
13
  type: string;
14
14
  message: string;
15
15
  requestId?: string;
16
+ error?: unknown;
16
17
  };
17
18
  type APIResult<T> = Result<T, APIError>;
18
19
  type Data<T> = {
@@ -24,7 +25,6 @@ type Err<U> = {
24
25
  error: U;
25
26
  };
26
27
  type Result<T, U> = NonNullable<Data<T> | Err<U>>;
27
- declare function generateKeelId(): string;
28
28
  interface KeelGetParams {
29
29
  }
30
30
  interface KeelListParams<Where = {}> {
@@ -50,6 +50,9 @@ interface KeelClient {
50
50
  api: {
51
51
  queries: Record<string, (i: any) => Promise<any>>;
52
52
  };
53
+ client: {
54
+ rawRequest: <T>(action: string, body: any) => Promise<APIResult<T>>;
55
+ };
53
56
  }
54
57
  interface SyncedKeelPropsManyBase<TRemote extends {
55
58
  id: string;
@@ -62,7 +65,7 @@ interface SyncedKeelPropsManyWhere<TRemote extends {
62
65
  }, TLocal, AOption extends CrudAsOption, Where extends Record<string, any>> extends SyncedKeelPropsManyBase<TRemote, TLocal, AOption> {
63
66
  list?: (params: KeelListParams<NoInfer<Where>>) => Promise<CrudResult<APIResult<{
64
67
  results: TRemote[];
65
- pageInfo: any;
68
+ pageInfo?: any;
66
69
  }>>>;
67
70
  where?: Where | (() => Where);
68
71
  }
@@ -71,7 +74,7 @@ interface SyncedKeelPropsManyNoWhere<TRemote extends {
71
74
  }, TLocal, AOption extends CrudAsOption> extends SyncedKeelPropsManyBase<TRemote, TLocal, AOption> {
72
75
  list?: (params: KeelListParams<{}>) => Promise<CrudResult<APIResult<{
73
76
  results: TRemote[];
74
- pageInfo: any;
77
+ pageInfo?: any;
75
78
  }>>>;
76
79
  where?: never | {};
77
80
  }
@@ -88,12 +91,8 @@ interface SyncedKeelPropsSingle<TRemote extends {
88
91
  list?: never;
89
92
  as?: never;
90
93
  }
91
- interface ErrorDetails {
92
- type: 'create' | 'update' | 'delete';
93
- params: SyncedSetParams<any>;
94
- input: any;
94
+ interface KeelErrorParams extends CrudErrorParams {
95
95
  action: string;
96
- error: APIResult<any>['error'];
97
96
  }
98
97
  interface SyncedKeelPropsBase<TRemote extends {
99
98
  id: string;
@@ -102,7 +101,7 @@ interface SyncedKeelPropsBase<TRemote extends {
102
101
  create?: (i: NoInfer<Partial<TRemote>>) => Promise<APIResult<NoInfer<TRemote>>>;
103
102
  update?: (params: {
104
103
  where: any;
105
- values?: Partial<TRemote>;
104
+ values?: Partial<NoInfer<TRemote>>;
106
105
  }) => Promise<APIResult<TRemote>>;
107
106
  delete?: (params: {
108
107
  id: string;
@@ -113,7 +112,7 @@ interface SyncedKeelPropsBase<TRemote extends {
113
112
  };
114
113
  refreshAuth?: () => void | Promise<void>;
115
114
  requireAuth?: boolean;
116
- onError?: (error: Error, retryParams: OnErrorRetryParams, details: ErrorDetails) => void;
115
+ onError?: (error: Error, params: KeelErrorParams) => void;
117
116
  }
118
117
  declare function syncedKeel<TRemote extends {
119
118
  id: string;
@@ -122,4 +121,4 @@ declare function syncedKeel<TRemote extends {
122
121
  id: string;
123
122
  }, TLocal = TRemote, TOption extends CrudAsOption = 'object', Where extends Record<string, any> = {}>(props: SyncedKeelPropsBase<TRemote, TLocal> & SyncedKeelPropsMany<TRemote, TLocal, TOption, Where>): SyncedCrudReturnType<TLocal, Exclude<TOption, 'value'>>;
124
123
 
125
- export { type KeelClient, type KeelGetParams, type KeelKey, KeelKeys, type KeelListParams, type KeelObjectBase, type KeelRealtimePlugin, type OmitKeelBuiltins, type SyncedKeelPropsBase, generateKeelId, syncedKeel };
124
+ export { type KeelClient, type KeelErrorParams, type KeelGetParams, type KeelKey, KeelKeys, type KeelListParams, type KeelObjectBase, type KeelRealtimePlugin, type OmitKeelBuiltins, type SyncedKeelPropsBase, syncedKeel };
@@ -1,5 +1,5 @@
1
- import { SyncedGetSetSubscribeBaseParams, OnErrorRetryParams, SyncedSetParams } from '@legendapp/state/sync';
2
- import { SyncedCrudPropsBase, SyncedCrudReturnType, CrudAsOption, SyncedCrudPropsSingle, CrudResult, SyncedCrudPropsMany } from '@legendapp/state/sync-plugins/crud';
1
+ import { SyncedGetSetSubscribeBaseParams } from '@legendapp/state/sync';
2
+ import { CrudErrorParams, SyncedCrudPropsBase, SyncedCrudReturnType, CrudAsOption, SyncedCrudPropsSingle, CrudResult, SyncedCrudPropsMany } from '@legendapp/state/sync-plugins/crud';
3
3
 
4
4
  interface KeelObjectBase {
5
5
  id: string;
@@ -13,6 +13,7 @@ type APIError = {
13
13
  type: string;
14
14
  message: string;
15
15
  requestId?: string;
16
+ error?: unknown;
16
17
  };
17
18
  type APIResult<T> = Result<T, APIError>;
18
19
  type Data<T> = {
@@ -24,7 +25,6 @@ type Err<U> = {
24
25
  error: U;
25
26
  };
26
27
  type Result<T, U> = NonNullable<Data<T> | Err<U>>;
27
- declare function generateKeelId(): string;
28
28
  interface KeelGetParams {
29
29
  }
30
30
  interface KeelListParams<Where = {}> {
@@ -50,6 +50,9 @@ interface KeelClient {
50
50
  api: {
51
51
  queries: Record<string, (i: any) => Promise<any>>;
52
52
  };
53
+ client: {
54
+ rawRequest: <T>(action: string, body: any) => Promise<APIResult<T>>;
55
+ };
53
56
  }
54
57
  interface SyncedKeelPropsManyBase<TRemote extends {
55
58
  id: string;
@@ -62,7 +65,7 @@ interface SyncedKeelPropsManyWhere<TRemote extends {
62
65
  }, TLocal, AOption extends CrudAsOption, Where extends Record<string, any>> extends SyncedKeelPropsManyBase<TRemote, TLocal, AOption> {
63
66
  list?: (params: KeelListParams<NoInfer<Where>>) => Promise<CrudResult<APIResult<{
64
67
  results: TRemote[];
65
- pageInfo: any;
68
+ pageInfo?: any;
66
69
  }>>>;
67
70
  where?: Where | (() => Where);
68
71
  }
@@ -71,7 +74,7 @@ interface SyncedKeelPropsManyNoWhere<TRemote extends {
71
74
  }, TLocal, AOption extends CrudAsOption> extends SyncedKeelPropsManyBase<TRemote, TLocal, AOption> {
72
75
  list?: (params: KeelListParams<{}>) => Promise<CrudResult<APIResult<{
73
76
  results: TRemote[];
74
- pageInfo: any;
77
+ pageInfo?: any;
75
78
  }>>>;
76
79
  where?: never | {};
77
80
  }
@@ -88,12 +91,8 @@ interface SyncedKeelPropsSingle<TRemote extends {
88
91
  list?: never;
89
92
  as?: never;
90
93
  }
91
- interface ErrorDetails {
92
- type: 'create' | 'update' | 'delete';
93
- params: SyncedSetParams<any>;
94
- input: any;
94
+ interface KeelErrorParams extends CrudErrorParams {
95
95
  action: string;
96
- error: APIResult<any>['error'];
97
96
  }
98
97
  interface SyncedKeelPropsBase<TRemote extends {
99
98
  id: string;
@@ -102,7 +101,7 @@ interface SyncedKeelPropsBase<TRemote extends {
102
101
  create?: (i: NoInfer<Partial<TRemote>>) => Promise<APIResult<NoInfer<TRemote>>>;
103
102
  update?: (params: {
104
103
  where: any;
105
- values?: Partial<TRemote>;
104
+ values?: Partial<NoInfer<TRemote>>;
106
105
  }) => Promise<APIResult<TRemote>>;
107
106
  delete?: (params: {
108
107
  id: string;
@@ -113,7 +112,7 @@ interface SyncedKeelPropsBase<TRemote extends {
113
112
  };
114
113
  refreshAuth?: () => void | Promise<void>;
115
114
  requireAuth?: boolean;
116
- onError?: (error: Error, retryParams: OnErrorRetryParams, details: ErrorDetails) => void;
115
+ onError?: (error: Error, params: KeelErrorParams) => void;
117
116
  }
118
117
  declare function syncedKeel<TRemote extends {
119
118
  id: string;
@@ -122,4 +121,4 @@ declare function syncedKeel<TRemote extends {
122
121
  id: string;
123
122
  }, TLocal = TRemote, TOption extends CrudAsOption = 'object', Where extends Record<string, any> = {}>(props: SyncedKeelPropsBase<TRemote, TLocal> & SyncedKeelPropsMany<TRemote, TLocal, TOption, Where>): SyncedCrudReturnType<TLocal, Exclude<TOption, 'value'>>;
124
123
 
125
- export { type KeelClient, type KeelGetParams, type KeelKey, KeelKeys, type KeelListParams, type KeelObjectBase, type KeelRealtimePlugin, type OmitKeelBuiltins, type SyncedKeelPropsBase, generateKeelId, syncedKeel };
124
+ export { type KeelClient, type KeelErrorParams, type KeelGetParams, type KeelKey, KeelKeys, type KeelListParams, type KeelObjectBase, type KeelRealtimePlugin, type OmitKeelBuiltins, type SyncedKeelPropsBase, syncedKeel };