Skip to content

Commit

Permalink
feat: use simpler nitro instead of nestjs to implement mock service
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Jul 20, 2024
1 parent 9ec91ac commit 9987451
Show file tree
Hide file tree
Showing 122 changed files with 2,575 additions and 2,986 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ dist-ssr
dist.zip
dist.tar
dist.war
.nitro
.output
*-dist.zip
*-dist.tar
*-dist.war
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ node_modules
.nvmrc
coverage
CODEOWNERS
.nitro
.output


**/*.svg
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,6 @@
"tailwind.config.mjs": "postcss.*"
},
"commentTranslate.hover.enabled": true,
"i18n-ally.keystyle": "nested"
"i18n-ally.keystyle": "nested",
"commentTranslate.multiLineMerge": true
}
1 change: 1 addition & 0 deletions apps/backend-mock/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=5320
5 changes: 1 addition & 4 deletions apps/backend-mock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ Vben Admin 数据 mock 服务,没有对接任何的数据库,所有数据都
# development
$ pnpm run start

# watch mode
$ pnpm run start:dev

# production mode
$ pnpm run start:prod
$ pnpm run build
```
15 changes: 15 additions & 0 deletions apps/backend-mock/api/auth/codes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default eventHandler((event) => {
const token = getHeader(event, 'Authorization');

if (!token) {
setResponseStatus(event, 401);
return useResponseError('UnauthorizedException', 'Unauthorized Exception');
}

const username = Buffer.from(token, 'base64').toString('utf8');

const codes =
MOCK_CODES.find((item) => item.username === username)?.codes ?? [];

return useResponseSuccess(codes);
});
20 changes: 20 additions & 0 deletions apps/backend-mock/api/auth/login.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default defineEventHandler(async (event) => {
const { password, username } = await readBody(event);

const findUser = MOCK_USERS.find(
(item) => item.username === username && item.password === password,
);

if (!findUser) {
setResponseStatus(event, 403);
return useResponseError('UnauthorizedException', '用户名或密码错误');
}

const accessToken = Buffer.from(username).toString('base64');

return useResponseSuccess({
accessToken,
// TODO: refresh token
refreshToken: accessToken,
});
});
14 changes: 14 additions & 0 deletions apps/backend-mock/api/menu/all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default eventHandler((event) => {
const token = getHeader(event, 'Authorization');

if (!token) {
setResponseStatus(event, 401);
return useResponseError('UnauthorizedException', 'Unauthorized Exception');
}

const username = Buffer.from(token, 'base64').toString('utf8');

const menus =
MOCK_MENUS.find((item) => item.username === username)?.menus ?? [];
return useResponseSuccess(menus);
});
5 changes: 5 additions & 0 deletions apps/backend-mock/api/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default eventHandler((event) => {
const { status } = getQuery(event);
setResponseStatus(event, Number(status));
return useResponseError(`${status}`);
});
1 change: 1 addition & 0 deletions apps/backend-mock/api/test.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default defineEventHandler(() => 'Test get handler');
1 change: 1 addition & 0 deletions apps/backend-mock/api/test.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default defineEventHandler(() => 'Test post handler');
14 changes: 14 additions & 0 deletions apps/backend-mock/api/user/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default eventHandler((event) => {
const token = getHeader(event, 'Authorization');
if (!token) {
setResponseStatus(event, 401);
return useResponseError('UnauthorizedException', 'Unauthorized Exception');
}

const username = Buffer.from(token, 'base64').toString('utf8');

const user = MOCK_USERS.find((item) => item.username === username);

const { password: _pwd, ...userInfo } = user;
return useResponseSuccess(userInfo);
});
23 changes: 0 additions & 23 deletions apps/backend-mock/ecosystem.config.cjs

This file was deleted.

7 changes: 7 additions & 0 deletions apps/backend-mock/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { NitroErrorHandler } from 'nitropack';

const errorHandler: NitroErrorHandler = function (error, event) {
event.res.end(`[error handler] ${error.stack}`);
};

export default errorHandler;
20 changes: 0 additions & 20 deletions apps/backend-mock/http/auth.http

This file was deleted.

3 changes: 0 additions & 3 deletions apps/backend-mock/http/health.http

This file was deleted.

6 changes: 0 additions & 6 deletions apps/backend-mock/http/menu.http

This file was deleted.

10 changes: 0 additions & 10 deletions apps/backend-mock/nest-cli.json

This file was deleted.

6 changes: 6 additions & 0 deletions apps/backend-mock/nitro.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import errorHandler from './error';

export default defineNitroConfig({
devErrorHandler: errorHandler,
errorHandler: '~/error',
});
37 changes: 3 additions & 34 deletions apps/backend-mock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,10 @@
"license": "MIT",
"author": "",
"scripts": {
"build": "nest build",
"dev": "pnpm run start:dev",
"start": "cross-env NODE_ENV=development node dist/main",
"start:dev": "cross-env NODE_ENV=development DEBUG=true nest start --watch",
"start:prod": "nest build && cross-env NODE_ENV=production node dist/main"
"start": "nitro dev",
"build": "nitro build"
},
"dependencies": {
"@nestjs/common": "^10.3.10",
"@nestjs/config": "^3.2.3",
"@nestjs/core": "^10.3.10",
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.3.10",
"@types/js-yaml": "^4.0.9",
"bcryptjs": "^2.4.3",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"cross-env": "^7.0.3",
"joi": "^17.13.3",
"js-yaml": "^4.1.0",
"mockjs": "^1.1.0",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@nestjs/cli": "^10.4.2",
"@nestjs/schematics": "^10.1.2",
"@types/express": "^4.17.21",
"@types/mockjs": "^1.0.10",
"@types/node": "^20.14.11",
"nodemon": "^3.1.4",
"ts-node": "^10.9.2",
"typescript": "^5.5.3"
"nitropack": "latest"
}
}
12 changes: 12 additions & 0 deletions apps/backend-mock/routes/[...].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default defineEventHandler(() => {
return `
<h1>Hello Vben Admin</h1>
<h2>Mock service is starting</h2>
<ul>
<li><a href="/api/user">/api/user/info</a></li>
<li><a href="/api/menu">/api/menu/all</a></li>
<li><a href="/api/auth/codes">/api/auth/codes</a></li>
<li><a href="/api/auth/login">/api/auth/login</a></li>
</ul>
`;
});
34 changes: 0 additions & 34 deletions apps/backend-mock/src/app.module.ts

This file was deleted.

8 changes: 0 additions & 8 deletions apps/backend-mock/src/config/dev.yml

This file was deleted.

23 changes: 0 additions & 23 deletions apps/backend-mock/src/config/index.ts

This file was deleted.

8 changes: 0 additions & 8 deletions apps/backend-mock/src/config/prod.yml

This file was deleted.

1 change: 0 additions & 1 deletion apps/backend-mock/src/core/decorator/index.ts

This file was deleted.

4 changes: 0 additions & 4 deletions apps/backend-mock/src/core/decorator/public.ts

This file was deleted.

40 changes: 0 additions & 40 deletions apps/backend-mock/src/core/filter/http-exception.filter.ts

This file was deleted.

1 change: 0 additions & 1 deletion apps/backend-mock/src/core/filter/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions apps/backend-mock/src/core/guard/index.ts

This file was deleted.

Loading

0 comments on commit 9987451

Please sign in to comment.