UNPKG

@tmlmobilidade/connectors

Version:

This package provides pre-made database connectors to streamline development and reduce boilerplate. By using these connectors, you can avoid re-implementing controller classes every time, ensuring consistency and saving development time.

86 lines (85 loc) 3.87 kB
import '@fastify/cookie'; import '@fastify/cors'; import { HttpResponse, WithPagination } from '@tmlmobilidade/utils'; import { type FastifyReply as _FastifyReply, type FastifyInstance as FastifyInstanceType } from 'fastify'; import { type ContextConfigDefault, type FastifyBaseLogger, type FastifySchema, type FastifyServerOptions, type FastifyTypeProviderDefault, type RawReplyDefaultExpression, type RawRequestDefaultExpression, type RawServerBase, type RawServerDefault, type RouteGenericInterface } from 'fastify'; export { type FastifyRequest } from 'fastify'; export type FastifyReply<T> = _FastifyReply<RouteGenericInterface, RawServerBase, RawRequestDefaultExpression<RawServerBase>, RawReplyDefaultExpression<RawServerBase>, ContextConfigDefault, FastifySchema, FastifyTypeProviderDefault, HttpResponse<T> | ReadableStream | WithPagination<HttpResponse<T>>>; export type FastifyResponse<T> = _FastifyReply<RouteGenericInterface & { Reply: HttpResponse<T> | WithPagination<HttpResponse<T>>; }, RawServerBase, RawRequestDefaultExpression<RawServerBase>, RawReplyDefaultExpression<RawServerBase>, ContextConfigDefault, FastifySchema, FastifyTypeProviderDefault, HttpResponse<T> | WithPagination<HttpResponse<T>>>; export type FastifyInstance = FastifyInstanceType<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, FastifyBaseLogger, FastifyTypeProviderDefault>; /** * FastifyServiceOptions interface defines the options for the Fastify server. * It extends FastifyServerOptions and adds optional properties for origin and port. */ export interface FastifyServiceOptions extends FastifyServerOptions { /** * The host on which the Fastify server will listen. * If not provided, it defaults to '0.0.0.0'. * @default '0.0.0.0' */ host?: string; /** * The origin for CORS requests. * Defaults to `true` if not provided. * @default true * @example 'https://example.com' */ origin?: RegExp | string | true; /** * The port on which the Fastify server will listen. * If not provided, it defaults to 5050. * @default 5050 */ port?: number; } /** * FastifyService is a singleton class that provides a Fastify server instance. * It allows for setting up routes, plugins, and starting/stopping the server. * This class is designed to be used as a service in a Node.js application. * It uses the Fastify framework for building web applications and APIs. */ export declare class FastifyService { private static _instance; readonly server: FastifyInstance; private readonly host; private readonly origin; private readonly port; /** * Creates an instance of FastifyService. * @param options The options for the Fastify server. */ private constructor(); /** * Gets the singleton instance of FastifyService. * @param options The options for the Fastify server. * @return The singleton instance of FastifyService. */ static getInstance(options?: FastifyServiceOptions): FastifyService; /** * Starts the Fastify server. * @return A promise that resolves to the URL of the Fastify server. * @throws Will throw an error if the server fails to start. */ start(): Promise<string>; /** * Stops the Fastify server. * @return A promise that resolves when the server is stopped. */ stop(): Promise<void>; /** * Sets the URL of the Fastify server. * @return The URL of the Fastify server. */ private _setupDefaultRoutes; /** * Sets up hooks for the Fastify server including error handling and response processing. */ private _setupHooks; /** * Sets up the plugins for the Fastify server. * @return A promise that resolves when the plugins are set up. */ private _setupPlugins; }