Skip to content

feat: response body validation and types#29

Merged
mnahkies merged 10 commits intomasterfrom
mn/response-body-validation
Apr 2, 2023
Merged

feat: response body validation and types#29
mnahkies merged 10 commits intomasterfrom
mn/response-body-validation

Conversation

@mnahkies
Copy link
Copy Markdown
Owner

@mnahkies mnahkies commented Apr 2, 2023

  • response type for implementation functions now a union of valid status code + response schema types
  • response bodies are passed through a validation schema at runtime based on status code

closes #11

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a pretty significant performance drop in terms of running typescript on the output of this branch, though I'm not sure there's much to be done about it - there's ~100k additional lines of code generated.

Before:

time yarn workspace typescript-koa validate 
yarn workspace typescript-koa validate  8.84s user 0.28s system 206% cpu 4.425 total

After:

time yarn workspace typescript-koa validate
yarn workspace typescript-koa validate  29.11s user 1.01s system 147% cpu 20.470 total

The github API spec can probably be considered unusually large/complex so perhaps it won't matter in practice.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving named schemas to their own file / duplication reduced timings to ~15s. Had a play with replacing the numerical literal types with template literal ones like 2${number}${number} but saw minimal difference - pretty confident the slow down is purely the number of (complex) values to type check

Comment thread integration-tests/typescript-koa/src/petstore-expanded.yml/generated.ts Outdated
Comment thread integration-tests/typescript-koa/src/petstore-expanded.yml/generated.ts Outdated
return {}
}

export function ifElseIfBuilder(parts: ({ condition?: string, body: string } | undefined)[]) {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused from a previous iteration of this branch

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping as something potentially useful, might get culled later

throw new Error(`unexpected status string '${status}'`)
}

const responseSchemas = Object.entries(operation.responses ?? {}).reduce((acc, [status, response]) => {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it should be extracted into it's own thing, but I think this will happen as part of supporting multiple response content types

@mnahkies mnahkies marked this pull request as ready for review April 2, 2023 18:45
@mnahkies mnahkies enabled auto-merge (squash) April 2, 2023 18:45
@mnahkies mnahkies merged commit ffc8f20 into master Apr 2, 2023
@mnahkies mnahkies deleted the mn/response-body-validation branch April 2, 2023 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

typescript-koa: response validation

1 participant