const objectAssign = require("object-assign");
const mach = require("../index");
const MaxLengthExceededError = require("../utils/MaxLengthExceededError");
const {is} = require("ramda");
mach.extend(
require("../extensions/server")
);
/**
* Automatically parses all request parameters and stores them in conn.params.
* This is the union of all GET (query string) and POST (content) parameters,
* such that all POST parameters with the same name take precedence.
*
* Valid options are:
*
* - maxLength The maximum length (in bytes) of the request content
*
* If the maximum allowed length is exceeded, this middleware returns a
* 413 Request Entity Too Large response.
*
* Note: This middleware parses all request parameters for all downstream apps. If
* you'd prefer to only do this work on some requests and not all, you can use
* conn.getParams inside your app instead.
*/
function parseParams(app, options) {
options = options || {};
Iif (is(Number, options)) {
options = {maxLength: options};
}
const maxLength = options.maxLength;
return function (conn) {
return conn.getParams(maxLength).then(function (params) {
Iif (conn.params) {
// Route params take precedence over content params.
conn.params = objectAssign(params, conn.params);
} else {
conn.params = params;
}
return conn.call(app);
}, function (error) {
Eif (is(MaxLengthExceededError, error)) {
return conn.text(413, "Request Entity Too Large");
}
throw error;
});
};
}
module.exports = parseParams;
|