/// import type { Context } from "baojs/dist/context"; /** * Options for serveStatic(). */ interface ServeStaticBaseOptions { /** * By default this module will send "index.html" files in response to a request on a directory. * To disable this, set it to `null`. To supply a new index, pass a string. * * @default "index.html" */ index?: string | null; /** * Redirect to trailing "/" when the pathname is a dir. * * @default true */ dirTrailingSlash?: boolean; /** * Collapse all slashes in the pathname (`//blog///test` => `/blog/test`). * * @default true */ collapseSlashes?: boolean; /** * Remove the first occurence of the specified string from the pathname. * Is not defined by default (no stripping). */ stripFromPathname?: string; /** * Headers to add to the response. The "Content-Type" header cannot be overwritten. If you want to * change the charset, use the `charset` option. If `collapseSlashes` or `dirTrailingSlash` is set, * a "Location" header will be set if the pathname needs to be changed. */ headers?: HeadersInit; /** * This option allows you to configure how the module handles dotfiles, i.e. files or directories that begin with a dot ("."). * Dotfiles return a 403 by default (when this is set to "deny"), but this can be changed with this option. * * @default "deny" */ dotfiles?: "allow" | "deny"; /** * The default mime type to send in the "Content-Type" HTTP header, when the file's cannot be determined. * * @default "text/plain" */ defaultMimeType?: string; /** * The "Content-Type" HTTP header charset parameter. * * @default "utf-8" */ charset?: string; } /** * Options for serveStatic() when used as a middleware. */ interface ServeStaticMiddlewareOptions extends ServeStaticBaseOptions { /** * The type of middleware to generate. * When set to `"bao"`, it will return a Bao.js compatible handler function instead. */ middlewareMode: "bao"; /** * If set to `false`, in the case of a 403 or 404 response, the unmodified context will be returned to Bao.js. * This allows you to handle the error yourself. * * If set to `true`, the error response will be sent to the client, without continuing the middleware chain. * * @default false */ handleErrors?: boolean; } /** * For use with {@link Bun.serve}'s fetch function directly. * * @example * * ```ts * import serveStatic from "serve-static-bun"; * * Bun.serve({ fetch: serveStatic("frontend") }); * ``` * @param root The path to the static files to serve * @param options */ export default function serveStatic(root: string, options?: ServeStaticBaseOptions): (req: Request) => Promise; /** * For use as a bao middleware. * * @example * * Serve files with Bao.js * * ```ts * import Bao from "baojs"; * import serveStatic from "serve-static-bun"; * * const app = new Bao(); * * // *any can be anything * // We need to strip /assets from the pathname, because when the root gets combined with the pathname, * // it results in /assets/assets/file.js. * app.get("/assets/*any", serveStatic("assets", { middlewareMode: "bao", stripFromPathname: "/assets" })); * * app.get("/", (ctx) => ctx.sendText("Hello Bao!")); * * app.listen(); * ``` * * @example * * Serve only static files with Bao.js * * **All** paths will be handled by `serve-static-bun`. * * ```ts * import Bao from "baojs"; * import serveStatic from "serve-static-bun"; * * const app = new Bao(); * * // *any can be anything * app.get("/*any", serveStatic("web", { middlewareMode: "bao" })); * * app.listen(); * ``` * @param root The path to the static files to serve * @param options */ export default function serveStatic(root: string, options: ServeStaticMiddlewareOptions): (ctx: Context) => Promise; export {};