-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: let users pass sync/async functions for resolving option values…
… for headers/params (#2184) Fix #2181 This PR adds support for function-based options in the TypeScript client's params and headers. Functions can be either synchronous or asynchronous and are resolved in parallel when needed. ```typescript const stream = new ShapeStream({ url: 'http://localhost:3000/v1/shape', params: { table: 'items', userId: () => getCurrentUserId(), filter: async () => await getUserPreferences() }, headers: { 'Authorization': async () => `Bearer ${await getAccessToken()}` } }) ``` ## Common Use Cases - Authentication tokens that need to be refreshed - User-specific parameters that may change - Dynamic filtering based on current state - Multi-tenant applications where context determines the request ## Design Decision We chose to implement this using direct function values rather than a middleware/interceptor pattern (as seen in libraries like Axios or ky) for several reasons: - Simplicity: Direct function values in config objects are more intuitive than middleware chains. There's a clear 1:1 relationship between the option and its value resolution. - Granularity: Each param/header can be individually dynamic. No need to set up middleware just to make a single value dynamic. - Performance: All async functions are resolved in parallel by default, which isn't always the case with sequential middleware chains. - Focused API Surface: As we're not a general-purpose HTTP client, we don't need the complexity of a full middleware system. This approach keeps our API surface smaller and more focused on Electric's specific needs.
- Loading branch information
1 parent
b9d31ea
commit dd5aeab
Showing
7 changed files
with
284 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
"@electric-sql/client": patch | ||
"@electric-sql/docs": patch | ||
--- | ||
|
||
This PR adds support for function-based options in the TypeScript client's params and headers. Functions can be either synchronous or asynchronous and are resolved in parallel when needed. | ||
|
||
```typescript | ||
const stream = new ShapeStream({ | ||
url: 'http://localhost:3000/v1/shape', | ||
params: { | ||
table: 'items', | ||
userId: () => getCurrentUserId(), | ||
filter: async () => await getUserPreferences() | ||
}, | ||
headers: { | ||
'Authorization': async () => `Bearer ${await getAccessToken()}` | ||
} | ||
}) | ||
``` | ||
|
||
## Common Use Cases | ||
- Authentication tokens that need to be refreshed | ||
- User-specific parameters that may change | ||
- Dynamic filtering based on current state | ||
- Multi-tenant applications where context determines the request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.