├── .gitignore ├── LICENSE ├── README-npm ├── README.md ├── bin └── cli.js ├── express-typescript.png ├── lib ├── cli.js ├── express-generator-typescript.js └── project-files │ ├── README.md │ ├── config.ts │ ├── config │ ├── .env.development │ ├── .env.production │ └── .env.test │ ├── eslint.config.ts │ ├── gitignore │ ├── package.json │ ├── scripts │ └── build.ts │ ├── src │ ├── common │ │ ├── constants │ │ │ ├── ENV.ts │ │ │ ├── HttpStatusCodes.ts │ │ │ ├── Paths.ts │ │ │ └── index.ts │ │ └── util │ │ │ ├── misc.ts │ │ │ ├── route-errors.ts │ │ │ └── validators.ts │ ├── index.ts │ ├── models │ │ ├── User.ts │ │ └── common │ │ │ └── types │ │ │ └── index.ts │ ├── public │ │ ├── scripts │ │ │ ├── http.js │ │ │ └── users.js │ │ └── stylesheets │ │ │ └── users.css │ ├── repos │ │ ├── MockOrm.ts │ │ ├── UserRepo.ts │ │ ├── database.json │ │ └── database.test.json │ ├── routes │ │ ├── UserRoutes.ts │ │ ├── common │ │ │ ├── types │ │ │ │ └── index.ts │ │ │ └── util │ │ │ │ └── index.ts │ │ └── index.ts │ ├── server.ts │ ├── services │ │ └── UserService.ts │ └── views │ │ └── users.html │ ├── tests │ ├── common │ │ ├── Paths.ts │ │ ├── types │ │ │ └── index.ts │ │ └── util │ │ │ └── index.ts │ ├── support │ │ └── setup.ts │ └── users.test.ts │ ├── tsconfig.json │ ├── tsconfig.prod.json │ └── vitest.config.mts ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | **/**/node_modules 2 | **/**/*.log 3 | lib/**/package-lock.json 4 | lib/project-files/.vscode 5 | lib/project-files/config.js 6 | **/**/dist 7 | 8 | test/ 9 | **/**/.DS_Store 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Sean Maxwell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README-npm: -------------------------------------------------------------------------------- 1 | overnightjs 2 | 3 | [Express](https://www.npmjs.com/package/express) with [TypeScript's](https://www.npmjs.com/package/typescript) application generator. 4 | 5 | NPM Version 6 | Package License 7 | NPM Downloads 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 here. 15 |
16 | 17 | 18 | ## Documenation 19 | 20 | Please refer to the official github repo for the most up-to-date documentation. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | overnightjs 2 | 3 | [Express](https://www.npmjs.com/package/express) with [TypeScript's](https://www.npmjs.com/package/typescript) application generator. 4 | 5 | NPM Version 6 | Package License 7 | NPM Downloads 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 here. 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 here. 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:
51 | With no options: `$ npx express-generator-typescript`
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 |
78 | 79 | 80 | Happy web deving :) 81 | 82 | 83 | ## License 84 | 85 | [MIT](LICENSE) 86 | -------------------------------------------------------------------------------- /bin/cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const path = require('path'), 4 | expressGenTs = require('../lib/express-generator-typescript'); 5 | 6 | 7 | /****************************************************************************** 8 | Run 9 | ******************************************************************************/ 10 | 11 | // Init 12 | console.log('Setting up new Express/TypeScript project...'); 13 | const args = process.argv.slice(2); 14 | 15 | // Setup use yarn 16 | let useYarn = false; 17 | const useYarnIdx = args.indexOf('--use-yarn'); 18 | if (useYarnIdx > -1) { 19 | useYarn = true; 20 | args.splice(useYarnIdx, 1); 21 | } 22 | 23 | // Setup destination 24 | let destination = 'express-gen-ts'; 25 | if (args.length > 0) { 26 | destination = args[0]; 27 | } 28 | destination = path.join(process.cwd(), destination); 29 | 30 | // Creating new project finished 31 | expressGenTs(destination, useYarn).then(() => { 32 | console.log('Project setup complete!'); 33 | }); 34 | -------------------------------------------------------------------------------- /express-typescript.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seanpmaxwell/express-generator-typescript/418c91b2fa7fe1b59d9ed6fa21871e5f453b2f77/express-typescript.png -------------------------------------------------------------------------------- /lib/cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const path = require('path'), 4 | expressGenTs = require('../lib/express-generator-typescript'); 5 | 6 | 7 | /****************************************************************************** 8 | Run 9 | ******************************************************************************/ 10 | 11 | // Init 12 | console.log('Setting up new Express/TypeScript project...'); 13 | const args = process.argv.slice(2); 14 | 15 | // Setup use yarn 16 | let useYarn = false; 17 | const useYarnIdx = args.indexOf('--use-yarn'); 18 | if (useYarnIdx > -1) { 19 | useYarn = true; 20 | args.splice(useYarnIdx, 1); 21 | } 22 | 23 | // Setup destination 24 | let destination = 'express-gen-ts'; 25 | if (args.length > 0) { 26 | destination = args[0]; 27 | } 28 | destination = path.join(process.cwd(), destination); 29 | 30 | // Creating new project finished 31 | expressGenTs(destination, useYarn).then(() => { 32 | console.log('Project setup complete!'); 33 | }); 34 | -------------------------------------------------------------------------------- /lib/express-generator-typescript.js: -------------------------------------------------------------------------------- 1 | const path = require('path'), 2 | editJsonFile = require('edit-json-file'), 3 | childProcess = require('child_process'), 4 | ncp = require('ncp').ncp, 5 | fs = require('fs'); 6 | 7 | 8 | /****************************************************************************** 9 | Constants 10 | ******************************************************************************/ 11 | 12 | // Project folder paths 13 | const PROJECT_FOLDER_PATH = './project-files'; 14 | 15 | // Project-folder dependencies 16 | const DEPENDENCIES = 'express dotenv morgan cookie-parser jet-logger ' + 17 | 'module-alias helmet jsonfile inserturlparams jet-paths dayjs jet-env ' + 18 | 'jet-validators', 19 | DEV_DEPENDENCIES = 'ts-node typescript nodemon find supertest vitest ' + 20 | '@types/node @types/find @types/morgan @types/cookie-parser ' + 21 | '@types/supertest fs-extra tsconfig-paths jiti @swc/core @types/jsonfile ' + 22 | '@types/fs-extra @types/module-alias @stylistic/eslint-plugin-js eslint ' + 23 | '@eslint/js typescript-eslint eslint-plugin-n @stylistic/eslint-plugin-ts'; 24 | 25 | // "ncp" options 26 | const ncpOpts = { 27 | filter: (fileName) => { 28 | return !(fileName === 'package-lock.json' || fileName === 'node_modules'); 29 | }, 30 | }; 31 | 32 | 33 | /****************************************************************************** 34 | Functions 35 | ******************************************************************************/ 36 | 37 | /** 38 | * Entry point 39 | */ 40 | async function expressGenTs(destination, useYarn) { 41 | try { 42 | await copyProjectFiles(destination); 43 | updatePackageJson(destination); 44 | await renameGitigoreFile(destination); 45 | downloadNodeModules(destination, useYarn); 46 | } catch (err) { 47 | console.error(err); 48 | } 49 | } 50 | 51 | /** 52 | * Copy project files 53 | */ 54 | function copyProjectFiles(destination) { 55 | const source = path.join(__dirname, PROJECT_FOLDER_PATH); 56 | return /** @type {Promise} */(new Promise((res, rej) => { 57 | return ncp(source, destination, ncpOpts, (err) => { 58 | return (!!err ? rej(err) : res()); 59 | }); 60 | })); 61 | } 62 | 63 | /** 64 | * Set update the package.json file. 65 | */ 66 | function updatePackageJson(destination) { 67 | let file = editJsonFile(destination + '/package.json', { 68 | autosave: true 69 | }); 70 | file.set('name', path.basename(destination)); 71 | file.set('dependencies', {}); 72 | file.set('devDependencies', {}); 73 | } 74 | 75 | /** 76 | * Because npm does not allow .gitignore to be published. 77 | */ 78 | function renameGitigoreFile(destination) { 79 | return /** @type {Promise} */(new Promise((res, rej) => 80 | fs.rename( 81 | (destination + '/gitignore'), 82 | (destination + '/.gitignore'), 83 | (err => !!err ? rej(err) : res()), 84 | ) 85 | )); 86 | } 87 | 88 | /** 89 | * Download the dependencies. 90 | */ 91 | function downloadNodeModules(destination, useYarn) { 92 | const options = { cwd: destination }; 93 | // Setup dependencies string 94 | let depStr = DEPENDENCIES, 95 | devDepStr = DEV_DEPENDENCIES; 96 | // Setup download command 97 | let downloadLibCmd, 98 | downloadDepCmd; 99 | if (useYarn) { 100 | downloadLibCmd = 'yarn add ' + depStr; 101 | downloadDepCmd = 'yarn add ' + devDepStr + ' -D'; 102 | } else { 103 | downloadLibCmd = 'npm i -s ' + depStr; 104 | downloadDepCmd = 'npm i -D ' + devDepStr; 105 | } 106 | // Execute command 107 | childProcess.execSync(downloadLibCmd, options); 108 | childProcess.execSync(downloadDepCmd, options); 109 | } 110 | 111 | 112 | /****************************************************************************** 113 | Export 114 | ******************************************************************************/ 115 | 116 | module.exports = expressGenTs; 117 | -------------------------------------------------------------------------------- /lib/project-files/README.md: -------------------------------------------------------------------------------- 1 | ## About 2 | 3 | This project was created with [express-generator-typescript](https://github.com/seanpmaxwell/express-generator-typescript). 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 | 18 | 19 | ## Available Scripts 20 | 21 | ### `npm run clean-install` 22 | 23 | Remove the existing `node_modules/` folder, `package-lock.json`, and reinstall all library modules. 24 | 25 | 26 | ### `npm run dev` or `npm run dev:hot` (hot reloading) 27 | 28 | Run the server in development mode.
29 | 30 | **IMPORTANT** development mode uses `swc` for performance reasons which DOES NOT check for typescript errors. Run `npm run type-check` to check for type errors. NOTE: you should use your IDE to prevent most type errors. 31 | 32 | 33 | ### `npm test` or `npm run test:hot` (hot reloading) 34 | 35 | Run all unit-tests. 36 | 37 | 38 | ### `npm test -- "name of test file" (i.e. users).` 39 | 40 | Run a single unit-test. 41 | 42 | 43 | ### `npm run lint` 44 | 45 | Check for linting errors. 46 | 47 | 48 | ### `npm run build` 49 | 50 | Build the project for production. 51 | 52 | 53 | ### `npm start` 54 | 55 | Run the production build (Must be built first). 56 | 57 | 58 | ### `npm run type-check` 59 | 60 | Check for typescript errors. 61 | 62 | 63 | ## Additional Notes 64 | 65 | - If `npm run dev` gives you issues with bcrypt on MacOS you may need to run: `npm rebuild bcrypt --build-from-source`. 66 | -------------------------------------------------------------------------------- /lib/project-files/config.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable n/no-process-env */ 2 | 3 | import path from 'path'; 4 | import dotenv from 'dotenv'; 5 | import moduleAlias from 'module-alias'; 6 | 7 | 8 | // Check the env 9 | const NODE_ENV = (process.env.NODE_ENV ?? 'development'); 10 | 11 | // Configure "dotenv" 12 | const result2 = dotenv.config({ 13 | path: path.join(__dirname, `./config/.env.${NODE_ENV}`), 14 | }); 15 | if (result2.error) { 16 | throw result2.error; 17 | } 18 | 19 | // Configure moduleAlias 20 | if (__filename.endsWith('js')) { 21 | moduleAlias.addAlias('@src', __dirname + '/dist'); 22 | } 23 | -------------------------------------------------------------------------------- /lib/project-files/config/.env.development: -------------------------------------------------------------------------------- 1 | ## Environment ## 2 | NODE_ENV=development 3 | 4 | 5 | ## Server ## 6 | PORT=3000 7 | HOST=localhost 8 | 9 | 10 | ## Setup jet-logger ## 11 | JET_LOGGER_MODE=CONSOLE 12 | JET_LOGGER_FILEPATH=jet-logger.log 13 | JET_LOGGER_TIMESTAMP=TRUE 14 | JET_LOGGER_FORMAT=LINE 15 | 16 | -------------------------------------------------------------------------------- /lib/project-files/config/.env.production: -------------------------------------------------------------------------------- 1 | ## Environment ## 2 | NODE_ENV=production 3 | 4 | 5 | ## Server ## 6 | PORT=8081 7 | HOST=localhost 8 | DISABLE_HELMET=TRUE 9 | 10 | 11 | ## Setup jet-logger ## 12 | JET_LOGGER_MODE=FILE 13 | JET_LOGGER_FILEPATH=jet-logger.log 14 | JET_LOGGER_TIMESTAMP=TRUE 15 | JET_LOGGER_FORMAT=LINE 16 | -------------------------------------------------------------------------------- /lib/project-files/config/.env.test: -------------------------------------------------------------------------------- 1 | ## Environment ## 2 | NODE_ENV=test 3 | 4 | 5 | ## Server ## 6 | PORT=4000 7 | HOST=localhost 8 | 9 | 10 | ## Setup jet-logger ## 11 | JET_LOGGER_MODE=CONSOLE 12 | JET_LOGGER_FILEPATH=jet-logger.log 13 | JET_LOGGER_TIMESTAMP=TRUE 14 | JET_LOGGER_FORMAT=LINE 15 | -------------------------------------------------------------------------------- /lib/project-files/eslint.config.ts: -------------------------------------------------------------------------------- 1 | import eslint from '@eslint/js'; 2 | import tseslint from 'typescript-eslint'; 3 | import stylisticJs from '@stylistic/eslint-plugin-js'; 4 | import stylisticTs from '@stylistic/eslint-plugin-ts'; 5 | import nodePlugin from 'eslint-plugin-n'; 6 | 7 | export default tseslint.config( 8 | eslint.configs.recommended, 9 | nodePlugin.configs['flat/recommended-script'], 10 | ...tseslint.configs.strictTypeChecked, 11 | ...tseslint.configs.stylisticTypeChecked, 12 | { 13 | ignores: [ 14 | '**/node_modules/*', 15 | '**/*.mjs', 16 | '**/*.js', 17 | ], 18 | }, 19 | { 20 | languageOptions: { 21 | parserOptions: { 22 | project: './tsconfig.json', 23 | warnOnUnsupportedTypeScriptVersion: false, 24 | }, 25 | }, 26 | }, 27 | { 28 | plugins: { 29 | '@stylistic/js': stylisticJs, 30 | '@stylistic/ts': stylisticTs, 31 | }, 32 | }, 33 | { 34 | files: ['**/*.ts'], 35 | }, 36 | { 37 | rules: { 38 | '@typescript-eslint/explicit-member-accessibility': 'warn', 39 | '@typescript-eslint/no-misused-promises': 0, 40 | '@typescript-eslint/no-floating-promises': 0, 41 | '@typescript-eslint/no-confusing-void-expression': 0, 42 | '@typescript-eslint/no-unnecessary-condition': 0, 43 | '@typescript-eslint/restrict-template-expressions': [ 44 | 'error', { allowNumber: true }, 45 | ], 46 | '@typescript-eslint/restrict-plus-operands': [ 47 | 'warn', { allowNumberAndString: true }, 48 | ], 49 | '@typescript-eslint/no-unused-vars': 'warn', 50 | '@typescript-eslint/no-unsafe-enum-comparison': 0, 51 | '@typescript-eslint/no-unnecessary-type-parameters': 0, 52 | '@stylistic/js/no-extra-semi': 'warn', 53 | 'max-len': [ 54 | 'warn', 55 | { 56 | 'code': 80, 57 | }, 58 | ], 59 | '@stylistic/ts/semi': ['warn', 'always'], 60 | '@stylistic/ts/member-delimiter-style': ['warn', { 61 | 'multiline': { 62 | 'delimiter': 'comma', 63 | 'requireLast': true, 64 | }, 65 | 'singleline': { 66 | 'delimiter': 'comma', 67 | 'requireLast': false, 68 | }, 69 | 'overrides': { 70 | 'interface': { 71 | 'singleline': { 72 | 'delimiter': 'semi', 73 | 'requireLast': false, 74 | }, 75 | 'multiline': { 76 | 'delimiter': 'semi', 77 | 'requireLast': true, 78 | }, 79 | }, 80 | }, 81 | }], 82 | '@typescript-eslint/no-non-null-assertion': 0, 83 | '@typescript-eslint/no-unused-expressions': 'warn', 84 | 'comma-dangle': ['warn', 'always-multiline'], 85 | 'no-console': 1, 86 | 'no-extra-boolean-cast': 0, 87 | 'indent': ['warn', 2], 88 | 'quotes': ['warn', 'single'], 89 | 'n/no-process-env': 1, 90 | 'n/no-missing-import': 0, 91 | 'n/no-unpublished-import': 0, 92 | 'prefer-const': 'warn', 93 | }, 94 | }, 95 | ); 96 | -------------------------------------------------------------------------------- /lib/project-files/gitignore: -------------------------------------------------------------------------------- 1 | **/node_modules 2 | dist/ 3 | temp/ 4 | **/**/*.log 5 | config.js -------------------------------------------------------------------------------- /lib/project-files/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-typescript-example", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "build": "ts-node ./scripts/build.ts", 6 | "clean-install": "rm -rf ./node_modules && rm -r package-lock.json && npm i", 7 | "dev": "NODE_ENV=development ts-node ./src", 8 | "dev:hot": "nodemon --exec \"npm run dev\" --watch ./src --ext .ts", 9 | "lint": "eslint .", 10 | "start": "NODE_ENV=production node -r ./config.js ./dist", 11 | "test": "NODE_ENV=test vitest", 12 | "type-check": "tsc --noEmit" 13 | }, 14 | "engines": { 15 | "node": ">=16.0.0" 16 | }, 17 | "dependencies": { 18 | "cookie-parser": "^1.4.7", 19 | "dayjs": "^1.11.13", 20 | "dotenv": "^16.5.0", 21 | "express": "^5.1.0", 22 | "helmet": "^8.1.0", 23 | "inserturlparams": "^2.0.5", 24 | "jet-env": "^1.1.4", 25 | "jet-logger": "^2.0.1", 26 | "jet-paths": "^1.1.0", 27 | "jet-validators": "^1.4.0", 28 | "jsonfile": "^6.1.0", 29 | "module-alias": "^2.2.3", 30 | "morgan": "^1.10.0" 31 | }, 32 | "devDependencies": { 33 | "@eslint/js": "^9.25.1", 34 | "@stylistic/eslint-plugin-js": "^4.2.0", 35 | "@stylistic/eslint-plugin-ts": "^4.2.0", 36 | "@swc/core": "^1.11.22", 37 | "@types/cookie-parser": "^1.4.8", 38 | "@types/find": "^0.2.4", 39 | "@types/fs-extra": "^11.0.4", 40 | "@types/jsonfile": "^6.1.4", 41 | "@types/module-alias": "^2.0.4", 42 | "@types/morgan": "^1.9.9", 43 | "@types/node": "^22.14.1", 44 | "@types/supertest": "^6.0.3", 45 | "eslint": "^9.25.1", 46 | "eslint-plugin-n": "^17.17.0", 47 | "find": "^0.3.0", 48 | "fs-extra": "^11.3.0", 49 | "jiti": "^2.4.2", 50 | "nodemon": "^3.1.10", 51 | "supertest": "^7.1.0", 52 | "ts-node": "^10.9.2", 53 | "tsconfig-paths": "^4.2.0", 54 | "typescript": "^5.8.3", 55 | "typescript-eslint": "^8.31.0", 56 | "vitest": "^3.1.2" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /lib/project-files/scripts/build.ts: -------------------------------------------------------------------------------- 1 | import fs from 'fs-extra'; 2 | import logger from 'jet-logger'; 3 | import childProcess from 'child_process'; 4 | 5 | 6 | /** 7 | * Start 8 | */ 9 | (async () => { 10 | try { 11 | // Remove current build 12 | await remove('./dist/'); 13 | await exec('npm run lint', './'); 14 | await exec('tsc --build tsconfig.prod.json', './'); 15 | // Copy 16 | await copy('./src/public', './dist/public'); 17 | await copy('./src/views', './dist/views'); 18 | await copy('./src/repos/database.json', './dist/repos/database.json'); 19 | await copy('./temp/config.js', './config.js'); 20 | await copy('./temp/src', './dist'); 21 | await remove('./temp/'); 22 | } catch (err) { 23 | logger.err(err); 24 | // eslint-disable-next-line n/no-process-exit 25 | process.exit(1); 26 | } 27 | })(); 28 | 29 | /** 30 | * Remove file 31 | */ 32 | function remove(loc: string): Promise { 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 { 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 { 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 | } 67 | -------------------------------------------------------------------------------- /lib/project-files/src/common/constants/ENV.ts: -------------------------------------------------------------------------------- 1 | import jetEnv, { num } from 'jet-env'; 2 | import { isEnumVal } from 'jet-validators'; 3 | 4 | import { NodeEnvs } from '.'; 5 | 6 | 7 | /****************************************************************************** 8 | Setup 9 | ******************************************************************************/ 10 | 11 | const ENV = jetEnv({ 12 | NodeEnv: isEnumVal(NodeEnvs), 13 | Port: num, 14 | }); 15 | 16 | 17 | /****************************************************************************** 18 | Export default 19 | ******************************************************************************/ 20 | 21 | export default ENV; 22 | -------------------------------------------------------------------------------- /lib/project-files/src/common/constants/HttpStatusCodes.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable max-len */ 2 | 3 | 4 | /** 5 | * Hypertext Transfer Protocol (HTTP) response status codes. 6 | * @see {@link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes} 7 | * 8 | * This file was copied from here: https://gist.github.com/scokmen/f813c904ef79022e84ab2409574d1b45 9 | */ 10 | enum HttpStatusCodes { 11 | 12 | /** 13 | * The server has received the request headers and the client should proceed to send the request body 14 | * (in the case of a request for which a body needs to be sent; for example, a POST request). 15 | * Sending a large request body to a server after a request has been rejected for inappropriate headers would be inefficient. 16 | * To have a server check the request's headers, a client must send Expect: 100-continue as a header in its initial request 17 | * and receive a 100 Continue status code in response before sending the body. The response 417 Expectation Failed indicates the request should not be continued. 18 | */ 19 | CONTINUE = 100, 20 | 21 | /** 22 | * The requester has asked the server to switch protocols and the server has agreed to do so. 23 | */ 24 | SWITCHING_PROTOCOLS = 101, 25 | 26 | /** 27 | * A WebDAV request may contain many sub-requests involving file operations, requiring a long time to complete the request. 28 | * This code indicates that the server has received and is processing the request, but no response is available yet. 29 | * This prevents the client from timing out and assuming the request was lost. 30 | */ 31 | PROCESSING = 102, 32 | 33 | /** 34 | * Standard response for successful HTTP requests. 35 | * The actual response will depend on the request method used. 36 | * In a GET request, the response will contain an entity corresponding to the requested resource. 37 | * In a POST request, the response will contain an entity describing or containing the result of the action. 38 | */ 39 | OK = 200, 40 | 41 | /** 42 | * The request has been fulfilled, resulting in the creation of a new resource. 43 | */ 44 | CREATED = 201, 45 | 46 | /** 47 | * The request has been accepted for processing, but the processing has not been completed. 48 | * The request might or might not be eventually acted upon, and may be disallowed when processing occurs. 49 | */ 50 | ACCEPTED = 202, 51 | 52 | /** 53 | * SINCE HTTP/1.1 54 | * The server is a transforming proxy that received a 200 OK from its origin, 55 | * but is returning a modified version of the origin's response. 56 | */ 57 | NON_AUTHORITATIVE_INFORMATION = 203, 58 | 59 | /** 60 | * The server successfully processed the request and is not returning any content. 61 | */ 62 | NO_CONTENT = 204, 63 | 64 | /** 65 | * The server successfully processed the request, but is not returning any content. 66 | * Unlike a 204 response, this response requires that the requester reset the document view. 67 | */ 68 | RESET_CONTENT = 205, 69 | 70 | /** 71 | * The server is delivering only part of the resource (byte serving) due to a range header sent by the client. 72 | * The range header is used by HTTP clients to enable resuming of interrupted downloads, 73 | * or split a download into multiple simultaneous streams. 74 | */ 75 | PARTIAL_CONTENT = 206, 76 | 77 | /** 78 | * The message body that follows is an XML message and can contain a number of separate response codes, 79 | * depending on how many sub-requests were made. 80 | */ 81 | MULTI_STATUS = 207, 82 | 83 | /** 84 | * The members of a DAV binding have already been enumerated in a preceding part of the (multistatus) response, 85 | * and are not being included again. 86 | */ 87 | ALREADY_REPORTED = 208, 88 | 89 | /** 90 | * The server has fulfilled a request for the resource, 91 | * and the response is a representation of the result of one or more instance-manipulations applied to the current instance. 92 | */ 93 | IM_USED = 226, 94 | 95 | /** 96 | * Indicates multiple options for the resource from which the client may choose (via agent-driven content negotiation). 97 | * For example, this code could be used to present multiple video format options, 98 | * to list files with different filename extensions, or to suggest word-sense disambiguation. 99 | */ 100 | MULTIPLE_CHOICES = 300, 101 | 102 | /** 103 | * This and all future requests should be directed to the given URI. 104 | */ 105 | MOVED_PERMANENTLY = 301, 106 | 107 | /** 108 | * This is an example of industry practice contradicting the standard. 109 | * The HTTP/1.0 specification (RFC 1945) required the client to perform a temporary redirect 110 | * (the original describing phrase was "Moved Temporarily"), but popular browsers implemented 302 111 | * with the functionality of a 303 See Other. Therefore, HTTP/1.1 added status codes 303 and 307 112 | * to distinguish between the two behaviours. However, some Web applications and frameworks 113 | * use the 302 status code as if it were the 303. 114 | */ 115 | FOUND = 302, 116 | 117 | /** 118 | * SINCE HTTP/1.1 119 | * The response to the request can be found under another URI using a GET method. 120 | * When received in response to a POST (or PUT/DELETE), the client should presume that 121 | * the server has received the data and should issue a redirect with a separate GET message. 122 | */ 123 | SEE_OTHER = 303, 124 | 125 | /** 126 | * Indicates that the resource has not been modified since the version specified by the request headers If-Modified-Since or If-None-Match. 127 | * In such case, there is no need to retransmit the resource since the client still has a previously-downloaded copy. 128 | */ 129 | NOT_MODIFIED = 304, 130 | 131 | /** 132 | * SINCE HTTP/1.1 133 | * The requested resource is available only through a proxy, the address for which is provided in the response. 134 | * Many HTTP clients (such as Mozilla and Internet Explorer) do not correctly handle responses with this status code, primarily for security reasons. 135 | */ 136 | USE_PROXY = 305, 137 | 138 | /** 139 | * No longer used. Originally meant "Subsequent requests should use the specified proxy." 140 | */ 141 | SWITCH_PROXY = 306, 142 | 143 | /** 144 | * SINCE HTTP/1.1 145 | * In this case, the request should be repeated with another URI; however, future requests should still use the original URI. 146 | * In contrast to how 302 was historically implemented, the request method is not allowed to be changed when reissuing the original request. 147 | * For example, a POST request should be repeated using another POST request. 148 | */ 149 | TEMPORARY_REDIRECT = 307, 150 | 151 | /** 152 | * The request and all future requests should be repeated using another URI. 153 | * 307 and 308 parallel the behaviors of 302 and 301, but do not allow the HTTP method to change. 154 | * So, for example, submitting a form to a permanently redirected resource may continue smoothly. 155 | */ 156 | PERMANENT_REDIRECT = 308, 157 | 158 | /** 159 | * The server cannot or will not process the request due to an apparent client error 160 | * (e.g., malformed request syntax, too large size, invalid request message framing, or deceptive request routing). 161 | */ 162 | BAD_REQUEST = 400, 163 | 164 | /** 165 | * Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet 166 | * been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the 167 | * requested resource. See Basic access authentication and Digest access authentication. 401 semantically means 168 | * "unauthenticated",i.e. the user does not have the necessary credentials. 169 | */ 170 | UNAUTHORIZED = 401, 171 | 172 | /** 173 | * Reserved for future use. The original intention was that this code might be used as part of some form of digital 174 | * cash or micro payment scheme, but that has not happened, and this code is not usually used. 175 | * Google Developers API uses this status if a particular developer has exceeded the daily limit on requests. 176 | */ 177 | PAYMENT_REQUIRED = 402, 178 | 179 | /** 180 | * The request was valid, but the server is refusing action. 181 | * The user might not have the necessary permissions for a resource. 182 | */ 183 | FORBIDDEN = 403, 184 | 185 | /** 186 | * The requested resource could not be found but may be available in the future. 187 | * Subsequent requests by the client are permissible. 188 | */ 189 | NOT_FOUND = 404, 190 | 191 | /** 192 | * A request method is not supported for the requested resource; 193 | * for example, a GET request on a form that requires data to be presented via POST, or a PUT request on a read-only resource. 194 | */ 195 | METHOD_NOT_ALLOWED = 405, 196 | 197 | /** 198 | * The requested resource is capable of generating only content not acceptable according to the Accept headers sent in the request. 199 | */ 200 | NOT_ACCEPTABLE = 406, 201 | 202 | /** 203 | * The client must first authenticate itself with the proxy. 204 | */ 205 | PROXY_AUTHENTICATION_REQUIRED = 407, 206 | 207 | /** 208 | * The server timed out waiting for the request. 209 | * According to HTTP specifications: 210 | * "The client did not produce a request within the time that the server was prepared to wait. The client MAY repeat the request without modifications at any later time." 211 | */ 212 | REQUEST_TIMEOUT = 408, 213 | 214 | /** 215 | * Indicates that the request could not be processed because of conflict in the request, 216 | * such as an edit conflict between multiple simultaneous updates. 217 | */ 218 | CONFLICT = 409, 219 | 220 | /** 221 | * Indicates that the resource requested is no longer available and will not be available again. 222 | * This should be used when a resource has been intentionally removed and the resource should be purged. 223 | * Upon receiving a 410 status code, the client should not request the resource in the future. 224 | * Clients such as search engines should remove the resource from their indices. 225 | * Most use cases do not require clients and search engines to purge the resource, and a "404 Not Found" may be used instead. 226 | */ 227 | GONE = 410, 228 | 229 | /** 230 | * The request did not specify the length of its content, which is required by the requested resource. 231 | */ 232 | LENGTH_REQUIRED = 411, 233 | 234 | /** 235 | * The server does not meet one of the preconditions that the requester put on the request. 236 | */ 237 | PRECONDITION_FAILED = 412, 238 | 239 | /** 240 | * The request is larger than the server is willing or able to process. Previously called "Request Entity Too Large". 241 | */ 242 | PAYLOAD_TOO_LARGE = 413, 243 | 244 | /** 245 | * The URI provided was too long for the server to process. Often the result of too much data being encoded as a query-string of a GET request, 246 | * in which case it should be converted to a POST request. 247 | * Called "Request-URI Too Long" previously. 248 | */ 249 | URI_TOO_LONG = 414, 250 | 251 | /** 252 | * The request entity has a media type which the server or resource does not support. 253 | * For example, the client uploads an image as image/svg+xml, but the server requires that images use a different format. 254 | */ 255 | UNSUPPORTED_MEDIA_TYPE = 415, 256 | 257 | /** 258 | * The client has asked for a portion of the file (byte serving), but the server cannot supply that portion. 259 | * For example, if the client asked for a part of the file that lies beyond the end of the file. 260 | * Called "Requested Range Not Satisfiable" previously. 261 | */ 262 | RANGE_NOT_SATISFIABLE = 416, 263 | 264 | /** 265 | * The server cannot meet the requirements of the Expect request-header field. 266 | */ 267 | EXPECTATION_FAILED = 417, 268 | 269 | /** 270 | * This code was defined in 1998 as one of the traditional IETF April Fools' jokes, in RFC 2324, Hyper Text Coffee Pot Control Protocol, 271 | * and is not expected to be implemented by actual HTTP servers. The RFC specifies this code should be returned by 272 | * teapots requested to brew coffee. This HTTP status is used as an Easter egg in some websites, including Google.com. 273 | */ 274 | I_AM_A_TEAPOT = 418, 275 | 276 | /** 277 | * The request was directed at a server that is not able to produce a response (for example because a connection reuse). 278 | */ 279 | MISDIRECTED_REQUEST = 421, 280 | 281 | /** 282 | * The request was well-formed but was unable to be followed due to semantic errors. 283 | */ 284 | UNPROCESSABLE_ENTITY = 422, 285 | 286 | /** 287 | * The resource that is being accessed is locked. 288 | */ 289 | LOCKED = 423, 290 | 291 | /** 292 | * The request failed due to failure of a previous request (e.g., a PROPPATCH). 293 | */ 294 | FAILED_DEPENDENCY = 424, 295 | 296 | /** 297 | * The client should switch to a different protocol such as TLS/1.0, given in the Upgrade header field. 298 | */ 299 | UPGRADE_REQUIRED = 426, 300 | 301 | /** 302 | * The origin server requires the request to be conditional. 303 | * Intended to prevent "the 'lost update' problem, where a client 304 | * GETs a resource's state, modifies it, and PUTs it back to the server, 305 | * when meanwhile a third party has modified the state on the server, leading to a conflict." 306 | */ 307 | PRECONDITION_REQUIRED = 428, 308 | 309 | /** 310 | * The user has sent too many requests in a given amount of time. Intended for use with rate-limiting schemes. 311 | */ 312 | TOO_MANY_REQUESTS = 429, 313 | 314 | /** 315 | * The server is unwilling to process the request because either an individual header field, 316 | * or all the header fields collectively, are too large. 317 | */ 318 | REQUEST_HEADER_FIELDS_TOO_LARGE = 431, 319 | 320 | /** 321 | * A server operator has received a legal demand to deny access to a resource or to a set of resources 322 | * that includes the requested resource. The code 451 was chosen as a reference to the novel Fahrenheit 451. 323 | */ 324 | UNAVAILABLE_FOR_LEGAL_REASONS = 451, 325 | 326 | /** 327 | * A generic error message, given when an unexpected condition was encountered and no more specific message is suitable. 328 | */ 329 | INTERNAL_SERVER_ERROR = 500, 330 | 331 | /** 332 | * The server either does not recognize the request method, or it lacks the ability to fulfill the request. 333 | * Usually this implies future availability (e.g., a new feature of a web-service API). 334 | */ 335 | NOT_IMPLEMENTED = 501, 336 | 337 | /** 338 | * The server was acting as a gateway or proxy and received an invalid response from the upstream server. 339 | */ 340 | BAD_GATEWAY = 502, 341 | 342 | /** 343 | * The server is currently unavailable (because it is overloaded or down for maintenance). 344 | * Generally, this is a temporary state. 345 | */ 346 | SERVICE_UNAVAILABLE = 503, 347 | 348 | /** 349 | * The server was acting as a gateway or proxy and did not receive a timely response from the upstream server. 350 | */ 351 | GATEWAY_TIMEOUT = 504, 352 | 353 | /** 354 | * The server does not support the HTTP protocol version used in the request 355 | */ 356 | HTTP_VERSION_NOT_SUPPORTED = 505, 357 | 358 | /** 359 | * Transparent content negotiation for the request results in a circular reference. 360 | */ 361 | VARIANT_ALSO_NEGOTIATES = 506, 362 | 363 | /** 364 | * The server is unable to store the representation needed to complete the request. 365 | */ 366 | INSUFFICIENT_STORAGE = 507, 367 | 368 | /** 369 | * The server detected an infinite loop while processing the request. 370 | */ 371 | LOOP_DETECTED = 508, 372 | 373 | /** 374 | * Further extensions to the request are required for the server to fulfill it. 375 | */ 376 | NOT_EXTENDED = 510, 377 | 378 | /** 379 | * The client needs to authenticate to gain network access. 380 | * Intended for use by intercepting proxies used to control access to the network (e.g., "captive portals" used 381 | * to require agreement to Terms of Service before granting full Internet access via a Wi-Fi hotspot). 382 | */ 383 | NETWORK_AUTHENTICATION_REQUIRED = 511 384 | } 385 | 386 | 387 | /****************************************************************************** 388 | Export default 389 | ******************************************************************************/ 390 | 391 | export default HttpStatusCodes; 392 | -------------------------------------------------------------------------------- /lib/project-files/src/common/constants/Paths.ts: -------------------------------------------------------------------------------- 1 | 2 | export default { 3 | Base: '/api', 4 | Users: { 5 | Base: '/users', 6 | Get: '/all', 7 | Add: '/add', 8 | Update: '/update', 9 | Delete: '/delete/:id', 10 | }, 11 | } as const; 12 | -------------------------------------------------------------------------------- /lib/project-files/src/common/constants/index.ts: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | Enums 4 | ******************************************************************************/ 5 | 6 | // NOTE: These need to match the names of your ".env" files 7 | export enum NodeEnvs { 8 | Dev = 'development', 9 | Test = 'test', 10 | Production = 'production' 11 | } 12 | -------------------------------------------------------------------------------- /lib/project-files/src/common/util/misc.ts: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | Functions 4 | ******************************************************************************/ 5 | 6 | /** 7 | * Get a random number between 1 and 1,000,000,000,000 8 | */ 9 | export function getRandomInt(): number { 10 | return Math.floor(Math.random() * 1_000_000_000_000); 11 | } 12 | -------------------------------------------------------------------------------- /lib/project-files/src/common/util/route-errors.ts: -------------------------------------------------------------------------------- 1 | import { IParseObjectError } from 'jet-validators/utils'; 2 | 3 | import HttpStatusCodes from '@src/common/constants/HttpStatusCodes'; 4 | 5 | 6 | /****************************************************************************** 7 | Classes 8 | ******************************************************************************/ 9 | 10 | /** 11 | * Error with status code and message. 12 | */ 13 | export class RouteError extends Error { 14 | public status: HttpStatusCodes; 15 | 16 | public constructor(status: HttpStatusCodes, message: string) { 17 | super(message); 18 | this.status = status; 19 | } 20 | } 21 | 22 | /** 23 | * Handle "parseObj" errors. 24 | */ 25 | export class ValidationError extends RouteError { 26 | 27 | public static MESSAGE = 'The parseObj() function discovered one or ' + 28 | 'more errors.'; 29 | 30 | public constructor(errors: IParseObjectError[]) { 31 | const msg = JSON.stringify({ 32 | message: ValidationError.MESSAGE, 33 | errors, 34 | }); 35 | super(HttpStatusCodes.BAD_REQUEST, msg); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /lib/project-files/src/common/util/validators.ts: -------------------------------------------------------------------------------- 1 | import { isNumber, isDate } from 'jet-validators'; 2 | import { transform } from 'jet-validators/utils'; 3 | 4 | 5 | /****************************************************************************** 6 | Functions 7 | ******************************************************************************/ 8 | 9 | /** 10 | * Database relational key. 11 | */ 12 | export function isRelationalKey(arg: unknown): arg is number { 13 | return isNumber(arg) && arg >= -1; 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 | ); 23 | -------------------------------------------------------------------------------- /lib/project-files/src/index.ts: -------------------------------------------------------------------------------- 1 | import logger from 'jet-logger'; 2 | 3 | import ENV from '@src/common/constants/ENV'; 4 | import server from './server'; 5 | 6 | 7 | /****************************************************************************** 8 | Constants 9 | ******************************************************************************/ 10 | 11 | const SERVER_START_MSG = ( 12 | 'Express server started on port: ' + ENV.Port.toString() 13 | ); 14 | 15 | 16 | /****************************************************************************** 17 | Run 18 | ******************************************************************************/ 19 | 20 | // Start the server 21 | server.listen(ENV.Port, err => { 22 | if (!!err) { 23 | logger.err(err.message); 24 | } else { 25 | logger.info(SERVER_START_MSG); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /lib/project-files/src/models/User.ts: -------------------------------------------------------------------------------- 1 | import { isString } from 'jet-validators'; 2 | import { parseObject, TParseOnError } from 'jet-validators/utils'; 3 | 4 | import { isRelationalKey, transIsDate } from '@src/common/util/validators'; 5 | import { IModel } from './common/types'; 6 | 7 | 8 | /****************************************************************************** 9 | Constants 10 | ******************************************************************************/ 11 | 12 | const DEFAULT_USER_VALS = (): IUser => ({ 13 | id: -1, 14 | name: '', 15 | created: new Date(), 16 | email: '', 17 | }); 18 | 19 | 20 | /****************************************************************************** 21 | Types 22 | ******************************************************************************/ 23 | 24 | export interface IUser extends IModel { 25 | name: string; 26 | email: string; 27 | } 28 | 29 | 30 | /****************************************************************************** 31 | Setup 32 | ******************************************************************************/ 33 | 34 | // Initialize the "parseUser" function 35 | const parseUser = parseObject({ 36 | id: isRelationalKey, 37 | name: isString, 38 | email: isString, 39 | created: transIsDate, 40 | }); 41 | 42 | 43 | /****************************************************************************** 44 | Functions 45 | ******************************************************************************/ 46 | 47 | /** 48 | * New user object. 49 | */ 50 | function __new__(user?: Partial): IUser { 51 | const retVal = { ...DEFAULT_USER_VALS(), ...user }; 52 | return parseUser(retVal, errors => { 53 | throw new Error('Setup new user failed ' + JSON.stringify(errors, null, 2)); 54 | }); 55 | } 56 | 57 | /** 58 | * Check is a user object. For the route validation. 59 | */ 60 | function test(arg: unknown, errCb?: TParseOnError): arg is IUser { 61 | return !!parseUser(arg, errCb); 62 | } 63 | 64 | 65 | /****************************************************************************** 66 | Export default 67 | ******************************************************************************/ 68 | 69 | export default { 70 | new: __new__, 71 | test, 72 | } as const; -------------------------------------------------------------------------------- /lib/project-files/src/models/common/types/index.ts: -------------------------------------------------------------------------------- 1 | 2 | export interface IModel { 3 | id: number; 4 | created: Date; 5 | } 6 | -------------------------------------------------------------------------------- /lib/project-files/src/public/scripts/http.js: -------------------------------------------------------------------------------- 1 | var Http = (() => { 2 | 3 | // Setup request for json 4 | var getOptions = (verb, data) => { 5 | var options = { 6 | dataType: 'json', 7 | method: verb, 8 | headers: { 9 | 'Accept': 'application/json', 10 | 'Content-Type': 'application/json', 11 | }, 12 | }; 13 | if (!!data) { 14 | options.body = JSON.stringify(data); 15 | } 16 | return options; 17 | }; 18 | 19 | // Set Http methods 20 | return { 21 | get: (path) => fetch(path, getOptions('GET')), 22 | post: (path, data) => fetch(path, getOptions('POST', data)), 23 | put: (path, data) => fetch(path, getOptions('PUT', data)), 24 | delete: (path) => fetch(path, getOptions('DELETE')), 25 | }; 26 | })(); 27 | -------------------------------------------------------------------------------- /lib/project-files/src/public/scripts/users.js: -------------------------------------------------------------------------------- 1 | // @ts-nocheck 2 | 3 | /****************************************************************************** 4 | Constants 5 | ******************************************************************************/ 6 | 7 | const DateFormatter = new Intl.DateTimeFormat('en-US', { 8 | year: 'numeric', 9 | month: '2-digit', 10 | day: '2-digit', 11 | }); 12 | 13 | const formatDate = (date) => DateFormatter.format(new Date(date)); 14 | 15 | 16 | /****************************************************************************** 17 | Run 18 | ******************************************************************************/ 19 | 20 | // Start 21 | displayUsers(); 22 | 23 | 24 | /****************************************************************************** 25 | Functions 26 | ******************************************************************************/ 27 | 28 | /** 29 | * Call api 30 | */ 31 | function displayUsers() { 32 | Http 33 | .get('/api/users/all') 34 | .then(resp => resp.json()) 35 | .then(resp => { 36 | var allUsersTemplate = document.getElementById('all-users-template'), 37 | allUsersTemplateHtml = allUsersTemplate.innerHTML, 38 | template = Handlebars.compile(allUsersTemplateHtml); 39 | var allUsersAnchor = document.getElementById('all-users-anchor'); 40 | allUsersAnchor.innerHTML = template({ 41 | users: resp.users.map(user => ({ 42 | ...user, 43 | createdFormatted: formatDate(user.created), 44 | })), 45 | }); 46 | }); 47 | } 48 | 49 | // Setup event listener for button click 50 | document.addEventListener('click', event => { 51 | event.preventDefault(); 52 | var ele = event.target; 53 | if (ele.matches('#add-user-btn')) { 54 | addUser(); 55 | } else if (ele.matches('.edit-user-btn')) { 56 | showEditView(ele.parentNode.parentNode); 57 | } else if (ele.matches('.cancel-edit-btn')) { 58 | cancelEdit(ele.parentNode.parentNode); 59 | } else if (ele.matches('.submit-edit-btn')) { 60 | submitEdit(ele); 61 | } else if (ele.matches('.delete-user-btn')) { 62 | deleteUser(ele); 63 | } 64 | }, false); 65 | 66 | /** 67 | * Add a new user. 68 | */ 69 | function addUser() { 70 | var nameInput = document.getElementById('name-input'); 71 | var emailInput = document.getElementById('email-input'); 72 | var data = { 73 | user: { 74 | id: -1, 75 | name: nameInput.value, 76 | email: emailInput.value, 77 | created: new Date(), 78 | }, 79 | }; 80 | // Call api 81 | Http 82 | .post('/api/users/add', data) 83 | .then(() => { 84 | nameInput.value = ''; 85 | emailInput.value = ''; 86 | displayUsers(); 87 | }); 88 | } 89 | 90 | /** 91 | * Show edit view. 92 | */ 93 | function showEditView(userEle) { 94 | var normalView = userEle.getElementsByClassName('normal-view')[0]; 95 | var editView = userEle.getElementsByClassName('edit-view')[0]; 96 | normalView.style.display = 'none'; 97 | editView.style.display = 'block'; 98 | } 99 | 100 | /** 101 | * Cancel edit. 102 | */ 103 | function cancelEdit(userEle) { 104 | var normalView = userEle.getElementsByClassName('normal-view')[0]; 105 | var editView = userEle.getElementsByClassName('edit-view')[0]; 106 | normalView.style.display = 'block'; 107 | editView.style.display = 'none'; 108 | } 109 | 110 | /** 111 | * Submit edit. 112 | */ 113 | function submitEdit(ele) { 114 | var userEle = ele.parentNode.parentNode; 115 | var nameInput = userEle.getElementsByClassName('name-edit-input')[0]; 116 | var emailInput = userEle.getElementsByClassName('email-edit-input')[0]; 117 | var id = ele.getAttribute('data-user-id'); 118 | var created = ele.getAttribute('data-user-created'); 119 | console.log(ele, created) 120 | var data = { 121 | user: { 122 | id: Number(id), 123 | name: nameInput.value, 124 | email: emailInput.value, 125 | created: new Date(created), 126 | }, 127 | }; 128 | Http 129 | .put('/api/users/update', data) 130 | .then(() => displayUsers()); 131 | } 132 | 133 | /** 134 | * Delete a user 135 | */ 136 | function deleteUser(ele) { 137 | var id = ele.getAttribute('data-user-id'); 138 | Http 139 | .delete('/api/users/delete/' + id) 140 | .then(() => displayUsers()); 141 | } 142 | -------------------------------------------------------------------------------- /lib/project-files/src/public/stylesheets/users.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 100px; 3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; 4 | } 5 | 6 | body .users-column { 7 | display: inline-block; 8 | margin-right: 2em; 9 | vertical-align: top; 10 | } 11 | 12 | body .users-column .column-header { 13 | padding-bottom: 5px; 14 | font-weight: 700; 15 | font-size: 1.2em; 16 | } 17 | 18 | 19 | /** Add User Column **/ 20 | 21 | body .add-user-col input { 22 | margin-bottom: 10px; 23 | } 24 | 25 | body .add-user-col #add-user-btn { 26 | margin-top: 2px; 27 | margin-bottom: 10px; 28 | } 29 | 30 | 31 | /** Users Display Column **/ 32 | 33 | .email-edit { 34 | padding-bottom: 8px; 35 | } 36 | 37 | body .users-column .user-display-ele { 38 | padding-bottom: 10px; 39 | } 40 | 41 | body .users-column .user-display-ele button { 42 | margin-top: 2px; 43 | margin-bottom: 10px; 44 | } 45 | 46 | body .users-column .user-display-ele .edit-view { 47 | display: none; 48 | } 49 | -------------------------------------------------------------------------------- /lib/project-files/src/repos/MockOrm.ts: -------------------------------------------------------------------------------- 1 | import jsonfile from 'jsonfile'; 2 | 3 | import ENV from '@src/common/constants/ENV'; 4 | import { NodeEnvs } from '@src/common/constants'; 5 | import { IUser } from '@src/models/User'; 6 | 7 | 8 | /****************************************************************************** 9 | Constants 10 | ******************************************************************************/ 11 | 12 | const DB_FILE_NAME = ( 13 | ENV.NodeEnv === NodeEnvs.Test 14 | ? 'database.test.json' 15 | : 'database.json' 16 | ); 17 | 18 | 19 | /****************************************************************************** 20 | Types 21 | ******************************************************************************/ 22 | 23 | interface IDb { 24 | users: IUser[]; 25 | } 26 | 27 | 28 | /****************************************************************************** 29 | Functions 30 | ******************************************************************************/ 31 | 32 | /** 33 | * Fetch the json from the file. 34 | */ 35 | function openDb(): Promise { 36 | return jsonfile.readFile(__dirname + '/' + DB_FILE_NAME) as Promise; 37 | } 38 | 39 | /** 40 | * Update the file. 41 | */ 42 | function saveDb(db: IDb): Promise { 43 | return jsonfile.writeFile((__dirname + '/' + DB_FILE_NAME), db); 44 | } 45 | 46 | /** 47 | * Empty the database 48 | */ 49 | function cleanDb(): Promise { 50 | return jsonfile.writeFile((__dirname + '/' + DB_FILE_NAME), {}); 51 | } 52 | 53 | 54 | /****************************************************************************** 55 | Export default 56 | ******************************************************************************/ 57 | 58 | export default { 59 | openDb, 60 | saveDb, 61 | cleanDb, 62 | } as const; 63 | -------------------------------------------------------------------------------- /lib/project-files/src/repos/UserRepo.ts: -------------------------------------------------------------------------------- 1 | import { IUser } from '@src/models/User'; 2 | import { getRandomInt } from '@src/common/util/misc'; 3 | 4 | import orm from './MockOrm'; 5 | 6 | 7 | /****************************************************************************** 8 | Functions 9 | ******************************************************************************/ 10 | 11 | /** 12 | * Get one user. 13 | */ 14 | async function getOne(email: string): Promise { 15 | const db = await orm.openDb(); 16 | for (const user of db.users) { 17 | if (user.email === email) { 18 | return user; 19 | } 20 | } 21 | return null; 22 | } 23 | 24 | /** 25 | * See if a user with the given id exists. 26 | */ 27 | async function persists(id: number): Promise { 28 | const db = await orm.openDb(); 29 | for (const user of db.users) { 30 | if (user.id === id) { 31 | return true; 32 | } 33 | } 34 | return false; 35 | } 36 | 37 | /** 38 | * Get all users. 39 | */ 40 | async function getAll(): Promise { 41 | const db = await orm.openDb(); 42 | return db.users; 43 | } 44 | 45 | /** 46 | * Add one user. 47 | */ 48 | async function add(user: IUser): Promise { 49 | const db = await orm.openDb(); 50 | user.id = getRandomInt(); 51 | db.users.push(user); 52 | return orm.saveDb(db); 53 | } 54 | 55 | /** 56 | * Update a user. 57 | */ 58 | async function update(user: IUser): Promise { 59 | const db = await orm.openDb(); 60 | for (let i = 0; i < db.users.length; i++) { 61 | if (db.users[i].id === user.id) { 62 | const dbUser = db.users[i]; 63 | db.users[i] = { 64 | ...dbUser, 65 | name: user.name, 66 | email: user.email, 67 | }; 68 | return orm.saveDb(db); 69 | } 70 | } 71 | } 72 | 73 | /** 74 | * Delete one user. 75 | */ 76 | async function delete_(id: number): Promise { 77 | const db = await orm.openDb(); 78 | for (let i = 0; i < db.users.length; i++) { 79 | if (db.users[i].id === id) { 80 | db.users.splice(i, 1); 81 | return orm.saveDb(db); 82 | } 83 | } 84 | } 85 | 86 | 87 | // **** Unit-Tests Only **** // 88 | 89 | /** 90 | * Delete every user record. 91 | */ 92 | async function deleteAllUsers(): Promise { 93 | const db = await orm.openDb(); 94 | db.users = []; 95 | return orm.saveDb(db); 96 | } 97 | 98 | /** 99 | * Insert multiple users. Can't do multiple at once cause using a plain file 100 | * for nmow. 101 | */ 102 | async function insertMult( 103 | users: IUser[] | readonly IUser[], 104 | ): Promise { 105 | const db = await orm.openDb(), 106 | usersF = [ ...users ]; 107 | for (const user of usersF) { 108 | user.id = getRandomInt(); 109 | user.created = new Date(); 110 | } 111 | db.users = [ ...db.users, ...users ]; 112 | await orm.saveDb(db); 113 | return usersF; 114 | } 115 | 116 | 117 | /****************************************************************************** 118 | Export default 119 | ******************************************************************************/ 120 | 121 | export default { 122 | getOne, 123 | persists, 124 | getAll, 125 | add, 126 | update, 127 | delete: delete_, 128 | deleteAllUsers, 129 | insertMult, 130 | } as const; 131 | -------------------------------------------------------------------------------- /lib/project-files/src/repos/database.json: -------------------------------------------------------------------------------- 1 | {"users":[{"id":366115170645,"name":"Sean Maxwell","email":"smaxwell@example.com","created":"2024-03-22T05:14:36.252Z"},{"id":310946254456,"name":"John Smith","email":"john.smith@example.com","created":"2024-03-22T05:20:55.079Z"},{"id":143027113460,"name":"Gordan Freeman","email":"nova@prospect.com","created":"2024-03-22T05:42:18.895Z"}]} 2 | -------------------------------------------------------------------------------- /lib/project-files/src/repos/database.test.json: -------------------------------------------------------------------------------- 1 | {"users":[{"id":701029715561,"name":"Bill","created":"2025-06-03T15:16:08.805Z","email":"sean.maxwell@gmail.com"},{"id":625166376007,"name":"John Smith","created":"2025-06-03T15:16:08.805Z","email":"john.smith@gmail.com"},{"id":595966642687,"name":"Gordan Freeman","created":"2025-06-03T15:16:08.805Z","email":"gordan.freeman@gmail.com"}]} 2 | -------------------------------------------------------------------------------- /lib/project-files/src/routes/UserRoutes.ts: -------------------------------------------------------------------------------- 1 | import { isNumber } from 'jet-validators'; 2 | import { transform } from 'jet-validators/utils'; 3 | 4 | import HttpStatusCodes from '@src/common/constants/HttpStatusCodes'; 5 | import UserService from '@src/services/UserService'; 6 | import User from '@src/models/User'; 7 | 8 | import { IReq, IRes } from './common/types'; 9 | import { parseReq } from './common/util'; 10 | 11 | 12 | /****************************************************************************** 13 | Constants 14 | ******************************************************************************/ 15 | 16 | const Validators = { 17 | add: parseReq({ user: User.test }), 18 | update: parseReq({ user: User.test }), 19 | delete: parseReq({ id: transform(Number, isNumber) }), 20 | } as const; 21 | 22 | 23 | /****************************************************************************** 24 | Functions 25 | ******************************************************************************/ 26 | 27 | /** 28 | * Get all users. 29 | */ 30 | async function getAll(_: IReq, res: IRes) { 31 | const users = await UserService.getAll(); 32 | res.status(HttpStatusCodes.OK).json({ users }); 33 | } 34 | 35 | /** 36 | * Add one user. 37 | */ 38 | async function add(req: IReq, res: IRes) { 39 | const { user } = Validators.add(req.body); 40 | await UserService.addOne(user); 41 | res.status(HttpStatusCodes.CREATED).end(); 42 | } 43 | 44 | /** 45 | * Update one user. 46 | */ 47 | async function update(req: IReq, res: IRes) { 48 | const { user } = Validators.update(req.body); 49 | await UserService.updateOne(user); 50 | res.status(HttpStatusCodes.OK).end(); 51 | } 52 | 53 | /** 54 | * Delete one user. 55 | */ 56 | async function delete_(req: IReq, res: IRes) { 57 | const { id } = Validators.delete(req.params); 58 | await UserService.delete(id); 59 | res.status(HttpStatusCodes.OK).end(); 60 | } 61 | 62 | 63 | /****************************************************************************** 64 | Export default 65 | ******************************************************************************/ 66 | 67 | export default { 68 | getAll, 69 | add, 70 | update, 71 | delete: delete_, 72 | } as const; 73 | -------------------------------------------------------------------------------- /lib/project-files/src/routes/common/types/index.ts: -------------------------------------------------------------------------------- 1 | import { Response, Request } from 'express'; 2 | 3 | 4 | /****************************************************************************** 5 | Types 6 | ******************************************************************************/ 7 | 8 | type TRecord = Record; 9 | export type IReq = Request; 10 | export type IRes = Response; 11 | 12 | -------------------------------------------------------------------------------- /lib/project-files/src/routes/common/util/index.ts: -------------------------------------------------------------------------------- 1 | import { parseObject, TSchema } from 'jet-validators/utils'; 2 | 3 | import { ValidationError } from '@src/common/util/route-errors'; 4 | 5 | 6 | /****************************************************************************** 7 | Functions 8 | ******************************************************************************/ 9 | 10 | /** 11 | * Throw a "ParseObjError" when "parseObject" fails. Also extract a nested 12 | * "ParseObjError" and add it to the nestedErrors array. 13 | */ 14 | export function parseReq(schema: U) { 15 | return parseObject(schema, errors => { 16 | throw new ValidationError(errors); 17 | }); 18 | } 19 | -------------------------------------------------------------------------------- /lib/project-files/src/routes/index.ts: -------------------------------------------------------------------------------- 1 | import { Router } from 'express'; 2 | 3 | import Paths from '@src/common/constants/Paths'; 4 | import UserRoutes from './UserRoutes'; 5 | 6 | 7 | /****************************************************************************** 8 | Setup 9 | ******************************************************************************/ 10 | 11 | const apiRouter = Router(); 12 | 13 | 14 | // ** Add UserRouter ** // 15 | 16 | // Init router 17 | const userRouter = Router(); 18 | 19 | // Get all users 20 | userRouter.get(Paths.Users.Get, UserRoutes.getAll); 21 | userRouter.post(Paths.Users.Add, UserRoutes.add); 22 | userRouter.put(Paths.Users.Update, UserRoutes.update); 23 | userRouter.delete(Paths.Users.Delete, UserRoutes.delete); 24 | 25 | // Add UserRouter 26 | apiRouter.use(Paths.Users.Base, userRouter); 27 | 28 | 29 | /****************************************************************************** 30 | Export default 31 | ******************************************************************************/ 32 | 33 | export default apiRouter; 34 | -------------------------------------------------------------------------------- /lib/project-files/src/server.ts: -------------------------------------------------------------------------------- 1 | import morgan from 'morgan'; 2 | import path from 'path'; 3 | import helmet from 'helmet'; 4 | import express, { Request, Response, NextFunction } from 'express'; 5 | import logger from 'jet-logger'; 6 | 7 | import BaseRouter from '@src/routes'; 8 | 9 | import Paths from '@src/common/constants/Paths'; 10 | import ENV from '@src/common/constants/ENV'; 11 | import HttpStatusCodes from '@src/common/constants/HttpStatusCodes'; 12 | import { RouteError } from '@src/common/util/route-errors'; 13 | import { NodeEnvs } from '@src/common/constants'; 14 | 15 | 16 | /****************************************************************************** 17 | Setup 18 | ******************************************************************************/ 19 | 20 | const app = express(); 21 | 22 | 23 | // **** Middleware **** // 24 | 25 | // Basic middleware 26 | app.use(express.json()); 27 | app.use(express.urlencoded({extended: true})); 28 | 29 | // Show routes called in console during development 30 | if (ENV.NodeEnv === NodeEnvs.Dev) { 31 | app.use(morgan('dev')); 32 | } 33 | 34 | // Security 35 | if (ENV.NodeEnv === NodeEnvs.Production) { 36 | // eslint-disable-next-line n/no-process-env 37 | if (!process.env.DISABLE_HELMET) { 38 | app.use(helmet()); 39 | } 40 | } 41 | 42 | // Add APIs, must be after middleware 43 | app.use(Paths.Base, BaseRouter); 44 | 45 | // Add error handler 46 | app.use((err: Error, _: Request, res: Response, next: NextFunction) => { 47 | if (ENV.NodeEnv !== NodeEnvs.Test.valueOf()) { 48 | logger.err(err, true); 49 | } 50 | let status = HttpStatusCodes.BAD_REQUEST; 51 | if (err instanceof RouteError) { 52 | status = err.status; 53 | res.status(status).json({ error: err.message }); 54 | } 55 | return next(err); 56 | }); 57 | 58 | 59 | // **** FrontEnd Content **** // 60 | 61 | // Set views directory (html) 62 | const viewsDir = path.join(__dirname, 'views'); 63 | app.set('views', viewsDir); 64 | 65 | // Set static directory (js and css). 66 | const staticDir = path.join(__dirname, 'public'); 67 | app.use(express.static(staticDir)); 68 | 69 | // Nav to users pg by default 70 | app.get('/', (_: Request, res: Response) => { 71 | return res.redirect('/users'); 72 | }); 73 | 74 | // Redirect to login if not logged in. 75 | app.get('/users', (_: Request, res: Response) => { 76 | return res.sendFile('users.html', { root: viewsDir }); 77 | }); 78 | 79 | 80 | /****************************************************************************** 81 | Export default 82 | ******************************************************************************/ 83 | 84 | export default app; 85 | -------------------------------------------------------------------------------- /lib/project-files/src/services/UserService.ts: -------------------------------------------------------------------------------- 1 | import { RouteError } from '@src/common/util/route-errors'; 2 | import HttpStatusCodes from '@src/common/constants/HttpStatusCodes'; 3 | 4 | import UserRepo from '@src/repos/UserRepo'; 5 | import { IUser } from '@src/models/User'; 6 | 7 | 8 | /****************************************************************************** 9 | Constants 10 | ******************************************************************************/ 11 | 12 | export const USER_NOT_FOUND_ERR = 'User not found'; 13 | 14 | 15 | /****************************************************************************** 16 | Functions 17 | ******************************************************************************/ 18 | 19 | /** 20 | * Get all users. 21 | */ 22 | function getAll(): Promise { 23 | return UserRepo.getAll(); 24 | } 25 | 26 | /** 27 | * Add one user. 28 | */ 29 | function addOne(user: IUser): Promise { 30 | return UserRepo.add(user); 31 | } 32 | 33 | /** 34 | * Update one user. 35 | */ 36 | async function updateOne(user: IUser): Promise { 37 | const persists = await UserRepo.persists(user.id); 38 | if (!persists) { 39 | throw new RouteError( 40 | HttpStatusCodes.NOT_FOUND, 41 | USER_NOT_FOUND_ERR, 42 | ); 43 | } 44 | // Return user 45 | return UserRepo.update(user); 46 | } 47 | 48 | /** 49 | * Delete a user by their id. 50 | */ 51 | async function _delete(id: number): Promise { 52 | const persists = await UserRepo.persists(id); 53 | if (!persists) { 54 | throw new RouteError( 55 | HttpStatusCodes.NOT_FOUND, 56 | USER_NOT_FOUND_ERR, 57 | ); 58 | } 59 | // Delete user 60 | return UserRepo.delete(id); 61 | } 62 | 63 | 64 | /****************************************************************************** 65 | Export default 66 | ******************************************************************************/ 67 | 68 | export default { 69 | getAll, 70 | addOne, 71 | updateOne, 72 | delete: _delete, 73 | } as const; 74 | -------------------------------------------------------------------------------- /lib/project-files/src/views/users.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Users 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
32 |
33 | Add User: 34 |
35 |
36 | 42 |
43 |
44 | 50 |
51 |
52 | 58 |
59 |
60 | 61 | 62 |
63 |
64 | Users: 65 |
66 | 67 |
68 | 127 |
128 | 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /lib/project-files/tests/common/Paths.ts: -------------------------------------------------------------------------------- 1 | import jetPaths from 'jet-paths'; 2 | import Paths from '@src/common/constants/Paths'; 3 | 4 | 5 | export default jetPaths(Paths); 6 | -------------------------------------------------------------------------------- /lib/project-files/tests/common/types/index.ts: -------------------------------------------------------------------------------- 1 | import { Response } from 'supertest'; 2 | 3 | 4 | /****************************************************************************** 5 | Types 6 | ******************************************************************************/ 7 | 8 | // Use generics to add properties to 'body' 9 | export type TRes = Omit & { 10 | body: T & { error?: string | IErrObj }, 11 | }; 12 | 13 | interface IErrObj { 14 | message: string; 15 | [key: string]: unknown; 16 | } 17 | -------------------------------------------------------------------------------- /lib/project-files/tests/common/util/index.ts: -------------------------------------------------------------------------------- 1 | import { Response } from 'supertest'; 2 | import { IParseObjectError, parseJson } from 'jet-validators/utils'; 3 | import { isString } from 'jet-validators'; 4 | 5 | 6 | /****************************************************************************** 7 | Types 8 | ******************************************************************************/ 9 | 10 | // Use generics to add properties to 'body' 11 | export type TRes = Omit & { 12 | body: T & { error?: string | IErrObj }, 13 | }; 14 | 15 | interface IErrObj { 16 | message: string; 17 | [key: string]: unknown; 18 | } 19 | 20 | interface IValidationErr { 21 | message: string; 22 | errors: IParseObjectError[]; 23 | } 24 | 25 | 26 | /****************************************************************************** 27 | Functions 28 | ******************************************************************************/ 29 | 30 | /** 31 | * JSON parse a validation error. 32 | */ 33 | export function parseValidationErr(arg: unknown): IValidationErr { 34 | if (!isString(arg)) { 35 | throw new Error('Not a string'); 36 | } 37 | return parseJson(arg); 38 | } 39 | -------------------------------------------------------------------------------- /lib/project-files/tests/support/setup.ts: -------------------------------------------------------------------------------- 1 | 2 | import { beforeAll } from 'vitest'; 3 | import supertest, { Test } from 'supertest'; 4 | import TestAgent from 'supertest/lib/agent'; 5 | 6 | import app from '@src/server'; 7 | import MockOrm from '@src/repos/MockOrm'; 8 | 9 | 10 | /****************************************************************************** 11 | Run 12 | ******************************************************************************/ 13 | 14 | let agent: TestAgent; 15 | 16 | beforeAll(async () => { 17 | agent = supertest.agent(app); 18 | await MockOrm.cleanDb(); 19 | }); 20 | 21 | 22 | /****************************************************************************** 23 | Export 24 | ******************************************************************************/ 25 | 26 | export { agent }; 27 | -------------------------------------------------------------------------------- /lib/project-files/tests/users.test.ts: -------------------------------------------------------------------------------- 1 | import insertUrlParams from 'inserturlparams'; 2 | import { customDeepCompare } from 'jet-validators/utils'; 3 | 4 | import UserRepo from '@src/repos/UserRepo'; 5 | import User, { IUser } from '@src/models/User'; 6 | import { USER_NOT_FOUND_ERR } from '@src/services/UserService'; 7 | 8 | import HttpStatusCodes from '@src/common/constants/HttpStatusCodes'; 9 | import { ValidationError } from '@src/common/util/route-errors'; 10 | 11 | import Paths from './common/Paths'; 12 | import { parseValidationErr, TRes } from './common/util'; 13 | import { agent } from './support/setup'; 14 | 15 | 16 | /****************************************************************************** 17 | Constants 18 | ******************************************************************************/ 19 | 20 | // Dummy users for GET req 21 | const DB_USERS = [ 22 | User.new({ name: 'Sean Maxwell', email: 'sean.maxwell@gmail.com' }), 23 | User.new({ name: 'John Smith', email: 'john.smith@gmail.com' }), 24 | User.new({ name: 'Gordan Freeman', email: 'gordan.freeman@gmail.com' }), 25 | ] as const; 26 | 27 | // Don't compare "id" and "created" cause those are set dynamically by the 28 | // database 29 | const compareUserArrays = customDeepCompare({ 30 | onlyCompareProps: ['name', 'email'], 31 | }); 32 | 33 | 34 | /****************************************************************************** 35 | Tests 36 | IMPORTANT: Following TypeScript best practices, we test all scenarios that 37 | can be triggered by a user under normal circumstances. Not all theoretically 38 | scenarios (i.e. a failed database connection). 39 | ******************************************************************************/ 40 | 41 | describe('UserRouter', () => { 42 | 43 | let dbUsers: IUser[] = []; 44 | 45 | // Run before all tests 46 | beforeEach(async () => { 47 | await UserRepo.deleteAllUsers(); 48 | dbUsers = await UserRepo.insertMult(DB_USERS); 49 | }); 50 | 51 | // Get all users 52 | describe(`"GET:${Paths.Users.Get}"`, () => { 53 | 54 | // Success 55 | it('should return a JSON object with all the users and a status code ' + 56 | `of "${HttpStatusCodes.OK}" if the request was successful.`, async () => { 57 | const res: TRes<{ users: IUser[]}> = await agent.get(Paths.Users.Get); 58 | expect(res.status).toBe(HttpStatusCodes.OK); 59 | expect(compareUserArrays(res.body.users, DB_USERS)).toBeTruthy(); 60 | }); 61 | }); 62 | 63 | // Test add user 64 | describe(`"POST:${Paths.Users.Add}"`, () => { 65 | 66 | // Test add user success 67 | it(`should return a status code of "${HttpStatusCodes.CREATED}" if the ` + 68 | 'request was successful.', async () => { 69 | const user = User.new({ name: 'a', email: 'a@a.com' }), 70 | res = await agent.post(Paths.Users.Add).send({ user }); 71 | expect(res.status).toBe(HttpStatusCodes.CREATED); 72 | }); 73 | 74 | // Missing param 75 | it('should return a JSON object with an error message of and a status ' + 76 | `code of "${HttpStatusCodes.BAD_REQUEST}" if the user param was ` + 77 | 'missing.', async () => { 78 | const res: TRes = await agent.post(Paths.Users.Add).send({ user: null }); 79 | expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST); 80 | const errorObj = parseValidationErr(res.body.error); 81 | expect(errorObj.message).toBe(ValidationError.MESSAGE); 82 | expect(errorObj.errors[0].prop).toBe('user'); 83 | }); 84 | }); 85 | 86 | // Update users 87 | describe(`"PUT:${Paths.Users.Update}"`, () => { 88 | 89 | // Success 90 | it(`should return a status code of "${HttpStatusCodes.OK}" if the ` + 91 | 'request was successful.', async () => { 92 | const user = DB_USERS[0]; 93 | user.name = 'Bill'; 94 | const res = await agent.put(Paths.Users.Update).send({ user }); 95 | expect(res.status).toBe(HttpStatusCodes.OK); 96 | }); 97 | 98 | // Id is the wrong data type 99 | it('should return a JSON object with an error message and a status code ' + 100 | `of "${HttpStatusCodes.BAD_REQUEST}" if the user param was missing`, 101 | async () => { 102 | const user = User.new(); 103 | user.id = ('5' as unknown as number); 104 | const res: TRes = await agent.put(Paths.Users.Update).send({ user }); 105 | expect(res.status).toBe(HttpStatusCodes.BAD_REQUEST); 106 | const errorObj = parseValidationErr(res.body.error); 107 | expect(errorObj.message).toBe(ValidationError.MESSAGE); 108 | expect(errorObj.errors[0].prop).toBe('user'); 109 | expect(errorObj.errors[0].children?.[0].prop).toBe('id'); 110 | }); 111 | 112 | // User not found 113 | it('should return a JSON object with the error message of ' + 114 | `"${USER_NOT_FOUND_ERR}" and a status code of ` + 115 | `"${HttpStatusCodes.NOT_FOUND}" if the id was not found.`, async () => { 116 | const user = User.new({ id: 4, name: 'a', email: 'a@a.com' }), 117 | res: TRes = await agent.put(Paths.Users.Update).send({ user }); 118 | expect(res.status).toBe(HttpStatusCodes.NOT_FOUND); 119 | expect(res.body.error).toBe(USER_NOT_FOUND_ERR); 120 | }); 121 | }); 122 | 123 | // Delete User 124 | describe(`"DELETE:${Paths.Users.Delete}"`, () => { 125 | 126 | const getPath = (id: number) => insertUrlParams(Paths.Users.Delete, 127 | { id }); 128 | 129 | // Success 130 | it(`should return a status code of "${HttpStatusCodes.OK}" if the ` + 131 | 'request was successful.', async () => { 132 | const id = dbUsers[0].id, 133 | res = await agent.delete(getPath(id)); 134 | expect(res.status).toBe(HttpStatusCodes.OK); 135 | }); 136 | 137 | // User not found 138 | it('should return a JSON object with the error message of ' + 139 | `"${USER_NOT_FOUND_ERR}" and a status code of ` + 140 | `"${HttpStatusCodes.NOT_FOUND}" if the id was not found.`, async () => { 141 | const res: TRes = await agent.delete(getPath(-1)); 142 | expect(res.status).toBe(HttpStatusCodes.NOT_FOUND); 143 | expect(res.body.error).toBe(USER_NOT_FOUND_ERR); 144 | }); 145 | }); 146 | }); 147 | -------------------------------------------------------------------------------- /lib/project-files/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES2020", 4 | "module": "CommonJS", 5 | "lib": ["ES2020"], 6 | "strict": true, 7 | "baseUrl": "./", 8 | "esModuleInterop": true, 9 | "skipLibCheck": true, 10 | "forceConsistentCasingInFileNames": true, 11 | "paths": { 12 | "@src/*": ["src/*"] 13 | }, 14 | "useUnknownInCatchVariables": false, 15 | "types": ["vitest/globals"], 16 | }, 17 | "ts-node": { 18 | "swc": true, 19 | "require": [ 20 | "tsconfig-paths/register", 21 | "./config.ts" 22 | ], 23 | }, 24 | "include": [ 25 | "src/**/*.ts", 26 | "tests/**/*.ts", 27 | "config.ts", 28 | "scripts", 29 | "eslint.config.ts", 30 | "vitest.config.mts" 31 | ], 32 | "exclude": [ 33 | "src/public/*" 34 | ] 35 | } 36 | -------------------------------------------------------------------------------- /lib/project-files/tsconfig.prod.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES2020", 4 | "module": "CommonJS", 5 | "lib": ["ES2020"], 6 | "strict": true, 7 | "baseUrl": "./", 8 | "outDir": "temp", 9 | "esModuleInterop": true, 10 | "skipLibCheck": true, 11 | "forceConsistentCasingInFileNames": true, 12 | "paths": { 13 | "@src/*": ["src/*"] 14 | }, 15 | "useUnknownInCatchVariables": false, 16 | "sourceMap": false, 17 | "removeComments": true 18 | }, 19 | "include": [ 20 | "src/**/*.ts", 21 | "config.ts" 22 | ], 23 | "exclude": [ 24 | "tests", 25 | "src/public/", 26 | "scripts", 27 | "eslint.config.ts", 28 | "vitest.config.js" 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /lib/project-files/vitest.config.mts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vitest/config'; 2 | import path from 'path'; 3 | 4 | const config = defineConfig({ 5 | test: { 6 | globals: true, 7 | environment: 'node', 8 | setupFiles: ['config.ts', './tests/support/setup.ts'], 9 | isolate: true, 10 | }, 11 | resolve: { 12 | alias: { 13 | '@src': path.resolve(__dirname, './src'), 14 | }, 15 | }, 16 | }); 17 | 18 | export default config; 19 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-generator-typescript", 3 | "version": "2.5.6", 4 | "lockfileVersion": 3, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "express-generator-typescript", 9 | "version": "2.5.6", 10 | "license": "MIT", 11 | "dependencies": { 12 | "edit-json-file": "^1.7.0", 13 | "jet-validators": "^1.0.6", 14 | "ncp": "^2.0.0" 15 | }, 16 | "bin": { 17 | "express-generator-typescript": "bin/cli.js" 18 | } 19 | }, 20 | "node_modules/edit-json-file": { 21 | "version": "1.8.0", 22 | "resolved": "https://registry.npmjs.org/edit-json-file/-/edit-json-file-1.8.0.tgz", 23 | "integrity": "sha512-IBOpbe2aQufNl5oZ4jsr2AmNVUy5bO7jS5hk0cCyWhOLdH59Xv41B3XQObE/JB89Ae5qDY9hVsq13/hgGhFBZg==", 24 | "dependencies": { 25 | "find-value": "^1.0.12", 26 | "iterate-object": "^1.3.4", 27 | "r-json": "^1.2.10", 28 | "set-value": "^4.1.0", 29 | "w-json": "^1.3.10" 30 | } 31 | }, 32 | "node_modules/find-value": { 33 | "version": "1.0.12", 34 | "resolved": "https://registry.npmjs.org/find-value/-/find-value-1.0.12.tgz", 35 | "integrity": "sha512-OCpo8LTk8eZ2sdDCwbU2Lc3ivYsdM6yod6jP2jHcNEFcjPhkgH0+POzTIol7xx1LZgtbI5rkO5jqxsG5MWtPjQ==" 36 | }, 37 | "node_modules/is-plain-object": { 38 | "version": "2.0.4", 39 | "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-2.0.4.tgz", 40 | "integrity": "sha512-h5PpgXkWitc38BBMYawTYMWJHFZJVnBquFE57xFpjB8pJFiF6gZ+bU+WyI/yqXiFR5mdLsgYNaPe8uao6Uv9Og==", 41 | "dependencies": { 42 | "isobject": "^3.0.1" 43 | }, 44 | "engines": { 45 | "node": ">=0.10.0" 46 | } 47 | }, 48 | "node_modules/is-primitive": { 49 | "version": "3.0.1", 50 | "resolved": "https://registry.npmjs.org/is-primitive/-/is-primitive-3.0.1.tgz", 51 | "integrity": "sha512-GljRxhWvlCNRfZyORiH77FwdFwGcMO620o37EOYC0ORWdq+WYNVqW0w2Juzew4M+L81l6/QS3t5gkkihyRqv9w==", 52 | "engines": { 53 | "node": ">=0.10.0" 54 | } 55 | }, 56 | "node_modules/isobject": { 57 | "version": "3.0.1", 58 | "resolved": "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz", 59 | "integrity": "sha512-WhB9zCku7EGTj/HQQRz5aUQEUeoQZH2bWcltRErOpymJ4boYE6wL9Tbr23krRPSZ+C5zqNSrSw+Cc7sZZ4b7vg==", 60 | "engines": { 61 | "node": ">=0.10.0" 62 | } 63 | }, 64 | "node_modules/iterate-object": { 65 | "version": "1.3.4", 66 | "resolved": "https://registry.npmjs.org/iterate-object/-/iterate-object-1.3.4.tgz", 67 | "integrity": "sha512-4dG1D1x/7g8PwHS9aK6QV5V94+ZvyP4+d19qDv43EzImmrndysIl4prmJ1hWWIGCqrZHyaHBm6BSEWHOLnpoNw==" 68 | }, 69 | "node_modules/jet-validators": { 70 | "version": "1.0.6", 71 | "resolved": "https://registry.npmjs.org/jet-validators/-/jet-validators-1.0.6.tgz", 72 | "integrity": "sha512-B83jcNkNqCykjOGbcW47f/74Fcj9HfqWME9DCeeUgKpgP5kaq0aFnjwS7I9msEC9j1MVnTe0Q7JfdcXnjBpePw==" 73 | }, 74 | "node_modules/ncp": { 75 | "version": "2.0.0", 76 | "resolved": "https://registry.npmjs.org/ncp/-/ncp-2.0.0.tgz", 77 | "integrity": "sha1-GVoh1sRuNh0vsSgbo4uR6d9727M=", 78 | "bin": { 79 | "ncp": "bin/ncp" 80 | } 81 | }, 82 | "node_modules/r-json": { 83 | "version": "1.3.0", 84 | "resolved": "https://registry.npmjs.org/r-json/-/r-json-1.3.0.tgz", 85 | "integrity": "sha512-xesd+RHCpymPCYd9DvDvUr1w1IieSChkqYF1EpuAYrvCfLXji9NP36DvyYZJZZB5soVDvZ0WUtBoZaU1g5Yt9A==", 86 | "dependencies": { 87 | "w-json": "1.3.10" 88 | } 89 | }, 90 | "node_modules/set-value": { 91 | "version": "4.1.0", 92 | "resolved": "https://registry.npmjs.org/set-value/-/set-value-4.1.0.tgz", 93 | "integrity": "sha512-zTEg4HL0RwVrqcWs3ztF+x1vkxfm0lP+MQQFPiMJTKVceBwEV0A569Ou8l9IYQG8jOZdMVI1hGsc0tmeD2o/Lw==", 94 | "funding": [ 95 | "https://github.com/sponsors/jonschlinkert", 96 | "https://paypal.me/jonathanschlinkert", 97 | "https://jonschlinkert.dev/sponsor" 98 | ], 99 | "dependencies": { 100 | "is-plain-object": "^2.0.4", 101 | "is-primitive": "^3.0.1" 102 | }, 103 | "engines": { 104 | "node": ">=11.0" 105 | } 106 | }, 107 | "node_modules/w-json": { 108 | "version": "1.3.10", 109 | "resolved": "https://registry.npmjs.org/w-json/-/w-json-1.3.10.tgz", 110 | "integrity": "sha512-XadVyw0xE+oZ5FGApXsdswv96rOhStzKqL53uSe5UaTadABGkWIg1+DTx8kiZ/VqTZTBneoL0l65RcPe4W3ecw==" 111 | } 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-generator-typescript", 3 | "version": "2.7.2", 4 | "description": "Generate new Express applications similar to express-generate which but sets it up to use TypeScript instead", 5 | "scripts": { 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", 9 | "start": "node bin/cli.js", 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "main": "lib/cli.js", 13 | "bin": { 14 | "express-generator-typescript": "bin/cli.js" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git+https://github.com/seanpmaxwell/express-generator-typescript.git" 19 | }, 20 | "keywords": [ 21 | "express", 22 | "ex", 23 | "generator", 24 | "generate", 25 | "gen", 26 | "typescript", 27 | "ts", 28 | "types", 29 | "typing", 30 | "overnight", 31 | "overnightjs", 32 | "js", 33 | "new", 34 | "app", 35 | "express-ts", 36 | "decorators", 37 | "decorate", 38 | "test", 39 | "testing", 40 | "routes", 41 | "router", 42 | "routing" 43 | ], 44 | "author": "sean maxwell", 45 | "license": "MIT", 46 | "bugs": { 47 | "url": "https://github.com/seanpmaxwell/express-generator-typescript/issues" 48 | }, 49 | "homepage": "https://github.com/seanpmaxwell/express-generator-typescript#readme", 50 | "dependencies": { 51 | "edit-json-file": "^1.7.0", 52 | "ncp": "^2.0.0" 53 | } 54 | } 55 | --------------------------------------------------------------------------------