express-generator-typescript 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,15 +24,17 @@ const ncpOpts = {
24
24
  };
25
25
 
26
26
  // Project-folder dependencies
27
- const dependencies = 'express dotenv http-status-codes morgan cookie-parser jet-logger ' +
28
- 'module-alias command-line-args express-async-errors helmet jsonfile bcrypt jsonwebtoken';
27
+ const dependencies = 'express dotenv morgan cookie-parser jet-logger ' +
28
+ 'module-alias command-line-args express-async-errors helmet jsonfile ' +
29
+ 'bcrypt jsonwebtoken';
29
30
 
30
31
  // Project-folder development dependencies
31
32
  const devDependencies = 'ts-node typescript nodemon find jasmine supertest ' +
32
- '@types/node @types/express @types/jasmine @types/find @types/morgan @types/cookie-parser ' +
33
- '@types/supertest fs-extra tsconfig-paths @types/jsonfile @types/command-line-args ' +
34
- '@typescript-eslint/eslint-plugin @typescript-eslint/parser eslint @types/fs-extra ' +
35
- '@types/bcrypt @types/jsonwebtoken eslint-plugin-node';
33
+ '@types/node @types/express @types/jasmine @types/find @types/morgan ' +
34
+ '@types/cookie-parser @types/supertest fs-extra tsconfig-paths ' +
35
+ '@types/jsonfile @types/command-line-args ' +
36
+ '@typescript-eslint/eslint-plugin @typescript-eslint/parser eslint ' +
37
+ '@types/fs-extra @types/bcrypt @types/jsonwebtoken eslint-plugin-node';
36
38
 
37
39
 
38
40
  // **** Functions **** //
