@instantdb/core 0.22.155 → 0.22.156

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instantdb/core",
3
- "version": "0.22.155",
3
+ "version": "0.22.156",
4
4
  "description": "Instant's core local abstraction",
5
5
  "homepage": "https://github.com/instantdb/instant/tree/main/client/packages/core",
6
6
  "repository": {
@@ -56,7 +56,7 @@
56
56
  "dependencies": {
57
57
  "mutative": "^1.0.10",
58
58
  "uuid": "^11.1.0",
59
- "@instantdb/version": "0.22.155"
59
+ "@instantdb/version": "0.22.156"
60
60
  },
61
61
  "scripts": {
62
62
  "test": "vitest",
@@ -1,9 +1,13 @@
1
1
  export class InstantError extends Error {
2
2
  hint?: unknown;
3
+ traceId?: string;
3
4
 
4
- constructor(message: string, hint?: unknown) {
5
+ constructor(message: string, hint?: unknown, traceId?: string) {
5
6
  super(message);
6
7
  this.hint = hint;
8
+ if (traceId) {
9
+ this.traceId = traceId;
10
+ }
7
11
 
8
12
  const actualProto = new.target.prototype;
9
13
  if (Object.setPrototypeOf) {
package/src/Reactor.js CHANGED
@@ -41,6 +41,7 @@ import { InstantStream } from './Stream.ts';
41
41
  /** @typedef {import('./reactorTypes.ts').QuerySubInStorage} QuerySubInStorage */
42
42
  /** @typedef {import('./clientTypes.ts').User} User */
43
43
  /** @typedef {import('./clientTypes.ts').AuthState} AuthState */
44
+ /** @typedef {import('./framework.ts').FrameworkClient} FrameworkClient */
44
45
 
45
46
  export const STATUS = {
46
47
  CONNECTING: 'connecting',
@@ -272,6 +273,8 @@ export default class Reactor {
272
273
  _pendingTxCleanupTimeout;
273
274
  _pendingMutationCleanupThreshold;
274
275
  _inFlightMutationEventIds = new Set();
276
+ /** @type FrameworkClient | null */
277
+ _frameworkClient = null;
275
278
 
276
279
  constructor(
277
280
  config,
@@ -406,6 +409,15 @@ export default class Reactor {
406
409
  }
407
410
  }
408
411
 
412
+ getFrameworkClient() {
413
+ return this._frameworkClient;
414
+ }
415
+
416
+ /** @param {FrameworkClient} client */
417
+ setFrameworkClient(client) {
418
+ this._frameworkClient = client;
419
+ }
420
+
409
421
  ensureAttrs() {
410
422
  if (!this.attrs) {
411
423
  throw new Error('attrs have not loaded.');
@@ -2105,6 +2117,9 @@ export default class Reactor {
2105
2117
  // bother updating
2106
2118
  return;
2107
2119
  }
2120
+
2121
+ // Clear any data in the framework client
2122
+ this._frameworkClient = null;
2108
2123
  await this.setCurrentUser(newUser);
2109
2124
  // We need to remove all `result` from querySubs,
2110
2125
  // as they are no longer valid for the new user
package/src/framework.ts CHANGED
@@ -3,8 +3,10 @@
3
3
  // The class is generic so that it can be a good starting off point to make other ssr adapters.
4
4
  import {
5
5
  coerceQuery,
6
+ InstantAPIError,
6
7
  InstantCoreDatabase,
7
8
  InstantDBAttr,
9
+ InstantError,
8
10
  weakHash,
9
11
  } from './index.ts';
10
12
  import * as s from './store.js';
@@ -109,6 +111,67 @@ export class FrameworkClient {
109
111
  }
110
112
  };
111
113
 
114
+ public removeCachedQueryResult = (queryHash: string) => {
115
+ this.resultMap.delete(queryHash);
116
+ };
117
+
118
+ // Run a query on the client and return a promise with the result
119
+ public queryClient = (
120
+ query_: any,
121
+ opts?: { ruleParams: RuleParams },
122
+ ): Promise<QueryPromise> => {
123
+ const { hash, query } = this.hashQuery(query_, opts);
124
+
125
+ let resolve;
126
+ let reject;
127
+
128
+ const promise: Promise<QueryPromise> = new Promise(
129
+ (resolvePromise, rejectPromise) => {
130
+ resolve = resolvePromise;
131
+ reject = rejectPromise;
132
+ },
133
+ );
134
+
135
+ let entry = {
136
+ status: 'pending' as 'pending' | 'success' | 'error',
137
+ type: 'session' as 'http' | 'session',
138
+ data: undefined as any,
139
+ error: undefined as any,
140
+ promise: promise as any,
141
+ };
142
+
143
+ let unsub: null | (() => void) = null;
144
+ let unsubImmediately = false;
145
+
146
+ unsub = this.db.subscribeQuery(query, (res) => {
147
+ if (res.error) {
148
+ entry.status = 'error';
149
+ entry.error = res.error;
150
+ entry.promise = null;
151
+ reject(res.error);
152
+ } else {
153
+ entry.status = 'success';
154
+ entry.data = res;
155
+ entry.promise = null;
156
+ resolve(res);
157
+ }
158
+ if (unsub !== null) {
159
+ unsub();
160
+ } else {
161
+ unsubImmediately;
162
+ }
163
+ });
164
+
165
+ // We may have gotten the result inside of subscribeQuery before
166
+ // we defined the `unsub` function
167
+ if (unsubImmediately) {
168
+ unsub();
169
+ }
170
+
171
+ this.resultMap.set(hash, entry);
172
+ return promise;
173
+ };
174
+
112
175
  // creates an entry in the results map
113
176
  // and returns the same thing added to the map
114
177
  public query = (
@@ -282,7 +345,19 @@ export class FrameworkClient {
282
345
  );
283
346
 
284
347
  if (!response.ok) {
285
- throw new Error('Error getting triples from server');
348
+ try {
349
+ const data = await response.json();
350
+ if ('message' in data) {
351
+ throw new InstantAPIError({ body: data, status: response.status });
352
+ } else {
353
+ throw new Error('Error getting triples from server');
354
+ }
355
+ } catch (e) {
356
+ if (e instanceof InstantError) {
357
+ throw e;
358
+ }
359
+ throw new Error('Error getting triples from server');
360
+ }
286
361
  }
287
362
 
288
363
  const data = await response.json();
@@ -307,6 +382,9 @@ export class FrameworkClient {
307
382
  pageInfo,
308
383
  };
309
384
  } catch (err: any) {
385
+ if (err instanceof InstantError) {
386
+ throw err;
387
+ }
310
388
  const errWithMessage = new Error(
311
389
  'Error getting triples from framework client',
312
390
  );
@@ -86,7 +86,7 @@ export class InstantAPIError extends InstantError {
86
86
  constructor(error: InstantIssue) {
87
87
  // Create a descriptive message based on the error
88
88
  const message = error.body?.message || `API Error (${error.status})`;
89
- super(message, (error.body as any).hint);
89
+ super(message, (error.body as any).hint, (error.body as any)['trace-id']);
90
90
 
91
91
  const actualProto = new.target.prototype;
92
92
  if (Object.setPrototypeOf) {