{"size": 764, "ext": "js", "max_stars_repo_path": "middleware/test-inject-request.js", "max_stars_repo_name": "chrisforrette/spife", "max_stars_repo_stars_event_min_datetime": "2019-02-28T14:28:49.000Z", "max_stars_repo_stars_event_max_datetime": "2019-02-28T14:28:49.000Z", "max_issues_repo_path": "middleware/test-inject-request.js", "max_issues_repo_name": "DalavanCloud/spife", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "middleware/test-inject-request.js", "max_forks_repo_name": "DalavanCloud/spife", "max_forks_repo_forks_event_min_datetime": "2020-04-22T21:19:18.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-22T21:19:18.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 22.4705882353, "max_line_length": 69, "alphanum_fraction": 0.554973822}
'use strict'

module.exports = createRequestInjector

const {inject} = require('shot')

function createRequestInjector () {
  return {
    async processSuite (suite, next) {
      suite.request = async _opts => {
        const opts = {..._opts}
        const {body, headers = {}} = opts

        if (body) {
          if (!('content-type' in headers)) {
            headers['content-type'] = 'application/json'
          }
          opts.payload = JSON.stringify(body)
          delete opts.body
        }

        const resp = await inject(suite.spife.onrequest, opts)

        if (/application\/json/.test(resp.headers['content-type'])) {
          resp.body = JSON.parse(resp.payload)
        }

        return resp
      }
      return next(suite)
    }
  }
}
