Skip to content

Commit

Permalink
feat: typescript template
Browse files Browse the repository at this point in the history
  • Loading branch information
RulerChen committed Feb 5, 2024
1 parent 8de2a00 commit 9178fee
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/templates/typescript/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=8000
1 change: 1 addition & 0 deletions packages/templates/typescript/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=
26 changes: 26 additions & 0 deletions packages/templates/typescript/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
9 changes: 9 additions & 0 deletions packages/templates/typescript/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"semi": true,
"printWidth": 140,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "all",
"arrowParens": "always",
"bracketSpacing": true
}
1 change: 0 additions & 1 deletion packages/templates/typescript/index.ts

This file was deleted.

27 changes: 25 additions & 2 deletions packages/templates/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
10 changes: 10 additions & 0 deletions packages/templates/typescript/process.env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare global {
namespace NodeJS {
interface ProcessEnv {
[key: string]: string | undefined;
PORT: string;
}
}
}

export {};
11 changes: 11 additions & 0 deletions packages/templates/typescript/src/controllers/user.ts
Original file line number Diff line number Diff line change
@@ -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,
};
22 changes: 22 additions & 0 deletions packages/templates/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
23 changes: 23 additions & 0 deletions packages/templates/typescript/src/models/user.ts
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions packages/templates/typescript/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions packages/templates/typescript/src/routes/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import express from 'express';

import UserController from '../controllers/user';

const router = express.Router();

router.get('/', UserController.getAllUsers);

export default router;
13 changes: 13 additions & 0 deletions packages/templates/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}

0 comments on commit 9178fee

Please sign in to comment.