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/to model operator #7521

Closed
wants to merge 2 commits into from
Closed

Conversation

aalamu
Copy link

@aalamu aalamu commented Dec 31, 2024

Description:

This PR introduces a new toModel operator that transforms an observable stream by mapping emitted values to instances of a specified model class.

The operator simplifies the process of converting raw data (e.g., from API responses) into model instances, improving type safety and reducing boilerplate code.

It follows the existing RxJS operator patterns and is fully tested.

Usage:

apiService.getData()
  .pipe(toModel(Person))
  .subscribe(userInstance => {
    // userInstance is now a Person object
  });

Related issue (if exists):

Yusuf Alamu Musa added 2 commits December 30, 2024 17:21
…odel instances

This commit introduces a new `toModel` operator that allows emitted values from
an observable to be mapped to instances of a specified class. The operator takes
a constructor as an argument and transforms the observable stream by mapping the
data to new instances of the given model class.

The feature enables developers to easily map API responses or other emitted
values into strongly-typed objects within an RxJS observable chain.
…odel instances

This change introduces a new `toModel` operator that allows emitted values from
an observable to be mapped to instances of a specified class. The operator takes
a constructor as an argument and transforms the observable stream by mapping the
data to new instances of the given model class.

The feature enables developers to easily map API responses or other emitted
values into strongly-typed objects within an RxJS observable chain.
@jakovljevic-mladen
Copy link
Member

@aalamu, thank you for the effort, however, RxJS core is trying to reduce core API surface and it doesn't accept new operators for now. Please create your operator in user-land space (by creating your own open source repository or NPM package).

The operator suggested in this PR is very simple and can be created with using map operator, so it most probably won't be included in library as such. This is how to use it with map operator:

.pipe(map((arg) => new Person(arg)))

@aalamu
Copy link
Author

aalamu commented Dec 31, 2024

Hi @jakovljevic-mladen ,

Thanks for the feedback, understood.

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.

2 participants