express-generator-typescript 2.0.1 → 2.0.4
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/bin/cli.js +24 -23
- package/lib/cli.js +24 -22
- package/lib/express-generator-typescript.js +19 -24
- package/lib/project-files/src/routes/auth-router.ts +4 -4
- package/lib/project-files/src/routes/middleware.ts +2 -2
- package/lib/project-files/src/routes/user-router.ts +6 -6
- package/lib/project-files/src/services/auth-service.ts +1 -1
- package/lib/project-files/src/shared/types.ts +22 -0
- package/lib/project-files/src/types/express/index.d.ts +0 -23
- package/lib/project-files/src/util/jwt-util.ts +4 -4
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -10,26 +10,27 @@ const path = require('path');
|
|
|
10
10
|
const expressGenTs = require('../lib/express-generator-typescript');
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
destination =
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
13
|
+
// Start
|
|
14
|
+
console.log('Setting up new Express/TypeScript project...');
|
|
15
|
+
|
|
16
|
+
// Setup use yarn
|
|
17
|
+
let useYarn = false;
|
|
18
|
+
const args = process.argv.slice(2);
|
|
19
|
+
let idx = -1;
|
|
20
|
+
idx = args.indexOf('--use-yarn');
|
|
21
|
+
if (idx > -1) {
|
|
22
|
+
useYarn = true;
|
|
23
|
+
args.splice(idx, 1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Setup destination
|
|
27
|
+
let destination = 'express-gen-ts';
|
|
28
|
+
if (args.length > 0) {
|
|
29
|
+
destination = args[0];
|
|
30
|
+
}
|
|
31
|
+
destination = path.join(process.cwd(), destination);
|
|
32
|
+
|
|
33
|
+
// Creating new project finished
|
|
34
|
+
expressGenTs(destination, useYarn).then(() => {
|
|
35
|
+
console.log('Project setup complete!');
|
|
36
|
+
});
|
package/lib/cli.js
CHANGED
|
@@ -10,25 +10,27 @@ const path = require('path');
|
|
|
10
10
|
const expressGenTs = require('../lib/express-generator-typescript');
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
destination =
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
13
|
+
// Start
|
|
14
|
+
console.log('Setting up new Express/TypeScript project...');
|
|
15
|
+
|
|
16
|
+
// Setup use yarn
|
|
17
|
+
let useYarn = false;
|
|
18
|
+
const args = process.argv.slice(2);
|
|
19
|
+
let idx = -1;
|
|
20
|
+
idx = args.indexOf('--use-yarn');
|
|
21
|
+
if (idx > -1) {
|
|
22
|
+
useYarn = true;
|
|
23
|
+
args.splice(idx, 1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Setup destination
|
|
27
|
+
let destination = 'express-gen-ts';
|
|
28
|
+
if (args.length > 0) {
|
|
29
|
+
destination = args[0];
|
|
30
|
+
}
|
|
31
|
+
destination = path.join(process.cwd(), destination);
|
|
32
|
+
|
|
33
|
+
// Creating new project finished
|
|
34
|
+
expressGenTs(destination, useYarn).then(() => {
|
|
35
|
+
console.log('Project setup complete!');
|
|
36
|
+
});
|
|
@@ -13,14 +13,27 @@ const fs = require('fs');
|
|
|
13
13
|
|
|
14
14
|
// **** Variables **** //
|
|
15
15
|
|
|
16
|
+
// Misc
|
|
16
17
|
const prjFolder = './project-files';
|
|
17
18
|
|
|
19
|
+
// ncp options
|
|
18
20
|
const ncpOpts = {
|
|
19
21
|
filter: (fileName) => {
|
|
20
22
|
return !(fileName === 'package-lock.json' || fileName === 'node_modules');
|
|
21
23
|
},
|
|
22
24
|
};
|
|
23
25
|
|
|
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';
|
|
29
|
+
|
|
30
|
+
// Project-folder development dependencies
|
|
31
|
+
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';
|
|
36
|
+
|
|
24
37
|
|
|
25
38
|
// **** Functions **** //
|
|
26
39
|
|
|
@@ -32,8 +45,7 @@ async function expressGenTs(destination, useYarn) {
|
|
|
32
45
|
await copyProjectFiles(destination);
|
|
33
46
|
updatePackageJson(destination);
|
|
34
47
|
await renameGitigoreFile(destination);
|
|
35
|
-
|
|
36
|
-
downloadNodeModules(destination, depStrings, useYarn);
|
|
48
|
+
downloadNodeModules(destination, useYarn);
|
|
37
49
|
} catch (err) {
|
|
38
50
|
console.error(err);
|
|
39
51
|
}
|
|
@@ -75,36 +87,19 @@ function renameGitigoreFile(destination) {
|
|
|
75
87
|
});
|
|
76
88
|
}
|
|
77
89
|
|
|
78
|
-
/**
|
|
79
|
-
* Setup dependency strings. This way gets you the latest version of everything.
|
|
80
|
-
*/
|
|
81
|
-
function getDepStrings() {
|
|
82
|
-
let dependencies = 'express dotenv http-status-codes morgan cookie-parser jet-logger ' +
|
|
83
|
-
'module-alias command-line-args express-async-errors helmet jsonfile bcrypt ' +
|
|
84
|
-
'jsonwebtoken';
|
|
85
|
-
let devDependencies = 'ts-node typescript nodemon find jasmine supertest ' +
|
|
86
|
-
'@types/node @types/express @types/jasmine @types/find @types/morgan ' +
|
|
87
|
-
'@types/cookie-parser @types/supertest fs-extra tsconfig-paths @types/jsonfile ' +
|
|
88
|
-
'@types/command-line-args @typescript-eslint/eslint-plugin @typescript-eslint/parser ' +
|
|
89
|
-
'eslint @types/fs-extra @types/bcrypt @types/jsonwebtoken ' +
|
|
90
|
-
'eslint-plugin-node';
|
|
91
|
-
// Return
|
|
92
|
-
return { dependencies, devDependencies };
|
|
93
|
-
}
|
|
94
|
-
|
|
95
90
|
/**
|
|
96
91
|
* Download the dependencies.
|
|
97
92
|
*/
|
|
98
|
-
function downloadNodeModules(destination,
|
|
93
|
+
function downloadNodeModules(destination, useYarn) {
|
|
99
94
|
const options = {cwd: destination};
|
|
100
95
|
let downloadLibCmd;
|
|
101
96
|
let downloadDepCmd;
|
|
102
97
|
if (useYarn) {
|
|
103
|
-
downloadLibCmd = 'yarn add ' +
|
|
104
|
-
downloadDepCmd = 'yarn add ' +
|
|
98
|
+
downloadLibCmd = 'yarn add ' + dependencies;
|
|
99
|
+
downloadDepCmd = 'yarn add ' + devDependencies + ' -D';
|
|
105
100
|
} else {
|
|
106
|
-
downloadLibCmd = 'npm i -s ' +
|
|
107
|
-
downloadDepCmd = 'npm i -D ' +
|
|
101
|
+
downloadLibCmd = 'npm i -s ' + dependencies;
|
|
102
|
+
downloadDepCmd = 'npm i -D ' + devDependencies;
|
|
108
103
|
}
|
|
109
104
|
childProcess.execSync(downloadLibCmd, options);
|
|
110
105
|
childProcess.execSync(downloadDepCmd, options);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Router } from 'express';
|
|
2
2
|
import StatusCodes from 'http-status-codes';
|
|
3
3
|
|
|
4
4
|
import authService from '@services/auth-service';
|
|
5
5
|
import { ParamMissingError } from '@shared/errors';
|
|
6
6
|
import envVars from 'src/shared/env-vars';
|
|
7
|
-
import { IReq } from '
|
|
7
|
+
import { IReq, IRes } from '@shared/types';
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
// **** Types **** //
|
|
@@ -34,7 +34,7 @@ export const p = {
|
|
|
34
34
|
/**
|
|
35
35
|
* Login a user.
|
|
36
36
|
*/
|
|
37
|
-
router.post(p.login, async (req: IReq<ILoginReq>, res:
|
|
37
|
+
router.post(p.login, async (req: IReq<ILoginReq>, res: IRes) => {
|
|
38
38
|
const { email, password } = req.body;
|
|
39
39
|
if (!email || !password) {
|
|
40
40
|
throw new ParamMissingError();
|
|
@@ -50,7 +50,7 @@ router.post(p.login, async (req: IReq<ILoginReq>, res: Response) => {
|
|
|
50
50
|
/**
|
|
51
51
|
* Logout the user.
|
|
52
52
|
*/
|
|
53
|
-
router.get(p.logout, (_:
|
|
53
|
+
router.get(p.logout, (_: IReq, res: IRes) => {
|
|
54
54
|
const { key, options } = envVars.cookieProps;
|
|
55
55
|
res.clearCookie(key, options);
|
|
56
56
|
return res.status(OK).end();
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import StatusCodes from 'http-status-codes';
|
|
2
2
|
import { Request, Response, NextFunction } from 'express';
|
|
3
|
+
import { JwtPayload } from 'jsonwebtoken';
|
|
3
4
|
|
|
4
5
|
import { IUser, UserRoles } from '@models/user-model';
|
|
5
6
|
import envVars from 'src/shared/env-vars';
|
|
6
7
|
import jwtUtil from '@util/jwt-util';
|
|
7
|
-
import { JwtPayload } from 'jsonwebtoken';
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
// **** Variables **** //
|
|
@@ -38,7 +38,7 @@ export async function adminMw(req: Request, res: Response, next: NextFunction) {
|
|
|
38
38
|
}
|
|
39
39
|
// Make sure user role is an admin
|
|
40
40
|
const clientData = await jwtUtil.decode<ISessionUser>(jwt);
|
|
41
|
-
if (clientData.role === UserRoles.Admin) {
|
|
41
|
+
if (typeof clientData === 'object' && clientData.role === UserRoles.Admin) {
|
|
42
42
|
res.locals.sessionUser = clientData;
|
|
43
43
|
next();
|
|
44
44
|
} else {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import StatusCodes from 'http-status-codes';
|
|
2
|
-
import {
|
|
2
|
+
import { Router } from 'express';
|
|
3
3
|
|
|
4
4
|
import userService from '@services/user-service';
|
|
5
5
|
import { ParamMissingError } from '@shared/errors';
|
|
6
6
|
import { IUser } from '@models/user-model';
|
|
7
|
-
import { IReq } from '
|
|
7
|
+
import { IReq, IRes } from '@shared/types';
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
// **** Variables **** //
|
|
@@ -27,7 +27,7 @@ export const p = {
|
|
|
27
27
|
/**
|
|
28
28
|
* Get all users.
|
|
29
29
|
*/
|
|
30
|
-
router.get(p.get, async (_:
|
|
30
|
+
router.get(p.get, async (_: IReq, res: IRes) => {
|
|
31
31
|
const users = await userService.getAll();
|
|
32
32
|
return res.status(OK).json({ users });
|
|
33
33
|
});
|
|
@@ -35,7 +35,7 @@ router.get(p.get, async (_: Request, res: Response) => {
|
|
|
35
35
|
/**
|
|
36
36
|
* Add one user.
|
|
37
37
|
*/
|
|
38
|
-
router.post(p.add, async (req: IReq<{user: IUser}>, res:
|
|
38
|
+
router.post(p.add, async (req: IReq<{user: IUser}>, res: IRes) => {
|
|
39
39
|
const { user } = req.body;
|
|
40
40
|
if (!user) {
|
|
41
41
|
throw new ParamMissingError();
|
|
@@ -47,7 +47,7 @@ router.post(p.add, async (req: IReq<{user: IUser}>, res: Response) => {
|
|
|
47
47
|
/**
|
|
48
48
|
* Update one user.
|
|
49
49
|
*/
|
|
50
|
-
router.put(p.update, async (req: IReq<{user: IUser}>, res:
|
|
50
|
+
router.put(p.update, async (req: IReq<{user: IUser}>, res: IRes) => {
|
|
51
51
|
const { user } = req.body;
|
|
52
52
|
if (!user) {
|
|
53
53
|
throw new ParamMissingError();
|
|
@@ -59,7 +59,7 @@ router.put(p.update, async (req: IReq<{user: IUser}>, res: Response) => {
|
|
|
59
59
|
/**
|
|
60
60
|
* Delete one user.
|
|
61
61
|
*/
|
|
62
|
-
router.delete(p.delete, async (req:
|
|
62
|
+
router.delete(p.delete, async (req: IReq, res: IRes) => {
|
|
63
63
|
const { id } = req.params;
|
|
64
64
|
if (!id) {
|
|
65
65
|
throw new ParamMissingError();
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as e from 'express';
|
|
2
|
+
import { Query } from 'express-serve-static-core';
|
|
3
|
+
|
|
4
|
+
import { ISessionUser } from '@routes/middleware';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
// **** Express **** //
|
|
8
|
+
|
|
9
|
+
export interface IReq<T = void> extends e.Request {
|
|
10
|
+
body: T;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface IReqQuery<T extends Query, U = void> extends e.Request {
|
|
14
|
+
query: T;
|
|
15
|
+
body: U;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface IRes extends e.Response {
|
|
19
|
+
locals: {
|
|
20
|
+
sessionUser: ISessionUser;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
import 'express';
|
|
2
|
-
import * as e from 'express';
|
|
3
|
-
import { Query } from 'express-serve-static-core';
|
|
4
|
-
|
|
5
|
-
import { ISessionUser } from '@routes/middleware';
|
|
6
|
-
|
|
7
2
|
|
|
8
3
|
|
|
9
4
|
// **** Declaration Merging **** //
|
|
@@ -13,22 +8,4 @@ declare module 'express' {
|
|
|
13
8
|
export interface Request {
|
|
14
9
|
signedCookies: Record<string, string>;
|
|
15
10
|
}
|
|
16
|
-
|
|
17
|
-
export interface Response {
|
|
18
|
-
locals: {
|
|
19
|
-
sessionUser: ISessionUser;
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// **** Generics for Request Object **** //
|
|
26
|
-
|
|
27
|
-
export interface IReq<T> extends e.Request {
|
|
28
|
-
body: T;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface IReqQuery<T extends Query, U = void> extends e.Request {
|
|
32
|
-
query: T;
|
|
33
|
-
body?: U;
|
|
34
11
|
}
|
|
@@ -20,10 +20,10 @@ const options = {
|
|
|
20
20
|
/**
|
|
21
21
|
* Encrypt data and return jwt.
|
|
22
22
|
*/
|
|
23
|
-
function sign(data:
|
|
24
|
-
return new Promise((
|
|
23
|
+
function sign(data: string | object | Buffer): Promise<string> {
|
|
24
|
+
return new Promise((res, rej) => {
|
|
25
25
|
jsonwebtoken.sign(data, envVars.jwt.secret, options, (err, token) => {
|
|
26
|
-
return err ?
|
|
26
|
+
return err ? rej(err) : res(token || '');
|
|
27
27
|
});
|
|
28
28
|
});
|
|
29
29
|
}
|
|
@@ -31,7 +31,7 @@ function sign(data: JwtPayload): Promise<string> {
|
|
|
31
31
|
/**
|
|
32
32
|
* Decrypt JWT and extract client data.
|
|
33
33
|
*/
|
|
34
|
-
function decode<T>(jwt: string): Promise<T> {
|
|
34
|
+
async function decode<T extends JwtPayload>(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.0.
|
|
3
|
+
"version": "2.0.4",
|
|
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",
|