@@ -80,11 +82,13 @@ function updatePackageJson(destination) {
80
82
  * Because npm does not allow .gitignore to be published.
81
83
  */
82
84
  function renameGitigoreFile(destination) {
83
- return new Promise((res, rej) => {
84
- fs.rename(destination + '/gitignore', destination + '/.gitignore', (err) => {
85
- return (!!err ? rej(err) : res());
86
- });
87
- });
85
+ return new Promise((res, rej) =>
86
+ fs.rename(
87
+ (destination + '/gitignore'),
88
+ (destination + '/.gitignore'),
89
+ (err => !!err ? rej(err) : res()),
90
+ )
91
+ );
88
92
  }
89
93
 
90
94
  /**
@@ -24,7 +24,8 @@
24
24
  "@services": "dist/services",
25
25
  "@repos": "dist/repos",
26
26
  "@models": "dist/models",
27
- "@shared": "dist/shared",
27
+ "@configurations": "dist/configurations",
28
+ "@declarations": "dist/declarations",
28
29
  "@server": "dist/server",
29
30
  "@routes": "dist/routes",
30
31
  "@util": "dist/util"
@@ -40,7 +41,6 @@
40
41
  "express": "^4.18.1",
41
42
  "express-async-errors": "^3.1.1",
42
43
  "helmet": "^6.0.0",
43
- "http-status-codes": "^2.2.0",
44
44
  "jet-logger": "^1.2.3",
45
45
  "jsonfile": "^6.1.0",
46
46
  "jsonwebtoken": "^8.5.1",
@@ -1,14 +1,12 @@
1
- import bcrypt from 'bcrypt';
2
1
  import { SuperTest, Test, Response } from 'supertest';
3
2
 
4
3
  import User, { UserRoles } from '@models/User';
5
4
  import userRepo from '@repos/user-repo';
5
+ import pwdUtil from '@util/pwd-util';
6
6
 
7
7
 
8
8
  // **** Variables **** //
9
9
 
10
- export const pwdSaltRounds = 12;
11
-
12
10
  const creds = {
13
11
  email: 'jsmith@gmail.com',
14
12
  password: 'Password@1',
@@ -23,7 +21,7 @@ const creds = {
23
21
  function login(beforeAgent: SuperTest<Test>, done: (arg: string) => void) {
24
22
  // Setup dummy data
25
23
  const role = UserRoles.Admin;
26
- const pwdHash = bcrypt.hashSync(creds.password, pwdSaltRounds);
24
+ const pwdHash = pwdUtil.hashSync(creds.password);
27
25
  const loginUser = User.new('john smith', creds.email, role, pwdHash);
28
26
  spyOn(userRepo, 'getOne').and.returnValue(Promise.resolve(loginUser));
29
27
  // Call Login API
@@ -1,15 +1,14 @@
1
- import bcrypt from 'bcrypt';
2
- import StatusCodes from 'http-status-codes';
3
1
  import supertest, { SuperTest, Test, Response } from 'supertest';
4
2
  import logger from 'jet-logger';
5
3
 
6
4
  import app from '@server';
5
+ import authRoutes from '@routes/auth-routes';
7
6
  import userRepo from '@repos/user-repo';
8
- import EnvVars from 'src/configurations/EnvVars';
7
+ import pwdUtil from '@util/pwd-util';
8
+ import EnvVars from '@configurations/EnvVars';
9
+ import HttpStatusCodes from '@configurations/HttpStatusCodes';
9
10
  import User, { UserRoles } from '@models/User';
10
- import authRoutes from '@routes/auth-routes';
11
- import { pwdSaltRounds } from 'spec/support/login-agent';
12
- import { UnauthorizedError } from 'src/declarations/errors';
11
+ import { errors as authServiceErrs } from '@services/auth-service';
13
12
 
14
13
 
15
14
  // **** Variables **** //
@@ -18,21 +17,23 @@ import { UnauthorizedError } from 'src/declarations/errors';
18
17
  const { paths } = authRoutes,
19
18
  authPath = ('/api' + paths.basePath),
20
19
  loginPath = `${authPath}${paths.login}`,
21
- logoutPath = `${authPath}${paths.logout}`,
22
- { BAD_REQUEST, OK, UNAUTHORIZED } = StatusCodes;
20
+ logoutPath = `${authPath}${paths.logout}`;
23
21
 
24
22
  // Test message
25
23
  const msgs = {
26
- goodLogin: `should return a response with a status of ${OK} and a cookie
27
- with a jwt if the login was successful.`,
28
- emailNotFound: `should return a response with a status of ${UNAUTHORIZED}
29
- and a json with the "error ${UnauthorizedError.Msg}" if the email was
30
- not found.`,
31
- pwdFailed: `should return a response with a status of ${UNAUTHORIZED} and a
32
- json with the error "${UnauthorizedError.Msg}" if the password failed.`,
33
- fallbackErr: `should return a response with a status of ${BAD_REQUEST} and
34
- a json with an error for all other bad responses.`,
35
- goodLogout: `should return a response with a status of ${OK}.`,
24
+ goodLogin: 'should return a response with a status of ' +
25
+ `"${HttpStatusCodes.OK}" and a cookie with a jwt if the login was ` +
26
+ 'successful.',
27
+ emailNotFound: 'should return a response with a status of ' +
28
+ `"${HttpStatusCodes.UNAUTHORIZED}" and a json with an error message if ` +
29
+ 'the email was not found.',
30
+ pwdFailed: 'should return a response with a status of ' +
31
+ `"${HttpStatusCodes.UNAUTHORIZED}" and a json with the error ` +
32
+ `"${authServiceErrs.unauth}" if the password failed.`,
33
+ fallbackErr: 'should return a response with a status of ' +
34
+ `"${HttpStatusCodes.BAD_REQUEST}" and a json with an error for all ` +
35
+ 'other bad responses.',
36
+ goodLogout: `should return a response with a status of ${HttpStatusCodes.OK}`,
36
37
  } as const;
37
38
 
38
39
  // Login credentials
@@ -69,14 +70,14 @@ describe('auth-router', () => {
69
70
  // Good login
70
71
  it(msgs.goodLogin, (done) => {
71
72
  const role = UserRoles.Standard;
72
- const pwdHash = hashPwd(loginCreds.password);
73
+ const pwdHash = pwdUtil.hashSync(loginCreds.password);
73
74
  const loginUser = User.new('john smith', loginCreds.email, role, pwdHash);
74
75
  spyOn(userRepo, 'getOne').and.returnValue(Promise.resolve(loginUser));
75
76
  // Call API
76
77
  callApi(loginCreds)
77
78
  .end((err: Error, res: Response) => {
78
79
  !!err && logger.err(err);
79
- expect(res.status).toBe(OK);
80
+ expect(res.status).toBe(HttpStatusCodes.OK);
80
81
  const cookie = res.headers['set-cookie'][0];
81
82
  expect(cookie).toContain(EnvVars.cookieProps.key);
82
83
  done();
@@ -89,8 +90,9 @@ describe('auth-router', () => {
89
90
  callApi(loginCreds)
90
91
  .end((err: Error, res: Response) => {
91
92
  !!err && logger.err(err);
92
- expect(res.status).toBe(UNAUTHORIZED);
93
- expect(res.body.error).toBe(UnauthorizedError.Msg);
93
+ expect(res.status).toBe(HttpStatusCodes.UNAUTHORIZED);
94
+ const errMsg = authServiceErrs.emailNotFound(loginCreds.email);
95
+ expect(res.body.error).toBe(errMsg);
94
96
  done();
95
97
  });
96
98
  });
@@ -98,15 +100,15 @@ describe('auth-router', () => {
98
100
  // Password failed
99
101
  it(msgs.pwdFailed, (done) => {
100
102
  const role = UserRoles.Standard;
101
- const pwdHash = hashPwd('bad password');
103
+ const pwdHash = pwdUtil.hashSync('bad password');
102
104
  const loginUser = User.new('john smith', loginCreds.email, role, pwdHash);
103
105
  spyOn(userRepo, 'getOne').and.returnValue(Promise.resolve(loginUser));
104
106
  // Call API
105
107
  callApi(loginCreds)
106
108
  .end((err: Error, res: Response) => {
107
109
  !!err && logger.err(err);
108
- expect(res.status).toBe(UNAUTHORIZED);
109
- expect(res.body.error).toBe(UnauthorizedError.Msg);
110
+ expect(res.status).toBe(HttpStatusCodes.UNAUTHORIZED);
111
+ expect(res.body.error).toBe(authServiceErrs.unauth);
110
112
  done();
111
113
  });
112
114
  });
@@ -117,7 +119,7 @@ describe('auth-router', () => {
117
119
  callApi(loginCreds)
118
120
  .end((err: Error, res: Response) => {
119
121
  !!err && logger.err(err);
120
- expect(res.status).toBe(BAD_REQUEST);
122
+ expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
121
123
  expect(res.body.error).toBeTruthy();
122
124
  done();
123
125
  });
@@ -132,16 +134,9 @@ describe('auth-router', () => {
132
134
  agent.get(logoutPath)
133
135
  .end((err: Error, res: Response) => {
134
136
  !!err && logger.err(err);
135
- expect(res.status).toBe(OK);
137
+ expect(res.status).toBe(HttpStatusCodes.OK);
136
138
  done();
137
139
  });
138
140
  });
139
141
  });
140
142
  });
141
-
142
-
143
- // **** Helper functions **** //
144
-
145
- function hashPwd(pwd: string) {
146
- return bcrypt.hashSync(pwd, pwdSaltRounds);
147
- }
@@ -1,5 +1,4 @@
1
1
  import supertest from 'supertest';
2
- import StatusCodes from 'http-status-codes';
3
2
  import { SuperTest, Test, Response } from 'supertest';
4
3
  import logger from 'jet-logger';
5
4
 
@@ -7,13 +6,10 @@ import app from '@server';
7
6
  import userRepo from '@repos/user-repo';
8
7
  import User, { IUser } from '@models/User';
9
8
  import userRoutes from '@routes/user-routes';
10
-
11
- import {
12
- ParamInvalidError,
13
- UserNotFoundError,
14
- ValidatorFnError,
15
- } from 'src/declarations/errors';
9
+ import HttpStatusCodes from '@configurations/HttpStatusCodes';
16
10
  import loginAgent from '../support/login-agent';
11
+ import { paramInvalidErr } from '@routes/middlware/validate';
12
+ import { userNotFoundErr } from '@services/user-service';
17
13
 
18
14
 
19
15
  // **** Variables **** //
@@ -24,8 +20,7 @@ const { paths } = userRoutes,
24
20
  getUsersPath = `${usersPath}${paths.get}`,
25
21
  addUsersPath = `${usersPath}${paths.add}`,
26
22
  updateUserPath = `${usersPath}${paths.update}`,
27
- deleteUserPath = `${usersPath}${paths.delete}`,
28
- { BAD_REQUEST, CREATED, OK } = StatusCodes;
23
+ deleteUserPath = `${usersPath}${paths.delete}`;
29
24
 
30
25
  // Dummy users for GET req
31
26
  const dummyGetAllUsers = [
@@ -41,35 +36,39 @@ const dummyUserData = {
41
36
 
42
37
  // Test messages
43
38
  const msgs = {
44
- getUsersSuccess: `should return a JSON object with all the users and a
45
- status code of "${OK}" if the request was successful.`,
46
- getUsersBad: `should return a JSON object containing an error message and a
47
- status code of "${BAD_REQUEST}" if the request was unsuccessful.`,
48
- addUserSuccess: `should return a status code of "${CREATED}" if the request
49
- was successful.`,
50
- addUserFailedMissingParam: `should return a JSON object with an error
51
- message of "${ValidatorFnError.Msg + 'instanceOf'}" and a status code
52
- of "${BAD_REQUEST}" if the user param was missing.`,
53
- addUserFallbackErr: `should return a JSON object with an error message and a
54
- status code of "${BAD_REQUEST}" if the request was unsuccessful.`,
55
- updateSuccess: `should return a status code of "${OK}" if the request was
56
- successful.`,
57
- updateParamMissing: `should return a JSON object with an error message of
58
- "${ValidatorFnError.Msg + 'instanceOf'}" and a status code of
59
- "${BAD_REQUEST}" if the user param was missing.`,
60
- updateUserNotFound: `should return a JSON object with the error message of
61
- ${UserNotFoundError.Msg} and a status code of
62
- "${UserNotFoundError.HttpStatus}" if the id was not found.`,
63
- updateFallbackErr: `should return a JSON object with an error message and a
64
- status code of "${BAD_REQUEST}" if the request was unsuccessful.`,
65
- deleteSuccessful: `should return a status code of "${OK}" if the request was
66
- successful.`,
67
- deleteUserNotFound: `should return a JSON object with the error message of
68
- ${UserNotFoundError.Msg} and a status code of
69
- "${UserNotFoundError.HttpStatus}" if the id was not found.`,
70
- deleteFallbackErr: `should return a JSON object with an error message and a
71
- status code of "${BAD_REQUEST}" if the request was unsuccessful.`,
72
- };
39
+ getUsersSuccess: 'should return a JSON object with all the users and a ' +
40
+ `status code of "${HttpStatusCodes.OK}" if the request was successful.`,
41
+ getUsersBad: 'should return a JSON object containing an error message ' +
42
+ `and a status code of "${HttpStatusCodes.BAD_REQUEST}" if the request ` +
43
+ 'was unsuccessful.',
44
+ addUserSuccess: 'should return a status code of ' +
45
+ `"${HttpStatusCodes.CREATED}" if the request was successful.`,
46
+ addUserFailedMissingParam: 'should return a JSON object with an error ' +
47
+ `message of "${paramInvalidErr}" and a status code of ` +
48
+ `"${HttpStatusCodes.BAD_REQUEST}" if the user param was missing.`,
49
+ addUserFallbackErr: 'should return a JSON object with an error message ' +
50
+ `and a status code of "${HttpStatusCodes.BAD_REQUEST}" if the request ` +
51
+ 'was unsuccessful.',
52
+ updateSuccess: `should return a status code of "${HttpStatusCodes.OK}" if ` +
53
+ 'the request was successful.',
54
+ updateParamMissing: 'should return a JSON object with an error message ' +
55
+ `of "${paramInvalidErr}" and a status code of ` +
56
+ `"${HttpStatusCodes.BAD_REQUEST}" if the user param was missing.`,
57
+ updateUserNotFound: 'should return a JSON object with the error message ' +
58
+ `of "${userNotFoundErr}" and a status code of ` +
59
+ `"${HttpStatusCodes.NOT_FOUND}" if the id was not found.`,
60
+ updateFallbackErr: 'should return a JSON object with an error message ' +
61
+ `and a status code of "${HttpStatusCodes.BAD_REQUEST}" if the request ` +
62
+ 'was unsuccessful.',
63
+ deleteSuccessful: `should return a status code of "${HttpStatusCodes.OK}" ` +
64
+ 'if the request was successful.',
65
+ deleteUserNotFound: 'should return a JSON object with the error message ' +
66
+ `of "${userNotFoundErr}" and a status code of ` +
67
+ `"${HttpStatusCodes.NOT_FOUND}" if the id was not found.`,
68
+ deleteFallbackErr: 'should return a JSON object with an error message ' +
69
+ `and a status code of "${HttpStatusCodes.BAD_REQUEST}" if the request ` +
70
+ 'was unsuccessful.',
71
+ } as const;
73
72
 
74
73
 
75
74
  // **** Types **** //
@@ -108,7 +107,7 @@ describe('user-router', () => {
108
107
  // Call API
109
108
  callApi().end((err: Error, res: Response) => {
110
109
  !!err && logger.err(err);
111
- expect(res.status).toBe(OK);
110
+ expect(res.status).toBe(HttpStatusCodes.OK);
112
111
  // Caste instance-objects to 'User' objects
113
112
  const respUsers = res.body.users;
114
113
  const retUsers = respUsers.map((user: IUser) => User.copy(user));
@@ -126,7 +125,7 @@ describe('user-router', () => {
126
125
  callApi()
127
126
  .end((err: Error, res: Response) => {
128
127
  !!err && logger.err(err);
129
- expect(res.status).toBe(BAD_REQUEST);
128
+ expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
130
129
  expect(res.body.error).toBe(errMsg);
131
130
  done();
132
131
  });
@@ -149,7 +148,7 @@ describe('user-router', () => {
149
148
  callApi(dummyUserData)
150
149
  .end((err: Error, res: Response) => {
151
150
  !!err && logger.err(err);
152
- expect(res.status).toBe(CREATED);
151
+ expect(res.status).toBe(HttpStatusCodes.CREATED);
153
152
  expect(res.body.error).toBeUndefined();
154
153
  done();
155
154
  });
@@ -160,8 +159,8 @@ describe('user-router', () => {
160
159
  callApi({})
161
160
  .end((err: Error, res: Response) => {
162
161
  !!err && logger.err(err);
163
- expect(res.status).toBe(BAD_REQUEST);
164
- expect(res.body.error).toBe(ValidatorFnError.Msg + 'instanceOf');
162
+ expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
163
+ expect(res.body.error).toBe(paramInvalidErr);
165
164
  done();
166
165
  });
167
166
  });
@@ -174,7 +173,7 @@ describe('user-router', () => {
174
173
  callApi(dummyUserData)
175
174
  .end((err: Error, res: Response) => {
176
175
  !!err && logger.err(err);
177
- expect(res.status).toBe(BAD_REQUEST);
176
+ expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
178
177
  expect(res.body.error).toBe(errMsg);
179
178
  done();
180
179
  });
@@ -197,7 +196,7 @@ describe('user-router', () => {
197
196
  callApi(dummyUserData)
198
197
  .end((err: Error, res: Response) => {
199
198
  !!err && logger.err(err);
200
- expect(res.status).toBe(OK);
199
+ expect(res.status).toBe(HttpStatusCodes.OK);
201
200
  expect(res.body.error).toBeUndefined();
202
201
  done();
203
202
  });
@@ -208,8 +207,8 @@ describe('user-router', () => {
208
207
  callApi({})
209
208
  .end((err: Error, res: Response) => {
210
209
  !!err && logger.err(err);
211
- expect(res.status).toBe(BAD_REQUEST);
212
- expect(res.body.error).toBe(ValidatorFnError.Msg + 'instanceOf');
210
+ expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
211
+ expect(res.body.error).toBe(paramInvalidErr);
213
212
  done();
214
213
  });
215
214
  });
@@ -219,8 +218,8 @@ describe('user-router', () => {
219
218
  callApi(dummyUserData)
220
219
  .end((err: Error, res: Response) => {
221
220
  !!err && logger.err(err);
222
- expect(res.status).toBe(UserNotFoundError.HttpStatus);
223
- expect(res.body.error).toBe(UserNotFoundError.Msg);
221
+ expect(res.status).toBe(HttpStatusCodes.NOT_FOUND);
222
+ expect(res.body.error).toBe(userNotFoundErr);
224
223
  done();
225
224
  });
226
225
  });
@@ -234,7 +233,7 @@ describe('user-router', () => {
234
233
  callApi(dummyUserData)
235
234
  .end((err: Error, res: Response) => {
236
235
  !!err && logger.err(err);
237
- expect(res.status).toBe(BAD_REQUEST);
236
+ expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
238
237
  expect(res.body.error).toBe(updateErrMsg);
239
238
  done();
240
239
  });
@@ -258,7 +257,7 @@ describe('user-router', () => {
258
257
  callApi(5)
259
258
  .end((err: Error, res: Response) => {
260
259
  !!err && logger.err(err);
261
- expect(res.status).toBe(OK);
260
+ expect(res.status).toBe(HttpStatusCodes.OK);
262
261
  expect(res.body.error).toBeUndefined();
263
262
  done();
264
263
  });
@@ -269,8 +268,8 @@ describe('user-router', () => {
269
268
  callApi(-1)
270
269
  .end((err: Error, res: Response) => {
271
270
  !!err && logger.err(err);
272
- expect(res.status).toBe(UserNotFoundError.HttpStatus);
273
- expect(res.body.error).toBe(UserNotFoundError.Msg);
271
+ expect(res.status).toBe(HttpStatusCodes.NOT_FOUND);
272
+ expect(res.body.error).toBe(userNotFoundErr);
274
273
  done();
275
274
  });
276
275
  });
@@ -280,8 +279,8 @@ describe('user-router', () => {
280
279
  callApi('horse' as unknown as number)
281
280
  .end((err: Error, res: Response) => {
282
281
  !!err && logger.err(err);
283
- expect(res.status).toBe(ParamInvalidError.HttpStatus);
284
- expect(res.body.error).toBe(ParamInvalidError.Msg);
282
+ expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
283
+ expect(res.body.error).toBe(paramInvalidErr);
285
284
  done();
286
285
  });
287
286
  });
@@ -295,7 +294,7 @@ describe('user-router', () => {
295
294
  callApi(1)
296
295
  .end((err: Error, res: Response) => {
297
296
  !!err && logger.err(err);
298
- expect(res.status).toBe(BAD_REQUEST);
297
+ expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
299
298
  expect(res.body.error).toBe(deleteErrMsg);
300
299
  done();
301
300
  });