Skip to content

Commit

Permalink
feat(crud): add support for new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
va-stefanek committed Jul 5, 2021
1 parent 23e92d6 commit d104a46
Show file tree
Hide file tree
Showing 19 changed files with 310 additions and 143 deletions.
4 changes: 4 additions & 0 deletions src/collection/crud/crud-schema.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ export interface CrudSchema {
operation: string[];
responseType?: string;
stateDir: string;
/**
* Specifies whether to use creator NgRx syntax.
*/
creators?: boolean;
}
7 changes: 7 additions & 0 deletions src/collection/crud/crud-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
"type": "string",
"description": "Add map operator to response pipe.",
"x-prompt": "What is response object path to map?"
},
"creators": {
"type": "boolean",
"default": true,
"description": "Specifies whether to use creators NgRx syntax",
"aliases": ["c"],
"x-prompt": "Do you want to use the creators NgRx syntax?"
}
}
}
3 changes: 2 additions & 1 deletion src/collection/crud/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ describe('crud', () => {
stateDir: '/libs/data-access-test/src/lib/+state',
operation: ['Read', 'ReadCollection', 'Create', 'Update', 'Delete'],
mapResponse: 'data',
responseType: 'ApiResponse<TestModel>'
responseType: 'ApiResponse<TestModel>',
creators: false
};

appTree = await runner.runSchematicAsync('crud', crudOpts, appTree).toPromise();
Expand Down
20 changes: 12 additions & 8 deletions src/collection/crud/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ import { crudFacadeSpec } from './rules/crud-facade-spec.rule';
import { crudFacade } from './rules/crud-facade.rule';
import { crudReducer } from './rules/crud-reducer.rule';

export interface CrudGenerate {
create: boolean;
read: boolean;
readCollection: boolean;
update: boolean;
delete: boolean;
}

export interface CrudOptions {
actionPrefix: string;
actionsAliasName: string;
Expand Down Expand Up @@ -66,13 +74,8 @@ export interface CrudOptions {
stateDir: StateFilePaths;
statePartialName: string;
statePath: string;
toGenerate: {
create: boolean;
read: boolean;
readCollection: boolean;
update: boolean;
delete: boolean;
};
toGenerate: CrudGenerate;
creators: boolean;
}

// tslint:disable-next-line:cognitive-complexity
Expand Down Expand Up @@ -221,7 +224,8 @@ export function parseOptions(host: Tree, options: CrudSchema): CrudOptions {
path: entityPath[0],
name: entity
},
statePath: stateDir
statePath: stateDir,
creators: options.creators || false
};
}

Expand Down
66 changes: 21 additions & 45 deletions src/collection/crud/rules/crud-actions.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import { CrudOptions } from '../index';
function createActionRules(options: CrudOptions): Rule[] {
const { toGenerate, entity, actionPrefix, statePath } = options;
const rules: Rule[] = [];
const genericOptions = {
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true,
creators: options.creators
};

if (toGenerate.read) {
rules.push(
action({
payload: getRequestPayloadClass(`Get${entity.name}`),
name: `Get${entity.name}`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
}),
action({
payload: 'HttpErrorResponse',
name: `Get${entity.name}Fail`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
}),
action({
payload: `${entity.name}`,
name: `Get${entity.name}Success`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
})
);
}
Expand All @@ -41,23 +41,17 @@ function createActionRules(options: CrudOptions): Rule[] {
action({
payload: getRequestPayloadClass(`Get${entity.name}Collection`),
name: `Get${entity.name}Collection`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
}),
action({
payload: 'HttpErrorResponse',
name: `Get${entity.name}CollectionFail`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
}),
action({
payload: `${entity.name}[]`,
name: `Get${entity.name}CollectionSuccess`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
})
);
}
Expand All @@ -67,23 +61,17 @@ function createActionRules(options: CrudOptions): Rule[] {
action({
payload: getRequestPayloadClass(`Create${entity.name}`),
name: `Create${entity.name}`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
}),
action({
payload: 'HttpErrorResponse',
name: `Create${entity.name}Fail`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
}),
action({
payload: entity.name,
name: `Create${entity.name}Success`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
})
);
}
Expand All @@ -93,23 +81,17 @@ function createActionRules(options: CrudOptions): Rule[] {
action({
payload: getRequestPayloadClass(`Update${entity.name}`),
name: `Update${entity.name}`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
}),
action({
payload: 'HttpErrorResponse',
name: `Update${entity.name}Fail`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
}),
action({
payload: entity.name,
name: `Update${entity.name}Success`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
})
);
}
Expand All @@ -119,23 +101,17 @@ function createActionRules(options: CrudOptions): Rule[] {
action({
payload: getRequestPayloadClass(`Remove${entity.name}`),
name: `Remove${entity.name}`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
}),
action({
payload: 'HttpErrorResponse',
name: `Remove${entity.name}Fail`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
}),
action({
payload: getRequestPayloadClass(`Remove${entity.name}`),
name: `Remove${entity.name}Success`,
prefix: actionPrefix,
stateDir: statePath,
skipFormat: true
...genericOptions
})
);
}
Expand Down
Loading

0 comments on commit d104a46

Please sign in to comment.