import * as _homer0_jimple from '@homer0/jimple'; import { J as Jimpex, H as MiddlewareLike, a as ControllerConnectFn } from '../../index-DAUqbQ6h.js'; import mime from 'mime'; import { RouterMethod, ExpressMiddleware } from '../../types/express.js'; import { DeepPartial } from '../../types/utils.js'; import { SendFile } from '../../services/common/sendFile.js'; import { Router } from 'express'; import '../../types/http.js'; import 'https'; import 'http'; import 'spdy'; import 'node-fetch'; import '@homer0/path-utils'; import '@homer0/simple-logger'; import '@homer0/simple-config'; import '@homer0/events-hub'; type Mime = typeof mime; /** * The definition for each file the controller handles. * * @group Controllers/Statics */ type StaticsControllerFile = { /** * The route, relative to the controller root, to the file. */ route: string; /** * The path to the file in the filesystem. Since the file is served using the * {@link SendFile} service, whether the file is relative to the project root or the * application executable depends on how the service is configured (relative to the * executable by default). */ path: string; /** * A dictionary of headers for the response. */ headers?: Record; }; /** * These are like "master paths" that get prepended to all the file paths and routes the * controller use. * * @group Controllers/Statics */ type StaticsControllerPathsOptions = { /** * A custom route to prefix all the file routes with. */ route: string; /** * A custom path to prefix all the file paths with. */ source: string; }; /** * The options to customize the controller. * * @group Controllers/Statics */ type StaticsControllerOptions = { /** * A list of filenames, or definitions for the files to handle. */ files: Array; /** * A dictionary with the allowed router (HTTP) methods the controller can use to serve * the files. If `all` is set to `true`, the rest of the values will be ignored. * * @default {get: true, all: false} */ methods: Partial>; /** * The "master paths" the controller can use to prefix the file paths and routes. * * @default {route: '', source: './',} */ paths: StaticsControllerPathsOptions; }; /** * The options to construct a {@link StaticsController}. * * @group Controllers/Statics */ type StaticsControllerConstructorOptions = DeepPartial & { /** * A dictionary with the dependencies to inject. */ inject: { sendFile: SendFile; mime: Mime; }; }; /** * A function to generate a list of middlewares that can be executed before the controller * main middleware. * * @group Controllers/Statics */ type StaticsControllerGetMiddlewaresFn = (app: Jimpex) => MiddlewareLike[]; /** * The options for the controller creator that mounts {@link StaticsController}. * * @group Controllers/Statics */ type StaticsControllerCreatorOptions = DeepPartial & { /** * A function to generate a list of middlewares that can be executed before the * controller main middleware. */ getMiddlewares?: StaticsControllerGetMiddlewaresFn; }; /** * The options for {@link StaticsController._addRoute}. * * @access protected * @group Controllers/Statics */ type AddStaticRouteOptions = { /** * The reference for the router in which the middlewares will be added. */ router: Router; /** * The router method in which the middlewares will be added. */ method: RouterMethod; /** * The definition of the file to serve. */ file: StaticsControllerFile; /** * The middleware created by {@link StaticsController}, that will serve the file. */ fileMiddleware: ExpressMiddleware; /** * A list of extra middlewares to execute before the file middleware. */ middlewares: ExpressMiddleware[]; }; /** * The controller class that allows the application to serve specific files from any * folder to any route without the need of mounting directories as "static". * * @group Controller Classes * @group Controllers/Statics * @prettierignore */ declare class StaticsController { /** * The service that serves static files. */ protected readonly _sendFile: SendFile; /** * The MIME type library. Since it's an ESM only module, Jimpex loads it on boot and makes * it available on the container. */ protected readonly _mime: Mime; /** * The controller customization options. */ protected _options: StaticsControllerOptions; /** * A dictionary with the formatted definitions of the files that will be served. * It uses the files' routes as keys, for easy access in the middleware. */ protected files: Record; /** * @param options The options to construct the controller. */ constructor({ inject, ...options }: StaticsControllerConstructorOptions); /** * Mounts the middlewares in the router in order to serve the files. * * @param router A reference to the application router. * @param middlewares A list of extra middlewares to execute before the file * middleware. */ addRoutes(router: Router, middlewares?: ExpressMiddleware[]): Router; /** * The controller options. */ get options(): Readonly; /** * Generates the middleware that will serve the file. * * @param file The definition of the file to serve. */ protected _getMiddleware(file: StaticsControllerFile): ExpressMiddleware; /** * Mounts the middleware(s) for a file in the router. * * @param options The information of the file and how it needs to be added. */ protected _addRoute({ router, method, file, fileMiddleware, middlewares, }: AddStaticRouteOptions): void; /** * Validates and formats the options sent to the constructor in order to get the final * set that will be stored in the controller. * * @param options The options to validate. * @throws If no files are specified. * @throws If methods is not defined. * @throws If no methods are enabled. * @throws If there's an invalid HTTP method. */ protected _validateOptions(options: StaticsControllerOptions): StaticsControllerOptions; /** * Parses the files received from the constructor's options, and formats them into * proper definitions the controller can use. */ protected _createFiles(): Record; } /** * A controller that allows the application to server specific files from any folder to * any route without the need of mounting directories as "static" folders. * * @group Controllers * @group Controllers/Statics */ declare const staticsController: _homer0_jimple.ResourceCreator<"controller", "connect", ({ getMiddlewares, ...options }?: StaticsControllerCreatorOptions) => (app: ContainerType) => Router, ControllerConnectFn>; export { type AddStaticRouteOptions, StaticsController, type StaticsControllerConstructorOptions, type StaticsControllerCreatorOptions, type StaticsControllerFile, type StaticsControllerGetMiddlewaresFn, type StaticsControllerOptions, type StaticsControllerPathsOptions, staticsController };