Fastify Type Provider for @effect/schema
import Fastify from "fastify";
import { serializerCompiler, validatorCompiler, EffectSchemaTypeProvider } from "fastify-type-provider-effect-schema";
import { pipe } from "effect";
import * as S from "@effect/schema/Schema";
const app = Fastify()
// Add schema validator and serializer
app.setValidatorCompiler(validatorCompiler);
app.setSerializerCompiler(serializerCompiler);
app.withTypeProvider<EffectSchemaTypeProvider>().route({
method: "GET",
url: "/",
// Define your schema
schema: {
querystring: S.struct({
name: pipe(S.string, S.minLength(4)),
}),
response: {
200: S.string,
},
},
handler: (req, res) => {
res.send(req.query.name);
},
});
app.listen({ port: 4949 });
import fastify from 'fastify';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUI from '@fastify/swagger-ui';
import { pipe } from "effect";
import * as S from "@effect/schema/Schema";
import {
jsonSchemaTransform,
createJsonSchemaTransform,
serializerCompiler,
validatorCompiler,
EffectSchemaTypeProvider,
} from 'fastify-type-provider-effect-schema';
const app = fastify();
app.setValidatorCompiler(validatorCompiler);
app.setSerializerCompiler(serializerCompiler);
app.register(fastifySwagger, {
openapi: {
info: {
title: 'SampleApi',
description: 'Sample backend service',
version: '1.0.0',
},
servers: [],
},
transform: jsonSchemaTransform,
// You can also create transform with custom skiplist of endpoints that should not be included in the specification:
//
// transform: createJsonSchemaTransform({
// skipList: [ '/documentation/static/*' ]
// })
});
app.register(fastifySwaggerUI, {
routePrefix: '/documentation',
});
const LOGIN_SCHEMA = S.struct({
username: pipe(
S.string,
S.maxLength(32),
S.description('Some description for username'),
),
password: pipe(S.string, S.maxLength(32)),
});
app.after(() => {
app.withTypeProvider<EffectSchemaTypeProvider>().route({
method: 'POST',
url: '/login',
schema: { body: LOGIN_SCHEMA },
handler: (req, res) => {
res.send('ok');
},
});
});
async function run() {
await app.ready();
await app.listen({
port: 4949,
});
console.log(`Documentation running at http://localhost:4949/documentation`);
}
run();
This library was forked from fastify-type-provider-zod.