-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
79 additions
and
0 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,79 @@ | ||
# typera-openapi - typera to OpenAPI generator | ||
|
||
 | ||
|
||
`typera-openapi` is an experimental tool that creates [OpenAPI v3] definitions | ||
from a project that uses [typera] for routes. | ||
|
||
## Getting started | ||
|
||
Install typera-openapi: | ||
|
||
```sh | ||
npm install typera-openapi | ||
``` | ||
|
||
Your route files must have a single default export that exports a typera router, | ||
like this: | ||
|
||
```typescript | ||
import { Route, route, router } from 'typera-express' | ||
|
||
const myRoute: Route<...> = route.get(...).handler(...) | ||
// ... | ||
|
||
export default router(myRoute, ...) | ||
``` | ||
|
||
Run the `typera-openapi` tool giving paths to your route files as command line | ||
arguments. Assuming you have two route files in your project: | ||
|
||
```sh | ||
npx typera-openapi src/routes/foo.ts src/routes/bar.ts | ||
``` | ||
|
||
This cerates `src/routes/foo.openapi.ts` and `src/routes/bar.openapi.ts` which | ||
contain the OpenAPI definitions. | ||
|
||
Use the definitions in your app to serve documentation: | ||
|
||
```typescript | ||
// This is src/app.ts | ||
import * as express from 'express' | ||
import { OpenAPIV3 } from 'openapi-types' | ||
import * as swaggerUi from 'swagger-ui-express' | ||
import { prefix } from 'typera-openapi' | ||
|
||
import foo from './routes/foo' | ||
import fooDefs from './routes/foo.openapi' | ||
import bar from './routes/bar' | ||
import barDefs from './routes/bar.openapi' | ||
|
||
const openapiDoc: OpenAPIV3.Document = { | ||
openapi: '3.0.0', | ||
info: { | ||
title: 'My cool API', | ||
version: '0.1.0', | ||
}, | ||
paths: { | ||
...prefix('/foo', fooDefs.paths), | ||
...prefix('/bar', barDefs.paths), | ||
}, | ||
} | ||
|
||
const app = express() | ||
app.use('/foo', foo.handler()) | ||
app.use('/bar', bar.handler()) | ||
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(openapiDoc)) | ||
|
||
app.listen(3000, () => { | ||
console.log('Listening on 127.0.0.1:3000') | ||
}) | ||
``` | ||
|
||
The `prefix` function is used to move OpenAPI path definitions to a different | ||
prefix, because the `foo` and `bar` routes are served from their respecive | ||
prefixes. | ||
|
||
[openapi v3]: https://swagger.io/specification/ | ||
[typera]: https://github.com/akheron/typera |