Transformations
Transformations provide a convenient way to automatically parse and serialize values within your ptsq application, offering flexibility in handling data exchanged between the client and server.
In scenarios where you prefer not to receive serialized types directly from the client, transformations allow you to customize the conversion process according to your requirements. This enables you to seamlessly translate data between different representations, such as converting JSON payloads to native JavaScript objects or vice versa, without manual intervention.
By leveraging transformations, you can ensure that data exchanged between the client and server is in the desired format, facilitating smooth communication and interoperability while maintaining flexibility and control over the data processing pipeline. This capability enhances the overall efficiency and robustness of your ptsq application, enabling seamless integration with various client-side technologies and data formats.
const urlSchema = Type.Transformation(
Type.String({
format: 'uri',
}),
)
.Decode((arg) => new URL(arg))
.Encode((arg) => arg.toISOString());
const urlResolver = resolver
.args(z.object({ url: urlSchema }))
.output(urlSchema)
.query(
({ input /* { url: { URL } } */ }) =>
new URL(`http://localhost:3000/${input.url.pathname}`),
);