Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
decache(path);
decache(_path.resolve(path));
// 控制器
if (isController(path)) {
const paths = _path.parse(path);
paths.dir = _.split(paths.dir, "/");
paths.dir.shift();
paths.dir = paths.dir.join("/");
const Ctrl = require(_path.resolve(path));
if (
Ctrl instanceof KoaRouter ||
_.isEqual(
Object.keys(Ctrl.__proto__),
Object.keys(KoaRouter.prototype)
)
) {
// 移除旧的
for (const layer of Ctrl.routes().router.stack) {
_.remove(doodoo.router.stack, o => {
return o.path === layer.path;
});
}
return;
}
if (!isClass(Ctrl)) {
return;
}
const methods = Object.getOwnPropertyNames(Ctrl.prototype);
const layers = [];
};
try {
let Koa = require('koa');
let originalUse = Koa.prototype.use;
Koa.prototype.use = function (middleware) {
let wrapped = wrapMiddleware(middleware);
return originalUse.call(this, wrapped);
};
Koa.prototype.use._original = originalUse;
registerWrapped(Koa.prototype, 'use');
try {
const Router = require('koa-router');
let originalRegister = Router.prototype.register;
Router.prototype.register = function () {
let middlewares = Array.isArray(arguments[2]) ? arguments[2] : [arguments[2]];
let wrappedMiddlewares = middlewares.map(middleware => wrapMiddleware(middleware));
arguments[2] = wrappedMiddlewares;
return originalRegister.apply(this, arguments);
};
Router.prototype.register._original = originalRegister;
registerWrapped(Router.prototype, 'register');
} catch (e) {
// app didn't use koa-router
}
} catch (e) {
// app didn't use koa
try {
const Router = require('koa-router');
let originalRegister = Router.prototype.register;
Router.prototype.register = function () {
let middlewares = Array.isArray(arguments[2]) ? arguments[2] : [arguments[2]];
let wrappedMiddlewares = middlewares.map(middleware => wrapMiddleware(middleware));
arguments[2] = wrappedMiddlewares;
return originalRegister.apply(this, arguments);
};
Router.prototype.register._original = originalRegister;
registerWrapped(Router.prototype, 'register');
} catch (e) {
// app didn't use koa-router
}
} catch (e) {
// app didn't use koa
throw new Error('koa-newrelic cannot work without koa');
}
}
// tansaction name
let parseTransactionName;
if (opts.customTransactionName && typeof opts.customTransactionName === 'function') {
parseTransactionName = opts.customTransactionName;
} else {
// newrelic has frontend display logic, which will format the transaction name if it's under express
// (method, path) => 'Expressjs/' + method + '/' + path;
registerWrapped(Koa.prototype, 'use');
try {
const Router = require('koa-router');
let originalRegister = Router.prototype.register;
Router.prototype.register = function () {
let middlewares = Array.isArray(arguments[2]) ? arguments[2] : [arguments[2]];
let wrappedMiddlewares = middlewares.map(middleware => wrapMiddleware(middleware));
arguments[2] = wrappedMiddlewares;
return originalRegister.apply(this, arguments);
};
Router.prototype.register._original = originalRegister;
registerWrapped(Router.prototype, 'register');
} catch (e) {
// app didn't use koa-router
}
} catch (e) {
// app didn't use koa
throw new Error('koa-newrelic cannot work without koa');
}
}
// tansaction name
let parseTransactionName;
if (opts.customTransactionName && typeof opts.customTransactionName === 'function') {
parseTransactionName = opts.customTransactionName;
} else {
// newrelic has frontend display logic, which will format the transaction name if it's under express
try {
let Koa = require('koa');
let originalUse = Koa.prototype.use;
Koa.prototype.use = function (middleware) {
let wrapped = wrapMiddleware(middleware);
return originalUse.call(this, wrapped);
};
Koa.prototype.use._original = originalUse;
registerWrapped(Koa.prototype, 'use');
try {
const Router = require('koa-router');
let originalRegister = Router.prototype.register;
Router.prototype.register = function () {
let middlewares = Array.isArray(arguments[2]) ? arguments[2] : [arguments[2]];
let wrappedMiddlewares = middlewares.map(middleware => wrapMiddleware(middleware));
arguments[2] = wrappedMiddlewares;
return originalRegister.apply(this, arguments);
};
Router.prototype.register._original = originalRegister;
registerWrapped(Router.prototype, 'register');
} catch (e) {
// app didn't use koa-router
}
} catch (e) {
// app didn't use koa
throw new Error('koa-newrelic cannot work without koa');
}
const urlJoin = require('url-join')
const website = require('../config/website')
if (website.semi_restful) {
Router.prototype.put = function (name, path, middleware) {
if (typeof path === 'string' || path instanceof RegExp) {
middleware = Array.prototype.slice.call(arguments, 2)
} else {
middleware = Array.prototype.slice.call(arguments, 1)
path = name
name = null
}
return this.post(urlJoin(path, '/update'), ...middleware)
}
Router.prototype.del = function (name, path, middleware) {
if (typeof path === 'string' || path instanceof RegExp) {
middleware = Array.prototype.slice.call(arguments, 2)
} else {
middleware = Array.prototype.slice.call(arguments, 1)
path = name
name = null
}
return this.post(urlJoin(path, '/delete'), ...middleware)
}
}
const router = new Router({
prefix: '/api'
})
const session = require('./session')
const Router = require('koa-router')
const urlJoin = require('url-join')
const website = require('../config/website')
if (website.semi_restful) {
Router.prototype.put = function (name, path, middleware) {
if (typeof path === 'string' || path instanceof RegExp) {
middleware = Array.prototype.slice.call(arguments, 2)
} else {
middleware = Array.prototype.slice.call(arguments, 1)
path = name
name = null
}
return this.post(urlJoin(path, '/update'), ...middleware)
}
Router.prototype.del = function (name, path, middleware) {
if (typeof path === 'string' || path instanceof RegExp) {
middleware = Array.prototype.slice.call(arguments, 2)
} else {
middleware = Array.prototype.slice.call(arguments, 1)
path = name
loadRouters() {
const controllers = glob.sync("*/controller/**/*.{js,js7}", {
cwd: this.root
});
for (const controller of controllers) {
const paths = path.parse(controller);
const Ctrl = require(path.resolve(this.root, controller));
if (
Ctrl instanceof KoaRouter ||
_.isEqual(
Object.keys(Ctrl.__proto__),
Object.keys(KoaRouter.prototype)
)
) {
this.router.use(
path
.join("/", paths.dir, paths.name)
.replace(/\\/g, "/")
.replace(/\/controller/, ""),
Ctrl.routes()
);
continue;
}
if (!isClass(Ctrl)) {
continue;
}
const methods = Object.getOwnPropertyNames(Ctrl.prototype);
Router.define = function (options) {
let {setup, ...rest} = options;
if (_.isFunction(options)) {
setup = options;
options = {};
}
options = rest || options;
setup = setup || (router => router)
let router = new Router(options);
setup(router);
return router;
}
Router.prototype.schema = function () {
return null;
}
export function schema(request, response) {
let request_schema = request ? joi_to_json_schema(request) : {};
let response_schema = response ? joi_to_json_schema(response) : {};
return {
schema:{
request: request_schema,
response: response_schema
},
example:{
request: !_.isEmpty(request_schema) ? jsf(request_schema) : {},
response: !_.isEmpty(response_schema) ? jsf(response_schema) : {}
}
}