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

Support Observable return types for Nestia SDK #747

Open
sourav-bhar opened this issue Jan 15, 2024 · 2 comments
Open

Support Observable return types for Nestia SDK #747

sourav-bhar opened this issue Jan 15, 2024 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@sourav-bhar
Copy link

Feature Request

When using the Nestia Client SDK, it can properly infer the controller method return value types when returning synchronously or returning a Promise. However, if we try to return an Observable, then the SDK is not able to generate types properly.

Since NestJS has first-class support for Observables, many times we work with Observables exclusively and would just like to return the Observable from the controller method instead of converting it to a Promise and then returning the Promise. However, returning an Observable type breaks Nestia's ability to properly type the response type.

Workaround's we need to use today:

  1. Use async / await and only work with Promises
  2. Use the firstValueFrom rxjs operator to convert Observable to Promise before returning the response from the controller.

This works fine:

   @TypedRoute.Get('user/:id')
   async getUser(@Param('id') id: string): Promise<User> {
     return firstValueFrom(this.userService.findById(id));
   }

This does not:

   @TypedRoute.Get('user/:id')
   getUser(@Param('id') id: string): Observable<User> {
     return this.userService.findById(id);
   }
@samchon
Copy link
Owner

samchon commented Jan 15, 2024

Can you give me an example repo?

I can't image the Observable type being used in the SDK, because in my memory, it does not have any property mebers and there're only member methods in the type.

@samchon samchon self-assigned this Jan 15, 2024
@samchon samchon added the question Further information is requested label Jan 15, 2024
@sourav-bhar
Copy link
Author

Ok, I will try to provide a sample repo tomorrow. Basically, when Nestia sees the Observable being returned, it should essentially consider it the same as Promise since it is irrelevant if Promise or Observable is being returned by the controller method. The fetch functions used by the Nestia SDK will always return a Promise as expected. However, all information about ReturnType is lost when returning Observable from the controller method, but it works fine when returning Promise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants