Skip to content

Commit

Permalink
chore(express-typeorm): update example.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jul 24, 2021
1 parent 246ecc1 commit 6f0162b
Show file tree
Hide file tree
Showing 16 changed files with 505 additions and 54 deletions.
56 changes: 52 additions & 4 deletions example/express-typeorm/api-test/user-account.http
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@


@baseUrl = http://localhost:3009/api
@baseUrl = http://localhost:3000/api
@contentType = application/json

@auth = {{login.response.body.$.token}}

### 用户登录

Expand All @@ -11,11 +10,60 @@ POST {{baseUrl}}/login
Content-Type: {{contentType}}

{
"username": "admin",
"username": "wcj",
"password": "123456"
}

### User Verify

GET {{baseUrl}}/verify
Content-Type: application/json

### Logout

POST {{baseUrl}}/logout
Content-Type: application/json

### Get User List

@userid = {{list.response.body.$[1].id}}
# @name list
GET {{baseUrl}}/user?token={{auth}}
Content-Type: application/json

### Get User Info

GET {{baseUrl}}/user/{{userid}}?token={{auth}}
Content-Type: application/json

### Get User Info

GET {{baseUrl}}/user/{{userid}}
Content-Type: application/json
Authorization: token {{auth}}

### Create User

POST {{baseUrl}}/user?token={{auth}}
Content-Type: application/json

{
"username": "wcj{{$randomInt 1 200}}",
"password": "wcc{{$randomInt 1 200}}"
}

### Update User

PUT {{baseUrl}}/user?token={{auth}}
Content-Type: application/json

{
"id": {{userid}},
"username": "wcj{{$randomInt 1 200}}",
"password": "wcc{{$randomInt 1 200}}"
}

### Delete User

DELETE {{baseUrl}}/user/{{userid}}?token={{auth}}
Content-Type: application/json
63 changes: 63 additions & 0 deletions example/express-typeorm/api-test/user-staff.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@


@baseUrl = http://localhost:3000/api
@contentType = application/json

### 用户登录

# @name login
@auth = {{login.response.body.$.token}}
POST {{baseUrl}}/login
Content-Type: {{contentType}}

{
"username": "wcj",
"password": "123456"
}

### 员工信息列表

@userId = {{getList.response.body.$[0].id}}

# @name getList
GET {{baseUrl}}/user/staff?page=1&per_page=30
Content-Type: {{contentType}}
Authorization: token {{auth}}


### 更新员工信息

PUT {{baseUrl}}/user/staff
Content-Type: {{contentType}}
Authorization: token {{auth}}

{
"id": "{{userId}}",
"name": "周杰伦",
"mobile": "1366666{{$randomInt 5 200}}",
"age": {{$randomInt 5 200}}
}


### 添加员工信息

POST {{baseUrl}}/user/staff
Content-Type: {{contentType}}
Authorization: token {{auth}}

{
"name": "周杰伦{{$randomInt 1 200}}",
"mobile": "136666{{$randomInt 5 200}}6666",
"age": 18
}

### 获取员工信息

GET {{baseUrl}}/user/staff/{{userId}}
Content-Type: {{contentType}}
Authorization: token {{auth}}

### 删除员工信息

