From 9178fee5193759e756c508907e9f7b2abdcfee04 Mon Sep 17 00:00:00 2001 From: RulerChen Date: Mon, 5 Feb 2024 23:30:47 +0800 Subject: [PATCH] feat: typescript template --- packages/templates/typescript/.env | 1 + .../templates/typescript/.env.development | 1 + packages/templates/typescript/.eslintrc.json | 26 ++++++++++++++++++ .../templates/typescript/.prettierrc.json | 9 +++++++ packages/templates/typescript/index.ts | 1 - packages/templates/typescript/package.json | 27 +++++++++++++++++-- .../templates/typescript/process.env.d.ts | 10 +++++++ .../typescript/src/controllers/user.ts | 11 ++++++++ packages/templates/typescript/src/index.ts | 22 +++++++++++++++ .../templates/typescript/src/models/user.ts | 23 ++++++++++++++++ .../templates/typescript/src/routes/index.ts | 13 +++++++++ .../templates/typescript/src/routes/user.ts | 9 +++++++ packages/templates/typescript/tsconfig.json | 13 +++++++++ 13 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 packages/templates/typescript/.env create mode 100644 packages/templates/typescript/.env.development create mode 100644 packages/templates/typescript/.eslintrc.json create mode 100644 packages/templates/typescript/.prettierrc.json delete mode 100644 packages/templates/typescript/index.ts create mode 100644 packages/templates/typescript/process.env.d.ts create mode 100644 packages/templates/typescript/src/controllers/user.ts create mode 100644 packages/templates/typescript/src/index.ts create mode 100644 packages/templates/typescript/src/models/user.ts create mode 100644 packages/templates/typescript/src/routes/index.ts create mode 100644 packages/templates/typescript/src/routes/user.ts create mode 100644 packages/templates/typescript/tsconfig.json diff --git a/packages/templates/typescript/.env b/packages/templates/typescript/.env new file mode 100644 index 0000000..83ebdaa --- /dev/null +++ b/packages/templates/typescript/.env @@ -0,0 +1 @@ +PORT=8000 \ No newline at end of file diff --git a/packages/templates/typescript/.env.development b/packages/templates/typescript/.env.development new file mode 100644 index 0000000..8f55cbd --- /dev/null +++ b/packages/templates/typescript/.env.development @@ -0,0 +1 @@ +PORT= \ No newline at end of file diff --git a/packages/templates/typescript/.eslintrc.json b/packages/templates/typescript/.eslintrc.json new file mode 100644 index 0000000..53d53b1 --- /dev/null +++ b/packages/templates/typescript/.eslintrc.json @@ -0,0 +1,26 @@ +{ + "env": { + "es2021": true, + "node": true + }, + "extends": ["standard-with-typescript", "prettier"], + "plugins": ["prettier"], + "parserOptions": { + "ecmaVersion": "latest", + "sourceType": "module" + }, + "rules": { + "prettier/prettier": [ + "error", + { + "semi": true, + "tabWidth": 2, + "printWidth": 140, + "singleQuote": true, + "trailingComma": "all", + "bracketSameLine": false, + "arrowParens": "always" + } + ] + } +} diff --git a/packages/templates/typescript/.prettierrc.json b/packages/templates/typescript/.prettierrc.json new file mode 100644 index 0000000..cfa3f4a --- /dev/null +++ b/packages/templates/typescript/.prettierrc.json @@ -0,0 +1,9 @@ +{ + "semi": true, + "printWidth": 140, + "tabWidth": 2, + "singleQuote": true, + "trailingComma": "all", + "arrowParens": "always", + "bracketSpacing": true +} diff --git a/packages/templates/typescript/index.ts b/packages/templates/typescript/index.ts deleted file mode 100644 index 6e163ac..0000000 --- a/packages/templates/typescript/index.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("This is a template for a Typescript project.") diff --git a/packages/templates/typescript/package.json b/packages/templates/typescript/package.json index 1a4dcf4..9c35952 100644 --- a/packages/templates/typescript/package.json +++ b/packages/templates/typescript/package.json @@ -4,12 +4,35 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "lint": "eslint --fix ./src/**/*.{ts,js}", + "dev": "cross-env NODE_ENV=development nodemon --exec ts-node ./src/index.ts", + "build": "tsc", + "start": "tsc && cross-env NODE_ENV=production node ./build/index.js" }, "keywords": [], "author": "", "license": "ISC", + "devDependencies": { + "@types/cors": "~2.8.17", + "@types/express": "~4.17.21", + "@types/node": "~20.11.16", + "@typescript-eslint/eslint-plugin": "~6.20.0", + "cross-env": "~7.0.3", + "eslint": "~8.56.0", + "eslint-config-prettier": "~9.1.0", + "eslint-config-standard-with-typescript": "~43.0.1", + "eslint-plugin-import": "~2.29.1", + "eslint-plugin-n": "~16.6.2", + "eslint-plugin-prettier": "~5.1.3", + "eslint-plugin-promise": "~6.1.1", + "nodemon": "~3.0.3", + "prettier": "~3.2.5", + "ts-node": "~10.9.2", + "typescript": "~5.3.3" + }, "dependencies": { - "express": "^4.18.2" + "cors": "~2.8.5", + "dotenv": "~16.4.1", + "express": "~4.18.2" } } diff --git a/packages/templates/typescript/process.env.d.ts b/packages/templates/typescript/process.env.d.ts new file mode 100644 index 0000000..c887da4 --- /dev/null +++ b/packages/templates/typescript/process.env.d.ts @@ -0,0 +1,10 @@ +declare global { + namespace NodeJS { + interface ProcessEnv { + [key: string]: string | undefined; + PORT: string; + } + } +} + +export {}; diff --git a/packages/templates/typescript/src/controllers/user.ts b/packages/templates/typescript/src/controllers/user.ts new file mode 100644 index 0000000..568695c --- /dev/null +++ b/packages/templates/typescript/src/controllers/user.ts @@ -0,0 +1,11 @@ +import { type Request, type Response } from 'express'; +import usersModel from '../models/user'; + +function getAllUsers(req: Request, res: Response): void { + const users = usersModel.getUsers(); + res.status(200).json(users); +} + +export default { + getAllUsers, +}; diff --git a/packages/templates/typescript/src/index.ts b/packages/templates/typescript/src/index.ts new file mode 100644 index 0000000..ca39c68 --- /dev/null +++ b/packages/templates/typescript/src/index.ts @@ -0,0 +1,22 @@ +import express from 'express'; +import dotenv from 'dotenv'; +import cors from 'cors'; + +import routes from './routes/index'; + +dotenv.config(); + +const app = express(); +const PORT = process.env.PORT ?? 3000; + +app.use(cors()); +app.use(express.urlencoded({ extended: true })); +app.use(express.json()); + +app.use(`/api`, routes); + +app.listen(PORT, () => { + console.log(`Server is running on port ${PORT}`); +}); + +export default app; diff --git a/packages/templates/typescript/src/models/user.ts b/packages/templates/typescript/src/models/user.ts new file mode 100644 index 0000000..e0851ec --- /dev/null +++ b/packages/templates/typescript/src/models/user.ts @@ -0,0 +1,23 @@ +interface User { + name: string; + descrition: string; +} + +class UsersModel { + users: User[]; + + constructor() { + this.users = [ + { name: 'RulerChen', descrition: 'Author of this project' }, + { name: 'joshtu0627', descrition: 'Author of this project' }, + ]; + } + + getUsers(): User[] { + return this.users; + } +} + +const usersModel = new UsersModel(); + +export default usersModel; diff --git a/packages/templates/typescript/src/routes/index.ts b/packages/templates/typescript/src/routes/index.ts new file mode 100644 index 0000000..abdaa0b --- /dev/null +++ b/packages/templates/typescript/src/routes/index.ts @@ -0,0 +1,13 @@ +import express from 'express'; + +import UserRoutes from './user'; + +const router = express.Router(); + +router.get('/', (req, res) => { + res.send('This is the API root!'); +}); + +router.use('/users', UserRoutes); + +export default router; diff --git a/packages/templates/typescript/src/routes/user.ts b/packages/templates/typescript/src/routes/user.ts new file mode 100644 index 0000000..1eef3d0 --- /dev/null +++ b/packages/templates/typescript/src/routes/user.ts @@ -0,0 +1,9 @@ +import express from 'express'; + +import UserController from '../controllers/user'; + +const router = express.Router(); + +router.get('/', UserController.getAllUsers); + +export default router; diff --git a/packages/templates/typescript/tsconfig.json b/packages/templates/typescript/tsconfig.json new file mode 100644 index 0000000..347816d --- /dev/null +++ b/packages/templates/typescript/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "baseUrl": "src", + "outDir": "build", + "sourceMap": true, + "strict": true, + "types": ["node"] + }, + "include": ["process.env.d.ts", "./src/**/*.ts"] +}