Initial commit
This commit is contained in:
4
lib/assets/javascripts/carto-node/index.js
Normal file
4
lib/assets/javascripts/carto-node/index.js
Normal file
@@ -0,0 +1,4 @@
|
||||
module.exports = exports = {
|
||||
PublicClient: require('./lib/clients/public'),
|
||||
AuthenticatedClient: require('./lib/clients/authenticated')
|
||||
};
|
||||
199
lib/assets/javascripts/carto-node/lib/clients/authenticated.js
Normal file
199
lib/assets/javascripts/carto-node/lib/clients/authenticated.js
Normal file
@@ -0,0 +1,199 @@
|
||||
const PublicClient = require('./public.js');
|
||||
|
||||
class AuthenticatedClient extends PublicClient {
|
||||
getConfig (callback) {
|
||||
var CONFIG_PATH = 'api/v3/me';
|
||||
|
||||
return this.get([CONFIG_PATH], callback);
|
||||
}
|
||||
|
||||
getVisualization (vizUrl, params, callback) {
|
||||
const VIZ_API_PATH = 'api/v1/viz';
|
||||
const uriParams = this.paramsToURI(params);
|
||||
|
||||
return this.get([VIZ_API_PATH, vizUrl, uriParams], callback);
|
||||
}
|
||||
|
||||
getDerivedVisualizations (options, callback) {
|
||||
const VIZ_API_PATH = `api/v1/viz`;
|
||||
var params = Object.assign({
|
||||
type: 'derived',
|
||||
privacy: 'public'
|
||||
}, options);
|
||||
|
||||
const uriParams = this.paramsToURI(params);
|
||||
|
||||
return this.get([VIZ_API_PATH, uriParams], callback);
|
||||
}
|
||||
|
||||
getMap (mapId, callback) {
|
||||
const MAPS_API_PATH = 'api/v1/maps';
|
||||
|
||||
return this.get([MAPS_API_PATH, mapId], callback);
|
||||
}
|
||||
|
||||
putConfig (payload, callback) {
|
||||
const CONFIG_PATH = 'api/v3/me';
|
||||
var opts = {
|
||||
data: JSON.stringify(payload),
|
||||
dataType: 'json'
|
||||
};
|
||||
return this.put([CONFIG_PATH], opts, callback);
|
||||
}
|
||||
|
||||
deleteUser (payload, callback) {
|
||||
const CONFIG_PATH = 'api/v3/me';
|
||||
var opts = {
|
||||
data: JSON.stringify(payload),
|
||||
dataType: 'json'
|
||||
};
|
||||
return this.delete([CONFIG_PATH], opts, callback);
|
||||
}
|
||||
|
||||
like (itemId, callback) {
|
||||
const CONFIG_PATH = 'api/v1/viz';
|
||||
var opts = {
|
||||
dataType: 'json'
|
||||
};
|
||||
return this.post([CONFIG_PATH, itemId, 'like'], opts, callback);
|
||||
}
|
||||
|
||||
deleteLike (itemId, callback) {
|
||||
const CONFIG_PATH = 'api/v1/viz';
|
||||
var opts = {
|
||||
dataType: 'json'
|
||||
};
|
||||
return this.delete([CONFIG_PATH, itemId, 'like'], opts, callback);
|
||||
}
|
||||
|
||||
updateNotification (userId, apiKey, notification, callback) {
|
||||
const CONFIG_PATH = [`/api/v3/users/${userId}/notifications/${notification.id}?api_key=${apiKey}`];
|
||||
const opts = {
|
||||
data: JSON.stringify({
|
||||
notification
|
||||
}),
|
||||
dataType: 'json'
|
||||
};
|
||||
|
||||
return this.put(CONFIG_PATH, opts, callback);
|
||||
}
|
||||
|
||||
getTags (options, callback) {
|
||||
const URIParts = ['api/v3/tags'];
|
||||
const URLParameters = {
|
||||
q: options.q,
|
||||
page: options.page || 1,
|
||||
per_page: options.perPage || 6,
|
||||
types: options.types || 'derived,table',
|
||||
include_shared: options.include_shared || false
|
||||
};
|
||||
|
||||
const queryOptions = {
|
||||
data: URLParameters
|
||||
};
|
||||
|
||||
return this.get(URIParts, queryOptions, callback);
|
||||
}
|
||||
|
||||
previewSearch (query, callback) {
|
||||
const URIParts = ['api/v3/search_preview'];
|
||||
const URLParameters = {
|
||||
types: 'derived,table,tag',
|
||||
limit: 4
|
||||
};
|
||||
|
||||
const queryOptions = {
|
||||
data: URLParameters
|
||||
};
|
||||
|
||||
return this.get([URIParts, encodeURIComponent(query)], queryOptions, callback);
|
||||
}
|
||||
|
||||
getApiKeys (type, callback) {
|
||||
const URIParts = ['api/v3/api_keys'];
|
||||
const URLParameters = {
|
||||
type: type
|
||||
};
|
||||
|
||||
const queryOptions = {
|
||||
data: URLParameters
|
||||
};
|
||||
|
||||
return this.get(URIParts, queryOptions, callback);
|
||||
}
|
||||
|
||||
getOAuthApps (apiKey, callback) {
|
||||
const CONFIG_PATH = [`api/v4/oauth_apps?api_key=${apiKey}`];
|
||||
|
||||
return this.get(CONFIG_PATH, callback);
|
||||
}
|
||||
|
||||
createApp (apiKey, app, callback) {
|
||||
const CONFIG_PATH = [`api/v4/oauth_apps?api_key=${apiKey}`];
|
||||
const opts = {
|
||||
data: JSON.stringify(app),
|
||||
dataType: 'json'
|
||||
};
|
||||
return this.post(CONFIG_PATH, opts, callback);
|
||||
}
|
||||
|
||||
updateApp (apiKey, app, callback) {
|
||||
const CONFIG_PATH = [`api/v4/oauth_apps/${app.id}?api_key=${apiKey}`];
|
||||
const opts = {
|
||||
data: JSON.stringify(app),
|
||||
dataType: 'json'
|
||||
};
|
||||
return this.put(CONFIG_PATH, opts, callback);
|
||||
}
|
||||
|
||||
deleteApp (apiKey, app, callback) {
|
||||
const CONFIG_PATH = [`api/v4/oauth_apps/${app.id}?api_key=${apiKey}`];
|
||||
const opts = {
|
||||
data: JSON.stringify(app),
|
||||
dataType: 'json'
|
||||
};
|
||||
return this.delete(CONFIG_PATH, opts, callback);
|
||||
}
|
||||
|
||||
regenerateClientSecret (apiKey, app, callback) {
|
||||
const CONFIG_PATH = [`api/v4/oauth_apps/${app.id}/regenerate_secret?api_key=${apiKey}`];
|
||||
const opts = {
|
||||
data: JSON.stringify(app),
|
||||
dataType: 'json'
|
||||
};
|
||||
return this.post(CONFIG_PATH, opts, callback);
|
||||
}
|
||||
|
||||
uploadLogo (apiKey, userId, filename, callback) {
|
||||
const CONFIG_PATH = [`api/v1/users/${userId}/assets?api_key=${apiKey}`];
|
||||
|
||||
const data = new FormData();
|
||||
data.append('kind', 'orgavatar');
|
||||
data.append('filename', filename);
|
||||
|
||||
const opts = {
|
||||
data,
|
||||
doNoSetDefaultContentType: true,
|
||||
processData: false
|
||||
};
|
||||
|
||||
return this.post(CONFIG_PATH, opts, callback);
|
||||
}
|
||||
|
||||
getConnectedApps (apiKey, callback) {
|
||||
const CONFIG_PATH = [`api/v4/granted_oauth_apps?api_key=${apiKey}`];
|
||||
|
||||
return this.get(CONFIG_PATH, callback);
|
||||
}
|
||||
|
||||
revokeOAuthApp (apiKey, app, callback) {
|
||||
const CONFIG_PATH = [`api/v4/oauth_apps/${app.id}/revoke?api_key=${apiKey}`];
|
||||
const opts = {
|
||||
data: JSON.stringify(app),
|
||||
dataType: 'json'
|
||||
};
|
||||
return this.post(CONFIG_PATH, opts, callback);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AuthenticatedClient;
|
||||
122
lib/assets/javascripts/carto-node/lib/clients/public.js
Normal file
122
lib/assets/javascripts/carto-node/lib/clients/public.js
Normal file
@@ -0,0 +1,122 @@
|
||||
const $ = require('jquery');
|
||||
|
||||
window.StaticConfig = window.StaticConfig || {};
|
||||
|
||||
class PublicClient {
|
||||
constructor (apiURI = '') {
|
||||
this.apiURI = apiURI;
|
||||
}
|
||||
|
||||
get (...args) {
|
||||
return this.request('get', ...args);
|
||||
}
|
||||
|
||||
put (...args) {
|
||||
return this.request('put', ...args);
|
||||
}
|
||||
|
||||
post (...args) {
|
||||
return this.request('post', ...args);
|
||||
}
|
||||
|
||||
delete (...args) {
|
||||
return this.request('delete', ...args);
|
||||
}
|
||||
|
||||
addHeaders (obj, additional) {
|
||||
return Object.assign(
|
||||
{},
|
||||
obj.headers,
|
||||
additional
|
||||
);
|
||||
}
|
||||
|
||||
paramsToURI (params) {
|
||||
const DEFAULT_PARAMS = '';
|
||||
|
||||
return this.checkParams(params)
|
||||
? `?${Object
|
||||
.keys(params)
|
||||
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
|
||||
.join('&')}`
|
||||
: DEFAULT_PARAMS;
|
||||
}
|
||||
|
||||
checkParams (params) {
|
||||
const OBJECT_TYPE = '[object Object]';
|
||||
|
||||
return params &&
|
||||
Object.prototype.toString.call(params) === OBJECT_TYPE &&
|
||||
Object.keys(params).length;
|
||||
}
|
||||
|
||||
makeRelativeURI (parts) {
|
||||
return `/${parts.join('/')}`;
|
||||
}
|
||||
|
||||
makeAbsoluteURI (relativeURI) {
|
||||
return `${this.apiURI}${relativeURI}`;
|
||||
}
|
||||
|
||||
successCallback (callback) {
|
||||
return (data, textStatus, jqXHR) => {
|
||||
try {
|
||||
data = JSON.parse(JSON.stringify(data));
|
||||
} catch (e) {
|
||||
data = null;
|
||||
}
|
||||
|
||||
callback(null, textStatus, data);
|
||||
};
|
||||
}
|
||||
|
||||
errorCallback (callback) {
|
||||
return (jqXHR, textStatus, errorThrown) => {
|
||||
const err = errorThrown || new Error('Failed to fetch');
|
||||
callback(err, textStatus, jqXHR);
|
||||
};
|
||||
}
|
||||
|
||||
getAssetsBaseUrl () {
|
||||
const { host, protocol } = window.location;
|
||||
|
||||
const regExp = window.location.href.match(/(\/(u|user)\/[a-z0-9\-]+)\//);
|
||||
const path = regExp && regExp[1] || '';
|
||||
|
||||
return window.StaticConfig.baseUrl || `${protocol}//${host}${path}`;
|
||||
}
|
||||
|
||||
request (method, uriParts, opts = {}, callback) {
|
||||
if (!callback && typeof opts === 'function') {
|
||||
callback = opts;
|
||||
opts = {};
|
||||
}
|
||||
|
||||
const contentType = opts.doNoSetDefaultContentType
|
||||
? false
|
||||
: 'application/json; charset=utf-8';
|
||||
|
||||
Object.assign(opts, {
|
||||
contentType: contentType,
|
||||
method: method.toUpperCase()
|
||||
});
|
||||
|
||||
this.addHeaders(opts);
|
||||
|
||||
const baseUrl = opts.baseUrl || this.getAssetsBaseUrl();
|
||||
const url = uriParts.length !== 0
|
||||
? this.makeAbsoluteURI(this.makeRelativeURI(uriParts))
|
||||
: '';
|
||||
|
||||
const requestOptions = Object.assign({}, opts,
|
||||
{
|
||||
success: this.successCallback(callback),
|
||||
error: this.errorCallback(callback)
|
||||
}
|
||||
);
|
||||
|
||||
$.ajax(`${baseUrl}${url}`, requestOptions);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = PublicClient;
|
||||
Reference in New Issue
Block a user