@electric-sql/client 0.7.0 → 0.7.2

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/src/client.ts CHANGED
@@ -31,6 +31,18 @@ import {
31
31
  REPLICA_PARAM,
32
32
  } from './constants'
33
33
 
34
+ const RESERVED_PARAMS = new Set([
35
+ DATABASE_ID_QUERY_PARAM,
36
+ COLUMNS_QUERY_PARAM,
37
+ LIVE_CACHE_BUSTER_QUERY_PARAM,
38
+ SHAPE_HANDLE_QUERY_PARAM,
39
+ LIVE_QUERY_PARAM,
40
+ OFFSET_QUERY_PARAM,
41
+ TABLE_QUERY_PARAM,
42
+ WHERE_QUERY_PARAM,
43
+ REPLICA_PARAM,
44
+ ])
45
+
34
46
  type Replica = `full` | `default`
35
47
 
36
48
  /**
@@ -50,9 +62,9 @@ export interface ShapeStreamOptions<T = never> {
50
62
  databaseId?: string
51
63
 
52
64
  /**
53
- * The root table for the shape.
65
+ * The root table for the shape. Passed as a query parameter. Not required if you set the table in your proxy.
54
66
  */
55
- table: string
67
+ table?: string
56
68
 
57
69
  /**
58
70
  * The where clauses for the shape.
@@ -98,6 +110,12 @@ export interface ShapeStreamOptions<T = never> {
98
110
  */
99
111
  headers?: Record<string, string>
100
112
 
113
+ /**
114
+ * Additional request parameters to attach to the URL.
115
+ * These will be merged with Electric's standard parameters.
116
+ */
117
+ params?: Record<string, string>
118
+
101
119
  /**
102
120
  * Automatically fetch updates to the Shape. If you just want to sync the current
103
121
  * shape and stop, pass false.
@@ -246,7 +264,26 @@ export class ShapeStream<T extends Row<unknown> = Row>
246
264
  this.options.subscribe
247
265
  ) {
248
266
  const fetchUrl = new URL(url)
249
- fetchUrl.searchParams.set(TABLE_QUERY_PARAM, table)
267
+
268
+ // Add any custom parameters first
269
+ if (this.options.params) {
270
+ // Check for reserved parameter names
271
+ const reservedParams = Object.keys(this.options.params).filter(
272
+ (key) => RESERVED_PARAMS.has(key)
273
+ )
274
+ if (reservedParams.length > 0) {
275
+ throw new Error(
276
+ `Cannot use reserved Electric parameter names in custom params: ${reservedParams.join(`, `)}`
277
+ )
278
+ }
279
+
280
+ for (const [key, value] of Object.entries(this.options.params)) {
281
+ fetchUrl.searchParams.set(key, value)
282
+ }
283
+ }
284
+
285
+ // Add Electric's internal parameters
286
+ if (table) fetchUrl.searchParams.set(TABLE_QUERY_PARAM, table)
250
287
  if (where) fetchUrl.searchParams.set(WHERE_QUERY_PARAM, where)
251
288
  if (columns && columns.length > 0)
252
289
  fetchUrl.searchParams.set(COLUMNS_QUERY_PARAM, columns.join(`,`))
@@ -465,9 +502,6 @@ function validateOptions<T>(options: Partial<ShapeStreamOptions<T>>): void {
465
502
  if (!options.url) {
466
503
  throw new Error(`Invalid shape options. It must provide the url`)
467
504
  }
468
- if (!options.table) {
469
- throw new Error(`Invalid shape options. It must provide the table`)
470
- }
471
505
  if (options.signal && !(options.signal instanceof AbortSignal)) {
472
506
  throw new Error(
473
507
  `Invalid signal option. It must be an instance of AbortSignal.`