Comparing version 1.1.0 to 1.2.0
105
app.js
@@ -1,14 +0,8 @@ | ||
/**! | ||
* Plugin initialization, loading from directories following the conventions. | ||
*/ | ||
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const assert = require('assert'); | ||
const loadApi = require('./lib/load_api'); | ||
// load all js files in app/apis/ directory automatically | ||
module.exports = function(app) { | ||
module.exports = app => { | ||
// remove existed rest plugin configuration to prevent error configurations. | ||
@@ -22,98 +16,5 @@ const index = app.config.appMiddleware.indexOf('rest'); | ||
process.nextTick(function() { | ||
loadRestApis(app); | ||
loadApi(app); | ||
}); | ||
}; | ||
function loadRestApis(app) { | ||
// load rest api | ||
const apisDir = path.join(app.config.baseDir, 'app', 'apis'); | ||
// register routing automatically | ||
let urlprefix = app.config.rest.urlprefix; | ||
// /api/ => /api, / => "", ///a// => /a | ||
urlprefix = urlprefix.replace(/\/+$/, '').replace(/^\/+/, '/'); | ||
// load the middleware only and if only the rest plugin enabled | ||
registerDir(app, urlprefix, apisDir, 0); | ||
function registerDir(app, prefix, dir, level) { | ||
const names = fs.readdirSync(dir); | ||
for (const name of names) { | ||
const filepath = path.join(dir, name); | ||
const stat = fs.statSync(filepath); | ||
if (stat.isDirectory()) { | ||
// nesting is supported, for only two layers at most, `/api/parents/:parent_id/children/:child_id/objects/:id` | ||
if (level === 0) { | ||
registerDir(app, prefix + '/' + name + '/:parent_id', filepath, level + 1); | ||
} else if (level === 1) { | ||
registerDir(app, prefix + '/' + name + '/:child_id', filepath, level + 1 ); | ||
} else { | ||
app.loggers.coreLogger.warn('[egg:rest] for directory "%s", the nesting is too deep(%d layer), one layer at most, which means `/api/parents/:parent_id/objects/:id`', filepath, level + 1); | ||
} | ||
continue; | ||
} | ||
if (stat.isFile() && path.extname(name) === '.js') { | ||
let handler = require(filepath); | ||
// support `module.exports = function (app) { return exports; }` | ||
if (typeof handler === 'function') { | ||
handler = handler(app); | ||
} | ||
let objectNames = path.basename(name, '.js'); | ||
// apis/sites/index.js => GET /sites | ||
if (level >= 1 && objectNames === 'index') { | ||
objectNames = path.basename(dir); | ||
register(app, prefix.replace('/' + objectNames + '/:parent_id', ''), objectNames, handler); | ||
} else { | ||
register(app, prefix, objectNames, handler); | ||
} | ||
} | ||
} | ||
} | ||
function register(app, prefix, objectNames, handler) { | ||
const routeConfigs = { | ||
index: { | ||
method: 'get', | ||
url: '/{objects}', | ||
}, | ||
show: { | ||
method: 'get', | ||
url: '/{objects}/:id', | ||
}, | ||
create: { | ||
method: 'post', | ||
url: '/{objects}', | ||
}, | ||
update: { | ||
method: 'put', | ||
url: '/{objects}/:id', | ||
}, | ||
destroy: { | ||
method: 'delete', | ||
url: '/{objects}/:id', | ||
}, | ||
}; | ||
// check: index(), show(), create(), update(), destroy() | ||
for (const fname in handler) { | ||
const fn = handler[fname]; | ||
const routeConfig = routeConfigs[fname]; | ||
if (!routeConfig) { | ||
continue; | ||
} | ||
const url = prefix + routeConfig.url.replace('{objects}', objectNames); | ||
const routerName = routeConfig.method + ':' + url; | ||
const restapi = require('./api')(app.config.rest, { | ||
fname, | ||
objects: objectNames, | ||
fn, | ||
rule: handler[fname + 'Rule'], | ||
}); | ||
app[routeConfig.method](routerName, url, restapi); | ||
app.loggers.coreLogger.info('[egg:rest] register router: %s %s => %s.%s()', | ||
routeConfig.method.toUpperCase(), url, objectNames, fname); | ||
} | ||
} | ||
} |
'use strict'; | ||
module.exports = function(options) { | ||
module.exports = options => { | ||
return function* rest(next) { | ||
@@ -5,0 +5,0 @@ if (this.url.indexOf(options.urlprefix) === 0) { |
'use strict'; | ||
module.exports = function(antx) { | ||
module.exports = appInfo => { | ||
const exports = {}; | ||
@@ -18,3 +18,3 @@ | ||
urlprefix: '/api/', | ||
production: antx.env === 'prod', | ||
production: appInfo.env === 'prod', | ||
authRequest: null, | ||
@@ -21,0 +21,0 @@ // authRequest: function* (ctx) { |
1.2.0 / 2016-11-25 | ||
================== | ||
* test: update deps && test style (#7) | ||
1.1.0 / 2016-09-29 | ||
@@ -3,0 +8,0 @@ ================== |
{ | ||
"name": "egg-rest", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Restful API plugin for egg", | ||
@@ -10,7 +10,8 @@ "eggPlugin": { | ||
"scripts": { | ||
"lint": "eslint . --fix", | ||
"test": "egg-bin test", | ||
"cov": "egg-bin cov", | ||
"ci": "npm run lint && npm run cov", | ||
"autod": "autod" | ||
"autod": "autod", | ||
"lint": "eslint .", | ||
"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", | ||
"ci": "npm run lint && npm run cov" | ||
}, | ||
@@ -24,2 +25,3 @@ "repository": { | ||
"config", | ||
"lib", | ||
"api.js", | ||
@@ -34,30 +36,30 @@ "app.js" | ||
], | ||
"author": "shaoshuai0102 <shaoshuai0102@gmail.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/eggjs/egg-rest/issues" | ||
}, | ||
"homepage": "https://github.com/eggjs/egg-rest#readme", | ||
"dependencies": { | ||
"debug": "^2.2.0", | ||
"utility": "^1.8.0" | ||
"debug": "^2.3.3", | ||
"utility": "^1.9.0" | ||
}, | ||
"devDependencies": { | ||
"autod": "^2.7.1", | ||
"egg": "^0.2.0", | ||
"egg-bin": "^1.3.0", | ||
"egg-ci": "^1.0.2", | ||
"egg-mock": "^0.0.4", | ||
"eslint": "^3.5.0", | ||
"eslint-config-egg": "^3.1.0", | ||
"pedding": "^1.0.0", | ||
"should": "^11.1.0", | ||
"supertest": "^2.0.0" | ||
"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", | ||
"pedding": "^1.1.0", | ||
"power-assert": "^1.4.2", | ||
"supertest": "^2.0.1", | ||
"webstorm-disable-index": "^1.0.11" | ||
}, | ||
"ci": { | ||
"version": "4, 6, 7" | ||
}, | ||
"engines": { | ||
"node": ">= 4.0.0" | ||
}, | ||
"ci": { | ||
"version": "4, 6" | ||
} | ||
"author": "shaoshuai0102 <shaoshuai0102@gmail.com>", | ||
"bugs": "https://github.com/eggjs/egg-rest/issues", | ||
"homepage": "https://github.com/eggjs/egg-rest#readme", | ||
"license": "MIT" | ||
} |
@@ -32,3 +32,3 @@ # egg-rest | ||
```bash | ||
$ npm i egg-rest | ||
$ npm i egg-rest --save | ||
``` | ||
@@ -109,3 +109,3 @@ | ||
**Note:** It does not support more than three level deep nesting. Example: `/api/users/3/posts/1/replies/2/answer` won't match file path | ||
**Note:** It does not support more than three level deep nesting. Example: `/api/users/3/posts/1/replies/2/answer` won't match file path | ||
`apis/users/posts/replies/answer.js`. Currently, it can only retrieve maximum three query parameters. | ||
@@ -112,0 +112,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
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
33453
8
12
2
Updateddebug@^2.3.3
Updatedutility@^1.9.0