{"size": 2026, "ext": "js", "max_stars_repo_path": "transforms/paths/requestBody.js", "max_stars_repo_name": "paulish/express-jsdoc-swagger", "max_stars_repo_stars_event_min_datetime": "2020-06-01T13:39:29.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-30T15:29:31.000Z", "max_issues_repo_path": "transforms/paths/requestBody.js", "max_issues_repo_name": "paulish/express-jsdoc-swagger", "max_issues_repo_issues_event_min_datetime": "2020-06-01T22:33:10.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T08:01:32.000Z", "max_forks_repo_path": "transforms/paths/requestBody.js", "max_forks_repo_name": "paulish/express-jsdoc-swagger", "max_forks_repo_forks_event_min_datetime": "2020-06-12T09:17:10.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-20T11:29:20.000Z"}
{"max_stars_count": 164, "max_issues_count": 101, "max_forks_count": 22, "avg_line_length": 30.696969697, "max_line_length": 93, "alphanum_fraction": 0.672260612}
const setProperty = require('../utils/setProperty')('parameter');
const getContent = require('./content')('@paramBody');
const mapDescription = require('../utils/mapDescription');
const formParams = require('./formParams');
const formatExampleValues = require('../utils/formatExamples')('requestBody');

const REQUIRED = 'required';
const FORM_TYPE = 'form';

const formatExamples = (exampleValues = []) => exampleValues
  .reduce((exampleMap, example, i) => ({
    ...exampleMap,
    [`example${i + 1}`]: {
      summary: example.summary,
      value: example.value,
    },
  }), {});

const checkExamples = examples => {
  const isExample = Array.isArray(examples) && examples.length > 0;
  return isExample ? formatExamples(examples) : undefined;
};

const parseBodyParameter = (currentState, body, examples) => {
  const [name, ...extraOptions] = body.name.split('.');
  const isRequired = extraOptions.includes(REQUIRED);
  const hasForm = extraOptions.includes(FORM_TYPE);
  const [description, contentType] = mapDescription(body.description);
  const options = { name, required: isRequired, description };

  const examplesParsed = examples.map(example => (
    formatExampleValues(example, contentType)
  ));

  if (hasForm) {
    return formParams(currentState, name, body, isRequired, checkExamples(examplesParsed));
  }

  return {
    ...currentState,
    description: setProperty(options, 'description', {
      type: 'string',
    }),
    required: setProperty(options, 'required', {
      type: 'boolean',
      defaultValue: false,
    }),
    content: {
      ...currentState.content,
      ...getContent(body.type, contentType, body.description, checkExamples(examplesParsed)),
    },
  };
};

const INITIAL_STATE = { content: {} };

const requestBodyGenerator = (params = [], examples) => {
  if (!params || !Array.isArray(params)) return {};

  return params.reduce((acc, body) => (
    { ...acc, ...parseBodyParameter(acc, body, examples) }
  ), INITIAL_STATE);
};

module.exports = requestBodyGenerator;
