travelers
Advanced tools
Comparing version 2.1.5 to 2.1.6
@@ -24,4 +24,4 @@ import { travelersApis } from "./lib/api"; | ||
before?: (app: Express) => void; | ||
security?: { | ||
[key: string]: (req: Req, res: Res) => Promise<any>; | ||
security: { | ||
[key: string]: (req: Req, res: Res, next: NextFunction) => Promise<any>; | ||
}; | ||
@@ -28,0 +28,0 @@ apis: { |
/// <reference types="express" /> | ||
import * as joi from "joi"; | ||
import { Req, Res, Express } from "../index"; | ||
import { Req, Res, Express, NextFunction } from "../index"; | ||
declare const swaggerConfigDefalut: { | ||
@@ -64,3 +64,3 @@ swagger: string; | ||
declare function apiManage(app: Express, security: { | ||
[key: string]: (req: Req, res: Res) => Promise<any>; | ||
[key: string]: (req: Req, res: Res, next: NextFunction) => Promise<any>; | ||
}, apis: ManageApis, controllers: ManageControllers, config: { | ||
@@ -67,0 +67,0 @@ [key: string]: any; |
@@ -86,44 +86,43 @@ "use strict"; | ||
koaPath = koaPath.replace(/{/g, ":"); | ||
app[method](koaPath, (req, res, next) => { | ||
(async function () { | ||
// 验证 | ||
for (const item of security) { | ||
if (selfSecurity[item]) | ||
await selfSecurity[item](req, res); | ||
let funcs = [koaPath]; | ||
for (const securityItme of security) { | ||
if (selfSecurity[securityItme]) | ||
funcs.push(selfSecurity[securityItme]); | ||
} | ||
funcs.push(async (req, res, next) => { | ||
const _query = item.req.query || {}; | ||
const _body = item.req.body || {}; | ||
const _params = item.req.params || {}; | ||
let { params, query, body } = req; | ||
try { | ||
let queryKeys = Object.keys(_query); | ||
for (const queryKey of queryKeys) { | ||
await joi.validate(query[queryKey], _query[queryKey]); | ||
} | ||
const _query = item.req.query || {}; | ||
const _body = item.req.body || {}; | ||
const _params = item.req.params || {}; | ||
let { params, query, body } = req; | ||
try { | ||
let queryKeys = Object.keys(_query); | ||
for (const queryKey of queryKeys) { | ||
await joi.validate(query[queryKey], _query[queryKey]); | ||
} | ||
let paramsKeys = Object.keys(_params); | ||
for (const paramsKey of paramsKeys) { | ||
await joi.validate(params[paramsKey], _params[paramsKey]); | ||
} | ||
if (_body) | ||
await joi.validate(body, _body); | ||
let paramsKeys = Object.keys(_params); | ||
for (const paramsKey of paramsKeys) { | ||
await joi.validate(params[paramsKey], _params[paramsKey]); | ||
} | ||
catch (error) { | ||
res.status(400).send(error); | ||
return; | ||
if (_body) | ||
await joi.validate(body, _body); | ||
} | ||
catch (error) { | ||
res.status(400).send(error); | ||
return; | ||
} | ||
try { | ||
if (controllers[item.operationId]) { | ||
const result = await controllers[item.operationId](req, res); | ||
if (result) | ||
res.json(result); | ||
} | ||
try { | ||
if (controllers[item.operationId]) { | ||
const result = await controllers[item.operationId](req, res); | ||
if (result) | ||
res.json(result); | ||
} | ||
else { | ||
next(); | ||
} | ||
else { | ||
next(); | ||
} | ||
catch (error) { | ||
res.status(error.code || 500).send(error); | ||
} | ||
})(); | ||
} | ||
catch (error) { | ||
res.status(error.code || 500).send(error); | ||
} | ||
}); | ||
app[method].apply(app, funcs); | ||
}); | ||
@@ -130,0 +129,0 @@ }); |
{ | ||
"name": "travelers", | ||
"version": "2.1.5", | ||
"version": "2.1.6", | ||
"description": "I wanna write code like travelers", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
1464076
6020