forked from eggjs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: 零弌 <[email protected]>
- Loading branch information
Showing
12 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "eslint-config-egg/typescript" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "biz-module", | ||
"eggModule": { | ||
"name": "biz" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)', | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "@eggjs/tsconfig" | ||
} |