express-generator-typescript 2.7.4 → 2.7.6
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 +79 -0
- package/README.md +12 -79
- package/lib/project-files/eslint.config.ts +1 -1
- package/lib/project-files/gitignore +2 -1
- package/lib/project-files/package.json +5 -5
- package/lib/project-files/scripts/build.ts +7 -45
- package/lib/project-files/scripts/common/utils.ts +93 -0
- package/lib/project-files/src/common/constants/ENV.ts +3 -5
- package/lib/project-files/src/common/constants/HTTP_STATUS_CODES.ts +81 -0
- package/lib/project-files/src/common/constants/PATHS.ts +15 -0
- package/lib/project-files/src/common/constants/index.ts +6 -6
- package/lib/project-files/src/common/util/route-errors.ts +4 -3
- package/lib/project-files/src/common/util/validators.ts +0 -1
- package/lib/project-files/src/models/User.ts +5 -8
- package/lib/project-files/src/repos/MockOrm.ts +7 -6
- package/lib/project-files/src/repos/UserRepo.ts +6 -3
- package/lib/project-files/src/repos/database.json +1 -1
- package/lib/project-files/src/repos/database.test.json +1 -1
- package/lib/project-files/src/routes/UserRoutes.ts +5 -8
- package/lib/project-files/src/routes/common/types/index.ts +0 -1
- package/lib/project-files/src/routes/common/util/index.ts +0 -2
- package/lib/project-files/src/routes/index.ts +6 -8
- package/lib/project-files/src/server.ts +10 -8
- package/lib/project-files/src/services/UserService.ts +3 -6
- package/lib/project-files/tests/common/types/index.ts +2 -2
- package/lib/project-files/tests/common/util/index.ts +32 -15
- package/lib/project-files/tests/users.test.ts +25 -33
- package/package.json +1 -1
- package/README-npm +0 -20
- package/lib/project-files/src/common/constants/HttpStatusCodes.ts +0 -391
- package/lib/project-files/src/common/constants/Paths.ts +0 -11
- package/lib/project-files/tests/common/Paths.ts +0 -5
package/README-git
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img alt="express-generator-typescript" src="https://github.com/seanpmaxwell/express-generator-typescript/raw/master/express-typescript.png" width="420">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://www.npmjs.com/package/express-generator-typescript"><img src="https://img.shields.io/npm/v/express-generator-typescript.svg?style=for-the-badge&logo=npm" alt="NPM Version"></a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/express-generator-typescript"><img src="https://img.shields.io/npm/dm/express-generator-typescript.svg?style=for-the-badge" alt="NPM Downloads"></a>
|
|
8
|
+
<a href="https://www.npmjs.com/package/express-generator-typescript"><img src="https://img.shields.io/npm/l/express-generator-typescript.svg?style=for-the-badge" alt="License"></a>
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
# express-generator-typescript
|
|
12
|
+
|
|
13
|
+
Production-ready Express application scaffolding with TypeScript baked in. Generate a modern API starter in seconds that follows the [TypeScript best practices](https://github.com/seanpmaxwell/Typescript-Best-Practices/blob/main/README.md) used to build this tool.
|
|
14
|
+
|
|
15
|
+
## Overview
|
|
16
|
+
|
|
17
|
+
`express-generator-typescript` creates a new Express application similar to the classic `express-generator` package, but the generated project is fully wired for TypeScript from day one. You get strict typing, linting, hot reloading, production builds, testing utilities, and sane defaults that focus on APIs (no view engine or opinionated ORM). Path aliases are preconfigured through `tsconfig-paths` and `_moduleAliases`, so referencing modules stays clean even as the app grows.
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
- **TypeScript-first** – strict compiler settings, linting, and sensible tsconfig defaults ready to go.
|
|
22
|
+
- **API-centric** – no view engine or extra dependencies; ideal for SPAs, mobile backends, or services.
|
|
23
|
+
- **Productivity tooling** – includes nodemon, ts-node, hot reload scripts, Jest, ESLint, and production builds.
|
|
24
|
+
- **Path aliases** – aliases configured in `tsconfig.json` and `preload.js` so you can import modules cleanly.
|
|
25
|
+
- **Keeps dependencies lean** – no bundled ORM or UI layers; only the essentials for Express + TS development.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npx express-generator-typescript
|
|
31
|
+
# or install globally
|
|
32
|
+
npm install -g express-generator-typescript
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Quick Start
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# generate a project (defaults to express-gen-ts)
|
|
39
|
+
npx express-generator-typescript my-api
|
|
40
|
+
|
|
41
|
+
cd my-api
|
|
42
|
+
|
|
43
|
+
# start developing at http://localhost:3000
|
|
44
|
+
npm run dev
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Use `--use-yarn` if you prefer Yarn over npm. If you omit the project name, the generator creates `express-gen-ts`.
|
|
48
|
+
|
|
49
|
+
## CLI Options
|
|
50
|
+
|
|
51
|
+
| Option | Description |
|
|
52
|
+
| ----------------- | --------------------------------------------------------------------------- |
|
|
53
|
+
| `project name` | Folder to create. Defaults to `express-gen-ts` if omitted. |
|
|
54
|
+
| `--use-yarn` | Installs dependencies with Yarn instead of npm. |
|
|
55
|
+
| `--version` | Prints the generator version. |
|
|
56
|
+
|
|
57
|
+
> The historical `--with-auth` switch was removed in v2.5+. For an auth-ready example see the [express-jsonwebtoken-demo](https://github.com/seanpmaxwell/express-jsonwebtoken-demo) project.
|
|
58
|
+
|
|
59
|
+
## Sample Project
|
|
60
|
+
|
|
61
|
+
The generated code includes CRUD routes for illustrative `User` resources to demonstrate controllers, services, and routing patterns in Express + TypeScript. Replace these samples with your own modules after scaffolding.
|
|
62
|
+
|
|
63
|
+
## Available Scripts
|
|
64
|
+
|
|
65
|
+
- `npm run dev` / `npm run dev:hot` – Run the server with live reload.
|
|
66
|
+
- `npm run test` / `npm run test:hot` – Execute all Jest tests, optionally with watch mode.
|
|
67
|
+
- `npm run test -- users.test.ts` – Target a single test file.
|
|
68
|
+
- `npm run lint` – Run ESLint checks.
|
|
69
|
+
- `npm run build` – Compile the project for production.
|
|
70
|
+
- `npm start` – Serve the built project.
|
|
71
|
+
- `npm run type-check` – Run the TypeScript compiler without emitting files.
|
|
72
|
+
|
|
73
|
+
## Debugging
|
|
74
|
+
|
|
75
|
+
Development uses `nodemon` so the server restarts when files change. To enable the Node.js inspector, edit `nodemonConfig` in `package.json` (and `spec/nodemon.json` for tests) and change the `exec` value from `ts-node` to `node --inspect -r ts-node/register`.
|
|
76
|
+
|
|
77
|
+
## License
|
|
78
|
+
|
|
79
|
+
[MIT](LICENSE)
|
package/README.md
CHANGED
|
@@ -1,85 +1,18 @@
|
|
|
1
|
-
<
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img alt="express-generator-typescript" src="https://github.com/seanpmaxwell/express-generator-typescript/raw/master/express-typescript.png" width="420">
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://www.npmjs.com/package/express-generator-typescript"><img src="https://img.shields.io/npm/v/express-generator-typescript.svg?style=for-the-badge&logo=npm" alt="NPM Version"></a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/express-generator-typescript"><img src="https://img.shields.io/npm/dm/express-generator-typescript.svg?style=for-the-badge" alt="NPM Downloads"></a>
|
|
8
|
+
<a href="https://www.npmjs.com/package/express-generator-typescript"><img src="https://img.shields.io/npm/l/express-generator-typescript.svg?style=for-the-badge" alt="License"></a>
|
|
9
|
+
</p>
|
|
4
10
|
|
|
5
|
-
|
|
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>
|
|
11
|
+
# express-generator-typescript
|
|
8
12
|
|
|
13
|
+
Production-ready Express application scaffolding with TypeScript baked in. Generate a modern API starter in seconds that follows the [TypeScript best practices](https://github.com/seanpmaxwell/Typescript-Best-Practices/blob/main/README.md) used to build this tool.
|
|
9
14
|
|
|
10
|
-
## What is it?
|
|
11
15
|
|
|
12
|
-
|
|
16
|
+
## Documenation
|
|
13
17
|
|
|
14
|
-
|
|
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 run test -- "name of test file" (i.e. users.test.ts)`.
|
|
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
|
-
<br/>
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
Happy web deving :)
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
## License
|
|
84
|
-
|
|
85
|
-
[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.
|
|
@@ -2,7 +2,7 @@ import eslint from '@eslint/js';
|
|
|
2
2
|
import tseslint from 'typescript-eslint';
|
|
3
3
|
import stylistic from '@stylistic/eslint-plugin';
|
|
4
4
|
import nodePlugin from 'eslint-plugin-n';
|
|
5
|
-
import { defineConfig } from 'eslint/config'
|
|
5
|
+
import { defineConfig } from 'eslint/config';
|
|
6
6
|
|
|
7
7
|
export default defineConfig(
|
|
8
8
|
eslint.configs.recommended,
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"lint": "eslint .",
|
|
10
10
|
"start": "NODE_ENV=production node -r ./config.js ./dist",
|
|
11
11
|
"test": "NODE_ENV=test vitest",
|
|
12
|
-
"type-check": "tsc --noEmit"
|
|
12
|
+
"type-check": "tsc -b --noEmit"
|
|
13
13
|
},
|
|
14
14
|
"engines": {
|
|
15
15
|
"node": ">=16.0.0"
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"helmet": "^8.1.0",
|
|
23
23
|
"inserturlparams": "^2.0.5",
|
|
24
24
|
"jet-env": "^1.1.4",
|
|
25
|
-
"jet-logger": "^2.
|
|
26
|
-
"jet-paths": "^
|
|
27
|
-
"jet-validators": "^1.
|
|
25
|
+
"jet-logger": "^2.1.1",
|
|
26
|
+
"jet-paths": "^2.0.1",
|
|
27
|
+
"jet-validators": "^1.6.5",
|
|
28
28
|
"jsonfile": "^6.1.0",
|
|
29
29
|
"module-alias": "^2.2.3",
|
|
30
30
|
"morgan": "^1.10.0"
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"ts-node": "^10.9.2",
|
|
52
52
|
"tsconfig-paths": "^4.2.0",
|
|
53
53
|
"typescript": "^5.8.3",
|
|
54
|
-
"typescript-eslint": "^8.
|
|
54
|
+
"typescript-eslint": "^8.32.0",
|
|
55
55
|
"vitest": "^3.1.2"
|
|
56
56
|
}
|
|
57
57
|
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import fs from 'fs-extra';
|
|
2
1
|
import logger from 'jet-logger';
|
|
3
|
-
import
|
|
2
|
+
import { copy, copyFilesRec, exec, remove } from './common/utils';
|
|
4
3
|
|
|
4
|
+
/******************************************************************************
|
|
5
|
+
Run
|
|
6
|
+
******************************************************************************/
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Start
|
|
@@ -10,12 +12,10 @@ import childProcess from 'child_process';
|
|
|
10
12
|
try {
|
|
11
13
|
// Remove current build
|
|
12
14
|
await remove('./dist/');
|
|
13
|
-
await exec('npm run lint', '
|
|
14
|
-
await exec('tsc --
|
|
15
|
+
await exec('npm run lint', '../');
|
|
16
|
+
await exec('tsc --project tsconfig.prod.json', '../');
|
|
15
17
|
// Copy
|
|
16
|
-
await
|
|
17
|
-
await copy('./src/views', './dist/views');
|
|
18
|
-
await copy('./src/repos/database.json', './dist/repos/database.json');
|
|
18
|
+
await copyFilesRec('./src', './dist', ['.ts']);
|
|
19
19
|
await copy('./temp/config.js', './config.js');
|
|
20
20
|
await copy('./temp/src', './dist');
|
|
21
21
|
await remove('./temp/');
|
|
@@ -26,41 +26,3 @@ import childProcess from 'child_process';
|
|
|
26
26
|
}
|
|
27
27
|
})();
|
|
28
28
|
|
|
29
|
-
/**
|
|
30
|
-
* Remove file
|
|
31
|
-
*/
|
|
32
|
-
function remove(loc: string): Promise<void> {
|
|
33
|
-
return new Promise((res, rej) => {
|
|
34
|
-
return fs.remove(loc, err => {
|
|
35
|
-
return (!!err ? rej(err) : res());
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Copy file.
|
|
42
|
-
*/
|
|
43
|
-
function copy(src: string, dest: string): Promise<void> {
|
|
44
|
-
return new Promise((res, rej) => {
|
|
45
|
-
return fs.copy(src, dest, err => {
|
|
46
|
-
return (!!err ? rej(err) : res());
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Do command line command.
|
|
53
|
-
*/
|
|
54
|
-
function exec(cmd: string, loc: string): Promise<void> {
|
|
55
|
-
return new Promise((res, rej) => {
|
|
56
|
-
return childProcess.exec(cmd, {cwd: loc}, (err, stdout, stderr) => {
|
|
57
|
-
if (!!stdout) {
|
|
58
|
-
logger.info(stdout);
|
|
59
|
-
}
|
|
60
|
-
if (!!stderr) {
|
|
61
|
-
logger.warn(stderr);
|
|
62
|
-
}
|
|
63
|
-
return (!!err ? rej(err) : res());
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import logger from 'jet-logger';
|
|
3
|
+
import childProcess from 'child_process';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
|
|
6
|
+
/******************************************************************************
|
|
7
|
+
Functions
|
|
8
|
+
*******************************************************************************
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Copy file.
|
|
12
|
+
*/
|
|
13
|
+
export function copy(src: string, dest: string): Promise<void> {
|
|
14
|
+
return new Promise((res, rej) => {
|
|
15
|
+
return fs.copy(src, dest, err => {
|
|
16
|
+
return (!!err ? rej(err) : res());
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Remove file
|
|
23
|
+
*/
|
|
24
|
+
export function remove(loc: string): Promise<void> {
|
|
25
|
+
return new Promise((res, rej) => {
|
|
26
|
+
return fs.remove(loc, err => {
|
|
27
|
+
return (!!err ? rej(err) : res());
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Run command-line command.
|
|
34
|
+
*/
|
|
35
|
+
export function exec(cmd: string, loc = './'): Promise<string> {
|
|
36
|
+
// Setup directory
|
|
37
|
+
const callingDir = path.dirname(require.main?.filename ?? './'),
|
|
38
|
+
cwd = path.join(callingDir, loc);
|
|
39
|
+
// Wrap in promise
|
|
40
|
+
return new Promise((res, rej) =>
|
|
41
|
+
childProcess.exec(cmd, { cwd }, (err, stdout, stderr) => {
|
|
42
|
+
if (!!stdout) {
|
|
43
|
+
logger.info(stdout);
|
|
44
|
+
}
|
|
45
|
+
if (!!stderr) {
|
|
46
|
+
logger.err(stderr);
|
|
47
|
+
}
|
|
48
|
+
if (!!err) {
|
|
49
|
+
return rej(err);
|
|
50
|
+
} else if (!!stderr) {
|
|
51
|
+
return rej(new Error(stderr));
|
|
52
|
+
} else {
|
|
53
|
+
return res(stdout);
|
|
54
|
+
}
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Recursively search for a folder for all files and copy them to the new
|
|
61
|
+
* destination keeping their paths relative to the "src" param.
|
|
62
|
+
*
|
|
63
|
+
* Example:
|
|
64
|
+
* If file "./src/foo/bar.txt" exists then:
|
|
65
|
+
* copyFilesRec('./src', './dest') => ["./dest/foo/bar.txt"]
|
|
66
|
+
*
|
|
67
|
+
* @param excludeExt Exlude a list of file extensions.
|
|
68
|
+
*/
|
|
69
|
+
export async function copyFilesRec(
|
|
70
|
+
src: string,
|
|
71
|
+
dest: string,
|
|
72
|
+
excludeExt?: string[],
|
|
73
|
+
): Promise<void> {
|
|
74
|
+
const copyReqs: Promise<void>[] = [];
|
|
75
|
+
try {
|
|
76
|
+
const entries = await fs.readdir(src, { withFileTypes: true });
|
|
77
|
+
for (const entry of entries) {
|
|
78
|
+
const currPath = src + '/' + entry.name,
|
|
79
|
+
currDest = dest + '/' + entry.name,
|
|
80
|
+
ext = path.extname(entry.name);
|
|
81
|
+
if (entry.isFile() && !excludeExt?.includes(ext)) {
|
|
82
|
+
const req = fs.copy(currPath, currDest);
|
|
83
|
+
copyReqs.push(req);
|
|
84
|
+
} else if (entry.isDirectory()) {
|
|
85
|
+
const req = copyFilesRec(currPath, currDest, excludeExt);
|
|
86
|
+
copyReqs.push(req);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
} catch (error) {
|
|
90
|
+
logger.err(`Error searching in ${src}: ` + String(error));
|
|
91
|
+
}
|
|
92
|
+
await Promise.all(copyReqs);
|
|
93
|
+
}
|
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import jetEnv, { num } from 'jet-env';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { NodeEnvs } from '.';
|
|
2
|
+
import { isValueOf } from 'jet-validators';
|
|
5
3
|
|
|
4
|
+
import { NODE_ENVS } from '.';
|
|
6
5
|
|
|
7
6
|
/******************************************************************************
|
|
8
7
|
Setup
|
|
9
8
|
******************************************************************************/
|
|
10
9
|
|
|
11
10
|
const ENV = jetEnv({
|
|
12
|
-
NodeEnv:
|
|
11
|
+
NodeEnv: isValueOf(NODE_ENVS),
|
|
13
12
|
Port: num,
|
|
14
13
|
});
|
|
15
14
|
|
|
16
|
-
|
|
17
15
|
/******************************************************************************
|
|
18
16
|
Export default
|
|
19
17
|
******************************************************************************/
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/* eslint-disable max-len */
|
|
2
|
+
|
|
3
|
+
import { ValueOf } from 'jet-validators';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Hypertext Transfer Protocol (HTTP) response status codes.
|
|
8
|
+
* @see {@link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes}
|
|
9
|
+
*
|
|
10
|
+
* This file was copied from here: https://gist.github.com/scokmen/f813c904ef79022e84ab2409574d1b45
|
|
11
|
+
*/
|
|
12
|
+
const HTTP_STATUS_CODES = {
|
|
13
|
+
Continue: 100,
|
|
14
|
+
SwitchingProtocols: 101,
|
|
15
|
+
Processing: 102,
|
|
16
|
+
Ok: 200,
|
|
17
|
+
Created: 201,
|
|
18
|
+
Accepted: 202,
|
|
19
|
+
NonAuthoritativeInformation: 203,
|
|
20
|
+
NoContent: 204,
|
|
21
|
+
ResetContent: 205,
|
|
22
|
+
PartialContent: 206,
|
|
23
|
+
MultiStatus: 207,
|
|
24
|
+
AlreadyReported: 208,
|
|
25
|
+
ImUsed: 226,
|
|
26
|
+
MultipleChoices: 300,
|
|
27
|
+
MovedPermanently: 301,
|
|
28
|
+
Found: 302,
|
|
29
|
+
SeeOther: 303,
|
|
30
|
+
NotModified: 304,
|
|
31
|
+
UseProxy: 305,
|
|
32
|
+
SwitchProxy: 306,
|
|
33
|
+
TemporaryRedirect: 307,
|
|
34
|
+
PermanentRedirect: 308,
|
|
35
|
+
BadRequest: 400,
|
|
36
|
+
Unauthorized: 401,
|
|
37
|
+
PaymentRequired: 402,
|
|
38
|
+
Forbidden: 403,
|
|
39
|
+
NotFound: 404,
|
|
40
|
+
MethodNotAllowed: 405,
|
|
41
|
+
NotAcceptable: 406,
|
|
42
|
+
ProxyAuthenticationRequired: 407,
|
|
43
|
+
RequestTimeout: 408,
|
|
44
|
+
Conflict: 409,
|
|
45
|
+
Gone: 410,
|
|
46
|
+
LengthRequired: 411,
|
|
47
|
+
PreconditionFailed: 412,
|
|
48
|
+
PayloadTooLarge: 413,
|
|
49
|
+
UriTooLong: 414,
|
|
50
|
+
UnsupportedMediaType: 415,
|
|
51
|
+
RangeNotSatisfiable: 416,
|
|
52
|
+
ExpectationFailed: 417,
|
|
53
|
+
IamaTeapot: 418,
|
|
54
|
+
MisdirectedRequest: 421,
|
|
55
|
+
UnprocessableEntity: 422,
|
|
56
|
+
Locked: 423,
|
|
57
|
+
FailedDependency: 424,
|
|
58
|
+
UpgradeRequired: 426,
|
|
59
|
+
PreconditionRequired: 428,
|
|
60
|
+
TooManyRequests: 429,
|
|
61
|
+
RequestHeaderFieldsTooLarge: 431,
|
|
62
|
+
UnavailableForLegalReasons: 451,
|
|
63
|
+
InternalServerError: 500,
|
|
64
|
+
NotImplemented: 501,
|
|
65
|
+
BadGateway: 502,
|
|
66
|
+
ServiceUnavailable: 503,
|
|
67
|
+
GatewayTimeout: 504,
|
|
68
|
+
HttpVersionNotSupported: 505,
|
|
69
|
+
VariantAlsoNegotiates: 506,
|
|
70
|
+
InsuffientStorage: 507,
|
|
71
|
+
LoopDetected: 508,
|
|
72
|
+
NotExtended: 510,
|
|
73
|
+
NetworkAuthenticationRequired: 511,
|
|
74
|
+
} as const;
|
|
75
|
+
|
|
76
|
+
/******************************************************************************
|
|
77
|
+
Export default
|
|
78
|
+
******************************************************************************/
|
|
79
|
+
|
|
80
|
+
export type HttpStatusCodes = ValueOf<typeof HTTP_STATUS_CODES>;
|
|
81
|
+
export default HTTP_STATUS_CODES;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import jetPaths from 'jet-paths';
|
|
2
|
+
|
|
3
|
+
const PATHS = {
|
|
4
|
+
_: '/api',
|
|
5
|
+
Users: {
|
|
6
|
+
_: '/users',
|
|
7
|
+
Get: '/all',
|
|
8
|
+
Add: '/add',
|
|
9
|
+
Update: '/update',
|
|
10
|
+
Delete: '/delete/:id',
|
|
11
|
+
},
|
|
12
|
+
} as const;
|
|
13
|
+
|
|
14
|
+
export const JET_PATHS = jetPaths(PATHS);
|
|
15
|
+
export default PATHS;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
|
|
2
2
|
/******************************************************************************
|
|
3
|
-
|
|
3
|
+
Constants
|
|
4
4
|
******************************************************************************/
|
|
5
5
|
|
|
6
6
|
// NOTE: These need to match the names of your ".env" files
|
|
7
|
-
export
|
|
8
|
-
Dev
|
|
9
|
-
Test
|
|
10
|
-
Production
|
|
11
|
-
}
|
|
7
|
+
export const NODE_ENVS = {
|
|
8
|
+
Dev: 'development',
|
|
9
|
+
Test: 'test',
|
|
10
|
+
Production: 'production',
|
|
11
|
+
} as const;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { IParseObjectError } from 'jet-validators/utils';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
|
|
3
|
+
import HTTP_STATUS_CODES, {
|
|
4
|
+
HttpStatusCodes,
|
|
5
|
+
} from '@src/common/constants/HTTP_STATUS_CODES';
|
|
5
6
|
|
|
6
7
|
/******************************************************************************
|
|
7
8
|
Classes
|
|
@@ -32,6 +33,6 @@ export class ValidationError extends RouteError {
|
|
|
32
33
|
message: ValidationError.MESSAGE,
|
|
33
34
|
errors,
|
|
34
35
|
});
|
|
35
|
-
super(
|
|
36
|
+
super(HTTP_STATUS_CODES.BadRequest, msg);
|
|
36
37
|
}
|
|
37
38
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { isNumber, isDate } from 'jet-validators';
|
|
2
2
|
import { transform } from 'jet-validators/utils';
|
|
3
3
|
|
|
4
|
-
|
|
5
4
|
/******************************************************************************
|
|
6
5
|
Functions
|
|
7
6
|
******************************************************************************/
|
|
@@ -4,18 +4,16 @@ import { parseObject, TParseOnError } from 'jet-validators/utils';
|
|
|
4
4
|
import { isRelationalKey, transIsDate } from '@src/common/util/validators';
|
|
5
5
|
import { IModel } from './common/types';
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
/******************************************************************************
|
|
9
8
|
Constants
|
|
10
9
|
******************************************************************************/
|
|
11
10
|
|
|
12
|
-
const DEFAULT_USER_VALS
|
|
11
|
+
const DEFAULT_USER_VALS: IUser = {
|
|
13
12
|
id: -1,
|
|
14
13
|
name: '',
|
|
15
14
|
created: new Date(),
|
|
16
15
|
email: '',
|
|
17
|
-
}
|
|
18
|
-
|
|
16
|
+
} as const;
|
|
19
17
|
|
|
20
18
|
/******************************************************************************
|
|
21
19
|
Types
|
|
@@ -26,7 +24,6 @@ export interface IUser extends IModel {
|
|
|
26
24
|
email: string;
|
|
27
25
|
}
|
|
28
26
|
|
|
29
|
-
|
|
30
27
|
/******************************************************************************
|
|
31
28
|
Setup
|
|
32
29
|
******************************************************************************/
|
|
@@ -39,7 +36,6 @@ const parseUser = parseObject<IUser>({
|
|
|
39
36
|
created: transIsDate,
|
|
40
37
|
});
|
|
41
38
|
|
|
42
|
-
|
|
43
39
|
/******************************************************************************
|
|
44
40
|
Functions
|
|
45
41
|
******************************************************************************/
|
|
@@ -48,7 +44,9 @@ const parseUser = parseObject<IUser>({
|
|
|
48
44
|
* New user object.
|
|
49
45
|
*/
|
|
50
46
|
function __new__(user?: Partial<IUser>): IUser {
|
|
51
|
-
const
|
|
47
|
+
const defaults = { ...DEFAULT_USER_VALS };
|
|
48
|
+
defaults.created = new Date();
|
|
49
|
+
const retVal = { ...defaults, ...user };
|
|
52
50
|
return parseUser(retVal, errors => {
|
|
53
51
|
throw new Error('Setup new user failed ' + JSON.stringify(errors, null, 2));
|
|
54
52
|
});
|
|
@@ -61,7 +59,6 @@ function test(arg: unknown, errCb?: TParseOnError): arg is IUser {
|
|
|
61
59
|
return !!parseUser(arg, errCb);
|
|
62
60
|
}
|
|
63
61
|
|
|
64
|
-
|
|
65
62
|
/******************************************************************************
|
|
66
63
|
Export default
|
|
67
64
|
******************************************************************************/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import jsonfile from 'jsonfile';
|
|
2
2
|
|
|
3
3
|
import ENV from '@src/common/constants/ENV';
|
|
4
|
-
import {
|
|
4
|
+
import { NODE_ENVS } from '@src/common/constants';
|
|
5
5
|
import { IUser } from '@src/models/User';
|
|
6
6
|
|
|
7
7
|
|
|
@@ -10,7 +10,7 @@ import { IUser } from '@src/models/User';
|
|
|
10
10
|
******************************************************************************/
|
|
11
11
|
|
|
12
12
|
const DB_FILE_NAME = (
|
|
13
|
-
ENV.NodeEnv ===
|
|
13
|
+
ENV.NodeEnv === NODE_ENVS.Test
|
|
14
14
|
? 'database.test.json'
|
|
15
15
|
: 'database.json'
|
|
16
16
|
);
|
|
@@ -20,7 +20,7 @@ const DB_FILE_NAME = (
|
|
|
20
20
|
Types
|
|
21
21
|
******************************************************************************/
|
|
22
22
|
|
|
23
|
-
interface
|
|
23
|
+
interface IDatabase {
|
|
24
24
|
users: IUser[];
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -32,14 +32,15 @@ interface IDb {
|
|
|
32
32
|
/**
|
|
33
33
|
* Fetch the json from the file.
|
|
34
34
|
*/
|
|
35
|
-
function openDb(): Promise<
|
|
36
|
-
return jsonfile.readFile(__dirname + '/' + DB_FILE_NAME) as
|
|
35
|
+
function openDb(): Promise<IDatabase> {
|
|
36
|
+
return jsonfile.readFile(__dirname + '/' + DB_FILE_NAME) as
|
|
37
|
+
Promise<IDatabase>;
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
/**
|
|
40
41
|
* Update the file.
|
|
41
42
|
*/
|
|
42
|
-
function saveDb(db:
|
|
43
|
+
function saveDb(db: IDatabase): Promise<void> {
|
|
43
44
|
return jsonfile.writeFile((__dirname + '/' + DB_FILE_NAME), db);
|
|
44
45
|
}
|
|
45
46
|
|