import fetchJsonP from 'fetch-jsonp';
import queryString from 'query-string';
import url from 'url';

export default function shortenURL(urlToShorten) {
  let longURL = urlToShorten;
  if (!longURL.startsWith('https://perf-html.io/')) {
    const parsedURL = url.parse(longURL);
    const parsedURLOnCanonicalHost = Object.assign({}, parsedURL, {
      protocol: 'https:',
      host: 'perf-html.io',
    });
    longURL = url.format(parsedURLOnCanonicalHost);
  }

  const bitlyQueryURL = 'https://api-ssl.bitly.com/v3/shorten?' +
    queryString.stringify({
      'longUrl': longURL,
      'domain': 'perfht.ml',
      'access_token': 'b177b00a130faf3ecda6960e8b59fde73e902422',
    });
  return fetchJsonP(bitlyQueryURL).then(response => response.json()).then(json => json.data.url);
}

import fetchJsonP from 'fetch-jsonp';
import queryString from 'query-string';
import url from 'url';

export default function shortenURL(urlToShorten) {
  let longURL = urlToShorten;
  if (!longURL.startsWith('https://perf-html.io/')) {
    const parsedURL = url.parse(longURL);
    const parsedURLOnCanonicalHost = Object.assign({}, parsedURL, {
      protocol: 'https:',
      host: 'perf-html.io',
    });
    longURL = url.format(parsedURLOnCanonicalHost);
  }

  const bitlyQueryURL = 'https://api-ssl.bitly.com/v3/shorten?' +
    queryString.stringify({
      'longUrl': longURL,
      'domain': 'perfht.ml',
      'format': 'json',
      'access_token': 'b177b00a130faf3ecda6960e8b59fde73e902422',
    });
  return fetchJsonP(bitlyQueryURL).then(response => response.json()).then(json => json.data.url);
}

Add missing 'format' key in the bit.ly API request.
Add missing 'format' key in the bit.ly API request.

Bit.ly have recently made a change on their side to ignore the jsonp callback argument
if format=json was not specified, and that made our requests fail.
