turkerdev / fastify-type-provider-zod

MIT License
411 stars 25 forks source link

Add replacer option to serializerCompiler #112

Closed Bram-dc closed 1 month ago

Bram-dc commented 1 month ago

This is my final addition to the type provider. I use this to serialize dates like this:

import { createSerializerCompiler } from 'fastify-type-provider-zod'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function replacer(this: any, key: string, value: any) {
    if (this[key] instanceof Date) {
        return {
            _type: 'date',
            value,
        }
    }
    return value
}

export const serializerCompiler = createSerializerCompiler({ replacer })
kibertoad commented 1 month ago

Thank you so much! I'll release 3.0.0 later today