diff --git a/package.json b/package.json index 5420a7e..2c6f158 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,10 @@ "description": "A minimal, modern, generic, hot-reloading local web server to help web developers.", "main": "server.js", "type": "module", + "exports": { + "types": "./server.d.ts", + "default": "./server.js" + }, "scripts": { "test": "npx ava --verbose", "sample": "node cmd.cjs --input=test/stubs" diff --git a/server.d.ts b/server.d.ts new file mode 100644 index 0000000..0862111 --- /dev/null +++ b/server.d.ts @@ -0,0 +1,187 @@ +import type { IncomingMessage, ServerResponse } from "node:http"; +import type { Server as HttpServer } from "node:http"; +import type { Http2SecureServer } from "node:http2"; +import type { FSWatcher } from "chokidar"; +import type WebSocket from "ws"; + +export interface MessageOnStartArgs { + hosts: string[]; + localhostUrl: string; + startupTime: number; + version: string; + options: EleventyDevServerOptions; +} + +export interface MessageOnCloseArgs { + version: string; + options: EleventyDevServerOptions; +} + +export interface OnRequestArgs { + url: URL; + pattern: URLPattern; + patternGroups: Record; +} + +export interface OnRequestResult { + status?: number; + headers?: Headers | Record; + body?: string | Buffer; +} + +export type OnRequestHandler = ( + args: OnRequestArgs, +) => + | string + | Response + | OnRequestResult + | Promise + | void; + +export interface Logger { + info(...args: any[]): void; + log(...args: any[]): void; + error(...args: any[]): void; +} + +export interface HttpsOptions { + key?: string; + cert?: string; +} + +export interface EleventyDevServerOptions { + port?: number; + + reloadPort?: number | false; + + liveReload?: boolean; + + showAllHosts?: boolean; + + injectedScriptsFolder?: string; + + portReassignmentRetryCount?: number; + + https?: HttpsOptions; + + domDiff?: boolean; + + showVersion?: boolean; + + encoding?: BufferEncoding; + + pathPrefix?: string; + + watch?: string[]; + + chokidarOptions?: Record; + + chokidar?: FSWatcher; + + aliases?: Record; + + indexFileName?: string; + + useCache?: boolean; + + headers?: Record; + + middleware?: Array< + (req: IncomingMessage, res: ServerResponse, next?: () => void) => any + >; + + onRequest?: Record; + + messageOnStart?: (args: MessageOnStartArgs) => string | false | void; + + messageOnClose?: (args: MessageOnCloseArgs) => string | false | void; + + logger?: Logger; + + /** + * deprecated aliases + */ + folder?: string; + + domdiff?: boolean; + + enabled?: boolean; +} + +export interface ReloadTemplate { + url: string; + inputPath: string; + content: string; +} + +export interface ReloadEvent { + subtype?: string; + + files?: string[]; + + build?: { + templates?: ReloadTemplate[]; + }; +} + +export default class EleventyDevServer { + constructor(name: string, dir: string, options?: EleventyDevServerOptions); + + static getServer( + name: string, + dir: string, + options?: EleventyDevServerOptions, + ): EleventyDevServer; + + options: EleventyDevServerOptions; + + dir: string; + + fileCache: Record; + + updateServer?: WebSocket.Server; + + logger: Logger; + + watcher?: FSWatcher; + + server: HttpServer | Http2SecureServer; + + normalizeOptions(options?: EleventyDevServerOptions): void; + + cleanupPathPrefix(pathPrefix?: string): string; + + setAliases(aliases: Record): void; + + getWatcher(): FSWatcher | undefined; + + watchFiles(targets: string[]): void; + + serve(port: number): void; + + ready(): Promise; + + close(): Promise; + + reload(event?: ReloadEvent): void; + + reloadFiles(files: string[], useDomDiffingForHtml?: boolean): void; + + sendError(args: { error: Error }): void; + + sendUpdateNotification(obj: any): void; + + getHosts(): string[]; + + getServerUrl(host: string, pathname?: string): string; + + getServerUrlRaw(host: string, pathname?: string, isRaw?: boolean): string; + + getServerPath(pathname: string): string; + + getPort(): Promise; + + getUrlsFromFilePath(path: string): string[]; + + getBuildTemplatesFromFilePath(path: string): ReloadTemplate[]; +}