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

Add asAsyncData helper method for fluent Promises (e.g., Postgrest-js) integration #443

Open
leynier opened this issue Dec 16, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@leynier
Copy link

leynier commented Dec 16, 2024

Is your feature request related to a problem? Please describe.

When using libraries like postgrest-js in a Nuxt project, the returned objects (e.g., PostgrestBuilder or PostgrestFilterBuilder) implement PromiseLike. While this allows resolving results with await or .then(), the preferred Nuxt pattern for handling asynchronous data is through the useAsyncData composable.

Currently, manually wrapping these fluent chains in useAsyncData can feel verbose and repetitive, reducing code clarity and developer experience.


Describe the solution you'd like

It would be helpful to introduce a helper method like asAsyncData() directly on PromiseLike objects (e.g., PostgrestBuilder in postgrest-js). This method would wrap the current fluent chain into useAsyncData, enabling a more concise and fluent syntax.

Example:

Instead of:

const { data, status, error } = await useAsyncData(
  'fetch-user',
  () => supabase.from('users').select('*').eq('id', 1)
);

You could write:

const { data, status, error } = await supabase
  .from('users')
  .select('*')
  .eq('id', 1)
  .asAsyncData('fetch-user');

Describe alternatives you've considered

  • Manual wrapping: Continue manually wrapping each fluent chain in useAsyncData. While functional, this approach leads to repetitive code and reduces readability.
  • Utility functions: Creating a standalone helper function to wrap the promises. However, this doesn't integrate cleanly into the fluent interface pattern of libraries like postgrest-js.

Additional context

The solution can leverage prototype chaining to extend the behavior of PromiseLike objects such as PostgrestBuilder, and declaration merging to ensure proper TypeScript type support. This approach avoids modifying the original library code and provides a clean, composable integration into Nuxt.

Related Nuxt documentation: useAsyncData.

@leynier leynier added the enhancement New feature or request label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant