Massimo provides a Fastify plugin to attach the generated client to the Fastify request object.
Basic Setup:
import fastify from "fastify";
import pltClient from "massimo/fastify-plugin.js";
const server = fastify();
server.register(pltClient, {
url: "http://example.com",
type: "openapi", // or "graphql"
});
server.post("/", async (request, reply) => {
const res = await request.movies.createMovie({ title: "foo" });
return res;
});
server.listen({ port: 3000 });
TypeScript Support:
To get type safety on request.movies, extend the FastifyRequest interface.
Method A: Global extension
declare module "fastify" {
interface FastifyRequest {
movies: MoviesClient;
}
}
Method B: Local extension
interface MoviesRequest extends FastifyRequest {
movies: MoviesClient;
}
server.get("/movies", async (request: MoviesRequest, reply: FastifyReply) => {
return request.movies.getMovies();
});
import fastify from "fastify";
import pltClient from "massimo/fastify-plugin.js";
const server = fastify();
server.register(pltClient, {
url: "http://example.com",
type: "openapi",
});
server.post("/", async (request, reply) => {
const res = await request.movies.createMovie({ title: "foo" });
return res;
});
server.listen({ port: 3000 });