express-generator-typescript 2.0.15 → 2.1.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.
- package/lib/project-files/.eslintrc.json +2 -2
- package/lib/project-files/spec/tests/auth.spec.ts +9 -8
- package/lib/project-files/spec/tests/users.spec.ts +30 -23
- package/lib/project-files/src/pre-start/index.ts +3 -2
- package/lib/project-files/src/routes/validators.ts +8 -5
- package/lib/project-files/src/server.ts +15 -4
- package/lib/project-files/src/shared/errors.ts +6 -3
- package/lib/project-files/src/util/jwt-util.ts +2 -2
- package/package.json +1 -1
|
@@ -22,14 +22,15 @@ const authPath = ('/api' + paths.basePath),
|
|
|
22
22
|
|
|
23
23
|
// Test message
|
|
24
24
|
const msgs = {
|
|
25
|
-
goodLogin: `should return a response with a status of ${OK} and a cookie
|
|
26
|
-
was successful.`,
|
|
27
|
-
emailNotFound: `should return a response with a status of ${UNAUTHORIZED}
|
|
28
|
-
"error ${UnauthorizedError.Msg}" if the email was
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
goodLogin: `should return a response with a status of ${OK} and a cookie
|
|
26
|
+
with a jwt if the login was successful.`,
|
|
27
|
+
emailNotFound: `should return a response with a status of ${UNAUTHORIZED}
|
|
28
|
+
and a json with the "error ${UnauthorizedError.Msg}" if the email was
|
|
29
|
+
not found.`,
|
|
30
|
+
pwdFailed: `should return a response with a status of ${UNAUTHORIZED} and a
|
|
31
|
+
json with the error "${UnauthorizedError.Msg}" if the password failed.`,
|
|
32
|
+
fallbackErr: `should return a response with a status of ${BAD_REQUEST} and
|
|
33
|
+
a json with an error for all other bad responses.`,
|
|
33
34
|
goodLogout: `should return a response with a status of ${OK}.`,
|
|
34
35
|
} as const;
|
|
35
36
|
|
|
@@ -8,8 +8,12 @@ import userRepo from '@repos/user-repo';
|
|
|
8
8
|
import User, { IUser } from '@models/user-model';
|
|
9
9
|
|
|
10
10
|
import { p as userPaths } from '@routes/user-router';
|
|
11
|
+
import {
|
|
12
|
+
ParamMissingError,
|
|
13
|
+
UserNotFoundError,
|
|
14
|
+
ValidatorFnError,
|
|
15
|
+
} from '@shared/errors';
|
|
11
16
|
import loginAgent from '../support/login-agent';
|
|
12
|
-
import { ParamMissingError, UserNotFoundError, ValidatorFnError } from '@shared/errors';
|
|
13
17
|
|
|
14
18
|
|
|
15
19
|
// **** Variables **** //
|
|
@@ -36,31 +40,34 @@ const dummyUserData = {
|
|
|
36
40
|
|
|
37
41
|
// Test messages
|
|
38
42
|
const msgs = {
|
|
39
|
-
getUsersSuccess: `should return a JSON object with all the users and a
|
|
40
|
-
"${OK}" if the request was successful.`,
|
|
41
|
-
getUsersBad: `should return a JSON object containing an error message and a
|
|
42
|
-
"${BAD_REQUEST}" if the request was unsuccessful.`,
|
|
43
|
-
addUserSuccess: `should return a status code of "${CREATED}" if the request
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
43
|
+
getUsersSuccess: `should return a JSON object with all the users and a
|
|
44
|
+
status code of "${OK}" if the request was successful.`,
|
|
45
|
+
getUsersBad: `should return a JSON object containing an error message and a
|
|
46
|
+
status code of "${BAD_REQUEST}" if the request was unsuccessful.`,
|
|
47
|
+
addUserSuccess: `should return a status code of "${CREATED}" if the request
|
|
48
|
+
was successful.`,
|
|
49
|
+
addUserFailedMissingParam: `should return a JSON object with an error
|
|
50
|
+
message of "${ParamMissingError.Msg}" and a status code of "${BAD_REQUEST}"
|
|
51
|
+
if the user param was missing.`,
|
|
52
|
+
addUserFallbackErr: `should return a JSON object with an error message and a
|
|
53
|
+
status code of "${BAD_REQUEST}" if the request was unsuccessful.`,
|
|
54
|
+
updateSuccess: `should return a status code of "${OK}" if the request was
|
|
55
|
+
successful.`,
|
|
50
56
|
updateParamMissing: `should return a JSON object with an error message of
|
|
51
|
-
"${ParamMissingError.Msg}" and a status code of "${BAD_REQUEST}" if the
|
|
52
|
-
missing.`,
|
|
57
|
+
"${ParamMissingError.Msg}" and a status code of "${BAD_REQUEST}" if the
|
|
58
|
+
user param was missing.`,
|
|
53
59
|
updateUserNotFound: `should return a JSON object with the error message of
|
|
54
|
-
${UserNotFoundError.Msg} and a status code of
|
|
55
|
-
was not found.`,
|
|
56
|
-
updateFallbackErr: `should return a JSON object with an error message and a
|
|
57
|
-
"${BAD_REQUEST}" if the request was unsuccessful.`,
|
|
58
|
-
deleteSuccessful: `should return a status code of "${OK}" if the request was
|
|
60
|
+
${UserNotFoundError.Msg} and a status code of
|
|
61
|
+
"${UserNotFoundError.HttpStatus}" if the id was not found.`,
|
|
62
|
+
updateFallbackErr: `should return a JSON object with an error message and a
|
|
63
|
+
status code of "${BAD_REQUEST}" if the request was unsuccessful.`,
|
|
64
|
+
deleteSuccessful: `should return a status code of "${OK}" if the request was
|
|
65
|
+
successful.`,
|
|
59
66
|
deleteUserNotFound: `should return a JSON object with the error message of
|
|
60
|
-
${UserNotFoundError.Msg} and a status code of
|
|
61
|
-
not found.`,
|
|
62
|
-
deleteFallbackErr: `should return a JSON object with an error message and a
|
|
63
|
-
"${BAD_REQUEST}" if the request was unsuccessful.`,
|
|
67
|
+
${UserNotFoundError.Msg} and a status code of
|
|
68
|
+
"${UserNotFoundError.HttpStatus}" if the id was not found.`,
|
|
69
|
+
deleteFallbackErr: `should return a JSON object with an error message and a
|
|
70
|
+
status code of "${BAD_REQUEST}" if the request was unsuccessful.`,
|
|
64
71
|
};
|
|
65
72
|
|
|
66
73
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Pre-start is where we want to place things that must run BEFORE the express
|
|
3
|
-
* This is useful for environment variables, command-line
|
|
2
|
+
* Pre-start is where we want to place things that must run BEFORE the express
|
|
3
|
+
* server is started. This is useful for environment variables, command-line
|
|
4
|
+
* arguments, and cron-jobs.
|
|
4
5
|
*/
|
|
5
6
|
|
|
6
7
|
import path from 'path';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Put other validator functions here too that may be passed to the validate()
|
|
2
|
+
* Put other validator functions here too that may be passed to the validate()
|
|
3
|
+
* function.
|
|
3
4
|
*/
|
|
4
5
|
|
|
5
6
|
import { ParamInvalidError, ValidatorFnError } from '@shared/errors';
|
|
@@ -15,11 +16,13 @@ type TParamFull = TParam | TValidatorFn;
|
|
|
15
16
|
// **** Functions **** //
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
|
-
* Make sure parameter is of the specified type. If the type is not provided
|
|
19
|
-
* used as the default type. Instead of a type you can also
|
|
20
|
-
* which must return 'true' or 'false'. Return false
|
|
19
|
+
* Make sure parameter is of the specified type. If the type is not provided
|
|
20
|
+
* then 'string' is used as the default type. Instead of a type you can also
|
|
21
|
+
* pass a validator function which must return 'true' or 'false'. Return false
|
|
22
|
+
* if invalid.
|
|
21
23
|
*
|
|
22
|
-
* Example argument: [id, 'number'], id is the incoming variable, 'number' is
|
|
24
|
+
* Example argument: [id, 'number'], id is the incoming variable, 'number' is
|
|
25
|
+
* the type.
|
|
23
26
|
*
|
|
24
27
|
* Example 1: validate(email, [user, 'object'], [isAdmin, 'boolean'])
|
|
25
28
|
* Example 2: validate(password) // will check that password is a string
|
|
@@ -40,11 +40,22 @@ if (envVars.nodeEnv === 'production') {
|
|
|
40
40
|
// Add APIs
|
|
41
41
|
app.use('/api', BaseRouter);
|
|
42
42
|
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
// Setup error handler
|
|
44
|
+
app.use((
|
|
45
|
+
err: Error | CustomError,
|
|
46
|
+
_: Request,
|
|
47
|
+
res: Response,
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
49
|
+
__: NextFunction,
|
|
50
|
+
) => {
|
|
46
51
|
logger.err(err, true);
|
|
47
|
-
|
|
52
|
+
// Status
|
|
53
|
+
const status = (
|
|
54
|
+
err instanceof CustomError
|
|
55
|
+
? err.HttpStatus
|
|
56
|
+
: StatusCodes.BAD_REQUEST
|
|
57
|
+
);
|
|
58
|
+
// Return
|
|
48
59
|
return res.status(status).json({
|
|
49
60
|
error: err.message,
|
|
50
61
|
});
|
|
@@ -13,7 +13,8 @@ export abstract class CustomError extends Error {
|
|
|
13
13
|
|
|
14
14
|
export class ParamMissingError extends CustomError {
|
|
15
15
|
|
|
16
|
-
public static readonly Msg = 'One or more of the required parameters
|
|
16
|
+
public static readonly Msg = 'One or more of the required parameters ' +
|
|
17
|
+
'was missing.';
|
|
17
18
|
public static readonly HttpStatus = HttpStatusCodes.BAD_REQUEST;
|
|
18
19
|
|
|
19
20
|
constructor() {
|
|
@@ -23,7 +24,8 @@ export class ParamMissingError extends CustomError {
|
|
|
23
24
|
|
|
24
25
|
export class ParamInvalidError extends CustomError {
|
|
25
26
|
|
|
26
|
-
public static readonly Msg = 'One or more of the required was missing
|
|
27
|
+
public static readonly Msg = 'One or more of the required was missing ' +
|
|
28
|
+
'or invalid.';
|
|
27
29
|
public static readonly HttpStatus = HttpStatusCodes.BAD_REQUEST;
|
|
28
30
|
|
|
29
31
|
constructor() {
|
|
@@ -43,7 +45,8 @@ export class ValidatorFnError extends CustomError {
|
|
|
43
45
|
|
|
44
46
|
export class UserNotFoundError extends CustomError {
|
|
45
47
|
|
|
46
|
-
public static readonly Msg = 'A user with the given id does not exists
|
|
48
|
+
public static readonly Msg = 'A user with the given id does not exists ' +
|
|
49
|
+
'in the database.';
|
|
47
50
|
public static readonly HttpStatus = HttpStatusCodes.NOT_FOUND;
|
|
48
51
|
|
|
49
52
|
constructor() {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import jsonwebtoken
|
|
1
|
+
import jsonwebtoken from 'jsonwebtoken';
|
|
2
2
|
import envVars from '../shared/env-vars';
|
|
3
3
|
|
|
4
4
|
|
|
@@ -31,7 +31,7 @@ function sign(data: string | object | Buffer): Promise<string> {
|
|
|
31
31
|
/**
|
|
32
32
|
* Decrypt JWT and extract client data.
|
|
33
33
|
*/
|
|
34
|
-
async function decode<T
|
|
34
|
+
async function decode<T>(jwt: string): Promise<string | undefined | T> {
|
|
35
35
|
return new Promise((res, rej) => {
|
|
36
36
|
jsonwebtoken.verify(jwt, envVars.jwt.secret, (err, decoded) => {
|
|
37
37
|
return err ? rej(errors.validation) : res(decoded as T);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-generator-typescript",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "Generate new Express applications similar to express-generate which but sets it up to use TypeScript instead",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"express-generator-typescript": "node bin/cli.js",
|