diff --git a/lib/index.js b/lib/index.js index 0ecdfdb..a036edd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -39,8 +39,8 @@ module.exports = function (user, key) { return cartodb; }; -function CartoDB(user, key) { - this.url = 'https://' + user + '.carto.com/api/v2/sql'; +function CartoDB(user, key, opts) { + this.url = createBaseUrl(user, opts); this.key = key; this.isBatch = false; this.jobs = new Set(); @@ -75,6 +75,20 @@ CartoDB.prototype.onError = function (query) { } return this; } +function createBaseUrl (user, opts) { + if (!opts || (!opts.domain && !opts.subdomainless)) { + return `https://${user}.carto.com/api/v2/sql`; + } + if (opts.domain) { + if (opts.subdomainless) { + return `https://${opts.domain}/user/${user}/api/v2/sql`; + } else { + return `https://${user}.${opts.domain}/api/v2/sql`; + } + } else if (opts.subdomainless) { + return `https://carto.com/user/${user}/api/v2/sql`; + } +} CartoDB.prototype.request = function (sql) { var out = new PassThrough(); var query = qs.stringify({