UNPKG

@adonisjs/bodyparser

Version:

BodyParser middleware for AdonisJS http server to read and parse request body

89 lines (88 loc) • 2.49 kB
import { t as MultipartFile } from "../file-B17PwJUz.js"; import lodash from "@poppinss/utils/lodash"; import { t as defineConfig } from "../define_config-BCQPNQYi.js"; import { t as BodyParserMiddleware } from "../bodyparser_middleware-BoTS6Xjm.js"; // factories/file_factory.ts /** * File factory exposes the API to create fake multipart file instances * for testing purposes */ var MultipartFileFactory = class { /** * Internal parameters for creating the file */ #parameters = {}; /** * Merge additional factory parameters with existing ones * * @param params - Parameters to merge with existing factory parameters */ merge(params) { this.#parameters = Object.assign(this.#parameters, params); return this; } /** * Create an instance of multipart file with the configured parameters * * @param validationOptions - Optional validation options for the file */ create(validationOptions) { const file = new MultipartFile({ fieldName: this.#parameters.fieldName || "file", clientName: this.#parameters.clientName || this.#parameters.extname ? `file.${this.#parameters.extname}` : "file", headers: this.#parameters.headers || {} }, validationOptions || {}); file.size = this.#parameters.size || 0; file.extname = this.#parameters.extname; file.type = this.#parameters.type; file.subtype = this.#parameters.subtype; file.state = "consumed"; file.validate(); return file; } }; // factories/middleware_factory.ts /** * Factory to create bodyparser middleware instances with custom configuration */ var BodyParserMiddlewareFactory = class { /** * The bodyparser configuration */ #config = defineConfig({}); /** * Feature flags to pass to the middleware */ #featureFlags; /** * Get the current configuration */ #getConfig() { return this.#config; } /** * Merge additional configuration options with the existing configuration * * @param config - Configuration options to merge */ merge(config) { this.#config = lodash.merge(this.#config, config); return this; } /** * Specify the feature flags to share with the bodyparser middleware * * @param featureFlags - Feature flags to configure */ withFeatureFlags(featureFlags) { this.#featureFlags = featureFlags; return this; } /** * Create a new BodyParserMiddleware instance with the configured options */ create() { return new BodyParserMiddleware(this.#getConfig(), this.#featureFlags); } }; export { BodyParserMiddlewareFactory, MultipartFileFactory };