Skip to content

Commit becf33a

Browse files
author
hirsch88
committed
Update inversify and adjust naming of all annotations
1 parent 76d8258 commit becf33a

File tree

6 files changed

+50
-57
lines changed

6 files changed

+50
-57
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@
103103
"handlebars": "^4.0.10",
104104
"helmet": "^3.6.1",
105105
"inquirer": "^3.2.0",
106-
"inversify": "^4.1.1",
107-
"inversify-express-utils": "^3.5.1",
106+
"inversify": "^4.2.0",
107+
"inversify-express-utils": "^4.0.0",
108108
"jsonwebtoken": "^7.4.1",
109109
"knex": "^0.12.0",
110110
"lodash": "^4.17.4",

src/api/controllers/UserController.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { inject, named } from 'inversify';
10-
import { Controller, Get, Post, Put, Delete, RequestParam, RequestBody, Response, Request } from 'inversify-express-utils';
10+
import { controller, httpGet, httpPost, httpPut, httpDelete, response, request, requestBody, requestParam } from 'inversify-express-utils';
1111
import { app } from '../../app';
1212
import { Types, Targets } from '../../constants';
1313
import { UserService } from '../services/UserService';
@@ -17,42 +17,42 @@ const populateUser = app.IoC.getNamed<interfaces.Middleware>(Types.Middleware, T
1717
const authenticate = app.IoC.getNamed<interfaces.Middleware>(Types.Middleware, Targets.Middleware.AuthenticateMiddleware);
1818

1919

20-
@Controller('/users', authenticate.use)
20+
@controller('/users', authenticate.use)
2121
export class UserController {
2222

2323
constructor( @inject(Types.Service) @named(Targets.Service.UserService) private userService: UserService) { }
2424

25-
@Get('/')
26-
public async findAll( @Response() res: myExpress.Response): Promise<any> {
25+
@httpGet('/')
26+
public async findAll( @response() res: myExpress.Response): Promise<any> {
2727
const users = await this.userService.findAll();
2828
return res.found(users.toJSON());
2929
}
3030

31-
@Post('/')
32-
public async create( @Response() res: myExpress.Response, @RequestBody() body: any): Promise<any> {
31+
@httpPost('/')
32+
public async create( @response() res: myExpress.Response, @requestBody() body: any): Promise<any> {
3333
const user = await this.userService.create(body);
3434
return res.created(user.toJSON());
3535
}
3636

37-
@Get('/me', populateUser.use)
38-
public async findMe( @Request() req: myExpress.Request, @Response() res: myExpress.Response): Promise<any> {
37+
@httpGet('/me', populateUser.use)
38+
public async findMe( @request() req: myExpress.Request, @response() res: myExpress.Response): Promise<any> {
3939
return res.found(req.user);
4040
}
4141

42-
@Get('/:id')
43-
public async findOne( @Response() res: myExpress.Response, @RequestParam('id') id: string): Promise<any> {
42+
@httpGet('/:id')
43+
public async findOne( @response() res: myExpress.Response, @requestParam('id') id: string): Promise<any> {
4444
const user = await this.userService.findOne(parseInt(id, 10));
4545
return res.found(user.toJSON());
4646
}
4747

48-
@Put('/:id')
49-
public async update( @Response() res: myExpress.Response, @RequestParam('id') id: string, @RequestBody() body: any): Promise<any> {
48+
@httpPut('/:id')
49+
public async update( @response() res: myExpress.Response, @requestParam('id') id: string, @requestBody() body: any): Promise<any> {
5050
const user = await this.userService.update(parseInt(id, 10), body);
5151
return res.updated(user.toJSON());
5252
}
5353

54-
@Delete('/:id')
55-
public async destroy( @Response() res: myExpress.Response, @RequestParam('id') id: string): Promise<any> {
54+
@httpDelete('/:id')
55+
public async destroy( @response() res: myExpress.Response, @requestParam('id') id: string): Promise<any> {
5656
await this.userService.destroy(parseInt(id, 10));
5757
return res.destroyed();
5858
}

src/api/services/UserService.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { inject, named } from 'inversify';
1212
import { Types, Core, Targets } from '../../constants';
1313
import { Logger as LoggerType } from '../../core/Logger';
1414
import { EventEmitter } from '../../core/api/events';
15-
import { Validate, Request } from '../../core/api/Validate';
15+
import { validate, request } from '../../core/api/Validate';
1616
import { NotFoundException } from '../exceptions/NotFoundException';
1717
import { UserCreateRequest } from '../requests/user/UserCreateRequest';
1818
import { UserUpdateRequest } from '../requests/user/UserUpdateRequest';
@@ -77,8 +77,8 @@ export class UserService {
7777
* @param {*} data is the json body of the request
7878
* @returns {Promise<User>}
7979
*/
80-
@Validate()
81-
public async create( @Request(UserCreateRequest) data: any): Promise<User> {
80+
@validate()
81+
public async create( @request(UserCreateRequest) data: any): Promise<User> {
8282
// If the request body was valid we will create the user
8383
const user = await this.userRepo.create(data);
8484
this.events.emit(UserCreatedListener.Event, user.toJSON());
@@ -93,8 +93,8 @@ export class UserService {
9393
* @param {*} newUser is the json body of the request
9494
* @returns {Promise<User>}
9595
*/
96-
@Validate()
97-
public async update(id: number, @Request(UserUpdateRequest) newUser: any): Promise<User> {
96+
@validate()
97+
public async update(id: number, @request(UserUpdateRequest) newUser: any): Promise<User> {
9898
// Find or fail
9999
const user = await this.findOne(id);
100100
// Set new values

src/console/templates/controller.hbs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{{#if isResourceTemplate}}
22
import { inject, named } from 'inversify';
3-
import { Controller, Get, Post, Put, Delete, RequestParam, RequestBody, Response } from 'inversify-express-utils';
3+
import { controller, httpGet, httpPost, httpPut, httpDelete, response, request, requestBody, requestParam } from 'inversify-express-utils';
44
import { Types, Targets } from '../../{{deepness}}constants';
55
import { app } from '../../{{deepness}}app';
66
import { {{name.capitalize}}Service } from '../{{deepness}}services/{{name.capitalize}}Service';
77
{{else}}
8-
import { Controller } from 'inversify-express-utils';
8+
import { controller } from 'inversify-express-utils';
99
import { app } from '../../{{deepness}}app';
1010
import { Types, Targets } from '../../{{deepness}}constants';
1111
{{/if}}
@@ -14,37 +14,37 @@ import { Types, Targets } from '../../{{deepness}}constants';
1414
const authenticate = app.IoC.getNamed<interfaces.Middleware>(Types.Middleware, Targets.Middleware.AuthenticateMiddleware);
1515

1616

17-
@Controller('/{{name.pluralize}}', authenticate.use)
17+
@controller('/{{name.pluralize}}', authenticate.use)
1818
export class {{name.capitalize}}Controller {
1919

2020
{{#if isResourceTemplate}}constructor( @inject(Types.Service) @named(Targets.Service.{{name.capitalize}}Service) private {{name.camelCase}}Service: {{name.capitalize}}Service) { }
2121

22-
@Get('/')
23-
public async findAll( @Response() res: myExpress.Response): Promise<any> {
22+
@httpGet('/')
23+
public async findAll( @response() res: myExpress.Response): Promise<any> {
2424
const {{name.camelCase}}s = await this.{{name.camelCase}}Service.findAll();
2525
return res.found({{name.camelCase}}s.toJSON());
2626
}
2727

28-
@Post('/')
29-
public async create( @Response() res: myExpress.Response, @RequestBody() body: any): Promise<any> {
28+
@httpPost('/')
29+
public async create( @response() res: myExpress.Response, @requestBody() body: any): Promise<any> {
3030
const {{name.camelCase}} = await this.{{name.camelCase}}Service.create(body);
3131
return res.created({{name.camelCase}}.toJSON());
3232
}
3333

34-
@Get('/:id')
35-
public async findOne( @Response() res: myExpress.Response, @RequestParam('id') id: string): Promise<any> {
34+
@httpGet('/:id')
35+
public async findOne( @response() res: myExpress.Response, @requestParam('id') id: string): Promise<any> {
3636
const {{name.camelCase}} = await this.{{name.camelCase}}Service.findOne(parseInt(id, 10));
3737
return res.found({{name.camelCase}}.toJSON());
3838
}
3939

40-
@Put('/:id')
41-
public async update( @Response() res: myExpress.Response, @RequestParam('id') id: string, @RequestBody() body: any): Promise<any> {
40+
@httpPut('/:id')
41+
public async update( @response() res: myExpress.Response, @requestParam('id') id: string, @requestBody() body: any): Promise<any> {
4242
const {{name.camelCase}} = await this.{{name.camelCase}}Service.update(parseInt(id, 10), body);
4343
return res.updated({{name.camelCase}}.toJSON());
4444
}
4545

46-
@Delete('/:id')
47-
public async destroy( @Response() res: myExpress.Response, @RequestParam('id') id: string): Promise<any> {
46+
@httpDelete('/:id')
47+
public async destroy( @response() res: myExpress.Response, @requestParam('id') id: string): Promise<any> {
4848
await this.{{name.camelCase}}Service.destroy(parseInt(id, 10));
4949
return res.destroyed();
5050
}{{/if}}

src/core/api/Validate.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ interface RequestParameter {
2424
*
2525
* @param request
2626
*/
27-
export const Request = (request: typeof RequestBody) => (target: object, propertyKey: string | symbol, parameterIndex: number): any => {
27+
export const request = (requestBody: typeof RequestBody) => (target: object, propertyKey: string | symbol, parameterIndex: number): any => {
2828
const existingRequestParameters: RequestParameter[] = Reflect.getOwnMetadata(requestMetadataKey, target, propertyKey) || [];
2929
existingRequestParameters.push({
30-
request,
30+
request: requestBody,
3131
index: parameterIndex
3232
});
3333
Reflect.defineMetadata(requestMetadataKey, existingRequestParameters, target, propertyKey);
@@ -40,14 +40,14 @@ export const Request = (request: typeof RequestBody) => (target: object, propert
4040
* @param propertyName
4141
* @param descriptor
4242
*/
43-
export const Validate = () => (target: any, propertyName: string, descriptor: TypedPropertyDescriptor<any>): any => {
43+
export const validate = () => (target: any, propertyName: string, descriptor: TypedPropertyDescriptor<any>): any => {
4444
const method = descriptor.value;
4545
descriptor.value = async function(...args: any[]): Promise<any> {
4646
const requestParameters: RequestParameter[] = Reflect.getOwnMetadata(requestMetadataKey, target, propertyName);
4747
if (requestParameters.length > 0) {
4848
for (const requestParameter of requestParameters) {
49-
const request = new requestParameter.request(args[requestParameter.index]);
50-
await request.validate();
49+
const requestBody = new requestParameter.request(args[requestParameter.index]);
50+
await requestBody.validate();
5151
}
5252
}
5353
return method && method.apply(this, args);

yarn.lock

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,13 +1208,13 @@ debug@2.3.3:
12081208
dependencies:
12091209
ms "0.7.2"
12101210

1211-
debug@2.6.7, debug@^2.1.3:
1211+
debug@2.6.7:
12121212
version "2.6.7"
12131213
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.7.tgz#92bad1f6d05bbb6bba22cca88bcd0ec894c2861e"
12141214
dependencies:
12151215
ms "2.0.0"
12161216

1217-
debug@2.6.8, debug@^2.1.1, debug@^2.2.0, debug@^2.6.3:
1217+
debug@2.6.8, debug@^2.1.1, debug@^2.1.3, debug@^2.2.0, debug@^2.6.3:
12181218
version "2.6.8"
12191219
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc"
12201220
dependencies:
@@ -2119,15 +2119,15 @@ invariant@^2.2.0:
21192119
dependencies:
21202120
loose-envify "^1.0.0"
21212121

2122-
inversify-express-utils@^3.5.1:
2123-
version "3.5.2"
2124-
resolved "https://registry.yarnpkg.com/inversify-express-utils/-/inversify-express-utils-3.5.2.tgz#b5db32ab013f5711cb634e3c7ad762c1a9121ffb"
2122+
inversify-express-utils@^4.0.0:
2123+
version "4.0.0"
2124+
resolved "https://registry.yarnpkg.com/inversify-express-utils/-/inversify-express-utils-4.0.0.tgz#1fd5f95e9eb6ee8cd053f54083868f532e93b90a"
21252125
dependencies:
21262126
express "^4.14.1"
21272127

2128-
inversify@^4.1.1:
2129-
version "4.1.1"
2130-
resolved "https://registry.yarnpkg.com/inversify/-/inversify-4.1.1.tgz#a95edb34ae082bb1175336d51dcd5f011134c625"
2128+
inversify@^4.2.0:
2129+
version "4.2.0"
2130+
resolved "https://registry.yarnpkg.com/inversify/-/inversify-4.2.0.tgz#71966a5005b1ef735ba2709e66c5b5f7765087e3"
21312131

21322132
invert-kv@^1.0.0:
21332133
version "1.0.0"
@@ -3861,11 +3861,11 @@ rx-lite@*, rx-lite@^4.0.8:
38613861
version "4.0.8"
38623862
resolved "https://registry.yarnpkg.com/rx-lite/-/rx-lite-4.0.8.tgz#0b1e11af8bc44836f04a6407e92da42467b79444"
38633863

3864-
safe-buffer@5.0.1, safe-buffer@^5.0.1:
3864+
safe-buffer@5.0.1:
38653865
version "5.0.1"
38663866
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.0.1.tgz#d263ca54696cd8a306b5ca6551e92de57918fbe7"
38673867

3868-
safe-buffer@5.1.1:
3868+
safe-buffer@5.1.1, safe-buffer@^5.0.1:
38693869
version "5.1.1"
38703870
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"
38713871

@@ -4121,14 +4121,7 @@ string-width@^1.0.1, string-width@^1.0.2:
41214121
is-fullwidth-code-point "^1.0.0"
41224122
strip-ansi "^3.0.0"
41234123

4124-
string-width@^2.0.0:
4125-
version "2.0.0"
4126-
resolved "https://registry.yarnpkg.com/string-width/-/string-width-2.0.0.tgz#635c5436cc72a6e0c387ceca278d4e2eec52687e"
4127-
dependencies:
4128-
is-fullwidth-code-point "^2.0.0"
4129-
strip-ansi "^3.0.0"
4130-
4131-
string-width@^2.1.0:
4124+
string-width@^2.0.0, string-width@^2.1.0:
41324125
version "2.1.0"
41334126
resolved "https://registry.yarnpkg.com/string-width/-/string-width-2.1.0.tgz#030664561fc146c9423ec7d978fe2457437fe6d0"
41344127
dependencies:

0 commit comments

Comments
 (0)