diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4c7f8a8 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +# 🎨 editorconfig.org + +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true +insert_final_newline = true \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..8d20e22 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,7 @@ +{ + "extends": "./node_modules/mwts/", + "ignorePatterns": ["node_modules", "dist", "test", "jest.config.js", "typings"], + "env": { + "jest": true + } +} diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..13bc3a6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +logs/ +npm-debug.log +yarn-error.log +node_modules/ +package-lock.json +yarn.lock +coverage/ +dist/ +.idea/ +run/ +.DS_Store +*.sw* +*.un~ +.tsbuildinfo +.tsbuildinfo.* diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000..b964930 --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1,3 @@ +module.exports = { + ...require('mwts/.prettierrc.json') +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b5bfad1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,34 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Midway Debug", + "type": "node", + "autoAttachChildProcesses": true, + "console": "integratedTerminal", + "env": { + "NODE_ENV": "local" + }, + "port": 9229, + // "preLaunchTask": "TypeScript compile", + "protocol": "auto", + "request": "launch", + "restart": true, + "runtimeArgs": [ + "run", + "debug", + "--", + "--inspect-brk" + ], + "runtimeExecutable": "npm", + "skipFiles": [ + // "${workspaceFolder}/node_modules/**/*.js", + "${workspaceFolder}/node_modules/rxjs/**/*.js", + "/**/*.js" + ] + } + ] +} diff --git a/README.md b/README.md old mode 100644 new mode 100755 index f1ae8d2..74afd3e --- a/README.md +++ b/README.md @@ -1 +1,30 @@ -#cool-admin +# my_midway_project + +## QuickStart + + + +see [midway docs][midway] for more detail. + +### Development + +```bash +$ npm i +$ npm run dev +$ open http://localhost:7001/ +``` + +### Deploy + +```bash +$ npm start +$ npm stop +``` + +### npm scripts + +- Use `npm run lint` to check code style. +- Use `npm test` to run unit test. + + +[midway]: https://midwayjs.org diff --git a/README.zh-CN.md b/README.zh-CN.md new file mode 100755 index 0000000..6e1cbad --- /dev/null +++ b/README.zh-CN.md @@ -0,0 +1,30 @@ +# my_midway_project + +## 快速入门 + + + +如需进一步了解,参见 [midway 文档][midway]。 + +### 本地开发 + +```bash +$ npm i +$ npm run dev +$ open http://localhost:7001/ +``` + +### 部署 + +```bash +$ npm start +$ npm stop +``` + +### 内置指令 + +- 使用 `npm run lint` 来做代码风格检查。 +- 使用 `npm test` 来执行单元测试。 + + +[midway]: https://midwayjs.org diff --git a/package.json b/package.json new file mode 100755 index 0000000..911ddef --- /dev/null +++ b/package.json @@ -0,0 +1,49 @@ +{ + "name": "my_midway_project", + "version": "1.0.0", + "description": "", + "private": true, + "dependencies": { + "@midwayjs/web": "^2.3.0", + "@midwayjs/decorator": "^2.3.0", + "egg": "^2.0.0", + "egg-scripts": "^2.10.0" + }, + "devDependencies": { + "@midwayjs/cli": "^1.0.0", + "@midwayjs/egg-ts-helper": "^1.0.1", + "@midwayjs/mock": "^2.3.0", + "@types/jest": "^26.0.10", + "@types/node": "14", + "cross-env": "^6.0.0", + "mwts": "^1.0.5", + "jest": "^26.4.0", + "ts-jest": "^26.2.0", + "typescript": "^3.9.0" + }, + "engines": { + "node": ">=12.0.0" + }, + "scripts": { + "start": "egg-scripts start --daemon --title=midway-server-my_midway_project --framework=@midwayjs/web", + "stop": "egg-scripts stop --title=midway-server-my_midway_project", + "start_build": "npm run build && cross-env NODE_ENV=development midway-bin dev", + "dev": "cross-env ets && cross-env NODE_ENV=local midway-bin dev --ts", + "test": "midway-bin test", + "cov": "midway-bin cov", + "lint": "mwts check", + "lint:fix": "mwts fix", + "ci": "npm run cov", + "build": "midway-bin build -c" + }, + "midway-bin-clean": [ + ".vscode/.tsbuildinfo", + "dist" + ], + "repository": { + "type": "git", + "url": "" + }, + "author": "anonymous", + "license": "MIT" +} \ No newline at end of file diff --git a/src/config/config.default.ts b/src/config/config.default.ts new file mode 100644 index 0000000..fb86f18 --- /dev/null +++ b/src/config/config.default.ts @@ -0,0 +1,15 @@ +import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg'; + +export type DefaultConfig = PowerPartial; + +export default (appInfo: EggAppInfo) => { + const config = {} as DefaultConfig; + + // use for cookie sign key, should change to your own and keep security + config.keys = appInfo.name + '_1610416114197_5726'; + + // add your config here + config.middleware = []; + + return config; +}; diff --git a/src/config/config.local.ts b/src/config/config.local.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/config/config.unittest.ts b/src/config/config.unittest.ts new file mode 100644 index 0000000..b5f095b --- /dev/null +++ b/src/config/config.unittest.ts @@ -0,0 +1,3 @@ +export const security = { + csrf: false +} \ No newline at end of file diff --git a/src/config/plugin.ts b/src/config/plugin.ts new file mode 100755 index 0000000..fabdc59 --- /dev/null +++ b/src/config/plugin.ts @@ -0,0 +1,4 @@ +import { EggPlugin } from 'egg'; +export default { + static: false, // default is true +} as EggPlugin; diff --git a/src/controller/api.ts b/src/controller/api.ts new file mode 100644 index 0000000..1e75740 --- /dev/null +++ b/src/controller/api.ts @@ -0,0 +1,19 @@ +import { Inject, Controller, Post, Provide, Query } from '@midwayjs/decorator'; +import { Context } from 'egg'; +import { UserService } from '../service/user'; + +@Provide() +@Controller('/api') +export class APIController { + @Inject() + ctx: Context; + + @Inject() + userService: UserService; + + @Post('/get_user') + async getUser(@Query() uid) { + const user = await this.userService.getUser({ uid }); + return { success: true, message: 'OK', data: user }; + } +} diff --git a/src/controller/home.ts b/src/controller/home.ts new file mode 100755 index 0000000..b0d35f0 --- /dev/null +++ b/src/controller/home.ts @@ -0,0 +1,10 @@ +import { Controller, Get, Provide } from '@midwayjs/decorator'; + +@Provide() +@Controller('/') +export class HomeController { + @Get('/') + async home() { + return 'Hello Midwayjs!'; + } +} diff --git a/src/interface.ts b/src/interface.ts new file mode 100644 index 0000000..13daae8 --- /dev/null +++ b/src/interface.ts @@ -0,0 +1,6 @@ +/** + * @description User-Service parameters + */ +export interface IUserOptions { + uid: number; +} diff --git a/src/service/user.ts b/src/service/user.ts new file mode 100644 index 0000000..059480a --- /dev/null +++ b/src/service/user.ts @@ -0,0 +1,14 @@ +import { Provide } from '@midwayjs/decorator'; +import { IUserOptions } from '../interface'; + +@Provide() +export class UserService { + async getUser(options: IUserOptions) { + return { + uid: options.uid, + username: 'mockedName', + phone: '12345678901', + email: 'xxx.xxx@xxx.com', + }; + } +} diff --git a/test/controller/api.test.ts b/test/controller/api.test.ts new file mode 100755 index 0000000..f7c9394 --- /dev/null +++ b/test/controller/api.test.ts @@ -0,0 +1,25 @@ +import { createApp, close, createHttpRequest } from '@midwayjs/mock'; +import { Framework } from '@midwayjs/web'; +import * as assert from 'assert'; + +describe('test/controller/home.test.ts', () => { + + it('should POST /api/get_user', async () => { + // create app + const app = await createApp(); + + // make request + const result = await createHttpRequest(app).post('/api/get_user').query({ uid: 123 }); + + // use expect by jest + expect(result.status).toBe(200); + expect(result.body.message).toBe('OK'); + + // or use assert + assert.deepStrictEqual(result.status, 200); + assert.deepStrictEqual(result.body.data.uid, '123'); + + // close app + await close(app); + }); +}); diff --git a/test/controller/home.test.ts b/test/controller/home.test.ts new file mode 100755 index 0000000..7ac0cd6 --- /dev/null +++ b/test/controller/home.test.ts @@ -0,0 +1,26 @@ +import { createApp, close, createHttpRequest } from '@midwayjs/mock'; +import { Framework } from '@midwayjs/web'; +import * as assert from 'assert'; + +describe('test/controller/home.test.ts', () => { + + it('should GET /', async () => { + // create app + const app = await createApp(); + + // make request + const result = await createHttpRequest(app).get('/'); + + // use expect by jest + expect(result.status).toBe(200); + expect(result.text).toBe('Hello Midwayjs!'); + + // or use assert + assert.deepStrictEqual(result.status, 200); + assert.deepStrictEqual(result.text, 'Hello Midwayjs!'); + + // close app + await close(app); + }); + +}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..a5037a3 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compileOnSave": true, + "compilerOptions": { + "target": "ES2018", + "module": "commonjs", + "moduleResolution": "node", + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "inlineSourceMap":true, + "noImplicitThis": true, + "noUnusedLocals": true, + "stripInternal": true, + "pretty": true, + "declaration": true, + "typeRoots": [ "./typings", "./node_modules/@types"], + "outDir": "dist" + }, + "exclude": [ + "dist", + "node_modules", + "test" + ] +} \ No newline at end of file diff --git a/typings/app/index.d.ts b/typings/app/index.d.ts new file mode 100644 index 0000000..69543bc --- /dev/null +++ b/typings/app/index.d.ts @@ -0,0 +1,7 @@ +// This file is created by egg-ts-helper@1.25.8 +// Do not modify this file!!!!!!!!! + +import 'egg'; +import '@midwayjs/web'; +export * from 'egg'; +export as namespace Egg; diff --git a/typings/config/index.d.ts b/typings/config/index.d.ts new file mode 100644 index 0000000..ca60818 --- /dev/null +++ b/typings/config/index.d.ts @@ -0,0 +1,32 @@ +// This file is created by egg-ts-helper@1.25.8 +// Do not modify this file!!!!!!!!! + +import '@midwayjs/web'; +import 'egg'; +import 'egg-onerror'; +import 'egg-session'; +import 'egg-i18n'; +import 'egg-multipart'; +import 'egg-security'; +import 'egg-schedule'; +import 'egg-jsonp'; +import 'egg-view'; +import 'midway-schedule'; +import { EggPluginItem } from 'egg'; +declare module 'egg' { + interface EggPlugin { + onerror?: EggPluginItem; + session?: EggPluginItem; + i18n?: EggPluginItem; + watcher?: EggPluginItem; + multipart?: EggPluginItem; + security?: EggPluginItem; + development?: EggPluginItem; + logrotator?: EggPluginItem; + schedule?: EggPluginItem; + static?: EggPluginItem; + jsonp?: EggPluginItem; + view?: EggPluginItem; + schedulePlus?: EggPluginItem; + } +} \ No newline at end of file diff --git a/typings/config/plugin.d.ts b/typings/config/plugin.d.ts new file mode 100644 index 0000000..e857358 --- /dev/null +++ b/typings/config/plugin.d.ts @@ -0,0 +1,31 @@ +// This file is created by egg-ts-helper@1.25.8 +// Do not modify this file!!!!!!!!! + +import 'egg'; +import 'egg-onerror'; +import 'egg-session'; +import 'egg-i18n'; +import 'egg-multipart'; +import 'egg-security'; +import 'egg-schedule'; +import 'egg-jsonp'; +import 'egg-view'; +import 'midway-schedule'; +import { EggPluginItem } from 'egg'; +declare module 'egg' { + interface EggPlugin { + onerror?: EggPluginItem; + session?: EggPluginItem; + i18n?: EggPluginItem; + watcher?: EggPluginItem; + multipart?: EggPluginItem; + security?: EggPluginItem; + development?: EggPluginItem; + logrotator?: EggPluginItem; + schedule?: EggPluginItem; + static?: EggPluginItem; + jsonp?: EggPluginItem; + view?: EggPluginItem; + schedulePlus?: EggPluginItem; + } +} \ No newline at end of file