Skip to content

Commit

Permalink
Make it possible to move routes under a prefix again
Browse files Browse the repository at this point in the history
  • Loading branch information
akheron committed Mar 18, 2022
1 parent dc41839 commit 844e004
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ const myRoute: Route<Response.Ok<MyResult> | Response.BadRequest<string>> =

// ...

/**
* @prefix /api
*/
export default router(myRoute, ...)

// The optional @prefix JSDoc tag prepends the prefix to all route paths.
```

Run the `typera-openapi` tool giving paths to your route files as command line
Expand Down
18 changes: 16 additions & 2 deletions docs/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,28 @@ const openapiDoc: OpenAPIV3.Document = {
version: '0.1.0',
},
paths: {
...routeDefs.paths,
...otherDefs.paths
...prefix('/api', routeDefs.paths),
...prefix('/other', otherDefs.paths),
}
}
```

New:

Use the `@prefix` JSDoc tag to move routes to a different prefix:

`routes.ts`

```
/**
* @prefix /api
*/
export default router(myRoute, ...)
```

The file that has the final OpenAPI document doesn't need to use the `prefix`
function anymore:

```
import openapi from './openapi'
Expand Down
13 changes: 11 additions & 2 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const visitTopLevelNode = (
node: ts.Node
): OpenAPIV3.PathsObject | undefined => {
if (ts.isExportAssignment(node) && !node.isExportEquals) {
const prefix = getRouterPrefix(node)

// 'export default' statement
const argSymbols = getRouterCallArgSymbols(ctx, node.expression)
if (!argSymbols) return
Expand All @@ -96,9 +98,10 @@ const visitTopLevelNode = (
)
if (routeDeclaration) {
const [path, method, operation] = routeDeclaration
const pathsItemObject = paths[path]
const prefixedPath = prefix + path
const pathsItemObject = paths[prefixedPath]
if (!pathsItemObject) {
paths[path] = { [method]: operation }
paths[prefixedPath] = { [method]: operation }
} else {
pathsItemObject[method] = operation
}
Expand Down Expand Up @@ -213,6 +216,12 @@ const getRouteTags = (symbol: ts.Symbol): string[] | undefined =>
.flatMap((symbolDisplayPart) => symbolDisplayPart.text.split(','))
.map((tag) => tag.trim())

const getRouterPrefix = (node: ts.Node): string =>
ts
.getJSDocTags(node)
.filter((tag) => tag.tagName.escapedText === 'prefix')
.flatMap((tag) => (typeof tag.comment === 'string' ? [tag.comment] : []))[0] ?? ''

const operationRequestBody = (
contentSchema: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined,
contentType = 'application/json'
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/generate.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ Object {
},
},
},
"/other-route": Object {
"/other-stuff/other-route": Object {
"get": Object {
"responses": Object {
"200": Object {
Expand Down
1 change: 1 addition & 0 deletions tests/other-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { Response, route, Route, router } from 'typera-express'

const otherRoute: Route<Response.Ok<string>> = route.get('/other-route').handler(() => Response.ok('hello'))

/** @prefix /other-stuff */
export default router(otherRoute)

0 comments on commit 844e004

Please sign in to comment.