-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use async function and support egg@2 (#8)
- Loading branch information
1 parent
a1fd088
commit f19322f
Showing
11 changed files
with
124 additions
and
50 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
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
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 |
---|---|---|
@@ -1,15 +1,19 @@ | ||
'use strict'; | ||
|
||
module.exports = options => { | ||
return function* userservice(next) { | ||
if (!this.user && options.service.getUser) { | ||
this.user = yield options.service.getUser(this); | ||
module.exports = (options, app) => { | ||
// both support generator function and async function | ||
if (options.service.getUser) { | ||
options.service.getUser = app.toAsyncFunction(options.service.getUser); | ||
} | ||
return async function userservice(ctx, next) { | ||
if (!ctx.user && options.service.getUser) { | ||
ctx.user = await options.service.getUser(ctx); | ||
} | ||
|
||
if (!this.userId && options.service.getUserId) { | ||
this.userId = options.service.getUserId(this); | ||
if (!ctx.userId && options.service.getUserId) { | ||
ctx.userId = options.service.getUserId(ctx); | ||
} | ||
|
||
yield next; | ||
return next(); | ||
}; | ||
}; |
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
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
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,8 @@ | ||
'use strict'; | ||
|
||
module.exports = function* () { | ||
this.body = { | ||
userId: this.userId, | ||
user: this.user, | ||
}; | ||
}; |
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,5 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
app.get('/', app.controller.home); | ||
}; |
20 changes: 20 additions & 0 deletions
20
test/fixtures/apps/userservice-async/config/config.default.js
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,20 @@ | ||
'use strict'; | ||
|
||
exports.userservice = { | ||
service: { | ||
getUserId(ctx) { | ||
return ctx.user && ctx.user.uid; | ||
}, | ||
async getUser(ctx) { | ||
if (!ctx.query.uid || !ctx.query.name) { | ||
return null; | ||
} | ||
return { | ||
uid: ctx.query.uid, | ||
name: ctx.query.name, | ||
}; | ||
}, | ||
}, | ||
}; | ||
|
||
exports.keys = 'keys'; |
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 @@ | ||
{ | ||
"name": "userservice" | ||
} |
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 |
---|---|---|
|
@@ -16,3 +16,5 @@ exports.userservice = { | |
}, | ||
}, | ||
}; | ||
|
||
exports.keys = 'keys'; |
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 |
---|---|---|
@@ -1,37 +1,70 @@ | ||
'use strict'; | ||
|
||
const request = require('supertest'); | ||
const mock = require('egg-mock'); | ||
|
||
describe('test/userservice.test.js', () => { | ||
let app; | ||
before(done => { | ||
app = mock.app({ | ||
baseDir: 'apps/userservice', | ||
describe('generator', () => { | ||
before(done => { | ||
app = mock.app({ | ||
baseDir: 'apps/userservice', | ||
}); | ||
app.ready(done); | ||
}); | ||
|
||
it('should get user and userId', done => { | ||
app.httpRequest() | ||
.get('/?uid=456&name=shaoshuai0102') | ||
.expect({ | ||
userId: '456', | ||
user: { | ||
uid: '456', | ||
name: 'shaoshuai0102', | ||
}, | ||
}) | ||
.expect(200, done); | ||
}); | ||
app.ready(done); | ||
}); | ||
|
||
it('should get user and userId', done => { | ||
request(app.callback()) | ||
.get('/?uid=456&name=shaoshuai0102') | ||
.expect({ | ||
userId: '456', | ||
user: { | ||
uid: '456', | ||
name: 'shaoshuai0102', | ||
}, | ||
}) | ||
.expect(200, done); | ||
it('should return empty', done => { | ||
app.httpRequest() | ||
.get('/') | ||
.expect({ | ||
userId: null, | ||
user: null, | ||
}) | ||
.expect(200, done); | ||
}); | ||
}); | ||
|
||
it('should return empty', done => { | ||
request(app.callback()) | ||
.get('/') | ||
.expect({ | ||
userId: null, | ||
user: null, | ||
}) | ||
.expect(200, done); | ||
describe('generator', () => { | ||
before(done => { | ||
app = mock.app({ | ||
baseDir: 'apps/userservice-async', | ||
}); | ||
app.ready(done); | ||
}); | ||
|
||
it('should get user and userId', done => { | ||
app.httpRequest() | ||
.get('/?uid=456&name=shaoshuai0102') | ||
.expect({ | ||
userId: '456', | ||
user: { | ||
uid: '456', | ||
name: 'shaoshuai0102', | ||
}, | ||
}) | ||
.expect(200, done); | ||
}); | ||
|
||
it('should return empty', done => { | ||
app.httpRequest() | ||
.get('/') | ||
.expect({ | ||
userId: null, | ||
user: null, | ||
}) | ||
.expect(200, done); | ||
}); | ||
}); | ||
}); |