Comparing version 1.3.0 to 2.0.0
@@ -19,2 +19,1 @@ 'use strict'; | ||
}; | ||
'use strict'; | ||
module.exports = options => { | ||
return function* rest(next) { | ||
if (this.url.indexOf(options.urlprefix) === 0) { | ||
return function rest(ctx, next) { | ||
if (ctx.url.indexOf(options.urlprefix) === 0) { | ||
// Make sure all RESTful APIs always respond with json format | ||
this.response.type = 'json'; | ||
ctx.response.type = 'json'; | ||
} | ||
yield* next; | ||
return next(); | ||
}; | ||
}; |
2.0.0 / 2017-11-23 | ||
================== | ||
**others** | ||
* [[`09f1534`](http://github.com/eggjs/egg-rest/commit/09f15340602fc1198d00b322ee179f58a4665aa6)] - refactor: use async function and support egg@2 (#14) (Yiyu He <<dead_horse@qq.com>>) | ||
* [[`77f2481`](http://github.com/eggjs/egg-rest/commit/77f248155c896216d48bd36b4774cd4715a1fbad)] - docs: add the config in readme.md (#13) (恬竹 <<2632807692@qq.com>>) | ||
* [[`5865626`](http://github.com/eggjs/egg-rest/commit/5865626f4aa00ce75ddd6c0c2af4e075a7574a40)] - docs: fix plugin.js (#12) (TZ | 天猪 <<atian25@qq.com>>) | ||
* [[`18546a4`](http://github.com/eggjs/egg-rest/commit/18546a444b975c0854b97091b861028ac2f77579)] - test: fix testcase (#11) (Haoliang Gao <<sakura9515@gmail.com>>) | ||
* [[`f9b3e93`](http://github.com/eggjs/egg-rest/commit/f9b3e93b0a0f5bff8e88b24eed539aa136eb5e94)] - chore: upgrade deps and fix test (#10) (Haoliang Gao <<sakura9515@gmail.com>>) | ||
1.3.0 / 2016-12-16 | ||
@@ -3,0 +13,0 @@ ================== |
@@ -48,7 +48,7 @@ 'use strict'; | ||
return function* restapi() { | ||
return async function restapi(ctx) { | ||
// Occurrence of User-Agent is required | ||
if (!this.get('User-Agent')) { | ||
this.status = 403; | ||
this.body = { | ||
if (!ctx.get('User-Agent')) { | ||
ctx.status = 403; | ||
ctx.body = { | ||
message: 'Please make sure your request has a User-Agent header', | ||
@@ -60,3 +60,3 @@ }; | ||
if (!routeConfig) { | ||
notFoundResponse(this); | ||
notFoundResponse(ctx); | ||
return; | ||
@@ -74,13 +74,13 @@ } | ||
try { | ||
accessToken = yield authRequest(this); | ||
accessToken = await authRequest(ctx); | ||
} catch (err) { | ||
err.status = err.status || 500; | ||
errorResponse(this, err); | ||
errorResponse(ctx, err); | ||
return; | ||
} | ||
debug('authRequest %s got %j', this.url, accessToken); | ||
debug('authRequest %s got %j', ctx.url, accessToken); | ||
if (!accessToken) { | ||
this.status = 401; | ||
this.body = { | ||
ctx.status = 401; | ||
ctx.body = { | ||
message: 'Bad credentials', | ||
@@ -92,3 +92,3 @@ }; | ||
// Make accessToken available for controllers. | ||
this.accessToken = accessToken; | ||
ctx.accessToken = accessToken; | ||
} | ||
@@ -98,9 +98,9 @@ | ||
// Deal with `/users/1,2,3,4` | ||
if (this.params.id.indexOf(',') >= 0) { | ||
const ids = utility.split(this.params.id, ','); | ||
if (ctx.params.id.indexOf(',') >= 0) { | ||
const ids = utility.split(ctx.params.id, ','); | ||
if (ids.length === 0) { | ||
// Invalid multiple id combination like `/users/,,,,`, | ||
// respond with 400, providing a hint of invalid id. | ||
this.status = 400; | ||
this.body = { | ||
ctx.status = 400; | ||
ctx.body = { | ||
message: 'ids format error', | ||
@@ -110,21 +110,21 @@ }; | ||
} | ||
this.params.ids = ids; | ||
ctx.params.ids = ids; | ||
} else { | ||
// Make sure this.params.ids always exist | ||
this.params.ids = [ this.params.id ]; | ||
// Make sure ctx.params.ids always exist | ||
ctx.params.ids = [ ctx.params.id ]; | ||
} | ||
} else if (fname === 'index') { | ||
// Paging | ||
if (this.query.page && RE_NUMBER.test(this.query.page)) { | ||
this.params.page = Number(this.query.page); | ||
if (ctx.query.page && RE_NUMBER.test(ctx.query.page)) { | ||
ctx.params.page = Number(ctx.query.page); | ||
} | ||
if (this.query.per_page && RE_NUMBER.test(this.query.per_page)) { | ||
this.params.per_page = Number(this.query.per_page); | ||
if (ctx.query.per_page && RE_NUMBER.test(ctx.query.per_page)) { | ||
ctx.params.per_page = Number(ctx.query.per_page); | ||
} | ||
} else if (fname === 'create' || fname === 'update') { | ||
// Detect invalid request automatically | ||
const requestData = this.request.body; | ||
const requestData = ctx.request.body; | ||
if (!requestData || typeof requestData !== 'object') { | ||
this.status = 400; | ||
this.body = { | ||
ctx.status = 400; | ||
ctx.body = { | ||
message: 'Body should be a JSON object', | ||
@@ -134,3 +134,3 @@ }; | ||
} | ||
this.params.data = requestData; | ||
ctx.params.data = requestData; | ||
} | ||
@@ -141,5 +141,5 @@ | ||
if (fname === 'show' || fname === 'index') { | ||
fields = getFields(this, fname); | ||
fields = getFields(ctx, fname); | ||
if (fields) { | ||
this.params.fields = fields; | ||
ctx.params.fields = fields; | ||
} | ||
@@ -150,13 +150,13 @@ } | ||
if (routeConfig.rule) { | ||
this.validate(routeConfig.rule, this.params.data); | ||
ctx.validate(routeConfig.rule, ctx.params.data); | ||
} | ||
yield routeConfig.fn.call(this); | ||
await routeConfig.fn.call(ctx, ctx); | ||
} catch (err) { | ||
err.status = err.status || 500; | ||
errorResponse(this, err); | ||
errorResponse(ctx, err); | ||
return; | ||
} | ||
if (fname === 'destroy' && !this.data && !this.body) { | ||
this.status = 204; | ||
if (fname === 'destroy' && !ctx.data && !ctx.body) { | ||
ctx.status = 204; | ||
return; | ||
@@ -166,4 +166,4 @@ } | ||
// update | ||
if (fname === 'update' && !this.data && !this.body) { | ||
this.status = 204; | ||
if (fname === 'update' && !ctx.data && !ctx.body) { | ||
ctx.status = 204; | ||
return; | ||
@@ -174,10 +174,10 @@ } | ||
if (fname === 'create') { | ||
this.status = 201; | ||
ctx.status = 201; | ||
} | ||
if (this.body) { | ||
if (ctx.body) { | ||
return; | ||
} | ||
let data = this.data; | ||
let data = ctx.data; | ||
if (data) { | ||
@@ -198,3 +198,3 @@ if (typeof data === 'object' && (fname === 'show' || fname === 'index')) { | ||
if (fname === 'show') { | ||
notFoundResponse(this); | ||
notFoundResponse(ctx); | ||
return; | ||
@@ -210,5 +210,5 @@ } else if (fname === 'index') { | ||
// try to include meta | ||
meta: this.meta, | ||
meta: ctx.meta, | ||
}; | ||
this.body = result; | ||
ctx.body = result; | ||
}; | ||
@@ -215,0 +215,0 @@ |
@@ -7,2 +7,7 @@ 'use strict'; | ||
module.exports = app => { | ||
// support both generator function and async function | ||
if (app.config.rest.authRequest) { | ||
app.config.rest.authRequest = app.toAsyncFunction(app.config.rest.authRequest); | ||
} | ||
// load rest api | ||
@@ -82,3 +87,4 @@ const apisDir = path.join(app.config.baseDir, 'app', 'apis'); | ||
for (const fname in handler) { | ||
const fn = handler[fname]; | ||
// support both generator function and async function | ||
const fn = app.toAsyncFunction(handler[fname]); | ||
const routeConfig = routeConfigs[fname]; | ||
@@ -85,0 +91,0 @@ if (!routeConfig) { |
{ | ||
"name": "egg-rest", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"description": "Restful API plugin for egg", | ||
"eggPlugin": { | ||
"name": "rest" | ||
"name": "rest", | ||
"dependencies": [ | ||
"validate" | ||
] | ||
}, | ||
@@ -13,4 +16,4 @@ "main": "index.js", | ||
"test": "npm run lint -- --fix && npm run test-local", | ||
"test-local": "egg-bin test -r intelli-espower-loader", | ||
"cov": "egg-bin cov -r intelli-espower-loader", | ||
"test-local": "egg-bin test", | ||
"cov": "egg-bin cov", | ||
"ci": "npm run lint && npm run cov" | ||
@@ -26,3 +29,2 @@ }, | ||
"lib", | ||
"api.js", | ||
"app.js" | ||
@@ -37,24 +39,24 @@ ], | ||
"dependencies": { | ||
"debug": "^2.3.3", | ||
"utility": "^1.9.0" | ||
"debug": "^3.1.0", | ||
"utility": "^1.13.1" | ||
}, | ||
"devDependencies": { | ||
"autod": "^2.7.1", | ||
"egg": "^0.5.0", | ||
"egg-bin": "^1.7.0", | ||
"egg-ci": "^1.1.0", | ||
"egg-mock": "^1.2.0", | ||
"eslint": "^3.10.2", | ||
"eslint-config-egg": "^3.2.0", | ||
"intelli-espower-loader": "^1.0.1", | ||
"autod": "^3.0.1", | ||
"egg": "^2.0.0", | ||
"egg-bin": "^4.3.5", | ||
"egg-ci": "^1.8.0", | ||
"egg-mock": "^3.13.1", | ||
"egg-validate": "^1.0.0", | ||
"eslint": "^4.11.0", | ||
"eslint-config-egg": "^5.1.1", | ||
"pedding": "^1.1.0", | ||
"power-assert": "^1.4.2", | ||
"supertest": "^2.0.1", | ||
"webstorm-disable-index": "^1.0.11" | ||
"power-assert": "^1.4.4", | ||
"supertest": "^3.0.0", | ||
"webstorm-disable-index": "^1.2.0" | ||
}, | ||
"ci": { | ||
"version": "4, 6, 7" | ||
"version": "8, 9" | ||
}, | ||
"engines": { | ||
"node": ">= 4.0.0" | ||
"node": ">= 8.0.0" | ||
}, | ||
@@ -61,0 +63,0 @@ "author": "shaoshuai0102 <shaoshuai0102@gmail.com>", |
@@ -40,12 +40,23 @@ # egg-rest | ||
```js | ||
exports.rest = true; | ||
exports.rest = { | ||
enable: true, | ||
package: 'egg-rest', | ||
}; | ||
``` | ||
Configure the rest plugin in `config/config.default.js`: | ||
## Configuration | ||
- `urlperfix`: Prefix of rest api url. Default to `/api/` | ||
- `authRequest`: a function for getting some value of authentication | ||
- `authIgnores`: allow some request to ignore authentication | ||
- `errorResponse`: Error handling function | ||
Example: Configure the rest plugin in `config/config.default.js`: | ||
```js | ||
exports.rest = { | ||
urlprefix: '/doc/api/', // Prefix of rest api url. Defaluts to /api/ | ||
urlprefix: '/doc/api/', // Prefix of rest api url. Default to /api/ | ||
authRequest: null, | ||
// authRequest: function* (ctx) { | ||
// authRequest: async ctx => { | ||
// // A truthy value must be returned when authentication succeeds. | ||
@@ -68,2 +79,3 @@ // // Otherwise the client will be responded with `401 Unauthorized` | ||
Controllers in files matching `${baseDir}/app/apis/**.js` will be loaded automatically according to routing rules. | ||
@@ -70,0 +82,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
34990
422
791
2
+ Addeddebug@3.2.7(transitive)
+ Addedms@2.1.3(transitive)
- Removeddebug@2.6.9(transitive)
- Removedms@2.0.0(transitive)
Updateddebug@^3.1.0
Updatedutility@^1.13.1