Skip to content

Commit

Permalink
init project
Browse files Browse the repository at this point in the history
  • Loading branch information
tkvern committed Oct 10, 2020
0 parents commit e3ce762
Show file tree
Hide file tree
Showing 22 changed files with 377 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./node_modules/mwts/",
"ignorePatterns": ["node_modules", "dist", "test", "jest.config.js", "typings"],
"env": {
"jest": true
}
}
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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.*
3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('mwts/.prettierrc.json')
}
34 changes: 34 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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",
"<node_internals>/**/*.js"
]
}
]
}
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# my_midway_project

{{description}}

## QuickStart

<!-- add docs here for user -->

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
39 changes: 39 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# my_midway_project

{{description}}

## 快速入门

<!-- 在此次添加使用文档 -->

如需进一步了解,参见 [midway 文档][midway]

### 本地开发

```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```

### 部署

```bash
$ npm start
$ npm stop
```

### 单元测试

- [midway-bin] 内置了 [mocha], [thunk-mocha], [power-assert], [istanbul] 等框架,让你可以专注于写单元测试,无需理会配套工具。
- 断言库非常推荐使用 [power-assert]
- 具体参见 [midway 文档 - 单元测试](https://eggjs.org/zh-cn/core/unittest)

### 内置指令

- 使用 `npm run lint` 来做代码风格检查。
- 使用 `npm test` 来执行单元测试。
- 使用 `npm run autod` 来自动检测依赖更新,详细参见 [autod](https://www.npmjs.com/package/autod)


[midway]: https://midwayjs.org
51 changes: 51 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "my_midway_project",
"version": "1.0.0",
"description": "{{description}}",
"private": true,
"dependencies": {
"@midwayjs/web": "^2.3.0",
"@midwayjs/decorator": "^2.3.0",
"egg": "^2.0.0",
"egg-scripts": "^2.10.0",
"midway": "^2.3.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=midway",
"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",
"debug": "cross-env NODE_ENV=local midway-bin debug --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"
}
19 changes: 19 additions & 0 deletions src/app/controller/api.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
}
10 changes: 10 additions & 0 deletions src/app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Controller, Get, Provide } from '@midwayjs/decorator';

@Provide()
@Controller('/')
export class HomeController {
@Get('/')
async home() {
return 'Hello Midwayjs!';
}
}
14 changes: 14 additions & 0 deletions src/app/service/user.ts
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
};
}
}
15 changes: 15 additions & 0 deletions src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';

export type DefaultConfig = PowerPartial<EggAppConfig>;

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 + '_1602294995416_4568';

// add your config here
config.middleware = [];

return config;
};
Empty file added src/config/config.local.ts
Empty file.
3 changes: 3 additions & 0 deletions src/config/config.unittest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const security = {
csrf: false
}
4 changes: 4 additions & 0 deletions src/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { EggPlugin } from 'egg';
export default {
static: false, // default is true
} as EggPlugin;
6 changes: 6 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @description User-Service parameters
*/
export interface IUserOptions {
uid: number;
}
25 changes: 25 additions & 0 deletions test/controller/api.test.ts
Original file line number Diff line number Diff line change
@@ -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<Framework>();

// 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);
});
});
26 changes: 26 additions & 0 deletions test/controller/home.test.ts
Original file line number Diff line number Diff line change
@@ -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<Framework>();

// 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);
});

});
23 changes: 23 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
7 changes: 7 additions & 0 deletions typings/app/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This file is created by [email protected]
// Do not modify this file!!!!!!!!!

import 'egg';
import '@midwayjs/web';
export * from 'egg';
export as namespace Egg;
1 change: 1 addition & 0 deletions typings/config/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@midwayjs/web';
Loading

0 comments on commit e3ce762

Please sign in to comment.