Skip to content

Commit 1352d83

Browse files
feat(Schematics): Add support for custom store interface name (#810)
1 parent 3afc956 commit 1352d83

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

docs/schematics/store.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ Provide the folder where the state files will be created.
3535

3636
- `--statePath`
3737
- Type: `string`
38-
- Default: `reducers`
38+
- Default: `reducers`
39+
40+
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.
41+
42+
- `--stateInterface`
43+
- Type: `string`
44+
- Default: `State`
3945

4046
#### Examples
4147

modules/schematics/src/store/files/__path__/__statePath__/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import {
77
} from '@ngrx/store';
88
import { environment } from '<%= environmentsPath %>';
99

10-
export interface State {
10+
export interface <%= classify(stateInterface) %> {
1111

1212
}
1313

14-
export const reducers: ActionReducerMap<State> = {
14+
export const reducers: ActionReducerMap<<%= classify(stateInterface) %>> = {
1515

1616
};
1717

1818

19-
export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];
19+
export const metaReducers: MetaReducer<<%= classify(stateInterface) %>>[] = !environment.production ? [] : [];

modules/schematics/src/store/index.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,45 @@ describe('Store Schematic', () => {
8484
const content = getFileContent(tree, '/src/app/app.module.ts');
8585
expect(content).toMatch(/import \* as fromFoo from '\.\/reducers';/);
8686
});
87+
88+
it('should support a default root state interface name', () => {
89+
const options = { ...defaultOptions, name: 'State' };
90+
91+
const tree = schematicRunner.runSchematic('store', options, appTree);
92+
const content = getFileContent(tree, '/src/app/reducers/index.ts');
93+
expect(content).toMatch(/export interface State {/);
94+
});
95+
96+
it('should support a custom root state interface name', () => {
97+
const options = {
98+
...defaultOptions,
99+
name: 'State',
100+
stateInterface: 'AppState',
101+
};
102+
103+
const tree = schematicRunner.runSchematic('store', options, appTree);
104+
const content = getFileContent(tree, '/src/app/reducers/index.ts');
105+
expect(content).toMatch(/export interface AppState {/);
106+
});
107+
108+
it('should support a default feature state interface name', () => {
109+
const options = { ...defaultOptions, root: false, name: 'Feature' };
110+
111+
const tree = schematicRunner.runSchematic('store', options, appTree);
112+
const content = getFileContent(tree, '/src/app/reducers/index.ts');
113+
expect(content).toMatch(/export interface State {/);
114+
});
115+
116+
it('should support a custom feature state interface name', () => {
117+
const options = {
118+
...defaultOptions,
119+
root: false,
120+
name: 'Feature',
121+
stateInterface: 'FeatureState',
122+
};
123+
124+
const tree = schematicRunner.runSchematic('store', options, appTree);
125+
const content = getFileContent(tree, '/src/app/reducers/index.ts');
126+
expect(content).toMatch(/export interface FeatureState {/);
127+
});
87128
});

modules/schematics/src/store/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ export default function(options: StoreOptions): Rule {
145145
options.module = findModuleFromOptions(host, options);
146146
}
147147

148+
if (
149+
options.root &&
150+
options.stateInterface &&
151+
options.stateInterface !== 'State'
152+
) {
153+
options.stateInterface = stringUtils.classify(options.stateInterface);
154+
}
155+
148156
const templateSource = apply(url('./files'), [
149157
template({
150158
...stringUtils,

modules/schematics/src/store/schema.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ export interface Schema {
1717
module?: string;
1818
statePath?: string;
1919
root?: boolean;
20+
/**
21+
* Specifies the interface for the state
22+
*/
23+
stateInterface?: string;
2024
}

modules/schematics/src/store/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
"type": "boolean",
4444
"default": false,
4545
"description": "Flag to setup the root state or feature state."
46+
},
47+
"stateInterface": {
48+
"type": "string",
49+
"default": "State",
50+
"description": "Specifies the interface for the state.",
51+
"alias": "si"
4652
}
4753
},
4854
"required": [

0 commit comments

Comments
 (0)