Skip to content

Commit

Permalink
feat: impl tegg example (eggjs#119)
Browse files Browse the repository at this point in the history
Co-authored-by: 零弌 <[email protected]>
  • Loading branch information
killagu and 零弌 authored Jul 23, 2021
1 parent 4b94602 commit 819d349
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hello-tegg/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg/typescript"
}
19 changes: 19 additions & 0 deletions hello-tegg/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
logs/
npm-debug.log
node_modules/
coverage/
.idea/
run/
logs/
.DS_Store
.vscode
*.swp
*.lock
*.js

app/**/*.js
test/**/*.js
config/**/*.js
app/**/*.map
test/**/*.map
config/**/*.map
13 changes: 13 additions & 0 deletions hello-tegg/app/biz/HelloService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
ContextProto,
AccessLevel,
} from '@eggjs/tegg';

@ContextProto({
accessLevel: AccessLevel.PUBLIC,
})
export class HelloService {
async hello(name: string): Promise<string> {
return `hello, ${name}`;
}
}
6 changes: 6 additions & 0 deletions hello-tegg/app/biz/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "biz-module",
"eggModule": {
"name": "biz"
}
}
40 changes: 40 additions & 0 deletions hello-tegg/app/controller/HelloController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
HTTPController,
HTTPMethod,
HTTPMethodEnum,
Context,
EggContext,
HTTPQuery,
Middleware,
Inject,
} from '@eggjs/tegg';
import { EggLogger } from 'egg';
import { traceMethod } from '../middleware/trace_method';
import { HelloService } from '../biz/HelloService';

@HTTPController()
@Middleware(traceMethod)
export class HelloController {
@Inject()
private readonly helloService: HelloService;

@Inject()
private readonly logger: EggLogger;

@HTTPMethod({
method: HTTPMethodEnum.GET,
path: '/hello',
})
async hello(@Context() ctx: EggContext, @HTTPQuery() name: string) {
this.logger.info('access url: %s', ctx.url);

const message = await this.helloService.hello(name);

return {
success: true,
data: {
message,
},
};
}
}
6 changes: 6 additions & 0 deletions hello-tegg/app/middleware/trace_method.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { EggContext, Next } from '@eggjs/tegg';

export async function traceMethod(ctx: EggContext, next: Next) {
await next();
ctx.body.data.message += ` (${ctx.method})`;
}
10 changes: 10 additions & 0 deletions hello-tegg/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { EggAppConfig, PowerPartial } from 'egg';

export default (appInfo: EggAppConfig) => {
const config = {} as PowerPartial<EggAppConfig>;

// override config from framework / plugin
config.keys = appInfo.name + '123456';

return config;
};
18 changes: 18 additions & 0 deletions hello-tegg/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { EggPlugin } from 'egg';

const plugin: EggPlugin = {
tegg: {
enable: true,
package: '@eggjs/tegg-plugin',
},
teggConfig: {
enable: true,
package: '@eggjs/tegg-config',
},
teggController: {
enable: true,
package: '@eggjs/tegg-controller-plugin',
},
};

export default plugin;
43 changes: 43 additions & 0 deletions hello-tegg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "hello-tegg",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "egg-scripts start",
"dev": "egg-bin dev -r egg-ts-helper/register",
"debug": "egg-bin debug -r egg-ts-helper/register",
"test-local": "egg-bin test -r egg-ts-helper/register",
"test": "npm run lint -- --fix && npm run test-local",
"cov": "egg-bin cov -r egg-ts-helper/register",
"tsc": "tsc -p tsconfig.json",
"ci": "npm run lint && npm run cov && npm run tsc",
"autod": "autod",
"lint": "eslint .",
"clean": "tsc -b --clean"
},
"egg": {
"typescript": true
},
"author": "",
"license": "ISC",
"dependencies": {
"@eggjs/tegg": "^0.1.1",
"@eggjs/tegg-config": "^0.1.0",
"@eggjs/tegg-controller-plugin": "^0.1.1",
"@eggjs/tegg-eventbus-plugin": "^0.1.1",
"@eggjs/tegg-plugin": "^0.1.1",
"egg": "^2.29.4"
},
"devDependencies": {
"@eggjs/tsconfig": "^1.0.0",
"@types/mocha": "^8.2.3",
"@types/node": "^16.4.0",
"egg-bin": "^4.16.4",
"egg-mock": "^3.26.0",
"egg-ts-helper": "^1.25.9",
"eslint": "^7.31.0",
"eslint-config-egg": "^9.0.0",
"typescript": "^4.3.5"
}
}
23 changes: 23 additions & 0 deletions hello-tegg/test/biz/HelloService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Context } from 'egg';
import assert from 'assert';
import { app } from 'egg-mock/bootstrap';
import { HelloService } from '../../app/biz/HelloService';

describe('test/biz/HelloService.test.ts', () => {
let ctx: Context;
let helloService: HelloService;

beforeEach(async () => {
ctx = await app.mockModuleContext();
helloService = await ctx.getEggObject(HelloService);
});

afterEach(async () => {
await app.destroyModuleContext(ctx);
});

it('should work', async () => {
const msg = await helloService.hello('killa');
assert(msg === 'hello, killa');
});
});
18 changes: 18 additions & 0 deletions hello-tegg/test/controller/HelloController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { app } from 'egg-mock/bootstrap';
import assert from 'assert';

describe('test/controller/HelloController.test.ts', () => {
it('should work', async () => {
await app.httpRequest()
.get('/hello?name=killa')
.expect(200)
.expect(res => {
assert.deepStrictEqual(res.body, {
success: true,
data: {
message: 'hello, killa (GET)',
},
});
});
});
});
3 changes: 3 additions & 0 deletions hello-tegg/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@eggjs/tsconfig"
}

0 comments on commit 819d349

Please sign in to comment.