Skip to content

Commit cbb30cf

Browse files
committed
feat(generators/app): refactor template & add types.ts that contains entity type def
1 parent 907030a commit cbb30cf

File tree

9 files changed

+67
-63
lines changed

9 files changed

+67
-63
lines changed

.lintstagedrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"*.{ts,js,json,md,yml}": "yarn format",
2+
"**/*.{js,json,md,yml}": "yarn format",
33
"!(**/templates/*).js": "yarn lint"
44
}

generators/app/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ module.exports = class extends Generator {
1313
const prompts = [
1414
{
1515
type: 'input',
16-
name: 'elementComponentName',
16+
name: 'compName',
1717
message: 'Name of the new component (singular)?',
1818
default: 'thing',
1919
},
2020
];
2121

2222
return this.prompt(prompts).then((props) => {
23-
const elementCompNameParamCase = changeCase.paramCase(
24-
props.elementComponentName,
25-
);
23+
const compNameParamCase = changeCase.paramCase(props.compName);
24+
const compNamePascalCase = changeCase.pascalCase(props.compName);
2625

2726
this.props = {
2827
...props,
29-
elementCompNameSingular: elementCompNameParamCase,
30-
elementCompNamePlural: pluralize(elementCompNameParamCase),
28+
compNameParamCase,
29+
compNamePascalCase,
30+
compNameParamCasePlural: pluralize(compNameParamCase),
3131
};
3232
});
3333
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const ENTITY = '<%= compNameParamCase %>';
2+
const RESOURCE = '<%= compNameParamCasePlural %>';
3+
4+
export { ENTITY, RESOURCE };

generators/app/templates/<%= elementCompNamePlural %>/controller.ts renamed to generators/app/templates/<%= compNameParamCasePlural %>/controller.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,47 @@ import { Request, Response, NextFunction } from 'express';
22
import { StatusCodes } from 'http-status-codes';
33
import { HttpError } from '@boringcodes/utils/error';
44

5+
import { <%= compNamePascalCase %> } from './types';
56
import { ENTITY } from './constants';
67

78
interface MyRequest extends Request {
8-
readonly [ENTITY]: any;
9+
readonly [ENTITY]: Required<<%= compNamePascalCase %>>;
910
}
1011

11-
const list = (_: Request, res: Response, next: NextFunction): void => {
12+
const getById = (req: Request, _: Response, next: NextFunction): void => {
1213
try {
13-
// TODO: list objects
14-
const objects: any[] = [];
14+
const { id = '' } = req.params;
15+
if (id === '') {
16+
throw new HttpError(StatusCodes.BAD_REQUEST, 'Invalid resource Id');
17+
}
1518

16-
res.send(objects);
19+
// TODO: get object by id
20+
const object = {};
21+
Object.assign(req, { [ENTITY]: object });
22+
23+
next();
1724
} catch (err) {
1825
next(new HttpError(err.code ?? StatusCodes.INTERNAL_SERVER_ERROR, err));
1926
}
2027
};
2128

22-
const create = (req: Request, res: Response, next: NextFunction): void => {
29+
const list = (_: Request, res: Response, next: NextFunction): void => {
2330
try {
24-
// TODO: create object
25-
const object = { ...req.body };
31+
// TODO: list objects
32+
const objects: <%= compNamePascalCase %>[] = [];
2633

27-
res.send(object);
34+
res.send(objects);
2835
} catch (err) {
2936
next(new HttpError(err.code ?? StatusCodes.INTERNAL_SERVER_ERROR, err));
3037
}
3138
};
3239

33-
const getById = (req: Request, _: Response, next: NextFunction): void => {
40+
const create = (req: Request, res: Response, next: NextFunction): void => {
3441
try {
35-
const { id = '' } = req.params;
36-
if (id === '') {
37-
throw new HttpError(StatusCodes.BAD_REQUEST, 'Invalid resource Id');
38-
}
39-
40-
// TODO: get object by id
41-
const object = {};
42-
Object.assign(req, { [ENTITY]: object });
42+
// TODO: create object
43+
const object = { ...req.body };
4344

44-
next();
45+
res.send(object);
4546
} catch (err) {
4647
next(new HttpError(err.code ?? StatusCodes.INTERNAL_SERVER_ERROR, err));
4748
}
@@ -73,7 +74,7 @@ const updatePartial = (
7374

7475
const update = (req: Request, res: Response, next: NextFunction): void => {
7576
try {
76-
// TODO: replace object
77+
// TODO: update object
7778
const object = { ...req.body };
7879

7980
res.send(object);
@@ -91,4 +92,4 @@ const del = (req: Request, res: Response, next: NextFunction): void => {
9192
}
9293
};
9394

94-
export { list, create, getById, get, updatePartial, update, del };
95+
export default { getById, list, create, get, updatePartial, update, del };
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Router } from 'express';
2+
3+
import { RouteOptions } from '../types';
4+
import { RESOURCE } from './constants';
5+
import controller from './controller';
6+
7+
const path = `/${RESOURCE}`;
8+
9+
const routes = (_: RouteOptions): Router => {
10+
const router = Router();
11+
12+
router.param('id', controller.getById);
13+
14+
router.route('/').get(controller.list).post(controller.create);
15+
16+
router
17+
.route('/:id')
18+
.get(controller.get)
19+
.patch(controller.updatePartial)
20+
.put(controller.update)
21+
.delete(controller.del);
22+
23+
return router;
24+
};
25+
26+
export default { path, routes };
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface <%= compNamePascalCase %> {
2+
readonly id?: string;
3+
readonly name?: string;
4+
}
5+
6+
export { <%= compNamePascalCase %> };

generators/app/templates/<%= elementCompNamePlural %>/constants.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

generators/app/templates/<%= elementCompNamePlural %>/index.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
],
1414
"scripts": {
1515
"purge": "rimraf node_modules",
16-
"format": "prettier --write --ignore-path .gitignore .",
17-
"format:check": "prettier --check --ignore-path .gitignore .",
16+
"format": "prettier --write --ignore-path .gitignore **/*.{js,json,md,yml}",
17+
"format:check": "prettier --check --ignore-path .gitignore **/*.{js,json,md,yml}",
1818
"lint": "eslint --quiet --ignore-path .gitignore --ignore-pattern templates .",
1919
"release": "standard-version",
2020
"release:major": "yarn release --release-as major",

0 commit comments

Comments
 (0)