UNPKG

@cretadoc/server

Version:
122 lines (117 loc) 2.87 kB
import { Maybe, ReadonlyDeep, PartialDeep } from '@cretadoc/utils'; import { APIInstance } from '@cretadoc/api'; import { Request, Response } from 'express'; import { ViteDevServer } from 'vite'; declare const ENVIRONMENT: { readonly DEVELOPMENT: "development"; readonly PRODUCTION: "production"; readonly TEST: "test"; }; type ServerMode = (typeof ENVIRONMENT)[keyof typeof ENVIRONMENT]; type HMRConfig = false | { /** * The port used by Vite HMR (dev mode). */ port: Maybe<number>; }; type SSRConfig = { /** * A path to the server entrypoint. */ entrypoint: string; /** * The route used for serve-side rendering. * @default '/' */ route: string; }; type StaticDirConfig = { /** * The static directory entrypoint. * @default 'index.html' */ entrypoint: string; /** * The static directory path. */ path: string; /** * The route used to serve the static directory. * @default '/static' */ route: string; }; type ServerConfig = { /** * An API instance. * @default undefined */ api: Maybe<APIInstance>; /** * The HMR configuration when using dev mode. */ hmr: Maybe<HMRConfig>; /** * The server hostname. * @default "localhost" */ hostname: string; /** * The server mode. * @default "development" */ mode: ServerMode; /** * The server port. * @default 3000 */ port: number; /** * The configuration to activate server-side rendering. * @default undefined */ ssr: Maybe<SSRConfig>; /** * A configuration object to serve static files. * @default undefined */ staticDir: Maybe<StaticDirConfig>; }; type CretadocServer = { /** * The server configuration. */ config: ReadonlyDeep<ServerConfig>; /** * A method to start the server. */ start: () => void; /** * A method to stop the server. */ stop: () => void; }; type RenderContext = { /** * The HTTP request. */ req: Request; /** * The HTTP response sent by Express. */ res: Response; /** * The vite server, only used in development mode. */ viteServer: Maybe<ViteDevServer>; }; type RenderFn = (ctx: RenderContext) => void | Promise<void>; type CreateServer = (config?: PartialDeep<ServerConfig>) => Promise<CretadocServer>; /** * Create a new server. * * @param {PartialDeep<ServerConfig>} [config] - The server configuration. * @returns {Promise<CretadocServer>} The methods to start/stop the server. */ declare const createServer: CreateServer; export { type CretadocServer, type HMRConfig, type RenderContext, type RenderFn, type SSRConfig, type ServerConfig, type ServerMode, type StaticDirConfig, createServer };