Skip to content

Commit

Permalink
refactor: use async function and support egg@2 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Nov 23, 2017
1 parent a1fd088 commit f19322f
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sudo: false
language: node_js
node_js:
- '6'
- '7'
- '8'
- '9'
install:
- npm i npminstall && npminstall
script:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ $ npm i egg-userservice
- `ctx.user`: current user data
- `ctx.userId`: the user id of current user
- `app.config.userservice.service.getUser(ctx)`:
- `app.config.userservice.service.getUserId(ctx)`:
- `app.config.userservice.service.getUserId(ctx)`:



Expand All @@ -64,11 +64,11 @@ Add your userservice configurations to `config/config.default.js`
```js
exports.userservice = {
service: {
* getUser(ctx) {
async getUser(ctx) {
// Retrieve your user data from cookie, redis, db, whatever
// For common web applications using cookie, you may get session id with ctx.cookies
},

getUserId(ctx) {
// The way to get userId
// eg. return ctx.user.userId
Expand Down
18 changes: 11 additions & 7 deletions app/middleware/userservice.js
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();
};
};
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- nodejs_version: '6'
- nodejs_version: '7'
- nodejs_version: '8'
- nodejs_version: '9'

install:
- ps: Install-Product node $env:nodejs_version
Expand All @@ -10,6 +10,6 @@ install:
test_script:
- node --version
- npm --version
- npm run ci
- npm run test

build: off
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,18 @@

},
"devDependencies": {
"autod": "^2.7.1",
"egg": "^0.9.0",
"egg-bin": "^2.0.2",
"egg-ci": "^1.1.0",
"egg-mock": "^2.3.1",
"eslint": "^3.14.1",
"eslint-config-egg": "^3.2.0",
"supertest": "^3.0.0"
"autod": "^3.0.1",
"egg": "^2.0.0",
"egg-bin": "^4.3.5",
"egg-ci": "^1.8.0",
"egg-mock": "^3.13.1",
"eslint": "^4.11.0",
"eslint-config-egg": "^5.1.1"
},
"engines": {
"node": ">= 6.0.0"
"node": ">= 8.0.0"
},
"ci": {
"version": "6, 7"
"version": "8, 9"
}
}
8 changes: 8 additions & 0 deletions test/fixtures/apps/userservice-async/app/controller/home.js
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,
};
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/userservice-async/app/router.js
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 test/fixtures/apps/userservice-async/config/config.default.js
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';
3 changes: 3 additions & 0 deletions test/fixtures/apps/userservice-async/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "userservice"
}
2 changes: 2 additions & 0 deletions test/fixtures/apps/userservice/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ exports.userservice = {
},
},
};

exports.keys = 'keys';
83 changes: 58 additions & 25 deletions test/userservice.test.js
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);
});
});
});

0 comments on commit f19322f

Please sign in to comment.