typera-openapi
Advanced tools
Comparing version 0.11.1 to 0.11.2
@@ -179,19 +179,42 @@ "use strict"; | ||
else if (fnName === 'handler') { | ||
const handlerFn = expr.arguments[0]; | ||
if (!handlerFn || | ||
(!ts.isArrowFunction(handlerFn) && | ||
!ts.isFunctionExpression(handlerFn))) { | ||
ctx.log('warn', 'Handler is not a function'); | ||
// routeConstructor: { | ||
// handler: (routeHandler: (req) => ...) | ||
// } | ||
// | ||
// We want to dig out the type of `req`. | ||
// | ||
const routeConstructor = lhs.expression; | ||
const routeConstructorType = ctx.checker.getTypeAtLocation(routeConstructor); | ||
const handlerSymbol = routeConstructorType.getProperty('handler'); | ||
if (!handlerSymbol) { | ||
ctx.log('warn', 'Could not get the type of handler()'); | ||
return; | ||
} | ||
const req = handlerFn.parameters[0]; | ||
if (req) { | ||
const type = ctx.checker.getTypeAtLocation(req); | ||
body = utils_1.getPropertyType(ctx.checker, req, type, 'body'); | ||
query = utils_1.getPropertyType(ctx.checker, req, type, 'query'); | ||
headers = utils_1.getPropertyType(ctx.checker, req, type, 'headers'); | ||
routeParams = utils_1.getPropertyType(ctx.checker, req, type, 'routeParams'); | ||
cookies = utils_1.getPropertyType(ctx.checker, req, type, 'cookies'); | ||
requestNode = req; | ||
const handlerType = ctx.checker.getTypeOfSymbolAtLocation(handlerSymbol, routeConstructor); | ||
const handlerParamTypes = handlerType | ||
.getCallSignatures() | ||
.flatMap((sig) => sig.getParameters()) | ||
.flatMap((param) => ctx.checker.getTypeOfSymbolAtLocation(param, routeConstructor)); | ||
if (handlerParamTypes.length !== 1) { | ||
ctx.log('warn', 'handler() should have one parameter'); | ||
return; | ||
} | ||
const routeHandler = handlerParamTypes[0]; | ||
const routeHandlerParamTypes = routeHandler | ||
.getCallSignatures() | ||
.flatMap((c) => c.getParameters()) | ||
.flatMap((param) => ctx.checker.getTypeOfSymbolAtLocation(param, routeConstructor)); | ||
if (routeHandlerParamTypes.length !== 1) { | ||
ctx.log('warn', 'Route handler should have one parameter'); | ||
return; | ||
} | ||
const reqType = routeHandlerParamTypes[0]; | ||
[body, query, headers, routeParams, cookies] = [ | ||
'body', | ||
'query', | ||
'headers', | ||
'routeParams', | ||
'cookies', | ||
].map((property) => utils_1.getPropertyType(ctx.checker, routeConstructor, reqType, property)); | ||
requestNode = routeConstructor; | ||
} | ||
@@ -198,0 +221,0 @@ expr = lhs.expression; |
{ | ||
"name": "typera-openapi", | ||
"version": "0.11.1", | ||
"version": "0.11.2", | ||
"description": "Generate OpenAPI spec from typera routes", | ||
@@ -28,3 +28,3 @@ "repository": "https://github.com/akheron/typera-openapi", | ||
"typescript": "^4.1.3", | ||
"yargs": "^16.2.0" | ||
"yargs": "^17.0.0" | ||
}, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
39770
769
+ Addedcliui@8.0.1(transitive)
+ Addedyargs@17.7.2(transitive)
+ Addedyargs-parser@21.1.1(transitive)
- Removedcliui@7.0.4(transitive)
- Removedyargs@16.2.0(transitive)
- Removedyargs-parser@20.2.9(transitive)
Updatedyargs@^17.0.0