@adonisjs/bodyparser
Version:
BodyParser middleware for AdonisJS http server to read and parse request body
74 lines (73 loc) • 1.5 kB
JavaScript
//#region src/define_config.ts
/**
* Defines configuration for the bodyparser middleware. The provided configuration
* is merged with sensible defaults for parsing JSON, form data, raw text, and
* multipart requests.
*
* @param config - Optional configuration overrides for body parsing
*
* @example
* ```ts
* export default defineConfig({
* allowedMethods: ['POST', 'PUT', 'PATCH'],
* json: {
* limit: '2mb'
* },
* multipart: {
* autoProcess: true,
* limit: '50mb'
* }
* })
* ```
*/
function defineConfig(config) {
return {
allowedMethods: config.allowedMethods || [
"POST",
"PUT",
"PATCH",
"DELETE"
],
form: {
encoding: "utf-8",
limit: "1mb",
queryString: {},
types: ["application/x-www-form-urlencoded"],
convertEmptyStringsToNull: true,
trimWhitespaces: true,
...config.form
},
json: {
encoding: "utf-8",
limit: "1mb",
strict: true,
types: [
"application/json",
"application/json-patch+json",
"application/vnd.api+json",
"application/csp-report"
],
convertEmptyStringsToNull: true,
trimWhitespaces: true,
...config.json
},
raw: {
encoding: "utf-8",
limit: "1mb",
types: ["text/*"],
...config.raw
},
multipart: {
autoProcess: true,
processManually: [],
maxFields: 1e3,
limit: "20mb",
types: ["multipart/form-data"],
convertEmptyStringsToNull: true,
trimWhitespaces: true,
...config.multipart
}
};
}
//#endregion
export { defineConfig as t };