DELETE {{baseUrl}}/user/staff/{{userId}}?token={{auth}}
Content-Type: application/json
16 changes: 9 additions & 7 deletions example/express-typeorm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,28 @@
"delay": "2500"
},
"devDependencies": {
"@types/compression": "1.7.0",
"@types/compression": "1.7.1",
"@types/cookie-parser": "1.4.2",
"@types/express": "4.17.12",
"@types/express-session": "1.17.3",
"@types/http-errors": "1.8.0",
"@types/node": "15.6.1",
"@types/express": "4.17.13",
"@types/express-session": "1.17.4",
"@types/http-errors": "1.8.1",
"@types/node": "16.4.2",
"nodemon": "2.0.12",
"ts-node": "10.0.0",
"ts-node": "10.1.0",
"ts-node-dev": "1.1.8",
"tsbb": "2.2.1"
},
"dependencies": {
"compression": "1.7.4",
"cookie-parser": "1.4.5",
"dotenv": "10.0.0",
"ejs": "3.1.6",
"express": "4.17.1",
"express-session": "1.17.2",
"http-errors": "1.8.0",
"pg": "8.6.0",
"reflect-metadata": "0.1.13",
"sqlite3": "5.0.2",
"typeorm-extension": "0.2.7",
"typeorm": "0.2.34",
"typeorm-store": "2.0.0"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { Express } from 'express';
import { UserController } from './controller/UserController';
import { RoutesData } from './';
import { UserController } from '../controller/UserController';

export type RoutesData = {
method: Extract<keyof Express, 'get' | 'post' | 'delete' | 'put' | 'patch'>;
route: string;
} & {
controller: typeof UserController;
action: keyof UserController;
};

export const Routes: RoutesData[] = [
export const user: RoutesData<UserController, typeof UserController>[] = [
{
method: 'post',
route: '/api/login',
Expand All @@ -25,24 +17,35 @@ export const Routes: RoutesData[] = [
{
method: 'get',
route: '/api/user',
auth: true,
controller: UserController,
action: 'all',
},
{
method: 'get',
route: '/api/user/:id',
auth: true,
controller: UserController,
action: 'one',
},
{
method: 'post',
route: '/api/user',
auth: true,
controller: UserController,
action: 'create',
},
{
method: 'put',
route: '/api/user',
auth: true,
controller: UserController,
action: 'save',
action: 'update',
},
{
method: 'delete',
route: '/api/user/:id',
auth: true,
controller: UserController,
action: 'remove',
},
Expand Down
40 changes: 40 additions & 0 deletions example/express-typeorm/src/api/UserStaff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { RoutesData } from './';
import { UserStaffController } from '../controller/UserStaffController';

export const userStaff: RoutesData<UserStaffController, typeof UserStaffController>[] = [
{
method: 'post',
route: '/api/user/staff',
auth: true,
controller: UserStaffController,
action: 'create',
},
{
method: 'put',
route: '/api/user/staff',
auth: true,
controller: UserStaffController,
action: 'update',
},
{
method: 'get',
route: '/api/user/staff',
auth: true,
controller: UserStaffController,
action: 'all',
},
{
method: 'get',
route: '/api/user/staff/:id',
auth: true,
controller: UserStaffController,
action: 'one',
},
{
method: 'delete',
route: '/api/user/staff/:id',
auth: true,
controller: UserStaffController,
action: 'remove',
},
];
16 changes: 16 additions & 0 deletions example/express-typeorm/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Express } from 'express';
import { user } from './User';
import { userStaff } from './UserStaff';

export interface RoutesData<T, E, K = keyof T> {
method: Extract<keyof Express, 'get' | 'post' | 'delete' | 'put' | 'patch'>;
route: string;
auth?: boolean;
controller: E;
action: K;
}

export const Routes = [
...userStaff,
...user,
];
10 changes: 6 additions & 4 deletions example/express-typeorm/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import compression from 'compression';
import createError from 'http-errors';
import cookieParser from 'cookie-parser';
import express, { Express, Response, Request, NextFunction } from 'express';
import { EntityManager } from 'typeorm';
import basicAuth from './middleware/basicAuth';
import routes from './routes/index';
import { Routes } from './routes';
import { Routes } from './api';
import { createSession } from './utils/session';

export async function expressApp(): Promise<Express> {
export async function expressApp(manager: EntityManager): Promise<Express> {
const app: Express = express();
app.use(createSession());
app.disable('x-powered-by');
Expand All @@ -24,8 +26,8 @@ export async function expressApp(): Promise<Express> {

// register express routes from defined application routes
Routes.forEach((route) => {
app[route.method](route.route, (req: Request, res: Response, next: NextFunction) => {
const result = new route.controller()[route.action](req, res, next);
app[route.method](route.route, basicAuth(route.auth), (req: Request, res: Response, next: NextFunction) => {
const result = (new route.controller(manager) as any)[route.action](req, res, next);
if (result instanceof Promise) {
result.then((result) => (result !== null && result !== undefined ? res.send(result) : undefined));
} else if (result !== null && result !== undefined) {
Expand Down

0 comments on commit 6f0162b

Please sign in to comment.