78 lines
1.9 KiB
TypeScript
78 lines
1.9 KiB
TypeScript
import type {
|
|
FastifyInstance,
|
|
RouteHandlerMethod,
|
|
RawRequestDefaultExpression,
|
|
RawReplyDefaultExpression,
|
|
RouteShorthandOptions,
|
|
FastifyPluginOptions,
|
|
RawServerDefault,
|
|
RouteGenericInterface,
|
|
} from "fastify";
|
|
|
|
type HandlerMethod<T extends RouteGenericInterface> = RouteHandlerMethod<
|
|
RawServerDefault,
|
|
RawRequestDefaultExpression<RawServerDefault>,
|
|
RawReplyDefaultExpression<RawServerDefault>,
|
|
T
|
|
>;
|
|
|
|
export class Router {
|
|
private fastifyInstance: FastifyInstance;
|
|
|
|
constructor(fastifyInstance: FastifyInstance) {
|
|
this.fastifyInstance = fastifyInstance;
|
|
}
|
|
|
|
public group = (path: string, combineRoutes: (router: Router) => void) => {
|
|
this.fastifyInstance.register(
|
|
(server: FastifyInstance, opts: FastifyPluginOptions, done: () => void) => {
|
|
const router = new Router(server);
|
|
|
|
combineRoutes(router);
|
|
done();
|
|
},
|
|
{ prefix: path }
|
|
);
|
|
};
|
|
|
|
public get = <T extends RouteGenericInterface>(
|
|
path: string,
|
|
handler: HandlerMethod<T>,
|
|
options: RouteShorthandOptions = {}
|
|
) => {
|
|
this.fastifyInstance.get(path, options, handler);
|
|
};
|
|
|
|
public post = <T extends RouteGenericInterface>(
|
|
path: string,
|
|
handler: HandlerMethod<T>,
|
|
options: RouteShorthandOptions = {}
|
|
) => {
|
|
this.fastifyInstance.post(path, options, handler);
|
|
};
|
|
|
|
public put = <T extends RouteGenericInterface>(
|
|
path: string,
|
|
handler: HandlerMethod<T>,
|
|
options: RouteShorthandOptions = {}
|
|
) => {
|
|
this.fastifyInstance.put(path, options, handler);
|
|
};
|
|
|
|
public patch = <T extends RouteGenericInterface>(
|
|
path: string,
|
|
handler: HandlerMethod<T>,
|
|
options: RouteShorthandOptions = {}
|
|
) => {
|
|
this.fastifyInstance.patch(path, options, handler);
|
|
};
|
|
|
|
public delete = <T extends RouteGenericInterface>(
|
|
path: string,
|
|
handler: HandlerMethod<T>,
|
|
options: RouteShorthandOptions = {}
|
|
) => {
|
|
this.fastifyInstance.delete(path, options, handler);
|
|
};
|
|
}
|