[d24f17c] | 1 | // These functions will update the request.
|
---|
| 2 | // They'll be given {req, value, paramter, spec, operation}.
|
---|
| 3 |
|
---|
| 4 | export default {
|
---|
| 5 | body: bodyBuilder,
|
---|
| 6 | header: headerBuilder,
|
---|
| 7 | query: queryBuilder,
|
---|
| 8 | path: pathBuilder,
|
---|
| 9 | formData: formDataBuilder
|
---|
| 10 | };
|
---|
| 11 |
|
---|
| 12 | // Add the body to the request
|
---|
| 13 | function bodyBuilder({
|
---|
| 14 | req,
|
---|
| 15 | value
|
---|
| 16 | }) {
|
---|
| 17 | req.body = value;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | // Add a form data object.
|
---|
| 21 | function formDataBuilder({
|
---|
| 22 | req,
|
---|
| 23 | value,
|
---|
| 24 | parameter
|
---|
| 25 | }) {
|
---|
| 26 | if (value || parameter.allowEmptyValue) {
|
---|
| 27 | req.form = req.form || {};
|
---|
| 28 | req.form[parameter.name] = {
|
---|
| 29 | value,
|
---|
| 30 | allowEmptyValue: parameter.allowEmptyValue,
|
---|
| 31 | collectionFormat: parameter.collectionFormat
|
---|
| 32 | };
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | // Add a header to the request
|
---|
| 37 | function headerBuilder({
|
---|
| 38 | req,
|
---|
| 39 | parameter,
|
---|
| 40 | value
|
---|
| 41 | }) {
|
---|
| 42 | req.headers = req.headers || {};
|
---|
| 43 | if (typeof value !== 'undefined') {
|
---|
| 44 | req.headers[parameter.name] = value;
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | // Replace path paramters, with values ( ie: the URL )
|
---|
| 49 | function pathBuilder({
|
---|
| 50 | req,
|
---|
| 51 | value,
|
---|
| 52 | parameter
|
---|
| 53 | }) {
|
---|
| 54 | req.url = req.url.split(`{${parameter.name}}`).join(encodeURIComponent(value));
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | // Add a query to the `query` object, which will later be stringified into the URL's search
|
---|
| 58 | function queryBuilder({
|
---|
| 59 | req,
|
---|
| 60 | value,
|
---|
| 61 | parameter
|
---|
| 62 | }) {
|
---|
| 63 | req.query = req.query || {};
|
---|
| 64 | if (value === false && parameter.type === 'boolean') {
|
---|
| 65 | value = 'false';
|
---|
| 66 | }
|
---|
| 67 | if (value === 0 && ['number', 'integer'].indexOf(parameter.type) > -1) {
|
---|
| 68 | value = '0';
|
---|
| 69 | }
|
---|
| 70 | if (value) {
|
---|
| 71 | req.query[parameter.name] = {
|
---|
| 72 | collectionFormat: parameter.collectionFormat,
|
---|
| 73 | value
|
---|
| 74 | };
|
---|
| 75 | } else if (parameter.allowEmptyValue && value !== undefined) {
|
---|
| 76 | const paramName = parameter.name;
|
---|
| 77 | req.query[paramName] = req.query[paramName] || {};
|
---|
| 78 | req.query[paramName].allowEmptyValue = true;
|
---|
| 79 | }
|
---|
| 80 | } |
---|