Skip to content

Commit

Permalink
Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
akheron committed May 27, 2023
1 parent a6f578e commit b8e0131
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ const bodyCodec = t.type({
* @summary You can also set a short summary
* @tags Tag1, Tag2
*
* @routeParam myRouteParam Description for route parameter
*
* @response 200 Success response description.
* @response 400 Another description for a response. This one
* spans multile lines.
* @routeParam myRouteParam pathparameter description
*/
const myRoute: Route<Response.Ok<MyResult> | Response.BadRequest<string>> =
route.post(...).use(Parser.body(bodyCodec)).handler(...)
Expand Down
26 changes: 16 additions & 10 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,18 +768,24 @@ const typeToRequestParameters = (
ctx: Context,
in_: 'path' | 'query' | 'header' | 'cookie',
type: ts.Type | undefined,
routeParameters?: Map<string, string | undefined>
descriptions?: Map<string, string | undefined>
): OpenAPIV3.ParameterObject[] => {
if (!type) return []

const props = ctx.checker.getPropertiesOfType(type)
const missingRouteParam = routeParameters
? [...routeParameters.keys()].find(
(param) => !props.map((prop) => prop.name).includes(param)

if (descriptions) {
const propNames = new Set(props.map((prop) => prop.name))
const extraneousDescriptions = [...descriptions.keys()].filter(
(name) => !propNames.has(name)
)
if (extraneousDescriptions.length > 0) {
throw new Error(
`Descriptions provided for unmatched ${in_} parameters: ${extraneousDescriptions.join(
', '
)}`
)
: undefined
if (missingRouteParam) {
throw new Error(`RouteParameter ${missingRouteParam} does not exist.`)
}
}
return props.map((prop): OpenAPIV3.ParameterObject => {
const description = getDescriptionFromComment(ctx, prop)
Expand All @@ -790,9 +796,9 @@ const typeToRequestParameters = (
schema: {
type: 'string',
},
...(routeParameters
? routeParameters.get(prop.name)
? { description: routeParameters.get(prop.name) }
...(descriptions
? descriptions.get(prop.name)
? { description: descriptions.get(prop.name) }
: undefined
: description
? { description }
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 @@ -556,7 +556,7 @@ Object {
"operationId": "routeWithRouteParameterTag",
"parameters": Array [
Object {
"description": "12345",
"description": "Description goes here",
"in": "path",
"name": "myRouteParameter",
"required": true,
Expand Down
2 changes: 1 addition & 1 deletion tests/other-routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Response, route, Route, router } from 'typera-express'

/**
* @routeParam myRouteParameter 12345
* @routeParam myRouteParameter Description goes here
*/
const routeWithRouteParameterTag: Route<
Response.Ok<string> | Response.BadRequest<string, undefined>
Expand Down

0 comments on commit b8e0131

Please sign in to comment.