diff --git a/docs/schematics/store.md b/docs/schematics/store.md index a6e3aed92e..eb34719118 100644 --- a/docs/schematics/store.md +++ b/docs/schematics/store.md @@ -35,7 +35,13 @@ Provide the folder where the state files will be created. - `--statePath` - Type: `string` - - Default: `reducers` + - Default: `reducers` + +Provide the name of the interface exported for your state. When defining with the `--root` option, the name of the store will be used to define the interface name. + +- `--stateInterface` + - Type: `string` + - Default: `State` #### Examples diff --git a/modules/schematics/src/store/files/__path__/__statePath__/index.ts b/modules/schematics/src/store/files/__path__/__statePath__/index.ts index a83f27b499..3051836e0a 100644 --- a/modules/schematics/src/store/files/__path__/__statePath__/index.ts +++ b/modules/schematics/src/store/files/__path__/__statePath__/index.ts @@ -7,13 +7,13 @@ import { } from '@ngrx/store'; import { environment } from '<%= environmentsPath %>'; -export interface State { +export interface <%= classify(stateInterface) %> { } -export const reducers: ActionReducerMap = { +export const reducers: ActionReducerMap<<%= classify(stateInterface) %>> = { }; -export const metaReducers: MetaReducer[] = !environment.production ? [] : []; +export const metaReducers: MetaReducer<<%= classify(stateInterface) %>>[] = !environment.production ? [] : []; diff --git a/modules/schematics/src/store/index.spec.ts b/modules/schematics/src/store/index.spec.ts index be8904452b..4c23651bd1 100644 --- a/modules/schematics/src/store/index.spec.ts +++ b/modules/schematics/src/store/index.spec.ts @@ -84,4 +84,45 @@ describe('Store Schematic', () => { const content = getFileContent(tree, '/src/app/app.module.ts'); expect(content).toMatch(/import \* as fromFoo from '\.\/reducers';/); }); + + it('should support a default root state interface name', () => { + const options = { ...defaultOptions, name: 'State' }; + + const tree = schematicRunner.runSchematic('store', options, appTree); + const content = getFileContent(tree, '/src/app/reducers/index.ts'); + expect(content).toMatch(/export interface State {/); + }); + + it('should support a custom root state interface name', () => { + const options = { + ...defaultOptions, + name: 'State', + stateInterface: 'AppState', + }; + + const tree = schematicRunner.runSchematic('store', options, appTree); + const content = getFileContent(tree, '/src/app/reducers/index.ts'); + expect(content).toMatch(/export interface AppState {/); + }); + + it('should support a default feature state interface name', () => { + const options = { ...defaultOptions, root: false, name: 'Feature' }; + + const tree = schematicRunner.runSchematic('store', options, appTree); + const content = getFileContent(tree, '/src/app/reducers/index.ts'); + expect(content).toMatch(/export interface State {/); + }); + + it('should support a custom feature state interface name', () => { + const options = { + ...defaultOptions, + root: false, + name: 'Feature', + stateInterface: 'FeatureState', + }; + + const tree = schematicRunner.runSchematic('store', options, appTree); + const content = getFileContent(tree, '/src/app/reducers/index.ts'); + expect(content).toMatch(/export interface FeatureState {/); + }); }); diff --git a/modules/schematics/src/store/index.ts b/modules/schematics/src/store/index.ts index 7858bb8d25..8060832c75 100644 --- a/modules/schematics/src/store/index.ts +++ b/modules/schematics/src/store/index.ts @@ -146,6 +146,14 @@ export default function(options: ServiceOptions): Rule { options.module = findModuleFromOptions(host, options); } + if ( + options.root && + options.stateInterface && + options.stateInterface !== 'State' + ) { + options.stateInterface = stringUtils.classify(options.stateInterface); + } + const templateSource = apply(url('./files'), [ template({ ...stringUtils, diff --git a/modules/schematics/src/store/schema.d.ts b/modules/schematics/src/store/schema.d.ts index 832110a700..a90c4473fd 100644 --- a/modules/schematics/src/store/schema.d.ts +++ b/modules/schematics/src/store/schema.d.ts @@ -17,4 +17,8 @@ export interface Schema { module?: string; statePath?: string; root?: boolean; + /** + * Specifies the interface for the state + */ + stateInterface?: string; } diff --git a/modules/schematics/src/store/schema.json b/modules/schematics/src/store/schema.json index 3626175526..e696422dcd 100644 --- a/modules/schematics/src/store/schema.json +++ b/modules/schematics/src/store/schema.json @@ -43,6 +43,12 @@ "type": "boolean", "default": false, "description": "Flag to setup the root state or feature state." + }, + "stateInterface": { + "type": "string", + "default": "State", + "description": "Specifies the interface for the state.", + "alias": "si" } }, "required": [