Skip to content

Commit

Permalink
fix: do validation for params off constructor so it can throw before …
Browse files Browse the repository at this point in the history
…starting (#1984)

did this wrong in #1978 so that combined with #1983, passing a reserved
parameter just means it silently fails atm.
  • Loading branch information
KyleAMathews authored Nov 18, 2024
1 parent 640fc04 commit 91e5b85
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
8 changes: 4 additions & 4 deletions examples/nextjs-example/.sst/platform/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import "./src/global.d.ts"
import "../types.generated"
import { AppInput, App, Config } from "./src/config"
import * as _neon from "@sst-provider/neon";
import * as _cloudflare from "@pulumi/cloudflare";
import * as _aws from "@pulumi/aws";
import * as _cloudflare from "@pulumi/cloudflare";


declare global {
// @ts-expect-error
export import neon = _neon
// @ts-expect-error
export import cloudflare = _cloudflare
// @ts-expect-error
export import aws = _aws
// @ts-expect-error
export import cloudflare = _cloudflare
interface Providers {
providers?: {
"neon"?: (_neon.ProviderArgs & { version?: string }) | boolean | string;
"cloudflare"?: (_cloudflare.ProviderArgs & { version?: string }) | boolean | string;
"aws"?: (_aws.ProviderArgs & { version?: string }) | boolean | string;
"cloudflare"?: (_cloudflare.ProviderArgs & { version?: string }) | boolean | string;
}
}
export const $config: (
Expand Down
29 changes: 28 additions & 1 deletion packages/typescript-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ const RESERVED_PARAMS = new Set([

type Replica = `full` | `default`

type ReservedParamKeys =
| typeof DATABASE_ID_QUERY_PARAM
| typeof COLUMNS_QUERY_PARAM
| typeof LIVE_CACHE_BUSTER_QUERY_PARAM
| typeof SHAPE_HANDLE_QUERY_PARAM
| typeof LIVE_QUERY_PARAM
| typeof OFFSET_QUERY_PARAM
| typeof TABLE_QUERY_PARAM
| typeof WHERE_QUERY_PARAM
| typeof REPLICA_PARAM

type ParamsRecord = Omit<Record<string, string>, ReservedParamKeys>

/**
* Options for constructing a ShapeStream.
*/
Expand Down Expand Up @@ -113,8 +126,10 @@ export interface ShapeStreamOptions<T = never> {
/**
* Additional request parameters to attach to the URL.
* These will be merged with Electric's standard parameters.
* Note: You cannot use Electric's reserved parameter names
* (table, where, columns, offset, handle, live, cursor, database_id, replica).
*/
params?: Record<string, string>
params?: ParamsRecord

/**
* Automatically fetch updates to the Shape. If you just want to sync the current
Expand Down Expand Up @@ -517,5 +532,17 @@ function validateOptions<T>(options: Partial<ShapeStreamOptions<T>>): void {
`shapeHandle is required if this isn't an initial fetch (i.e. offset > -1)`
)
}

// Check for reserved parameter names
if (options.params) {
const reservedParams = Object.keys(options.params).filter((key) =>
RESERVED_PARAMS.has(key)
)
if (reservedParams.length > 0) {
throw new Error(
`Cannot use reserved Electric parameter names in custom params: ${reservedParams.join(`, `)}`
)
}
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Shape > should throw on a reserved parameter 1`] = `[Error: Cannot use reserved Electric parameter names in custom params: database_id]`;
13 changes: 13 additions & 0 deletions packages/typescript-client/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ describe(`Shape`, () => {
expect(shape.lastSynced()).toBeLessThanOrEqual(Date.now() - start)
})

it(`should throw on a reserved parameter`, async () => {
expect(() => {
const shapeStream = new ShapeStream({
url: `${BASE_URL}/v1/shape`,
table: `foo`,
params: {
database_id: `foo`,
},
})
new Shape(shapeStream)
}).toThrowErrorMatchingSnapshot()
})

it(`should notify with the initial value`, async ({
issuesTableUrl,
insertIssues,
Expand Down

0 comments on commit 91e5b85

Please sign in to comment.