{"size": 1206, "ext": "js", "max_stars_repo_path": "lib/openapi2postman/index.js", "max_stars_repo_name": "online-bridge-hackathon/gcp-cf-docsgen", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/openapi2postman/index.js", "max_issues_repo_name": "online-bridge-hackathon/gcp-cf-docsgen", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/openapi2postman/index.js", "max_forks_repo_name": "online-bridge-hackathon/gcp-cf-docsgen", "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": 31.7368421053, "max_line_length": 107, "alphanum_fraction": 0.6500829187}
'use strict'

const axios = require('axios')
const convert = require('util').promisify(require('openapi-to-postmanv2').convert)
const XAPIKEY = process.env.POSTMAN_APIKEY || ''
/**
 * Http call to update a Postman Collection
 * @param {string} projectUid
 * @param {Object} collection
 * @returns {Promise<Object>} Http return call
 */
const postmanRequest = (projectUid, collection) => axios.put(
  `https://api.getpostman.com/collections/${projectUid}?format=2.1.0`,
  collection,
  {headers: {
    'X-Api-Key': XAPIKEY,
    'Content-Type': 'application/json'
  }})

/**
 * Converts an OpenApi Spec to a Postman v2 collection and updates/syncs within globalbridge-app team space
 * @module openapi2postman
 * @param {Object} spec
 */
exports.convert = spec => convert({ type: 'string', data: JSON.stringify(spec) }, {})
  .then(({result, output, reason}) =>
    ({
      ...output[0].data,
      info:
        {
          ...output[0].data.info,
          id: spec.info['x-postman'].id,
          _postman_id: spec.info['x-postman'].id
        }
    }))
  .then(collection => postmanRequest(spec.info['x-postman'].uid, {collection}))
  .then(() => console.info('Sucessfully updated POSTMAN collection'))
