turkerdev / fastify-type-provider-zod

MIT License
413 stars 25 forks source link

feat: added refs on top of v5 #98

Closed Bram-dc closed 4 months ago

Bram-dc commented 4 months ago

This is an additional commit on top of my v5 commit to also add ref support

Bram-dc commented 4 months ago

An example on how to use refs:

const schemas = {
    Category: CategorySchema,
    Group: GroupSchema,
    User: UserSchema,
} as const

(async () => {

    try {

        const app = fastify()
        app.setValidatorCompiler(validatorCompiler)
        app.setSerializerCompiler(serializerCompiler)

        await app.register(swagger, {
            openapi: {
                info: {
                    title: 'Zaaksite API',
                    version: '3.0.0',
                },
            },
            transform: jsonSchemaTransform,
            transformObject: createJsonSchemaTransformObject(schemas),
        })

        await app.register(router)

        await app.ready()

        const docs = app.swagger()

        await app.close()

        const filePath = path.resolve('../openapi.json')

        await fs.promises.writeFile(filePath, JSON.stringify(docs, null, 4))

        console.log(`Docs exported to ${filePath}`)

        process.exit(0)

    } catch (e) {

        console.error(e)

        process.exit(1)

    }

})()

This will create an openapi spec with objects: "Category", "Group" and "User", all the schemas that use these objects will have a ref created.