express-generator-typescript 2.5.16 → 2.5.18
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/index.ts +14 -3
- package/lib/project-files/spec/tests/users.spec.ts +14 -14
- package/lib/project-files/src/common/route-errors.ts +11 -7
- package/lib/project-files/src/models/User.ts +39 -6
- package/lib/project-files/src/repos/database.test.json +1 -1
- package/lib/project-files/src/routes/UserRoutes.ts +1 -1
- package/lib/project-files/src/routes/common/index.ts +15 -65
- package/lib/project-files/src/server.ts +4 -1
- 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/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,6 +1,7 @@
|
|
|
1
1
|
import { Response } from 'supertest';
|
|
2
|
-
import { TParseReqErr } from '@src/routes/common';
|
|
3
2
|
import UserRepo from '@src/repos/UserRepo';
|
|
3
|
+
import { IParseObjectError, parseJson } from 'jet-validators/utils';
|
|
4
|
+
import { isString } from 'jet-validators';
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
/******************************************************************************
|
|
@@ -17,9 +18,9 @@ interface IErrObj {
|
|
|
17
18
|
[key: string]: unknown;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
interface IValidationErr {
|
|
21
22
|
message: string;
|
|
22
|
-
|
|
23
|
+
errors: IParseObjectError[];
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
|
|
@@ -35,3 +36,13 @@ export async function cleanDatabase(): Promise<void> {
|
|
|
35
36
|
UserRepo.deleteAllUsers(),
|
|
36
37
|
]);
|
|
37
38
|
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* JSON parse a validation error.
|
|
42
|
+
*/
|
|
43
|
+
export function parseValidationErr(arg: unknown): IValidationErr {
|
|
44
|
+
if (!isString(arg)) {
|
|
45
|
+
throw new Error('Not a string');
|
|
46
|
+
}
|
|
47
|
+
return parseJson<IValidationErr>(arg);
|
|
48
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import supertest from 'supertest';
|
|
2
2
|
import insertUrlParams from 'inserturlparams';
|
|
3
|
-
import {
|
|
3
|
+
import { customDeepCompare } from 'jet-validators/utils';
|
|
4
4
|
|
|
5
5
|
import app from '@src/server';
|
|
6
6
|
|
|
@@ -9,11 +9,10 @@ import User, { IUser } from '@src/models/User';
|
|
|
9
9
|
import { USER_NOT_FOUND_ERR } from '@src/services/UserService';
|
|
10
10
|
|
|
11
11
|
import HttpStatusCodes from '@src/common/HttpStatusCodes';
|
|
12
|
-
import {
|
|
12
|
+
import { ValidationError } from '@src/common/route-errors';
|
|
13
13
|
|
|
14
14
|
import Paths from 'spec/support/Paths';
|
|
15
|
-
import { cleanDatabase,
|
|
16
|
-
import { isObject } from 'jet-validators';
|
|
15
|
+
import { cleanDatabase, parseValidationErr, TRes } from 'spec/support';
|
|
17
16
|
|
|
18
17
|
|
|
19
18
|
/******************************************************************************
|
|
@@ -93,10 +92,9 @@ describe('UserRouter', () => {
|
|
|
93
92
|
.send({ user: null })
|
|
94
93
|
.end((_, res: TRes) => {
|
|
95
94
|
expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
|
|
96
|
-
const errorObj =
|
|
97
|
-
|
|
98
|
-
expect(errorObj.
|
|
99
|
-
expect(isObject(parseErr) && parseErr.prop === 'user').toBeTruthy();
|
|
95
|
+
const errorObj = parseValidationErr(res.body.error);
|
|
96
|
+
expect(errorObj.message).toBe(ValidationError.MESSAGE);
|
|
97
|
+
expect(errorObj.errors[0].prop).toBe('user');
|
|
100
98
|
done();
|
|
101
99
|
});
|
|
102
100
|
});
|
|
@@ -119,19 +117,21 @@ describe('UserRouter', () => {
|
|
|
119
117
|
});
|
|
120
118
|
});
|
|
121
119
|
|
|
122
|
-
//
|
|
120
|
+
// Id is the wrong data type
|
|
123
121
|
it('should return a JSON object with an error message and a status code ' +
|
|
124
122
|
`of "${HttpStatusCodes.BAD_REQUEST}" if the user param was missing`,
|
|
125
123
|
done => {
|
|
124
|
+
const user = User.new();
|
|
125
|
+
user.id = ('5' as unknown as number);
|
|
126
126
|
agent
|
|
127
127
|
.put(Paths.Users.Update)
|
|
128
|
-
.send({ user
|
|
128
|
+
.send({ user })
|
|
129
129
|
.end((_, res: TRes) => {
|
|
130
130
|
expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST);
|
|
131
|
-
const errorObj =
|
|
132
|
-
|
|
133
|
-
expect(errorObj.
|
|
134
|
-
expect(
|
|
131
|
+
const errorObj = parseValidationErr(res.body.error);
|
|
132
|
+
expect(errorObj.message).toBe(ValidationError.MESSAGE);
|
|
133
|
+
expect(errorObj.errors[0].prop).toBe('user');
|
|
134
|
+
expect(errorObj.errors[0].children?.[0].prop).toBe('id');
|
|
135
135
|
done();
|
|
136
136
|
});
|
|
137
137
|
});
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { IParseObjectError } from 'jet-validators/utils';
|
|
2
|
+
|
|
1
3
|
import HttpStatusCodes from '@src/common/HttpStatusCodes';
|
|
2
4
|
|
|
3
5
|
|
|
4
6
|
/******************************************************************************
|
|
5
|
-
|
|
7
|
+
Classes
|
|
6
8
|
******************************************************************************/
|
|
7
9
|
|
|
8
10
|
/**
|
|
@@ -18,15 +20,17 @@ export class RouteError extends Error {
|
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
/**
|
|
21
|
-
*
|
|
23
|
+
* Handle "parseObj" errors.
|
|
22
24
|
*/
|
|
23
|
-
export class
|
|
24
|
-
|
|
25
|
+
export class ValidationError extends RouteError {
|
|
26
|
+
|
|
27
|
+
public static MESSAGE = 'The parseObj() function discovered one or ' +
|
|
28
|
+
'more errors.';
|
|
25
29
|
|
|
26
|
-
public constructor(
|
|
30
|
+
public constructor(errors: IParseObjectError[]) {
|
|
27
31
|
const msg = JSON.stringify({
|
|
28
|
-
message:
|
|
29
|
-
|
|
32
|
+
message: ValidationError.MESSAGE,
|
|
33
|
+
errors,
|
|
30
34
|
});
|
|
31
35
|
super(HttpStatusCodes.BAD_REQUEST, msg);
|
|
32
36
|
}
|
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
import { isString } from 'jet-validators';
|
|
2
|
+
import { parseObject, TParseOnError } from 'jet-validators/utils';
|
|
2
3
|
|
|
3
|
-
import
|
|
4
|
-
|
|
4
|
+
import { isRelationalKey, transIsDate } 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
|
+
* Check is a user object.
|
|
45
|
+
*/
|
|
46
|
+
function testUser(arg: unknown, errCb?: TParseOnError): arg is IUser {
|
|
47
|
+
return !!parseUser(arg, errCb);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Validate a user object.
|
|
52
|
+
*/
|
|
53
|
+
const parseUser = parseObject<IUser>({
|
|
24
54
|
id: isRelationalKey,
|
|
25
55
|
name: isString,
|
|
26
|
-
created: Date,
|
|
27
56
|
email: isString,
|
|
57
|
+
created: transIsDate,
|
|
28
58
|
});
|
|
29
59
|
|
|
30
60
|
|
|
@@ -32,4 +62,7 @@ const User = schema<IUser>({
|
|
|
32
62
|
Export default
|
|
33
63
|
******************************************************************************/
|
|
34
64
|
|
|
35
|
-
export default
|
|
65
|
+
export default {
|
|
66
|
+
new: newUser,
|
|
67
|
+
test: testUser,
|
|
68
|
+
} as const;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"users":[{"id":
|
|
1
|
+
{"users":[{"id":937353914582,"name":"Bill","created":"2025-03-10T08:02:45.482Z","email":"sean.maxwell@gmail.com"},{"id":564064331076,"name":"John Smith","created":"2025-03-10T08:02:45.482Z","email":"john.smith@gmail.com"},{"id":491773368070,"name":"Gordan Freeman","created":"2025-03-10T08:02:45.482Z","email":"gordan.freeman@gmail.com"}]}
|
|
@@ -5,7 +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
|
-
import {
|
|
8
|
+
import { IReq, IRes, parseReq } from './common';
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
/******************************************************************************
|
|
@@ -1,78 +1,28 @@
|
|
|
1
1
|
import { Response, Request } from 'express';
|
|
2
|
-
import { isObject, isString } from 'jet-validators';
|
|
3
2
|
import { parseObject, TSchema } from 'jet-validators/utils';
|
|
4
3
|
|
|
5
|
-
import {
|
|
4
|
+
import { ValidationError } from '@src/common/route-errors';
|
|
6
5
|
|
|
7
6
|
|
|
8
7
|
/******************************************************************************
|
|
9
|
-
|
|
10
|
-
******************************************************************************/
|
|
11
|
-
|
|
12
|
-
type TRecord = Record<string, unknown>;
|
|
13
|
-
export type IReq = Request<TRecord, void, TRecord, TRecord>;
|
|
14
|
-
export type IRes = Response<unknown, TRecord>;
|
|
15
|
-
|
|
16
|
-
export type TParseReqErr = {
|
|
17
|
-
prop: string,
|
|
18
|
-
value: unknown,
|
|
19
|
-
moreInfo?: string,
|
|
20
|
-
} | string;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
/******************************************************************************
|
|
24
|
-
Functions
|
|
8
|
+
Functions
|
|
25
9
|
******************************************************************************/
|
|
26
10
|
|
|
27
11
|
/**
|
|
28
|
-
*
|
|
12
|
+
* Throw a "ParseObjError" when "parseObject" fails. Also extract a nested
|
|
13
|
+
* "ParseObjError" and add it to the nestedErrors array.
|
|
29
14
|
*/
|
|
30
15
|
export function parseReq<U extends TSchema>(schema: U) {
|
|
31
|
-
return (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
arg = { ...arg };
|
|
35
|
-
}
|
|
36
|
-
// Setup error callback
|
|
37
|
-
const errArr: TParseReqErr[] = [],
|
|
38
|
-
errCb = setupErrorCb(errArr);
|
|
39
|
-
// Run Tests
|
|
40
|
-
const retVal = parseObject<U>(schema, errCb)(arg);
|
|
41
|
-
if (errArr.length > 0) {
|
|
42
|
-
throw new ValidationErr(errArr);
|
|
43
|
-
}
|
|
44
|
-
// Return
|
|
45
|
-
return retVal;
|
|
46
|
-
};
|
|
16
|
+
return parseObject(schema, errors => {
|
|
17
|
+
throw new ValidationError(errors);
|
|
18
|
+
});
|
|
47
19
|
}
|
|
48
20
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
) {
|
|
58
|
-
// Initialize err
|
|
59
|
-
let err: TParseReqErr;
|
|
60
|
-
if (arguments.length === 1) {
|
|
61
|
-
err = prop;
|
|
62
|
-
} else {
|
|
63
|
-
err = { prop, value };
|
|
64
|
-
}
|
|
65
|
-
// Check if there's a "caught error"
|
|
66
|
-
if (isObject(err) && caughtErr !== undefined) {
|
|
67
|
-
let moreInfo;
|
|
68
|
-
if (!isString(caughtErr)) {
|
|
69
|
-
moreInfo = JSON.stringify(caughtErr);
|
|
70
|
-
} else {
|
|
71
|
-
moreInfo = caughtErr;
|
|
72
|
-
}
|
|
73
|
-
err.moreInfo = moreInfo;
|
|
74
|
-
}
|
|
75
|
-
// Add error to array
|
|
76
|
-
errArr.push(err);
|
|
77
|
-
};
|
|
78
|
-
}
|
|
21
|
+
|
|
22
|
+
/******************************************************************************
|
|
23
|
+
Types
|
|
24
|
+
******************************************************************************/
|
|
25
|
+
|
|
26
|
+
type TRecord = Record<string, unknown>;
|
|
27
|
+
export type IReq = Request<TRecord, void, TRecord, TRecord>;
|
|
28
|
+
export type IRes = Response<unknown, TRecord>;
|
|
@@ -35,7 +35,10 @@ if (ENV.NodeEnv === NodeEnvs.Dev) {
|
|
|
35
35
|
|
|
36
36
|
// Security
|
|
37
37
|
if (ENV.NodeEnv === NodeEnvs.Production) {
|
|
38
|
-
|
|
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
|
|
@@ -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 transIsDate = 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.18",
|
|
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
|
-
});
|