{"size": 1055, "ext": "js", "max_stars_repo_path": "mock/utils/roadhog.js", "max_stars_repo_name": "padipata/vue-management", "max_stars_repo_stars_event_min_datetime": "2018-03-11T05:40:12.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-15T06:44:25.000Z", "max_issues_repo_path": "mock/utils/roadhog.js", "max_issues_repo_name": "hongzelin/vue-management", "max_issues_repo_issues_event_min_datetime": "2018-05-31T02:30:29.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-15T06:43:56.000Z", "max_forks_repo_path": "mock/utils/roadhog.js", "max_forks_repo_name": "hongzelin/vue-management", "max_forks_repo_forks_event_min_datetime": "2018-04-14T11:43:28.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-01T01:47:24.000Z"}
{"max_stars_count": 286, "max_issues_count": 7, "max_forks_count": 62, "avg_line_length": 21.1, "max_line_length": 82, "alphanum_fraction": 0.6009478673}
const assert = require('assert')

const config = require('../mock.js')

function parseKey(key) {
  let method = 'get'
  let path = key

  if (key.indexOf(' ') > -1) {
    const splited = key.split(' ')
    method = splited[0].toLowerCase()
    path = splited[1]
  }

  return { method, path }
}

function createMockHandler(method, path, value) {
  return function mockHandler(...args) {
    const res = args[1]
    if (typeof value === 'function') {
      value(...args)
    } else {
      res.json(value)
    }
  }
}

function init(server) {
  Object.keys(config).forEach(key => {
    const keyParsed = parseKey(key)
    assert(!!server[keyParsed.method], `method of ${key} is not valid`)
    assert(
      typeof config[key] === 'function' || typeof config[key] === 'object',
      `mock value of ${key} should be function or object, but got ${typeof config[
        key
      ]}`
    )

    server[keyParsed.method](
      keyParsed.path,
      createMockHandler(keyParsed.method, keyParsed.path, config[key])
    )
  })
}

module.exports = {
  init
}
