Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolvers can now be specified through operationHandlers as a Operati…
…onHandlerOptions
- Loading branch information
Showing
4 changed files
with
96 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as path from 'path'; | ||
import { RequestHandler } from "express"; | ||
import { RouteMetadata } from "./framework/openapi.spec.loader"; | ||
|
||
export function defaultResolver(handlersPath: string, route: RouteMetadata): RequestHandler { | ||
const tmpModules = {}; | ||
const { expressRoute, method, schema } = route; | ||
const oId = schema['x-eov-operation-id'] || schema['operationId']; | ||
const baseName = schema['x-eov-operation-handler']; | ||
if (oId && !baseName) { | ||
throw Error( | ||
`found x-eov-operation-id for route ${method} - ${expressRoute}]. x-eov-operation-handler required.`, | ||
); | ||
} | ||
if (!oId && baseName) { | ||
throw Error( | ||
`found x-eov-operation-handler for route [${method} - ${expressRoute}]. operationId or x-eov-operation-id required.`, | ||
); | ||
} | ||
if ( | ||
oId && | ||
baseName && | ||
typeof handlersPath === 'string' | ||
) { | ||
const modulePath = path.join(handlersPath, baseName); | ||
if (!tmpModules[modulePath]) { | ||
tmpModules[modulePath] = require(modulePath); | ||
if (!tmpModules[modulePath][oId]) { | ||
// if oId is not found only module, try the module's default export | ||
tmpModules[modulePath] = tmpModules[modulePath].default; | ||
} | ||
} | ||
if (!tmpModules[modulePath][oId]) { | ||
throw Error( | ||
`Could not find 'x-eov-operation-handler' with id ${oId} in module '${modulePath}'. Make sure operation '${oId}' defined in your API spec exists as a handler function in '${modulePath}'.`, | ||
); | ||
} | ||
return tmpModules[modulePath][oId]; | ||
} | ||
} |