TypeBox 0.31.0 and Transform Type Provider #99
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checklist
npm run test
andnpm run benchmark
and the Code of conduct
Overview
This PR updates TypeBox to 0.31.0 and implements experimental support for a new codec system called Transform types. This feature became available on TypeBox version 0.31.0. High level documentation on the feature can be found at the following Url.
https://github.com/sinclairzx81/typebox#types-transform
Transform Types
Transform types are a new feature that can enable Fastify to automatically decode Json values on http requests, as well as encode them back to Json on http responses. This feature works by integrating the new TypeBox Transform infrastructure with the Fastify
validationCompiler
andpreSerialization
stages for requests and responses.This PR implements this feature such that Fastify users leveraging transforms would only see a decoded Json value (and associated decoded inference type) appear within a Fastify route. This feature was written specifically with JavaScript Date decoding in mind (which tends to be an issue that many users run into when transmitting data using Json), but also as a general enhancement for users leveraging Fastify Type Providers.
Transform types work by allowing users to implement small mapping codecs at the type level. The transforms themselves work like regular types, however they have the ability to infer as both encoded and decoded type via new static inference infrastructure implemented in TypeBox. The effect of these types is that they enable users to define json value transformations once (for example a transform that decodes a number into a Date), then reuse that transform by embedding it in larger schematics.
The feature is intended to work as transparently as possible, allowing users to rely on surrounding infrastructure (in this case Fastify + Type Provider) to handle encoding and decoding automatically. This allows users to operate on the intended decoded value without being too concerned about how that value is encoded over the wire.
The following is the example included in the updated README which demonstrates decoding a json number value (representing a timestamp) into a JavaScript Date object.
Compatibility
TypeBox 0.31.0 is fully compatible with 0.30.0, so it's possible to update TypeBox without this feature. However as the TypeBox Transform infrastructure is new, it's not possible to integrate Transforms as standard without a major semver on this package.
As such, I've made the
TypeBoxTransformProvider
a sub module import from/transform
(which shouldn't impact users on previous versions of TypeBox), but it would be good to test via a npmdev
tag revision just to make sure. I've also set things up such that when this package goes a major revision tick, it's possible to uncomment the export inindex.ts
(I've left some notes in/src/index.ts
as reminder documentation)Submitting for consideration and review
S