Skip to content

Commit 295547b

Browse files
authored
feat(angular): add backwards compat support for the ngrx generator (#14348)
1 parent c94ac41 commit 295547b

23 files changed

+373
-24
lines changed

docs/generated/packages/angular/generators/ngrx.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
},
3333
"parent": {
3434
"type": "string",
35-
"description": "The path to the `NgModule` or the `Routes` definition file (for Standalone API usage) where the feature state will be registered. The host directory will create/use the new state directory.",
35+
"description": "The path to the `NgModule` or the `Routes` definition file (for Standalone API usage) where the feature state will be registered. _Note: The Standalone API usage is only supported in Angular versions >= 14.1.0_.",
3636
"x-prompt": "What is the path to the module or Routes definition where this NgRx state should be registered?"
3737
},
3838
"route": {
3939
"type": "string",
40-
"description": "The route that the Standalone NgRx Providers should be added to.",
40+
"description": "The route that the Standalone NgRx Providers should be added to. _Note: This is only supported in Angular versions >= 14.1.0_.",
4141
"default": "''"
4242
},
4343
"directory": {

packages/angular/package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
},
4141
"dependencies": {
4242
"@angular-devkit/schematics": "~15.1.0",
43-
"@nguniversal/builders": "~15.0.0",
4443
"@nrwl/cypress": "file:../cypress",
4544
"@nrwl/devkit": "file:../devkit",
4645
"@nrwl/jest": "file:../jest",
@@ -61,6 +60,15 @@
6160
"webpack": "^5.75.0",
6261
"webpack-merge": "5.7.3"
6362
},
63+
"peerDependencies": {
64+
"@nguniversal/builders": "~15.0.0",
65+
"rxjs": "^6.5.3 || ^7.5.0"
66+
},
67+
"peerDependenciesMeta": {
68+
"@nguniversal/builders": {
69+
"optional": true
70+
}
71+
},
6472
"publishConfig": {
6573
"access": "public"
6674
}

packages/angular/src/generators/ngrx/__snapshots__/ngrx.spec.ts.snap

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,117 @@ import { UsersEffects } from './+state/users.effects';
604604
import { UsersFacade } from './+state/users.facade';
605605
export const appRoutes: Routes = [{ path: '', component: NxWelcomeComponent , providers: [UsersFacade, provideState(fromUsers.USERS_FEATURE_KEY, fromUsers.usersReducer), provideEffects(UsersEffects)]}];"
606606
`;
607+
608+
exports[`ngrx angular v14 support should generate the ngrx effects using "inject" for versions >= 14.1.0 1`] = `
609+
"import { Injectable, inject } from '@angular/core';
610+
import { createEffect, Actions, ofType } from '@ngrx/effects';
611+
612+
import * as UsersActions from './users.actions';
613+
import * as UsersFeature from './users.reducer';
614+
615+
import {switchMap, catchError, of} from 'rxjs';
616+
617+
@Injectable()
618+
export class UsersEffects {
619+
private actions$ = inject(Actions);
620+
621+
init$ = createEffect(() => this.actions$.pipe(
622+
ofType(UsersActions.initUsers),
623+
switchMap(() => of(UsersActions.loadUsersSuccess({ users: [] }))),
624+
catchError((error) => {
625+
console.error('Error', error);
626+
return of(UsersActions.loadUsersFailure({ error }));
627+
}
628+
)
629+
));
630+
}
631+
"
632+
`;
633+
634+
exports[`ngrx angular v14 support should generate the ngrx effects with no usage of "inject" 1`] = `
635+
"import { Injectable } from '@angular/core';
636+
import { createEffect, Actions, ofType } from '@ngrx/effects';
637+
638+
import * as UsersActions from './users.actions';
639+
import * as UsersFeature from './users.reducer';
640+
641+
import {switchMap, catchError, of} from 'rxjs';
642+
643+
@Injectable()
644+
export class UsersEffects {
645+
init$ = createEffect(() => this.actions$.pipe(
646+
ofType(UsersActions.initUsers),
647+
switchMap(() => of(UsersActions.loadUsersSuccess({ users: [] }))),
648+
catchError((error) => {
649+
console.error('Error', error);
650+
return of(UsersActions.loadUsersFailure({ error }));
651+
}
652+
)
653+
));
654+
655+
constructor(private readonly actions$: Actions) {}
656+
}
657+
"
658+
`;
659+
660+
exports[`ngrx angular v14 support should generate the ngrx facade using "inject" for versions >= 14.1.0 1`] = `
661+
"import { Injectable, inject } from '@angular/core';
662+
import { select, Store, Action } from '@ngrx/store';
663+
664+
import * as UsersActions from './users.actions';
665+
import * as UsersFeature from './users.reducer';
666+
import * as UsersSelectors from './users.selectors';
667+
668+
@Injectable()
669+
export class UsersFacade {
670+
private readonly store = inject(Store);
671+
672+
/**
673+
* Combine pieces of state using createSelector,
674+
* and expose them as observables through the facade.
675+
*/
676+
loaded$ = this.store.pipe(select(UsersSelectors.selectUsersLoaded));
677+
allUsers$ = this.store.pipe(select(UsersSelectors.selectAllUsers));
678+
selectedUsers$ = this.store.pipe(select(UsersSelectors.selectEntity));
679+
680+
/**
681+
* Use the initialization action to perform one
682+
* or more tasks in your Effects.
683+
*/
684+
init() {
685+
this.store.dispatch(UsersActions.initUsers());
686+
}
687+
}
688+
"
689+
`;
690+
691+
exports[`ngrx angular v14 support should generate the ngrx facade with no usage of "inject" 1`] = `
692+
"import { Injectable } from '@angular/core';
693+
import { select, Store, Action } from '@ngrx/store';
694+
695+
import * as UsersActions from './users.actions';
696+
import * as UsersFeature from './users.reducer';
697+
import * as UsersSelectors from './users.selectors';
698+
699+
@Injectable()
700+
export class UsersFacade {
701+
/**
702+
* Combine pieces of state using createSelector,
703+
* and expose them as observables through the facade.
704+
*/
705+
loaded$ = this.store.pipe(select(UsersSelectors.selectUsersLoaded));
706+
allUsers$ = this.store.pipe(select(UsersSelectors.selectAllUsers));
707+
selectedUsers$ = this.store.pipe(select(UsersSelectors.selectEntity));
708+
709+
constructor(private readonly store: Store) {}
710+
711+
/**
712+
* Use the initialization action to perform one
713+
* or more tasks in your Effects.
714+
*/
715+
init() {
716+
this.store.dispatch(UsersActions.initUsers());
717+
}
718+
}
719+
"
720+
`;

0 commit comments

Comments
 (0)