Skip to content

Commit 819d349

Browse files
killagu零弌
and
零弌
authored
feat: impl tegg example (#119)
Co-authored-by: 零弌 <[email protected]>
1 parent 4b94602 commit 819d349

File tree

12 files changed

+202
-0
lines changed

12 files changed

+202
-0
lines changed

hello-tegg/.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "eslint-config-egg/typescript"
3+
}

hello-tegg/.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
logs/
2+
npm-debug.log
3+
node_modules/
4+
coverage/
5+
.idea/
6+
run/
7+
logs/
8+
.DS_Store
9+
.vscode
10+
*.swp
11+
*.lock
12+
*.js
13+
14+
app/**/*.js
15+
test/**/*.js
16+
config/**/*.js
17+
app/**/*.map
18+
test/**/*.map
19+
config/**/*.map

hello-tegg/app/biz/HelloService.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {
2+
ContextProto,
3+
AccessLevel,
4+
} from '@eggjs/tegg';
5+
6+
@ContextProto({
7+
accessLevel: AccessLevel.PUBLIC,
8+
})
9+
export class HelloService {
10+
async hello(name: string): Promise<string> {
11+
return `hello, ${name}`;
12+
}
13+
}

hello-tegg/app/biz/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "biz-module",
3+
"eggModule": {
4+
"name": "biz"
5+
}
6+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {
2+
HTTPController,
3+
HTTPMethod,
4+
HTTPMethodEnum,
5+
Context,
6+
EggContext,
7+
HTTPQuery,
8+
Middleware,
9+
Inject,
10+
} from '@eggjs/tegg';
11+
import { EggLogger } from 'egg';
12+
import { traceMethod } from '../middleware/trace_method';
13+
import { HelloService } from '../biz/HelloService';
14+
15+
@HTTPController()
16+
@Middleware(traceMethod)
17+
export class HelloController {
18+
@Inject()
19+
private readonly helloService: HelloService;
20+
21+
@Inject()
22+
private readonly logger: EggLogger;
23+
24+
@HTTPMethod({
25+
method: HTTPMethodEnum.GET,
26+
path: '/hello',
27+
})
28+
async hello(@Context() ctx: EggContext, @HTTPQuery() name: string) {
29+
this.logger.info('access url: %s', ctx.url);
30+
31+
const message = await this.helloService.hello(name);
32+
33+
return {
34+
success: true,
35+
data: {
36+
message,
37+
},
38+
};
39+
}
40+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { EggContext, Next } from '@eggjs/tegg';
2+
3+
export async function traceMethod(ctx: EggContext, next: Next) {
4+
await next();
5+
ctx.body.data.message += ` (${ctx.method})`;
6+
}

hello-tegg/config/config.default.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { EggAppConfig, PowerPartial } from 'egg';
2+
3+
export default (appInfo: EggAppConfig) => {
4+
const config = {} as PowerPartial<EggAppConfig>;
5+
6+
// override config from framework / plugin
7+
config.keys = appInfo.name + '123456';
8+
9+
return config;
10+
};

hello-tegg/config/plugin.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { EggPlugin } from 'egg';
2+
3+
const plugin: EggPlugin = {
4+
tegg: {
5+
enable: true,
6+
package: '@eggjs/tegg-plugin',
7+
},
8+
teggConfig: {
9+
enable: true,
10+
package: '@eggjs/tegg-config',
11+
},
12+
teggController: {
13+
enable: true,
14+
package: '@eggjs/tegg-controller-plugin',
15+
},
16+
};
17+
18+
export default plugin;

hello-tegg/package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "hello-tegg",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "egg-scripts start",
8+
"dev": "egg-bin dev -r egg-ts-helper/register",
9+
"debug": "egg-bin debug -r egg-ts-helper/register",
10+
"test-local": "egg-bin test -r egg-ts-helper/register",
11+
"test": "npm run lint -- --fix && npm run test-local",
12+
"cov": "egg-bin cov -r egg-ts-helper/register",
13+
"tsc": "tsc -p tsconfig.json",
14+
"ci": "npm run lint && npm run cov && npm run tsc",
15+
"autod": "autod",
16+
"lint": "eslint .",
17+
"clean": "tsc -b --clean"
18+
},
19+
"egg": {
20+
"typescript": true
21+
},
22+
"author": "",
23+
"license": "ISC",
24+
"dependencies": {
25+
"@eggjs/tegg": "^0.1.1",
26+
"@eggjs/tegg-config": "^0.1.0",
27+
"@eggjs/tegg-controller-plugin": "^0.1.1",
28+
"@eggjs/tegg-eventbus-plugin": "^0.1.1",
29+
"@eggjs/tegg-plugin": "^0.1.1",
30+
"egg": "^2.29.4"
31+
},
32+
"devDependencies": {
33+
"@eggjs/tsconfig": "^1.0.0",
34+
"@types/mocha": "^8.2.3",
35+
"@types/node": "^16.4.0",
36+
"egg-bin": "^4.16.4",
37+
"egg-mock": "^3.26.0",
38+
"egg-ts-helper": "^1.25.9",
39+
"eslint": "^7.31.0",
40+
"eslint-config-egg": "^9.0.0",
41+
"typescript": "^4.3.5"
42+
}
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Context } from 'egg';
2+
import assert from 'assert';
3+
import { app } from 'egg-mock/bootstrap';
4+
import { HelloService } from '../../app/biz/HelloService';
5+
6+
describe('test/biz/HelloService.test.ts', () => {
7+
let ctx: Context;
8+
let helloService: HelloService;
9+
10+
beforeEach(async () => {
11+
ctx = await app.mockModuleContext();
12+
helloService = await ctx.getEggObject(HelloService);
13+
});
14+
15+
afterEach(async () => {
16+
await app.destroyModuleContext(ctx);
17+
});
18+
19+
it('should work', async () => {
20+
const msg = await helloService.hello('killa');
21+
assert(msg === 'hello, killa');
22+
});
23+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { app } from 'egg-mock/bootstrap';
2+
import assert from 'assert';
3+
4+
describe('test/controller/HelloController.test.ts', () => {
5+
it('should work', async () => {
6+
await app.httpRequest()
7+
.get('/hello?name=killa')
8+
.expect(200)
9+
.expect(res => {
10+
assert.deepStrictEqual(res.body, {
11+
success: true,
12+
data: {
13+
message: 'hello, killa (GET)',
14+
},
15+
});
16+
});
17+
});
18+
});

hello-tegg/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@eggjs/tsconfig"
3+
}

0 commit comments

Comments
 (0)