-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from lifeomic/ep/openapi-introspection
feat: optional openapi schema introspection
- Loading branch information
Showing
5 changed files
with
235 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
IntrospectionConfig, | ||
IntrospectionResponse, | ||
OneSchemaDefinition, | ||
} from './types'; | ||
import type Router from '@koa/router'; | ||
import { toOpenAPISpec } from './openapi'; | ||
|
||
export const addIntrospection = ( | ||
introspection: IntrospectionConfig, | ||
/** | ||
* A function that returns the latest schema for the service. We need | ||
* to use a function because a `OneSchemaRouter`'s schema does not | ||
* fully exist yet during its constructor call, which is when this function | ||
* is executed. So any time the introspect route is called, the most | ||
* up-to-date schema will be returned. | ||
*/ | ||
getSchema: () => OneSchemaDefinition, | ||
/** | ||
* The service's default router that all the endpoints are | ||
* declared on. | ||
*/ | ||
router: Router<any, any>, | ||
) => { | ||
const introRouter = introspection.router || router; | ||
introRouter.get(introspection.route, (ctx, next) => { | ||
const response: IntrospectionResponse = { | ||
schema: getSchema(), | ||
serviceVersion: introspection.serviceVersion, | ||
}; | ||
|
||
ctx.body = response; | ||
ctx.status = 200; | ||
return next(); | ||
}); | ||
if (introspection.openApi) { | ||
const { info } = introspection.openApi; | ||
introRouter.get(introspection.openApi.route, (ctx, next) => { | ||
const response = toOpenAPISpec(getSchema(), { | ||
info: { | ||
...info, | ||
version: introspection.serviceVersion, | ||
}, | ||
}); | ||
ctx.body = response; | ||
ctx.status = 200; | ||
return next(); | ||
}); | ||
} | ||
}; |
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