The type representing the OpenAPI document.
ReadonlyappThe Fastify instance used for registering routes.
ReadonlydocumentThe OpenAPI document used for schema references and validation.
ReadonlyoptionsOptional configuration for the router.
ReadonlyroutesStores the registered routes and their method handlers.
The files to include.
The Fastify instance.
The handler function.
const spec = $.spec(<const>{
summary: "Create a new user",
requestBody: {
required: true,
content: {
"application/json": {
schema: $.ref('#/components/schemas/User')
}
}
},
responses: {
200: {
description: "User created",
content: {
"application/json": {
schema: $.ref('#/components/schemas/User')
}
}
}
}
})
$.handler<typeof spec>(async (request) => {
return {id: 1, username: "alice", email: "alice@example.com", role: "user"};
})
The operator object.
$.op(
<const>{
summary: "Create a new user",
requestBody: {
required: true,
content: {
"application/json": {
schema: $.ref('#/components/schemas/User')
}
}
},
responses: {
201: $.ref('#/components/responses/UserCreated')
}
},
async (request, reply) => {
const { username, email, password, role } = request.body as any;
const user = dbHelpers.addUser({ username, email, password, role });
const { password: _, ...rest } = user;
reply.code(201);
return rest;
}
)
The reference string.
The options for the reference.
The referenced schema object.
The path of the route.
The HTTP methods of the route.
The route object.
$.route("/users", {
post: $.op(
<const>{
summary: "Create a new user",
requestBody: {
required: true,
content: {
"application/json": {
schema: $.ref('#/components/schemas/User')
}
}
},
responses: {
201: $.ref('#/components/responses/UserCreated')
}
},
async (request, reply) => {
const { username, email, password, role } = request.body as any;
const user = dbHelpers.addUser({ username, email, password, role });
const { password: _, ...rest } = user;
reply.code(201);
return rest;
}
)
});
The OpenAPI specification.
The new OpenAPI specification.
OpenApiRouter is a class that integrates OpenAPI specifications with Fastify routing.
Example