ngrx / platform

Reactive State for Angular
https://ngrx.io
Other
7.96k stars 1.95k forks source link

feat(operators): add `mapResponse` #4302

Closed tom9744 closed 2 months ago

tom9744 commented 2 months ago

PR Checklist

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

export const loadAllUsers = createEffect((
  actions$ = inject(Actions),
  usersService = inject(UsersService)
) => {
  return actions$.pipe(
    ofType(UsersPageActions.opened),
    exhaustMap(() => {
      return usersService.getAll().pipe(
        map((users) => UsersApiActions.usersLoadedSuccess({ users })),
        catchError((error) =>
          of(UsersApiActions.usersLoadedFailure({ error }))
        )
      );
    })
  );
});

Closes #4230

What is the new behavior?

import { mapResponse } from '@ngrx/operators';

export const loadAllUsers = createEffect((
  actions$ = inject(Actions),
  usersService = inject(UsersService)
) => {
  return actions$.pipe(
    ofType(UsersPageActions.opened),
    exhaustMap(() => {
      return usersService.getAll().pipe(
        mapResponse({
          next: (users) => UsersApiActions.usersLoadedSuccess({ users }),
          error: (error) => UsersApiActions.usersLoadedFailure({ error }),
        })
      );
    })
  );
});

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

I am not 100% sure if I correctly understood the expected behavior of mapResponse, as @markostanimirovic suggested in #4230.

Please feel free to give me feedback.

Thanks.

netlify[bot] commented 2 months ago

Deploy Preview for ngrx-io ready!

Built without sensitive environment variables

Name Link
Latest commit f09ef970c94b6c0b120fdf515d3df881a4e9555e
Latest deploy log https://app.netlify.com/sites/ngrx-io/deploys/663f8781c9a5320008a5d281
Deploy Preview https://deploy-preview-4302--ngrx-io.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.

tom9744 commented 2 months ago

I appreciate your kind feedback on my work, @markostanimirovic!

I modified the code based on your suggestions, and am currently working on writing a document for ngrx.io.

tom9744 commented 2 months ago

I understood that typing the error parameter as unknown provides a developer more accuracy, but less convenience.

markostanimirovic commented 2 months ago

Thanks! @markostanimirovic do you think we should also add a JSDoc (with usagenotes) as we do with the other operators?

Good point Tim! I created an issue for this: https://github.com/ngrx/platform/issues/4336