express-generator-typescript 2.5.15 → 2.5.17
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/README-git +95 -0
- package/README.md +1 -78
- package/lib/express-generator-typescript.js +1 -1
- package/lib/project-files/README.md +13 -0
- package/lib/project-files/config/.env.production +1 -0
- package/lib/project-files/package.json +1 -2
- package/lib/project-files/spec/support/Paths.ts +1 -1
- package/lib/project-files/spec/support/index.ts +8 -3
- package/lib/project-files/spec/tests/users.spec.ts +17 -15
- package/lib/project-files/src/common/ENV.ts +9 -2
- package/lib/project-files/src/common/constants.ts +2 -1
- package/lib/project-files/src/common/route-errors.ts +0 -14
- package/lib/project-files/src/models/User.ts +40 -6
- package/lib/project-files/src/repos/MockOrm.ts +2 -2
- package/lib/project-files/src/repos/database.test.json +1 -1
- package/lib/project-files/src/routes/UserRoutes.ts +1 -2
- package/lib/project-files/src/routes/common/index.ts +26 -34
- package/lib/project-files/src/routes/index.ts +1 -1
- package/lib/project-files/src/server.ts +10 -7
- package/lib/project-files/src/util/misc.ts +4 -0
- package/lib/project-files/src/util/validators.ts +14 -1
- package/package.json +3 -1
- package/lib/project-files/src/util/schema.ts +0 -13
- /package/lib/project-files/src/{routes/common → common}/Paths.ts +0 -0
package/README-git
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
<img alt='overnightjs' src='https://github.com/seanpmaxwell/express-generator-typescript/raw/master/express-typescript.png' border='0'>
|
|
2
|
+
|
|
3
|
+
[Express](https://www.npmjs.com/package/express) with [TypeScript's](https://www.npmjs.com/package/typescript) application generator.
|
|
4
|
+
|
|
5
|
+
<a href="https://www.npmjs.com/package/express-generator-typescript" target="_blank"><img src="https://img.shields.io/npm/v/express-generator-typescript.svg" alt="NPM Version" /></a>
|
|
6
|
+
<a href="https://www.npmjs.com/package/express-generator-typescript" target="_blank"><img src="https://img.shields.io/npm/l/express-generator-typescript.svg" alt="Package License" /></a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/express-generator-typescript" target="_blank"><img src="https://img.shields.io/npm/dm/express-generator-typescript.svg" alt="NPM Downloads" /></a>
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## What is it?
|
|
11
|
+
|
|
12
|
+
Creates a new express application similar to the _express-generator_ module. Except this new application is configured to use TypeScript instead of plain JavaScript.
|
|
13
|
+
|
|
14
|
+
This project complies with Typescript best practices listed <a href="https://github.com/seanpmaxwell/Typescript-Best-Practices/blob/main/README.md">here</a>.
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## Why express-generator-typescript?
|
|
18
|
+
|
|
19
|
+
NodeJS is great for the rapid development of web-projects, but is often neglected because of the lack of type safety. TypeScript solves this issue and (along with its linter file) can even make your code more robust than some other static languages like Java.
|
|
20
|
+
|
|
21
|
+
There are some other tools out there to generate express apps with TypeScript such as _express-generator-ts_, but these either haven't been updated in a while or install a lot of junk in your project (such as an ORM).
|
|
22
|
+
|
|
23
|
+
Due to the heavy use of single-page-applications, no view-engine is configured by default. Express is only setup with the minimal settings for calling APIs and serving an index.html file. All the tools you need to run for development (while restarting on changes), building, testing, and running for production are packaged with this library.
|
|
24
|
+
|
|
25
|
+
In addition, relative paths are also setup, so you don't have to go through the trouble of installing and configuring _tsconfig-paths_ and _module-alias_. Just make sure to update `paths` in _tsconfig.json_ and `_moduleAliases` in _preload.js_ if you want to add/edit the relative paths.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## Sample-project
|
|
29
|
+
|
|
30
|
+
When you run _express-generator-typescript_, it sets up a simple application with routes for adding, updating, deleting, and fetching user objects. This is just to demonstrate how routing is done with express.
|
|
31
|
+
|
|
32
|
+
### `--with-auth` option no longer available for version 2.5+
|
|
33
|
+
|
|
34
|
+
For the command-line, you used to be able to pass the `--with-auth` option to generate an app which required a login before using the routes; however, maintaining two separate projects became quite cumbersome. If you want an example of how to do authentication in expressjs with json-web-tokens you can refer to this sample project <a href="https://github.com/seanpmaxwell/express-jsonwebtoken-demo">here</a>.
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
$ Just use 'npx'
|
|
41
|
+
Or
|
|
42
|
+
$ npm install -g express-generator-typescript
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
## Quick Start
|
|
47
|
+
|
|
48
|
+
The quickest way to get started is use npx and pass in the name of the project you want to create. If you don't specify a project name, the default _express-gen-ts_ will be used instead. If you want to use `yarn` instead of `npm`, pass the option `--use-yarn`.
|
|
49
|
+
|
|
50
|
+
Create the app:<br/>
|
|
51
|
+
With no options: `$ npx express-generator-typescript`<br/>
|
|
52
|
+
With all options (order doesn't matter): `$ npx express-generator-typescript --use-yarn "project name"`
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
Start your express-generator-typescript app in development mode at `http://localhost:3000/`:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
$ cd "project name" && npm run dev
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
## Available commands for the server.
|
|
63
|
+
|
|
64
|
+
- Run the server in development mode: `npm run dev` or `npm run dev:hot`.
|
|
65
|
+
- Run all unit-tests: `npm run test` or `npm run test:hot`.
|
|
66
|
+
- Run a single unit-test: `npm test -- --testFile="name of test file" (i.e. --testFile=Users)`.
|
|
67
|
+
- Check for linting errors: `npm run lint`.
|
|
68
|
+
- Build the project for production: `npm run build`.
|
|
69
|
+
- Run the production build: `npm start`.
|
|
70
|
+
- Check for typescript errors: `npm run type-check`.
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
## Debugging
|
|
74
|
+
|
|
75
|
+
During development, _express-generator-typescript_ uses `nodemon` to restart the server when changes are detected. If you want to enable debugging for node, you'll need to modify the nodemon configurations. This is located under `nodemonConfig:` in `package.json` for the server and `./spec/nodemon.json` for unit-testing. For the `exec` property, replace `ts-node` with `node --inspect -r ts-node/register`.
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
## Note for VSCode users
|
|
79
|
+
|
|
80
|
+
In order to use eslint as a typescript file with VSCode, you need to add a setting to your eslint options. At the root of your project, add a `.vscode/settings.json` file (if you don't have one already) and add the option `unstable_ts_config`:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
"eslint.options": {
|
|
84
|
+
"flags": ["unstable_ts_config"]
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
<br/>
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
Happy web deving :)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
## License
|
|
94
|
+
|
|
95
|
+
[MIT](LICENSE)
|
package/README.md
CHANGED
|
@@ -12,84 +12,7 @@
|
|
|
12
12
|
Creates a new express application similar to the _express-generator_ module. Except this new application is configured to use TypeScript instead of plain JavaScript.
|
|
13
13
|
|
|
14
14
|
This project complies with Typescript best practices listed <a href="https://github.com/seanpmaxwell/Typescript-Best-Practices/blob/main/README.md">here</a>.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
## Why express-generator-typescript?
|
|
18
|
-
|
|
19
|
-
NodeJS is great for the rapid development of web-projects, but is often neglected because of the lack of type safety. TypeScript solves this issue and (along with its linter file) can even make your code more robust than some other static languages like Java.
|
|
20
|
-
|
|
21
|
-
There are some other tools out there to generate express apps with TypeScript such as _express-generator-ts_, but these either haven't been updated in a while or install a lot of junk in your project (such as an ORM).
|
|
22
|
-
|
|
23
|
-
Due to the heavy use of single-page-applications, no view-engine is configured by default. Express is only setup with the minimal settings for calling APIs and serving an index.html file. All the tools you need to run for development (while restarting on changes), building, testing, and running for production are packaged with this library.
|
|
24
|
-
|
|
25
|
-
In addition, relative paths are also setup, so you don't have to go through the trouble of installing and configuring _tsconfig-paths_ and _module-alias_. Just make sure to update `paths` in _tsconfig.json_ and `_moduleAliases` in _preload.js_ if you want to add/edit the relative paths.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
## Sample-project
|
|
29
|
-
|
|
30
|
-
When you run _express-generator-typescript_, it sets up a simple application with routes for adding, updating, deleting, and fetching user objects. This is just to demonstrate how routing is done with express.
|
|
31
|
-
|
|
32
|
-
### `--with-auth` option no longer available for version 2.5+
|
|
33
|
-
|
|
34
|
-
For the command-line, you used to be able to pass the `--with-auth` option to generate an app which required a login before using the routes; however, maintaining two separate projects became quite cumbersome. If you want an example of how to do authentication in expressjs with json-web-tokens you can refer to this sample project <a href="https://github.com/seanpmaxwell/express-jsonwebtoken-demo">here</a>.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
## Installation
|
|
38
|
-
|
|
39
|
-
```sh
|
|
40
|
-
$ Just use 'npx'
|
|
41
|
-
Or
|
|
42
|
-
$ npm install -g express-generator-typescript
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
## Quick Start
|
|
47
|
-
|
|
48
|
-
The quickest way to get started is use npx and pass in the name of the project you want to create. If you don't specify a project name, the default _express-gen-ts_ will be used instead. If you want to use `yarn` instead of `npm`, pass the option `--use-yarn`.
|
|
49
|
-
|
|
50
|
-
Create the app:<br/>
|
|
51
|
-
With no options: `$ npx express-generator-typescript`<br/>
|
|
52
|
-
With all options (order doesn't matter): `$ npx express-generator-typescript --use-yarn "project name"`
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
Start your express-generator-typescript app in development mode at `http://localhost:3000/`:
|
|
56
|
-
|
|
57
|
-
```bash
|
|
58
|
-
$ cd "project name" && npm run dev
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
## Available commands for the server.
|
|
63
|
-
|
|
64
|
-
- Run the server in development mode: `npm run dev` or `npm run dev:hot`.
|
|
65
|
-
- Run all unit-tests: `npm run test` or `npm run test:hot`.
|
|
66
|
-
- Run a single unit-test: `npm test -- --testFile="name of test file" (i.e. --testFile=Users)`.
|
|
67
|
-
- Check for linting errors: `npm run lint`.
|
|
68
|
-
- Build the project for production: `npm run build`.
|
|
69
|
-
- Run the production build: `npm start`.
|
|
70
|
-
- Check for typescript errors: `npm run type-check"`.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
## Debugging
|
|
74
|
-
|
|
75
|
-
During development, _express-generator-typescript_ uses `nodemon` to restart the server when changes are detected. If you want to enable debugging for node, you'll need to modify the nodemon configurations. This is located under `nodemonConfig:` in `package.json` for the server and `./spec/nodemon.json` for unit-testing. For the `exec` property, replace `ts-node` with `node --inspect -r ts-node/register`.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
## Note for VSCode users
|
|
79
|
-
|
|
80
|
-
In order to use eslint as a typescript file with VSCode, you need to add a setting to your eslint options. At the root of your project, add a `.vscode/settings.json` file (if you don't have one already) and add the option `unstable_ts_config`:
|
|
81
|
-
|
|
82
|
-
```typescript
|
|
83
|
-
"eslint.options": {
|
|
84
|
-
"flags": ["unstable_ts_config"]
|
|
85
|
-
}
|
|
86
|
-
```
|
|
87
15
|
<br/>
|
|
88
16
|
|
|
89
17
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
## License
|
|
94
|
-
|
|
95
|
-
[MIT](LICENSE)
|
|
18
|
+
Please refer to the official <a href="https://github.com/seanpmaxwell/express-generator-typescript">github repo</a> for the most up-to-date documentation.
|
|
@@ -15,7 +15,7 @@ const PROJECT_FOLDER_PATH = './project-files';
|
|
|
15
15
|
// Project-folder dependencies
|
|
16
16
|
const DEPENDENCIES = 'express dotenv morgan cookie-parser jet-logger ' +
|
|
17
17
|
'module-alias express-async-errors helmet jsonfile inserturlparams ' +
|
|
18
|
-
'jet-paths dayjs jet-
|
|
18
|
+
'jet-paths dayjs jet-env jet-validators',
|
|
19
19
|
DEV_DEPENDENCIES = 'ts-node typescript nodemon find jasmine supertest ' +
|
|
20
20
|
'@types/node @types/express @types/jasmine @types/find @types/morgan ' +
|
|
21
21
|
'@types/cookie-parser @types/supertest fs-extra tsconfig-paths jiti @swc/core ' +
|
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
This project was created with [express-generator-typescript](https://github.com/seanpmaxwell/express-generator-typescript).
|
|
4
4
|
|
|
5
|
+
**IMPORTANT** for demo purposes I had to disable `helmet` in production. In any real world app you should change these 3 lines of code in `src/server.ts`:
|
|
6
|
+
```ts
|
|
7
|
+
// eslint-disable-next-line n/no-process-env
|
|
8
|
+
if (!process.env.DISABLE_HELMET) {
|
|
9
|
+
app.use(helmet());
|
|
10
|
+
}
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
To just this:
|
|
14
|
+
```ts
|
|
15
|
+
app.use(helmet());
|
|
16
|
+
```
|
|
17
|
+
|
|
5
18
|
|
|
6
19
|
## Available Scripts
|
|
7
20
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Response } from 'supertest';
|
|
2
2
|
import UserRepo from '@src/repos/UserRepo';
|
|
3
3
|
|
|
4
4
|
|
|
@@ -6,9 +6,14 @@ import UserRepo from '@src/repos/UserRepo';
|
|
|
6
6
|
Types
|
|
7
7
|
******************************************************************************/
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
// Use generics to add properties to 'body'
|
|
10
|
+
export type TRes<T = object> = Omit<Response, 'body'> & {
|
|
11
|
+
body: T & { error?: string | IErrObj },
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
interface IErrObj {
|
|
10
15
|
message: string;
|
|
11
|
-
|
|
16
|
+
[key: string]: unknown;
|
|
12
17
|
}
|
|
13
18
|
|
|
14
19
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import supertest from 'supertest';
|
|
2
2
|
import insertUrlParams from 'inserturlparams';
|
|
3
|
+
import { isObject } from 'jet-validators';
|
|
3
4
|
import { parseJson, customDeepCompare } from 'jet-validators/utils';
|
|
4
5
|
|
|
5
6
|
import app from '@src/server';
|
|
@@ -7,12 +8,11 @@ import app from '@src/server';
|
|
|
7
8
|
import UserRepo from '@src/repos/UserRepo';
|
|
8
9
|
import User, { IUser } from '@src/models/User';
|
|
9
10
|
import { USER_NOT_FOUND_ERR } from '@src/services/UserService';
|
|
10
|
-
|
|
11
|
+
import { IParseRequestError } from '@src/routes/common';
|
|
11
12
|
import HttpStatusCodes from '@src/common/HttpStatusCodes';
|
|
12
|
-
import { ValidationErr } from '@src/common/route-errors';
|
|
13
13
|
|
|
14
14
|
import Paths from 'spec/support/Paths';
|
|
15
|
-
import { cleanDatabase,
|
|
15
|
+
import { cleanDatabase, TRes } from 'spec/support';
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
/******************************************************************************
|
|
@@ -30,7 +30,7 @@ const DB_USERS = [
|
|
|
30
30
|
// database
|
|
31
31
|
const compareUserArrays = customDeepCompare({
|
|
32
32
|
onlyCompareProps: ['name', 'email'],
|
|
33
|
-
})
|
|
33
|
+
});
|
|
34
34
|
|
|
35
35
|
|
|
36
36
|
/******************************************************************************
|
|
@@ -59,7 +59,7 @@ describe('UserRouter', () => {
|
|
|
59
59
|
`of "${HttpStatusCodes.OK}" if the request was successful.`, done => {
|
|
60
60
|
agent
|
|
61
61
|
.get(Paths.Users.Get)
|
|
62
|
-
.end((_, res) => {
|
|
62
|
+
.end((_, res: TRes<{ users: IUser[]}>) => {
|
|
63
63
|
expect(res.status).toBe(HttpStatusCodes.OK);
|
|
64
64
|
expect(compareUserArrays(res.body.users, DB_USERS)).toBeTruthy();
|
|
65
65
|
done();
|
|
@@ -90,11 +90,12 @@ describe('UserRouter', () => {
|
|
|
90
90
|
agent
|
|
91
91
|
.post(Paths.Users.Add)
|
|
92
92
|
.send({ user: null })
|
|
93
|
-
.end((_, res) => {
|
|
93
|
+
.end((_, res: TRes) => {
|
|
94
94
|
expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
|
|
95
|
-
const errorObj = parseJson<
|
|
96
|
-
|
|
97
|
-
expect(errorObj.
|
|
95
|
+
const errorObj = parseJson<IParseRequestError>(res.body.error),
|
|
96
|
+
errItem = errorObj.errors[0];
|
|
97
|
+
expect(errorObj.message).toBe('Parse request validation failed');
|
|
98
|
+
expect(isObject(errItem) && errItem.prop === 'user').toBeTruthy();
|
|
98
99
|
done();
|
|
99
100
|
});
|
|
100
101
|
});
|
|
@@ -124,11 +125,12 @@ describe('UserRouter', () => {
|
|
|
124
125
|
agent
|
|
125
126
|
.put(Paths.Users.Update)
|
|
126
127
|
.send({ user: null })
|
|
127
|
-
.end((_, res) => {
|
|
128
|
+
.end((_, res: TRes) => {
|
|
128
129
|
expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
|
|
129
|
-
const errorObj = parseJson<
|
|
130
|
-
|
|
131
|
-
expect(errorObj.
|
|
130
|
+
const errorObj = parseJson<IParseRequestError>(res.body.error),
|
|
131
|
+
errItem = errorObj.errors[0];
|
|
132
|
+
expect(errorObj.message).toBe('Parse request validation failed');
|
|
133
|
+
expect(isObject(errItem) && errItem.prop === 'user').toBeTruthy();
|
|
132
134
|
done();
|
|
133
135
|
});
|
|
134
136
|
});
|
|
@@ -141,7 +143,7 @@ describe('UserRouter', () => {
|
|
|
141
143
|
agent
|
|
142
144
|
.put(Paths.Users.Update)
|
|
143
145
|
.send({ user })
|
|
144
|
-
.end((_, res) => {
|
|
146
|
+
.end((_, res: TRes) => {
|
|
145
147
|
expect(res.status).toBe(HttpStatusCodes.NOT_FOUND);
|
|
146
148
|
expect(res.body.error).toBe(USER_NOT_FOUND_ERR);
|
|
147
149
|
done();
|
|
@@ -172,7 +174,7 @@ describe('UserRouter', () => {
|
|
|
172
174
|
`"${HttpStatusCodes.NOT_FOUND}" if the id was not found.`, done => {
|
|
173
175
|
agent
|
|
174
176
|
.delete(getPath(-1))
|
|
175
|
-
.end((_, res) => {
|
|
177
|
+
.end((_, res: TRes) => {
|
|
176
178
|
expect(res.status).toBe(HttpStatusCodes.NOT_FOUND);
|
|
177
179
|
expect(res.body.error).toBe(USER_NOT_FOUND_ERR);
|
|
178
180
|
done();
|
|
@@ -5,10 +5,17 @@ import { NodeEnvs } from './constants';
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
/******************************************************************************
|
|
8
|
-
|
|
8
|
+
Setup
|
|
9
9
|
******************************************************************************/
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
const ENV = jetEnv({
|
|
12
12
|
NodeEnv: isEnumVal(NodeEnvs),
|
|
13
13
|
Port: num,
|
|
14
14
|
});
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
/******************************************************************************
|
|
18
|
+
Export default
|
|
19
|
+
******************************************************************************/
|
|
20
|
+
|
|
21
|
+
export default ENV;
|
|
@@ -17,17 +17,3 @@ export class RouteError extends Error {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
/**
|
|
21
|
-
* Validation in route layer errors.
|
|
22
|
-
*/
|
|
23
|
-
export class ValidationErr extends RouteError {
|
|
24
|
-
public static MSG = 'One or more parameters were missing or invalid.';
|
|
25
|
-
|
|
26
|
-
public constructor(errObj: unknown) {
|
|
27
|
-
const msg = JSON.stringify({
|
|
28
|
-
message: ValidationErr.MSG,
|
|
29
|
-
parameters: errObj,
|
|
30
|
-
});
|
|
31
|
-
super(HttpStatusCodes.BAD_REQUEST, msg);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
import { isString } from 'jet-validators';
|
|
2
|
+
import { parseObjectPlus, TParser } from 'jet-validators/utils';
|
|
2
3
|
|
|
3
|
-
import
|
|
4
|
-
|
|
4
|
+
import { isRelationalKey, tranIsDate } from '@src/util/validators';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/******************************************************************************
|
|
8
|
+
Variables
|
|
9
|
+
******************************************************************************/
|
|
10
|
+
|
|
11
|
+
const DEFAULT_USER_VALS = (): IUser => ({
|
|
12
|
+
id: -1,
|
|
13
|
+
name: '',
|
|
14
|
+
created: new Date(),
|
|
15
|
+
email: '',
|
|
16
|
+
});
|
|
5
17
|
|
|
6
18
|
|
|
7
19
|
/******************************************************************************
|
|
@@ -17,14 +29,32 @@ export interface IUser {
|
|
|
17
29
|
|
|
18
30
|
|
|
19
31
|
/******************************************************************************
|
|
20
|
-
|
|
32
|
+
Functions
|
|
21
33
|
******************************************************************************/
|
|
22
34
|
|
|
23
|
-
|
|
35
|
+
/**
|
|
36
|
+
* New user object.
|
|
37
|
+
*/
|
|
38
|
+
function newUser(user: Partial<IUser>): IUser {
|
|
39
|
+
const retVal = { ...DEFAULT_USER_VALS(), ...user };
|
|
40
|
+
return parseUser(retVal);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Validate user object but return type predicate.
|
|
45
|
+
*/
|
|
46
|
+
function testIsUser(arg: unknown): arg is IUser {
|
|
47
|
+
return !!parseUser(arg);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Validate a user object.
|
|
52
|
+
*/
|
|
53
|
+
const parseUser: TParser<IUser> = parseObjectPlus({
|
|
24
54
|
id: isRelationalKey,
|
|
25
55
|
name: isString,
|
|
26
|
-
created: Date,
|
|
27
56
|
email: isString,
|
|
57
|
+
created: tranIsDate,
|
|
28
58
|
});
|
|
29
59
|
|
|
30
60
|
|
|
@@ -32,4 +62,8 @@ const User = schema<IUser>({
|
|
|
32
62
|
Export default
|
|
33
63
|
******************************************************************************/
|
|
34
64
|
|
|
35
|
-
export default
|
|
65
|
+
export default {
|
|
66
|
+
new: newUser,
|
|
67
|
+
parse: parseUser,
|
|
68
|
+
test: testIsUser,
|
|
69
|
+
} as const;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"users":[{"id":
|
|
1
|
+
{"users":[{"id":39386845760,"name":"Bill","created":"2025-03-09T02:38:03.021Z","email":"sean.maxwell@gmail.com"},{"id":232226940074,"name":"John Smith","created":"2025-03-09T02:38:03.021Z","email":"john.smith@gmail.com"},{"id":847602476180,"name":"Gordan Freeman","created":"2025-03-09T02:38:03.021Z","email":"gordan.freeman@gmail.com"}]}
|
|
@@ -5,8 +5,7 @@ import HttpStatusCodes from '@src/common/HttpStatusCodes';
|
|
|
5
5
|
import UserService from '@src/services/UserService';
|
|
6
6
|
import User from '@src/models/User';
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
import { parseReq, IReq, IRes } from './common';
|
|
8
|
+
import { IReq, IRes, parseReq } from './common';
|
|
10
9
|
|
|
11
10
|
|
|
12
11
|
/******************************************************************************
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { Response, Request } from 'express';
|
|
2
|
-
import { isObject, isString } from 'jet-validators';
|
|
3
|
-
import { parseObject, TSchema } from 'jet-validators/utils';
|
|
4
2
|
|
|
5
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
ParseObjectError,
|
|
5
|
+
TParseErrorItem,
|
|
6
|
+
TSchema,
|
|
7
|
+
parseObjectPlus,
|
|
8
|
+
} from 'jet-validators/utils';
|
|
9
|
+
|
|
10
|
+
import HttpStatusCodes from '@src/common/HttpStatusCodes';
|
|
11
|
+
import { RouteError } from '@src/common/route-errors';
|
|
6
12
|
|
|
7
13
|
|
|
8
14
|
/******************************************************************************
|
|
@@ -13,10 +19,9 @@ type TRecord = Record<string, unknown>;
|
|
|
13
19
|
export type IReq = Request<TRecord, void, TRecord, TRecord>;
|
|
14
20
|
export type IRes = Response<unknown, TRecord>;
|
|
15
21
|
|
|
16
|
-
export interface
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
moreInfo?: string;
|
|
22
|
+
export interface IParseRequestError {
|
|
23
|
+
message: 'Parse request validation failed';
|
|
24
|
+
errors: TParseErrorItem[];
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
|
|
@@ -25,37 +30,24 @@ export interface IReqPropErr {
|
|
|
25
30
|
******************************************************************************/
|
|
26
31
|
|
|
27
32
|
/**
|
|
28
|
-
* Parse
|
|
33
|
+
* Parse an incoming route request object.
|
|
29
34
|
*/
|
|
30
35
|
export function parseReq<U extends TSchema>(schema: U) {
|
|
36
|
+
const parse = parseObjectPlus(schema);
|
|
31
37
|
return (arg: unknown) => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const err: IReqPropErr = { prop, value };
|
|
44
|
-
if (caughtErr !== undefined) {
|
|
45
|
-
let moreInfo;
|
|
46
|
-
if (!isString(caughtErr)) {
|
|
47
|
-
moreInfo = JSON.stringify(caughtErr);
|
|
48
|
-
} else {
|
|
49
|
-
moreInfo = caughtErr;
|
|
50
|
-
}
|
|
51
|
-
err.moreInfo = moreInfo;
|
|
38
|
+
let retVal;
|
|
39
|
+
try {
|
|
40
|
+
retVal = parse(arg);
|
|
41
|
+
} catch (err) {
|
|
42
|
+
if (err instanceof ParseObjectError) {
|
|
43
|
+
const errObj: IParseRequestError = {
|
|
44
|
+
message: 'Parse request validation failed',
|
|
45
|
+
errors: err.getErrors(),
|
|
46
|
+
};
|
|
47
|
+
const msg = JSON.stringify(errObj);
|
|
48
|
+
throw new RouteError(HttpStatusCodes.BAD_REQUEST, msg);
|
|
52
49
|
}
|
|
53
|
-
|
|
54
|
-
};
|
|
55
|
-
// Return
|
|
56
|
-
const retVal = parseObject<U>(schema, errCb)(arg);
|
|
57
|
-
if (errArr.length > 0) {
|
|
58
|
-
throw new ValidationErr(errArr);
|
|
50
|
+
throw err;
|
|
59
51
|
}
|
|
60
52
|
return retVal;
|
|
61
53
|
};
|
|
@@ -8,7 +8,7 @@ import 'express-async-errors';
|
|
|
8
8
|
|
|
9
9
|
import BaseRouter from '@src/routes';
|
|
10
10
|
|
|
11
|
-
import Paths from '@src/
|
|
11
|
+
import Paths from '@src/common/Paths';
|
|
12
12
|
import ENV from '@src/common/ENV';
|
|
13
13
|
import HttpStatusCodes from '@src/common/HttpStatusCodes';
|
|
14
14
|
import { RouteError } from '@src/common/route-errors';
|
|
@@ -16,26 +16,29 @@ import { NodeEnvs } from '@src/common/constants';
|
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
/******************************************************************************
|
|
19
|
-
|
|
19
|
+
Setup
|
|
20
20
|
******************************************************************************/
|
|
21
21
|
|
|
22
22
|
const app = express();
|
|
23
23
|
|
|
24
24
|
|
|
25
|
-
// ****
|
|
25
|
+
// **** Middleware **** //
|
|
26
26
|
|
|
27
27
|
// Basic middleware
|
|
28
28
|
app.use(express.json());
|
|
29
29
|
app.use(express.urlencoded({extended: true}));
|
|
30
30
|
|
|
31
31
|
// Show routes called in console during development
|
|
32
|
-
if (ENV.NodeEnv === NodeEnvs.Dev
|
|
32
|
+
if (ENV.NodeEnv === NodeEnvs.Dev) {
|
|
33
33
|
app.use(morgan('dev'));
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
// Security
|
|
37
|
-
if (ENV.NodeEnv === NodeEnvs.Production
|
|
38
|
-
|
|
37
|
+
if (ENV.NodeEnv === NodeEnvs.Production) {
|
|
38
|
+
// eslint-disable-next-line n/no-process-env
|
|
39
|
+
if (!process.env.DISABLE_HELMET) {
|
|
40
|
+
app.use(helmet());
|
|
41
|
+
}
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
// Add APIs, must be after middleware
|
|
@@ -55,7 +58,7 @@ app.use((err: Error, _: Request, res: Response, next: NextFunction) => {
|
|
|
55
58
|
});
|
|
56
59
|
|
|
57
60
|
|
|
58
|
-
// ****
|
|
61
|
+
// **** FrontEnd Content **** //
|
|
59
62
|
|
|
60
63
|
// Set views directory (html)
|
|
61
64
|
const viewsDir = path.join(__dirname, 'views');
|
|
@@ -1,9 +1,22 @@
|
|
|
1
|
-
import { isNumber } from 'jet-validators';
|
|
1
|
+
import { isNumber, isDate } from 'jet-validators';
|
|
2
|
+
import { transform } from 'jet-validators/utils';
|
|
2
3
|
|
|
3
4
|
|
|
5
|
+
/******************************************************************************
|
|
6
|
+
Functions
|
|
7
|
+
******************************************************************************/
|
|
8
|
+
|
|
4
9
|
/**
|
|
5
10
|
* Database relational key.
|
|
6
11
|
*/
|
|
7
12
|
export function isRelationalKey(arg: unknown): arg is number {
|
|
8
13
|
return isNumber(arg) && arg >= -1;
|
|
9
14
|
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Convert to date object then check is a validate date.
|
|
18
|
+
*/
|
|
19
|
+
export const tranIsDate = transform(
|
|
20
|
+
arg => new Date(arg as string),
|
|
21
|
+
arg => isDate(arg),
|
|
22
|
+
);
|
package/package.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-generator-typescript",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.17",
|
|
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",
|
|
7
|
+
"pre-publish": "mv README.md README-git && mv README-npm README.md",
|
|
8
|
+
"post-publish": "mv README.md README-npm && mv README-git README.md",
|
|
7
9
|
"start": "node bin/cli.js",
|
|
8
10
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
11
|
},
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import jetSchema from 'jet-schema';
|
|
2
|
-
import { isNumber, isString, isBoolean } from 'jet-validators';
|
|
3
|
-
import { isRelationalKey } from './validators';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export default jetSchema({
|
|
7
|
-
globals: [
|
|
8
|
-
{ vf: isRelationalKey, default: -1 },
|
|
9
|
-
{ vf: isNumber, default: 0 },
|
|
10
|
-
{ vf: isString, default: '' },
|
|
11
|
-
{ vf: isBoolean, default: false },
|
|
12
|
-
],
|
|
13
|
-
});
|
|
File without changes
|