Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: let users pass sync/async functions for resolving option values for headers/params #2184

Merged
merged 11 commits into from
Dec 19, 2024

Conversation

KyleAMathews
Copy link
Contributor

@KyleAMathews KyleAMathews commented Dec 18, 2024

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.

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.

Copy link

netlify bot commented Dec 18, 2024

Deploy Preview for electric-next ready!

Name Link
🔨 Latest commit 3d9cf21
🔍 Latest deploy log https://app.netlify.com/sites/electric-next/deploys/67645785a223ed0008643653
😎 Deploy Preview https://deploy-preview-2184--electric-next.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@KyleAMathews KyleAMathews changed the title feat: let users pass sync/async functions for resolving values for headers/params feat: let users pass sync/async functions for resolving option values for headers/params Dec 18, 2024
@KyleAMathews KyleAMathews requested review from thruflo, msfstef and kevin-dp and removed request for thruflo December 18, 2024 17:21
Copy link
Contributor

@thruflo thruflo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant -- the usage and docs are both 👌

@KyleAMathews
Copy link
Contributor Author

Brilliant -- the usage and docs are both 👌

Gotta thank my colleague Windsurf for these 😎

Copy link
Contributor

@msfstef msfstef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I'm a bit weary of adding async steps in the tight loop, and would like more protection (at both the type and runtime level) agains people setting reserved params and headers

packages/typescript-client/src/client.ts Outdated Show resolved Hide resolved
packages/typescript-client/src/client.ts Show resolved Hide resolved
packages/typescript-client/src/client.ts Show resolved Hide resolved
packages/typescript-client/src/client.ts Outdated Show resolved Hide resolved
packages/typescript-client/src/client.ts Outdated Show resolved Hide resolved
website/docs/api/clients/typescript.md Show resolved Hide resolved
website/docs/api/clients/typescript.md Show resolved Hide resolved
@KyleAMathews KyleAMathews merged commit dd5aeab into main Dec 19, 2024
32 of 33 checks passed
@KyleAMathews KyleAMathews deleted the function-options branch December 19, 2024 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support function for ShapeStream headers option.
4 participants