import fetch from 'fetch'; // eslint-disable-line
import { Promise } from 'rsvp'; // eslint-disable-line

const cartoUser = 'data';
const cartoDomain = 'carto.planninglabs.nyc';

const buildTemplate = (layergroupid, type) => { // eslint-disable-line
  return `https://${cartoDomain}/user/${cartoUser}/api/v1/map/${layergroupid}/{z}/{x}/{y}.${type}`;
};

const buildSqlUrl = (cleanedQuery, type = 'json') => { // eslint-disable-line
  return `https://${cartoDomain}/user/${cartoUser}/api/v2/sql?q=${cleanedQuery}&format=${type}`;
};

const carto = {
  SQL(query, type = 'json') {
    const cleanedQuery = query.replace('\n', '');
    const url = buildSqlUrl(cleanedQuery, type);

    return fetch(url)
      .then((response) => {
        if (response.ok) {
          return response.json();
        }
        throw new Error('Not found');
      })
      .then((d) => { // eslint-disable-line
        return type === 'json' ? d.rows : d;
      });
  },

  getVectorTileTemplate(SQLArray) {
    const CartoCSS = '#layer { polygon-fill: #FFF; }';
    const layers = SQLArray.map(sql => ({
      type: 'mapnik',
      options: {
        cartocss_version: '2.1.1',
        cartocss: CartoCSS,
        sql,
      },
    }));

    const params = {
      version: '1.3.0',
      layers,
    };

    return new Promise((resolve, reject) => {
      fetch(`https://${cartoDomain}/user/${cartoUser}/api/v1/map`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(params),
      })
        .catch(err => reject(err))
        .then(response => response.json())
        .then((json) => {
          resolve(buildTemplate(json.layergroupid, 'mvt'));
        });
    });
  },
};

export { buildSqlUrl };
export default carto;

@@ -103,34 +103,8 @@
 ne%0A%0A
-const cartoUser = 'data';%0A
 cons
@@ -124,14 +124,8 @@
  = '
-carto.
 plan
@@ -137,11 +137,17 @@
 abs.
-nyc
+carto.com
 ';%0A%0A
@@ -250,34 +250,16 @@
 Domain%7D/
-user/$%7BcartoUser%7D/
 api/v1/m
@@ -373,32 +373,32 @@
 nt-disable-line%0A
+
   return %60https:
@@ -414,34 +414,16 @@
 Domain%7D/
-user/$%7BcartoUser%7D/
 api/v2/s
@@ -1303,26 +1303,8 @@
 in%7D/
-user/$%7BcartoUser%7D/
 api/

Change carto endpoint