Skip to content

Commit 55d6b70

Browse files
committed
feat(generators): simplify generator props using singular & plural comp name; replace NAME & PLURAL_NAME consts with ENTITY & RESOURCE; remove count api & rename patch api to updatePartial; refactor controller
1 parent 4c79fec commit 55d6b70

File tree

5 files changed

+44
-59
lines changed

5 files changed

+44
-59
lines changed

generators/app/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,14 @@ module.exports = class extends Generator {
2020
];
2121

2222
return this.prompt(prompts).then((props) => {
23-
const elementComponentNameParamCase = changeCase.paramCase(
24-
props.elementComponentName,
25-
);
26-
const elementComponentNameCamelCase = changeCase.camelCase(
23+
const elementCompNameParamCase = changeCase.paramCase(
2724
props.elementComponentName,
2825
);
2926

3027
this.props = {
3128
...props,
32-
elementResourceName: elementComponentNameParamCase,
33-
elementResourceNamePlural: pluralize(elementComponentNameParamCase),
34-
elementDirName: pluralize(elementComponentNameCamelCase),
29+
elementCompNameSingular: elementCompNameParamCase,
30+
elementCompNamePlural: pluralize(elementCompNameParamCase),
3531
};
3632
});
3733
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const ENTITY = '<%= elementCompNameSingular %>';
2+
const RESOURCE = '<%= elementCompNamePlural %>';
3+
4+
export { ENTITY, RESOURCE };
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
1-
import { Request as ExpressRequest, Response, NextFunction } from 'express';
1+
import { Request, Response, NextFunction } from 'express';
22
import {
33
BAD_REQUEST,
44
NOT_FOUND,
55
INTERNAL_SERVER_ERROR,
66
} from 'http-status-codes';
77
import { HttpError } from '@boringcodes/utils/error';
88

9-
import { NAME } from './constants';
9+
import { ENTITY } from './constants';
1010

11-
interface Request extends ExpressRequest {
12-
readonly [NAME]: any;
11+
interface MyRequest extends Request {
12+
readonly [ENTITY]: any;
1313
}
1414

1515
const list = async (_: Request, res: Response, next: NextFunction) => {
1616
try {
1717
// TODO: list objects
18-
const objects = [];
18+
const objects: any[] = [];
1919

2020
res.send(objects);
2121
} catch (err) {
2222
next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err));
2323
}
2424
};
2525

26-
const count = async (_: Request, res: Response, next: NextFunction) => {
27-
try {
28-
// TODO: count objects
29-
const count = 0;
30-
31-
res.send({ count });
32-
} catch (err) {
33-
next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err));
34-
}
35-
};
36-
3726
const create = async (req: Request, res: Response, next: NextFunction) => {
3827
try {
3928
// TODO: create object
@@ -46,65 +35,68 @@ const create = async (req: Request, res: Response, next: NextFunction) => {
4635
};
4736

4837
const getById = async (req: Request, _: Response, next: NextFunction) => {
49-
if (!req.params.id) {
50-
next(new HttpError(BAD_REQUEST, 'Invalid resource Id'));
51-
52-
return;
53-
}
54-
5538
try {
56-
// TODO: get object
57-
const object = {};
39+
if (!req.params.id) {
40+
throw new HttpError(BAD_REQUEST, 'Invalid resource Id');
41+
}
5842

43+
// TODO: get object by id
44+
const object = {};
5945
if (!object) {
60-
next(new HttpError(NOT_FOUND, 'Resource not found'));
61-
62-
return;
46+
return next(new HttpError(NOT_FOUND, 'Resource not found'));
6347
}
64-
// tslint:disable-next-line:no-object-mutation
65-
Object.assign(req, { [NAME]: object });
48+
Object.assign(req, { [ENTITY]: object });
6649

6750
next();
6851
} catch (err) {
6952
next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err));
7053
}
7154
};
7255

73-
const get = (req: Request, res: Response) => {
74-
res.send(req[NAME]);
56+
const get = (req: Request, res: Response, next: NextFunction) => {
57+
try {
58+
// TODO: get object
59+
res.send((req as MyRequest)[ENTITY]);
60+
} catch (err) {
61+
next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err));
62+
}
7563
};
7664

77-
const patch = async (req: Request, res: Response, next: NextFunction) => {
65+
const updatePartial = async (
66+
req: Request,
67+
res: Response,
68+
next: NextFunction,
69+
) => {
7870
try {
79-
// TODO: patch object
80-
const object = { ...req[NAME], ...req.body };
71+
// TODO: update partial object
72+
const object = { ...(req as MyRequest)[ENTITY], ...req.body };
8173

8274
res.send(object);
8375
} catch (err) {
84-
next(err);
76+
next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err));
8577
}
8678
};
8779

8880
const update = async (req: Request, res: Response, next: NextFunction) => {
8981
try {
90-
// TODO: update object
82+
// TODO: replace object
9183
const object = req.body;
9284

9385
res.send(object);
9486
} catch (err) {
95-
next(err);
87+
next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err));
9688
}
9789
};
9890

9991
const del = async (req: Request, res: Response, next: NextFunction) => {
10092
try {
10193
// TODO: delete object
102-
const object = req[NAME];
94+
const object = (req as MyRequest)[ENTITY];
10395

10496
res.send(object);
10597
} catch (err) {
10698
next(new HttpError(err.code || INTERNAL_SERVER_ERROR, err));
10799
}
108100
};
109101

110-
export { list, create, count, getById, get, patch, update, del };
102+
export { list, create, getById, get, updatePartial, update, del };

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
import { Router } from 'express';
22

3-
import { Routes } from '../types';
4-
import { PLURAL_NAME } from './constants';
3+
import { RouteOptions } from '..';
4+
import { RESOURCE } from './constants';
55
import {
66
list,
7-
count,
87
create,
98
getById,
109
get,
11-
patch,
10+
updatePartial,
1211
update,
1312
del,
1413
} from './controller';
1514

16-
const path = `/${PLURAL_NAME}`;
15+
const path = `/${RESOURCE}`;
1716

18-
const routes = (_: Routes) => {
17+
const routes = (_: RouteOptions) => {
1918
const router = Router();
2019

2120
router.param('id', getById);
2221

2322
router.route('/').get(list).post(create);
2423

25-
router.route('/count').get(count);
26-
27-
router.route('/:id').get(get).patch(patch).put(update).delete(del);
24+
router.route('/:id').get(get).patch(updatePartial).put(update).delete(del);
2825

2926
return router;
3027
};

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

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

0 commit comments

Comments
 (0)