express-generator-typescript 3.0.4 → 3.1.0
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.md +1 -1
- package/bin/cli.js +1 -20
- package/lib/template/README.md +3 -2
- package/lib/template/package.json +5 -5
- package/lib/template/src/common/classes/route-errors.ts +0 -13
- package/lib/template/src/common/constants/Paths/Paths.ts +29 -0
- package/lib/template/src/common/constants/Paths/helpers.ts +29 -0
- package/lib/template/src/common/constants/Paths/index.ts +1 -0
- package/lib/template/src/common/constants/{environment-consts.ts → env.constants.ts} +13 -15
- package/lib/template/src/common/utils/{date-utils.ts → date.utils.ts} +10 -12
- package/lib/template/src/common/utils/validators.ts +0 -23
- package/lib/template/src/entities/User/module.ts +77 -28
- package/lib/template/src/entities/common/entity-base-types.ts +1 -1
- package/lib/template/src/main.ts +3 -3
- package/lib/template/src/public/scripts/lib/bootstrap.bundle.min.js +0 -1
- package/lib/template/src/public/scripts/users.js +19 -10
- package/lib/template/src/public/stylesheets/lib/bootstrap.min.css +0 -1
- package/lib/template/src/public/stylesheets/users.css +24 -1
- package/lib/template/src/repos/MockOrm.ts +2 -2
- package/lib/template/src/repos/UserRepo.ts +5 -15
- package/lib/template/src/repos/common/database.json +3 -3
- package/lib/template/src/{routes/apiRouter.ts → routers/api.router.ts} +3 -11
- package/lib/template/src/{routes/UserRoutes.ts → routers/user.router.ts} +37 -46
- package/lib/template/src/server.ts +20 -7
- package/lib/template/src/views/users.html +8 -0
- package/lib/template/tests/support/utils.ts +23 -0
- package/lib/template/tests/users.test.ts +99 -50
- package/package.json +6 -5
- package/lib/template/src/common/constants/Paths.ts +0 -16
- package/lib/template/src/entities/User/_internal/validators.ts +0 -47
- package/lib/template/src/routes/common/express-types.ts +0 -9
- package/lib/template/src/routes/common/parseReq.ts +0 -23
- package/lib/template/tests/common/comparators.ts +0 -36
- package/lib/template/tests/common/supertest-types.ts +0 -11
package/README.md
CHANGED
package/bin/cli.js
CHANGED
|
@@ -3,14 +3,13 @@ const path = require('path');
|
|
|
3
3
|
const { parseArgs } = require('util');
|
|
4
4
|
|
|
5
5
|
const expressGenTs = require('../lib/express-generator-typescript');
|
|
6
|
-
const { version
|
|
6
|
+
const { version } = require('../package.json');
|
|
7
7
|
|
|
8
8
|
// ========================================================================= //
|
|
9
9
|
// CONSTANTS //
|
|
10
10
|
// ========================================================================= //
|
|
11
11
|
|
|
12
12
|
const DEFAULT_DEST = 'express-gen-ts';
|
|
13
|
-
const MIN_NODE = engines.node.replace(/^>=/, '');
|
|
14
13
|
|
|
15
14
|
const HELP = `Usage: express-generator-typescript [project-name] [options]
|
|
16
15
|
|
|
@@ -62,7 +61,6 @@ async function main() {
|
|
|
62
61
|
if (positionals.length > 1) {
|
|
63
62
|
throw new Error('Expected at most one project name.\n\n' + HELP);
|
|
64
63
|
}
|
|
65
|
-
assertNodeVersion();
|
|
66
64
|
|
|
67
65
|
// ---- Run "express-generator-typescript"
|
|
68
66
|
const dest = path.resolve(process.cwd(), positionals[0] ?? DEFAULT_DEST);
|
|
@@ -73,20 +71,3 @@ async function main() {
|
|
|
73
71
|
});
|
|
74
72
|
console.log('Project setup complete!');
|
|
75
73
|
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* The generated project's tooling (vitest, eslint) requires a recent Node, so
|
|
79
|
-
* fail fast instead of producing a project that can't run.
|
|
80
|
-
*
|
|
81
|
-
* @returns {void}
|
|
82
|
-
*/
|
|
83
|
-
function assertNodeVersion() {
|
|
84
|
-
const toParts = (v) => v.split('.').map(Number);
|
|
85
|
-
const [maj, min] = toParts(process.versions.node);
|
|
86
|
-
const [reqMaj, reqMin] = toParts(MIN_NODE);
|
|
87
|
-
if (maj < reqMaj || (maj === reqMaj && min < reqMin)) {
|
|
88
|
-
throw new Error(
|
|
89
|
-
`Node ${MIN_NODE} or newer is required (found ${process.versions.node}).`,
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
}
|
package/lib/template/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
## About
|
|
2
2
|
|
|
3
|
-
This project was created with [express-generator-typescript](https://github.com/seanpmaxwell/express-generator-typescript). It requires Node.js 22.
|
|
3
|
+
This project was created with [express-generator-typescript](https://github.com/seanpmaxwell/express-generator-typescript). It requires Node.js 22.13+ (22.x), 24.x, or 26+.
|
|
4
4
|
|
|
5
5
|
The original template follows the [TypeScript best practices](https://github.com/seanpmaxwell/Typescript-Best-Practices).
|
|
6
6
|
|
|
@@ -52,7 +52,8 @@ Delete `node_modules/` and `package-lock.json`, then reinstall all dependencies.
|
|
|
52
52
|
- **Logging**
|
|
53
53
|
- **Request logging**: [morgan](https://github.com/expressjs/morgan) (development only)
|
|
54
54
|
- **General logging**: [jet-logger](https://github.com/seanpmaxwell/jet-logger)
|
|
55
|
-
- **Validation**: [
|
|
55
|
+
- **Validation**: [zod](https://github.com/colinhacks/zod)
|
|
56
|
+
- **Middleware Validation**: [express-zod-safe](https://github.com/AngaBlue/express-zod-safe)
|
|
56
57
|
- **Environment variables**: [dotenv](https://github.com/motdotla/dotenv) loads `config/.env.*`
|
|
57
58
|
- **Reloading**: [tsx](https://tsx.hirok.io) (`tsx watch` restarts the server) and [livereload](https://github.com/napcs/node-livereload) + [connect-livereload](https://github.com/intesso/connect-livereload) (refreshes the browser)
|
|
58
59
|
- **Testing**: [Vitest](https://vitest.dev) + [Supertest](https://github.com/ladjs/supertest)
|
|
@@ -14,19 +14,19 @@
|
|
|
14
14
|
"typecheck": "tsc -b --noEmit"
|
|
15
15
|
},
|
|
16
16
|
"engines": {
|
|
17
|
-
"node": "
|
|
17
|
+
"node": "^22.13.0 || ^24.0.0 || >=26.0.0"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"cross-env": "^10.1.0",
|
|
21
21
|
"dotenv": "^18.0.4",
|
|
22
22
|
"express": "^5.2.1",
|
|
23
|
+
"express-zod-safe": "^3.2.1",
|
|
23
24
|
"helmet": "^8.3.0",
|
|
24
|
-
"jet-id": "^1.3.1",
|
|
25
25
|
"jet-logger": "3.0.0",
|
|
26
|
-
"jet-paths": "^4.1.3",
|
|
27
|
-
"jet-validators": "^2.3.1",
|
|
28
26
|
"jsonfile": "^6.2.1",
|
|
29
|
-
"morgan": "^1.12.1"
|
|
27
|
+
"morgan": "^1.12.1",
|
|
28
|
+
"uuid": "^14.0.2",
|
|
29
|
+
"zod": "^4.6.5"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@eslint/js": "^10.0.1",
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type { ParseError } from 'jet-validators/utils';
|
|
2
|
-
|
|
3
1
|
import HttpStatusCodes from '@src/common/constants/HttpStatusCodes';
|
|
4
2
|
|
|
5
3
|
// ========================================================================= //
|
|
@@ -33,14 +31,3 @@ export class NotFoundError extends RouteError {
|
|
|
33
31
|
super(HttpStatusCodes.NOT_FOUND, message);
|
|
34
32
|
}
|
|
35
33
|
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Request data failed schema validation.
|
|
39
|
-
*/
|
|
40
|
-
export class ValidationError extends RouteError {
|
|
41
|
-
public static MESSAGE = 'Request validation failed.';
|
|
42
|
-
|
|
43
|
-
public constructor(errors: ParseError[]) {
|
|
44
|
-
super(HttpStatusCodes.BAD_REQUEST, ValidationError.MESSAGE, errors);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ep, pp } from './helpers';
|
|
2
|
+
|
|
3
|
+
// ========================================================================= //
|
|
4
|
+
// CONSTANTS //
|
|
5
|
+
// ========================================================================= //
|
|
6
|
+
|
|
7
|
+
const BASE = '/api';
|
|
8
|
+
const USERS_BASE = '/users';
|
|
9
|
+
const USERS = BASE + USERS_BASE;
|
|
10
|
+
|
|
11
|
+
const Paths = {
|
|
12
|
+
PATH: BASE,
|
|
13
|
+
Users: {
|
|
14
|
+
PATH: USERS_BASE,
|
|
15
|
+
Get: ep(USERS, '/all', 'GET'),
|
|
16
|
+
Add: ep(USERS, '/add', 'POST'),
|
|
17
|
+
Update: ep(USERS, '/update', 'PUT'),
|
|
18
|
+
Delete: {
|
|
19
|
+
...ep(USERS, `/delete/:id`, 'DELETE'),
|
|
20
|
+
format: (id: string) => `${USERS}/delete/${pp(id)}`,
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
// ========================================================================= //
|
|
26
|
+
// EXPORT //
|
|
27
|
+
// ========================================================================= //
|
|
28
|
+
|
|
29
|
+
export default Paths;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// ========================================================================= //
|
|
2
|
+
// TYPES //
|
|
3
|
+
// ========================================================================= //
|
|
4
|
+
|
|
5
|
+
type HttpVerb = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
6
|
+
|
|
7
|
+
// ========================================================================= //
|
|
8
|
+
// FUNCTIONS //
|
|
9
|
+
// ========================================================================= //
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `ep` stands for "Endpoint"
|
|
13
|
+
*/
|
|
14
|
+
export function ep<V extends HttpVerb>(base: string, path: string, verb: V) {
|
|
15
|
+
const template = `${base}${path}`;
|
|
16
|
+
return {
|
|
17
|
+
PATH: path,
|
|
18
|
+
FULL_PATH: template,
|
|
19
|
+
VERB: verb,
|
|
20
|
+
DISPLAY: `<${verb}>${template}`,
|
|
21
|
+
} as const;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* `pp` stands for path params
|
|
26
|
+
*/
|
|
27
|
+
export function pp(param: string | number | boolean): string {
|
|
28
|
+
return encodeURIComponent(param);
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as default } from './Paths';
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-process-env */
|
|
2
2
|
import logger from 'jet-logger';
|
|
3
|
-
|
|
4
|
-
import { isNumber, isValueOf, type ValueOf } from '../utils/validators';
|
|
3
|
+
import { z } from 'zod';
|
|
5
4
|
|
|
6
5
|
// ========================================================================= //
|
|
7
6
|
// CONSTANTS //
|
|
@@ -13,9 +12,9 @@ export const NodeEnvs = {
|
|
|
13
12
|
TEST: 'test',
|
|
14
13
|
PRODUCTION: 'production',
|
|
15
14
|
} as const;
|
|
16
|
-
export type NodeEnvs =
|
|
15
|
+
export type NodeEnvs = z.infer<typeof NodeEnvsEnum>;
|
|
17
16
|
|
|
18
|
-
export const
|
|
17
|
+
export const Env = {
|
|
19
18
|
NODE_ENV: process.env.NODE_ENV?.toLowerCase(),
|
|
20
19
|
PORT: Number(process.env.PORT),
|
|
21
20
|
} as const;
|
|
@@ -24,19 +23,18 @@ export const EnvVars = {
|
|
|
24
23
|
// EXEC //
|
|
25
24
|
// ========================================================================= //
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
const NodeEnvsEnum = z.enum(NodeEnvs, 'Not a valid node environment');
|
|
27
|
+
|
|
28
|
+
const EnvSchema = z.object({
|
|
29
|
+
NODE_ENV: NodeEnvsEnum,
|
|
30
|
+
PORT: z.uint32('Express port must be a positive integer'),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// ===================== Validate Environment Variables ==================== //
|
|
34
|
+
|
|
29
35
|
{
|
|
30
36
|
try {
|
|
31
|
-
|
|
32
|
-
if (!isNodeEnv(EnvVars.NODE_ENV)) {
|
|
33
|
-
throw new Error(
|
|
34
|
-
'process.env.NODE_ENV must be "development", "test", or "production"',
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
if (!isNumber(EnvVars.PORT)) {
|
|
38
|
-
throw new Error('process.env.PORT must be a valid number');
|
|
39
|
-
}
|
|
37
|
+
EnvSchema.parse(Env);
|
|
40
38
|
} catch (err) {
|
|
41
39
|
logger.err(err);
|
|
42
40
|
throw err;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
1
3
|
// ========================================================================= //
|
|
2
4
|
// TYPES //
|
|
3
5
|
// ========================================================================= //
|
|
@@ -5,6 +7,14 @@
|
|
|
5
7
|
export type ISOString =
|
|
6
8
|
`${number}-${number}-${number}T${number}:${number}:${number}${string}`;
|
|
7
9
|
|
|
10
|
+
// ========================================================================= //
|
|
11
|
+
// EXEC //
|
|
12
|
+
// ========================================================================= //
|
|
13
|
+
|
|
14
|
+
export const ISOStringSchema = z.iso
|
|
15
|
+
.datetime()
|
|
16
|
+
.transform((value) => value as ISOString);
|
|
17
|
+
|
|
8
18
|
// ========================================================================= //
|
|
9
19
|
// FUNCTIONS //
|
|
10
20
|
// ========================================================================= //
|
|
@@ -15,15 +25,3 @@ export type ISOString =
|
|
|
15
25
|
export function getISOString(date = new Date()): ISOString {
|
|
16
26
|
return date.toISOString() as ISOString;
|
|
17
27
|
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Validate an ISOString.
|
|
21
|
-
*/
|
|
22
|
-
export function isISOString(value: unknown): value is ISOString {
|
|
23
|
-
try {
|
|
24
|
-
const date = new Date(value as string);
|
|
25
|
-
return !isNaN(date.getTime()) && date.toISOString() === value;
|
|
26
|
-
} catch {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
@@ -3,26 +3,3 @@
|
|
|
3
3
|
// ========================================================================= //
|
|
4
4
|
|
|
5
5
|
export type ValueOf<T> = T[keyof T];
|
|
6
|
-
type Table = Record<string, string> | Record<string, number>;
|
|
7
|
-
|
|
8
|
-
// ========================================================================= //
|
|
9
|
-
// EXEC //
|
|
10
|
-
// ========================================================================= //
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Check if an `unknown` is a number type.
|
|
14
|
-
*/
|
|
15
|
-
export function isNumber<T>(val: T): val is Extract<T, number> {
|
|
16
|
-
return typeof val === 'number' && !isNaN(val);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Check if an `unknown` is a value of an object.
|
|
21
|
-
*/
|
|
22
|
-
export function isValueOf<T extends Table>(
|
|
23
|
-
table: T,
|
|
24
|
-
): (v: unknown) => v is ValueOf<T> {
|
|
25
|
-
const vals = Object.values(table);
|
|
26
|
-
const set = new Set(vals);
|
|
27
|
-
return (val: unknown): val is ValueOf<T> => set.has(val);
|
|
28
|
-
}
|
|
@@ -1,30 +1,45 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { getISOString, type ISOString } from '@src/common/utils/date-utils';
|
|
1
|
+
import { v7 as uuidv7 } from 'uuid';
|
|
2
|
+
import { z } from 'zod';
|
|
4
3
|
|
|
5
4
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import type { UserEntity } from './types';
|
|
5
|
+
getISOString,
|
|
6
|
+
type ISOString,
|
|
7
|
+
ISOStringSchema,
|
|
8
|
+
} from '@src/common/utils/date.utils';
|
|
9
|
+
|
|
10
|
+
import type { UserEntity, UserInput } from './types';
|
|
12
11
|
|
|
13
12
|
// ========================================================================= //
|
|
14
|
-
//
|
|
13
|
+
// CONSTANTS //
|
|
15
14
|
// ========================================================================= //
|
|
16
15
|
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
16
|
+
/** @ValueFactoryFunction */
|
|
17
|
+
const UserDefaults = () => ({
|
|
18
|
+
id: genId(),
|
|
19
|
+
name: '--',
|
|
20
|
+
email: '--@example.com',
|
|
21
|
+
created: getISOString(),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// ========================================================================= //
|
|
25
|
+
// EXEC //
|
|
26
|
+
// ========================================================================= //
|
|
27
|
+
|
|
28
|
+
const UserIdSchema = z.uuidv7('Not a valid uuid7 id');
|
|
29
|
+
|
|
30
|
+
const UserInputSchema = z.object({
|
|
31
|
+
name: z.string().min(1, 'Name cannot be empty'),
|
|
32
|
+
email: z.email('Not a valid email'),
|
|
33
|
+
}) satisfies z.ZodType<UserInput>;
|
|
34
|
+
|
|
35
|
+
const UserSchema = UserInputSchema.extend({
|
|
36
|
+
id: UserIdSchema,
|
|
37
|
+
created: ISOStringSchema,
|
|
38
|
+
}) satisfies z.ZodType<UserEntity>;
|
|
39
|
+
|
|
40
|
+
// ========================================================================= //
|
|
41
|
+
// FUNCTIONS //
|
|
42
|
+
// ========================================================================= //
|
|
28
43
|
|
|
29
44
|
/**
|
|
30
45
|
* Factory-function.
|
|
@@ -32,9 +47,7 @@ function getDefaults(): UserEntity {
|
|
|
32
47
|
* Create a `UserEntity` from a partial or `undefined`
|
|
33
48
|
*/
|
|
34
49
|
function create(user?: Partial<UserEntity>): UserEntity {
|
|
35
|
-
return
|
|
36
|
-
throw new Error('Setup new user failed ' + JSON.stringify(errors, null, 2));
|
|
37
|
-
});
|
|
50
|
+
return { ...UserDefaults(), ...user };
|
|
38
51
|
}
|
|
39
52
|
|
|
40
53
|
/**
|
|
@@ -43,13 +56,41 @@ function create(user?: Partial<UserEntity>): UserEntity {
|
|
|
43
56
|
* Create a `UserEntity` from individual properties
|
|
44
57
|
*/
|
|
45
58
|
function of(name: string, email?: string, created?: ISOString): UserEntity {
|
|
46
|
-
const retVal =
|
|
59
|
+
const retVal = UserDefaults();
|
|
47
60
|
if (name) retVal.name = name;
|
|
48
61
|
if (email) retVal.email = email;
|
|
49
62
|
if (created) retVal.created = created;
|
|
50
63
|
return retVal;
|
|
51
64
|
}
|
|
52
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Generate a uuid7 id.
|
|
68
|
+
*/
|
|
69
|
+
function genId(): string {
|
|
70
|
+
return uuidv7();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Check if a valid user object
|
|
75
|
+
*/
|
|
76
|
+
function isUser(val: unknown): val is UserEntity {
|
|
77
|
+
return UserSchema.safeParse(val).success;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Check if a valid end users input fields are valid.
|
|
82
|
+
*/
|
|
83
|
+
function isUserInput(val: unknown): val is UserInput {
|
|
84
|
+
return UserInputSchema.safeParse(val).success;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Test if an id is a valid user id.
|
|
89
|
+
*/
|
|
90
|
+
function isUserId(val: unknown): val is string {
|
|
91
|
+
return UserIdSchema.safeParse(val).success;
|
|
92
|
+
}
|
|
93
|
+
|
|
53
94
|
// ========================================================================= //
|
|
54
95
|
// EXPORT //
|
|
55
96
|
// ========================================================================= //
|
|
@@ -57,7 +98,15 @@ function of(name: string, email?: string, created?: ISOString): UserEntity {
|
|
|
57
98
|
export default {
|
|
58
99
|
of,
|
|
59
100
|
create,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
101
|
+
genId,
|
|
102
|
+
is: {
|
|
103
|
+
id: isUserId,
|
|
104
|
+
user: isUser,
|
|
105
|
+
input: isUserInput,
|
|
106
|
+
},
|
|
107
|
+
Schemas: {
|
|
108
|
+
Main: UserSchema,
|
|
109
|
+
Input: UserInputSchema,
|
|
110
|
+
Id: UserIdSchema,
|
|
111
|
+
},
|
|
63
112
|
} as const;
|
package/lib/template/src/main.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import logger from 'jet-logger';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { Env } from './common/constants/env.constants';
|
|
4
4
|
import server from './server';
|
|
5
5
|
|
|
6
6
|
// ========================================================================= //
|
|
7
7
|
// CONSTANTS //
|
|
8
8
|
// ========================================================================= //
|
|
9
9
|
|
|
10
|
-
const SERVER_START_MESSAGE = `Express server started on port: ${
|
|
10
|
+
const SERVER_START_MESSAGE = `Express server started on port: ${Env.PORT}`;
|
|
11
11
|
|
|
12
12
|
// ========================================================================= //
|
|
13
13
|
// EXEC //
|
|
14
14
|
// ========================================================================= //
|
|
15
15
|
|
|
16
16
|
// Start the server
|
|
17
|
-
server.listen(
|
|
17
|
+
server.listen(Env.PORT, (err) => {
|
|
18
18
|
if (!!err) {
|
|
19
19
|
logger.err(err.message);
|
|
20
20
|
} else {
|
|
@@ -64,9 +64,6 @@ function onDocumentClick(event) {
|
|
|
64
64
|
function addUser() {
|
|
65
65
|
var nameInput = document.getElementById('name-input');
|
|
66
66
|
var emailInput = document.getElementById('email-input');
|
|
67
|
-
if (!nameInput.value || !emailInput.value) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
67
|
var data = {
|
|
71
68
|
user: {
|
|
72
69
|
name: nameInput.value,
|
|
@@ -163,7 +160,7 @@ function deleteUser(ele) {
|
|
|
163
160
|
// ============================= Shared Helpers ============================ //
|
|
164
161
|
|
|
165
162
|
/**
|
|
166
|
-
*
|
|
163
|
+
* Log failed responses and their full JSON bodies, then reject the request.
|
|
167
164
|
*
|
|
168
165
|
* Used by:
|
|
169
166
|
* {@link displayUsers}
|
|
@@ -175,14 +172,25 @@ function deleteUser(ele) {
|
|
|
175
172
|
* @returns {Response | Promise<never>} The response when it succeeded.
|
|
176
173
|
*/
|
|
177
174
|
function checkResponse(resp) {
|
|
178
|
-
if (resp.ok)
|
|
179
|
-
return resp;
|
|
180
|
-
}
|
|
175
|
+
if (resp.ok) return resp;
|
|
181
176
|
return resp
|
|
182
177
|
.json()
|
|
183
178
|
.catch(() => ({}))
|
|
184
179
|
.then((body) => {
|
|
185
|
-
throw
|
|
180
|
+
throw JSON.stringify(
|
|
181
|
+
{
|
|
182
|
+
url: resp.url,
|
|
183
|
+
status: resp.status,
|
|
184
|
+
statusText: resp.statusText,
|
|
185
|
+
ok: resp.ok,
|
|
186
|
+
redirected: resp.redirected,
|
|
187
|
+
type: resp.type,
|
|
188
|
+
headers: Object.fromEntries(resp.headers.entries()),
|
|
189
|
+
body,
|
|
190
|
+
},
|
|
191
|
+
null,
|
|
192
|
+
2,
|
|
193
|
+
);
|
|
186
194
|
});
|
|
187
195
|
}
|
|
188
196
|
|
|
@@ -195,9 +203,10 @@ function checkResponse(resp) {
|
|
|
195
203
|
* {@link submitEdit}
|
|
196
204
|
* {@link deleteUser}
|
|
197
205
|
*
|
|
198
|
-
* @param {Error} err
|
|
206
|
+
* @param {string | Error} err
|
|
199
207
|
* @returns {void}
|
|
200
208
|
*/
|
|
201
209
|
function showError(err) {
|
|
202
|
-
|
|
210
|
+
document.getElementById('error-display').textContent =
|
|
211
|
+
typeof err === 'string' ? err : err.stack || err.message;
|
|
203
212
|
}
|
|
@@ -11795,4 +11795,3 @@ fieldset:disabled .btn {
|
|
|
11795
11795
|
display: none !important;
|
|
11796
11796
|
}
|
|
11797
11797
|
}
|
|
11798
|
-
/*# sourceMappingURL=bootstrap.min.css.map */
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
body {
|
|
2
|
+
display: flex;
|
|
3
|
+
flex-wrap: wrap;
|
|
4
|
+
gap: 2em;
|
|
2
5
|
padding: 100px;
|
|
3
6
|
font:
|
|
4
7
|
14px 'Lucida Grande',
|
|
@@ -9,7 +12,6 @@ body {
|
|
|
9
12
|
|
|
10
13
|
body .users-column {
|
|
11
14
|
display: inline-block;
|
|
12
|
-
margin-right: 2em;
|
|
13
15
|
vertical-align: top;
|
|
14
16
|
}
|
|
15
17
|
|
|
@@ -48,3 +50,24 @@ body .users-column .user-display-ele button {
|
|
|
48
50
|
body .users-column .user-display-ele .edit-view {
|
|
49
51
|
display: none;
|
|
50
52
|
}
|
|
53
|
+
|
|
54
|
+
/** Error Display Column **/
|
|
55
|
+
|
|
56
|
+
body .errors-column {
|
|
57
|
+
flex: 1 1 320px;
|
|
58
|
+
min-width: 0;
|
|
59
|
+
color: #a61b29;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
body .errors-column #error-display {
|
|
63
|
+
margin: 0;
|
|
64
|
+
padding: 16px;
|
|
65
|
+
border: 1px solid #e5a4aa;
|
|
66
|
+
border-radius: 6px;
|
|
67
|
+
background-color: #fff2f3;
|
|
68
|
+
color: inherit;
|
|
69
|
+
white-space: pre-wrap;
|
|
70
|
+
overflow-wrap: anywhere;
|
|
71
|
+
max-height: 65vh;
|
|
72
|
+
overflow-y: auto;
|
|
73
|
+
}
|
|
@@ -2,7 +2,7 @@ import fs from 'fs/promises';
|
|
|
2
2
|
import jsonfile from 'jsonfile';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Env, NodeEnvs } from '@src/common/constants/env.constants';
|
|
6
6
|
import type { UserEntity } from '@src/entities/User';
|
|
7
7
|
|
|
8
8
|
// ========================================================================= //
|
|
@@ -12,7 +12,7 @@ import type { UserEntity } from '@src/entities/User';
|
|
|
12
12
|
const DATABASE_FILE_PATH = path.join(
|
|
13
13
|
import.meta.dirname,
|
|
14
14
|
'common',
|
|
15
|
-
|
|
15
|
+
Env.NODE_ENV === NodeEnvs.TEST ? 'database.test.json' : 'database.json',
|
|
16
16
|
);
|
|
17
17
|
|
|
18
18
|
// ========================================================================= //
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { getISOString } from '@src/common/utils/date-utils';
|
|
4
|
-
import type { UserEntity } from '@src/entities/User';
|
|
1
|
+
import { type UserEntity } from '@src/entities/User';
|
|
5
2
|
|
|
6
3
|
import orm from './MockOrm';
|
|
7
4
|
|
|
@@ -15,9 +12,7 @@ import orm from './MockOrm';
|
|
|
15
12
|
async function persists(id: string): Promise<boolean> {
|
|
16
13
|
const db = await orm.openDb();
|
|
17
14
|
for (const user of db.users) {
|
|
18
|
-
if (user.id === id)
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
15
|
+
if (user.id === id) return true;
|
|
21
16
|
}
|
|
22
17
|
return false;
|
|
23
18
|
}
|
|
@@ -27,7 +22,7 @@ async function persists(id: string): Promise<boolean> {
|
|
|
27
22
|
*/
|
|
28
23
|
async function getAll(): Promise<UserEntity[]> {
|
|
29
24
|
const db = await orm.openDb();
|
|
30
|
-
return db.users;
|
|
25
|
+
return db.users.sort((a, b) => a.id.localeCompare(b.id));
|
|
31
26
|
}
|
|
32
27
|
|
|
33
28
|
/**
|
|
@@ -92,14 +87,9 @@ async function insertMultiple(
|
|
|
92
87
|
users: UserEntity[] | readonly UserEntity[],
|
|
93
88
|
): Promise<UserEntity[]> {
|
|
94
89
|
const db = await orm.openDb();
|
|
95
|
-
|
|
96
|
-
...user,
|
|
97
|
-
id: jetid(),
|
|
98
|
-
created: getISOString(),
|
|
99
|
-
}));
|
|
100
|
-
db.users = [...db.users, ...inserted];
|
|
90
|
+
db.users = [...db.users, ...users];
|
|
101
91
|
await orm.saveDb(db);
|
|
102
|
-
return
|
|
92
|
+
return [...users];
|
|
103
93
|
}
|
|
104
94
|
|
|
105
95
|
// ========================================================================= //
|