{"size": 1747, "ext": "js", "max_stars_repo_path": "lib/handler.js", "max_stars_repo_name": "vtfk/mdg-api", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/handler.js", "max_issues_repo_name": "vtfk/mdg-api", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/handler.js", "max_forks_repo_name": "vtfk/mdg-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.6393442623, "max_line_length": 77, "alphanum_fraction": 0.6645678306}
const { json, send } = require('micro')
const { readdirSync, readFileSync } = require('fs')
const render = require('es6-template-render')
const md = require('markdown-it')()
const path = require('path')
const getTemplateFields = require('./get-template-fields')

const getTemplates = () => {
  const dir = path.join(__dirname, './data/')
  const files = readdirSync(dir)

  return files.map(file => {
    const templateName = file.replace('.json', '')
    const template = getTemplate(templateName)
    const templateFields = getTemplateFields(template)

    return {
      name: templateName,
      fields: templateFields,
      template: JSON.parse(template)
    }
  })
}

const getTemplate = (file, response) => {
  try {
    const template = require(`./data/${file}.json`)
    return JSON.stringify(template)
  } catch (error) {
    throw Error(`Document type '${file}' not found`)
  }
}

module.exports.getDocumentTypes = async (request, response) => {
  send(response, 200, await getTemplates())
}

module.exports.getDocumentMetadata = async (request, response) => {
  try {
    const { id } = request.params
    const body = await json(request)
    const template = getTemplate(id, response)
    const data = render(template, body)

    // Verify that everything is replaced from the template
    const validation = getTemplateFields(data)
    if (validation && validation.length > 0) {
      send(response, 400, `${validation.join(', ')} needs to be filled out!`)
    }

    send(response, 200, data)
  } catch (error) {
    send(response, 400, error.message)
  }
}

module.exports.getFrontpage = async (request, response) => {
  const readme = readFileSync(`${__dirname}/../README.md`, 'utf-8')
  send(response, 200, md.render(readme))
}
