UNPKG

@adonisjs/bodyparser

Version:

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

91 lines (90 loc) 2.53 kB
import { t as MultipartFile } from "../file-CX_Xk_Sw.js"; import { t as defineConfig } from "../define_config-Jdzxr5yj.js"; import { t as BodyParserMiddleware } from "../bodyparser_middleware-CwdbAAgG.js"; import lodash from "@poppinss/utils/lodash"; //#region 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; } }; //#endregion //#region 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); } }; //#endregion export { BodyParserMiddlewareFactory, MultipartFileFactory };