mapbox
Version:
interface to mapbox services
439 lines (413 loc) • 13.7 kB
JavaScript
'use strict';
var invariant = require('../../vendor/invariant');
var hat = require('../../vendor/hat');
var makeService = require('../make_service');
/**
* @class MapboxDatasets
*/
var MapboxDatasets = module.exports = makeService('MapboxDatasets');
var API_DATASET_DATASETS = '/datasets/v1/{owner}{?access_token,limit,fresh}';
var API_DATASET_DATASET = '/datasets/v1/{owner}/{dataset}{?access_token}';
var API_DATASET_FEATURES = '/datasets/v1/{owner}/{dataset}/features{?access_token,limit}';
var API_DATASET_FEATURE = '/datasets/v1/{owner}/{dataset}/features/{id}{?access_token}';
/**
* To retrieve a listing of datasets for a particular account.
* This request requires an access token with the datasets:read scope.
*
* @param {Object} [opts={}] list options
* @param {number} opts.limit limit, for paging
* @param {boolean} opts.fresh whether to request fresh data
* @param {Function} callback called with (err, datasets)
* @returns {Promise} response
* @example
* var MapboxClient = require('mapbox');
* var client = new MapboxClient('ACCESSTOKEN');
* client.listDatasets(function(err, datasets) {
* console.log(datasets);
* // [
* // {
* // "owner": {account},
* // "id": {dataset id},
* // "name": {dataset name},
* // "description": {dataset description},
* // "created": {timestamp},
* // "modified": {timestamp}
* // },
* // {
* // "owner": {account},
* // "id": {dataset id},
* // "name": {dataset name},
* // "description": {dataset description},
* // "created": {timestamp},
* // "modified": {timestamp}
* // }
* // ]
* });
*/
MapboxDatasets.prototype.listDatasets = function(opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
return this.client({
path: API_DATASET_DATASETS,
params: {
limit: opts.limit,
fresh: opts.fresh,
owner: this.owner
},
callback: callback
});
};
/**
* To create a new dataset. Valid properties include title and description (not required).
* This request requires an access token with the datasets:write scope.
*
* @param {object} [options] an object defining a dataset's properties
* @param {string} [options.name] the dataset's name
* @param {string} [options.description] the dataset's description
* @param {Function} callback called with (err, dataset)
* @returns {Promise} response
* @example
* var MapboxClient = require('mapbox');
* var client = new MapboxClient('ACCESSTOKEN');
* client.createDataset({ name: 'foo', description: 'bar' }, function(err, dataset) {
* console.log(dataset);
* // {
* // "owner": {account},
* // "id": {dataset id},
* // "name": "foo",
* // "description": "description",
* // "created": {timestamp},
* // "modified": {timestamp}
* // }
* });
*/
MapboxDatasets.prototype.createDataset = function(options, callback) {
// permit the options argument to be omitted
if (callback === undefined && typeof options === 'function') {
callback = options;
options = {};
}
invariant(typeof options === 'object', 'options must be an object');
return this.client({
path: API_DATASET_DATASETS,
params: {
owner: this.owner
},
entity: options,
callback: callback
});
};
/**
* To retrieve information about a particular dataset.
* This request requires an access token with the datasets:read scope.
*
* @param {string} dataset the id for an existing dataset
* @param {Function} callback called with (err, dataset)
* @returns {Promise} response
* @example
* var MapboxClient = require('mapbox');
* var client = new MapboxClient('ACCESSTOKEN');
* client.readDataset('dataset-id', function(err, dataset) {
* console.log(dataset);
* // {
* // "owner": {account},
* // "id": "dataset-id",
* // "name": {dataset name},
* // "description": {dataset description},
* // "created": {timestamp},
* // "modified": {timestamp}
* // }
* });
*/
MapboxDatasets.prototype.readDataset = function(dataset, callback) {
invariant(typeof dataset === 'string', 'dataset must be a string');
return this.client({
path: API_DATASET_DATASET,
params: {
owner: this.owner,
dataset: dataset
},
callback: callback
});
};
/**
* To make updates to a particular dataset's properties.
* This request requires an access token with the datasets:write scope.
*
* @param {string} dataset the id for an existing dataset
* @param {object} [options] an object defining updates to the dataset's properties
* @param {string} [options.name] the updated dataset's name
* @param {string} [options.description] the updated dataset's description
* @param {Function} callback called with (err, dataset)
* @returns {Promise} response
* @example
* var MapboxClient = require('mapbox');
* var client = new MapboxClient('ACCESSTOKEN');
* var options = { name: 'foo' };
* client.updateDataset('dataset-id', options, function(err, dataset) {
* console.log(dataset);
* // {
* // "owner": {account},
* // "id": "dataset-id",
* // "name": "foo",
* // "description": {dataset description},
* // "created": {timestamp},
* // "modified": {timestamp}
* // }
* });
*/
MapboxDatasets.prototype.updateDataset = function(dataset, options, callback) {
invariant(typeof dataset === 'string', 'dataset must be a string');
invariant(typeof options === 'object', 'options must be an object');
invariant(!!options.name || !!options.description, 'options must include a name or a description');
return this.client({
path: API_DATASET_DATASET,
params: {
owner: this.owner,
dataset: dataset
},
method: 'patch',
entity: options,
callback: callback
});
};
/**
* To delete a particular dataset.
* This request requires an access token with the datasets:write scope.
*
* @param {string} dataset the id for an existing dataset
* @param {Function} callback called with (err)
* @returns {Promise} response
* @example
* var MapboxClient = require('mapbox');
* var client = new MapboxClient('ACCESSTOKEN');
* client.deleteDataset('dataset-id', function(err) {
* if (!err) console.log('deleted!');
* });
*/
MapboxDatasets.prototype.deleteDataset = function(dataset, callback) {
invariant(typeof dataset === 'string', 'dataset must be a string');
return this.client({
path: API_DATASET_DATASET,
params: {
owner: this.owner,
dataset: dataset
},
method: 'delete',
callback: callback
});
};
/**
* Retrive a list of the features in a particular dataset. The response body will be a GeoJSON FeatureCollection.
* This request requires an access token with the datasets:read scope.
*
* @param {string} dataset the id for an existing dataset
* @param {object} [options] an object for passing pagination arguments
* @param {number} [options.limit] The maximum number of objects to return. This value must be between 1 and 100. The API will attempt to return the requested number of objects, but receiving fewer objects does not necessarily signal the end of the collection. Receiving an empty page of results is the only way to determine when you are at the end of a collection.
* @param {Function} callback called with (err, collection)
* @returns {Promise} response
* @example
* var MapboxClient = require('mapbox');
* var client = new MapboxClient('ACCESSTOKEN');
* client.listFeatures('dataset-id', options, function(err, collection) {
* console.log(collection);
* {
* "type": "FeatureCollection",
* "features": [
* {
* "id": {feature id},
* "type": "Feature",
* "properties": {feature properties}
* "geometry": {feature geometry}
* },
* {
* "id": {feature id},
* "type": "Feature",
* "properties": {feature properties}
* "geometry": {feature geometry}
* }
* ]
* }
* });
*/
MapboxDatasets.prototype.listFeatures = function(dataset, options, callback) {
// permit the options argument to be omitted
if (callback === undefined && typeof options === 'function') {
callback = options;
options = {};
}
invariant(typeof dataset === 'string', 'dataset must be a string');
invariant(typeof options === 'object', 'options must be a object');
var params = {
owner: this.owner,
dataset: dataset
};
if (options.limit) {
invariant(typeof options.limit === 'number', 'limit option must be a number');
params.limit = options.limit;
}
return this.client({
path: API_DATASET_FEATURES,
params: params,
callback: callback
});
};
/**
* Insert a feature into a dataset. This can be a new feature, or overwrite an existing one.
* If overwriting an existing feature, make sure that the feature's `id` property correctly identifies
* the feature you wish to overwrite.
* For new features, specifying an `id` is optional. If you do not specify an `id`, one will be assigned
* and returned as part of the response.
* This request requires an access token with the datasets:write scope.
* There are a number of limits to consider when making this request:
* - a single feature cannot be larger than 500 KB
* - the dataset must not exceed 2000 total features
* - the dataset must not exceed a total of 5 MB
*
* @param {object} feature the feature to insert. Must be a valid GeoJSON feature per http://geojson.org/geojson-spec.html#feature-objects
* @param {string} dataset the id for an existing dataset
* @param {Function} callback called with (err, feature)
* @returns {Promise} response
* @example
* // Insert a brand new feature without an id
* var MapboxClient = require('mapbox');
* var client = new MapboxClient('ACCESSTOKEN');
* var feature = {
* "type": "Feature",
* "properties": {
* "name": "Null Island"
* },
* "geometry": {
* "type": "Point",
* "coordinates": [0, 0]
* }
* };
* client.insertFeature(feature, 'dataset-id', function(err, feature) {
* console.log(feature);
* // {
* // "id": {feature id},
* // "type": "Feature",
* // "properties": {
* // "name": "Null Island"
* // },
* // "geometry": {
* // "type": "Point",
* // "coordinates": [0, 0]
* // }
* // }
* });
* @example
* // Insert a brand new feature with an id, or overwrite an existing feature at that id
* var MapboxClient = require('mapbox');
* var client = new MapboxClient('ACCESSTOKEN');
* var feature = {
* "id": "feature-id",
* "type": "Feature",
* "properties": {
* "name": "Null Island"
* },
* "geometry": {
* "type": "Point",
* "coordinates": [0, 0]
* }
* };
* client.insertFeature(feature, 'dataset-id', function(err, feature) {
* console.log(feature);
* // {
* // "id": "feature-id",
* // "type": "Feature",
* // "properties": {
* // "name": "Null Island"
* // },
* // "geometry": {
* // "type": "Point",
* // "coordinates": [0, 0]
* // }
* // }
* });
*/
MapboxDatasets.prototype.insertFeature = function(feature, dataset, callback) {
invariant(typeof dataset === 'string', 'dataset must be a string');
var id = feature.id || hat();
invariant(typeof id === 'string', 'The GeoJSON feature\'s id must be a string');
return this.client({
path: API_DATASET_FEATURE,
params: {
owner: this.owner,
dataset: dataset,
id: id
},
method: 'put',
entity: feature,
callback: callback
});
};
/**
* Read an existing feature from a dataset.
* This request requires an access token with the datasets:read scope.
*
* @param {string} id the `id` of the feature to read
* @param {string} dataset the id for an existing dataset
* @param {Function} callback called with (err, feature)
* @returns {Promise} response
* @example
* var MapboxClient = require('mapbox');
* var client = new MapboxClient('ACCESSTOKEN');
* client.readFeature('feature-id', 'dataset-id', function(err, feature) {
* console.log(feature);
* // {
* // "id": "feature-id",
* // "type": "Feature",
* // "properties": {
* // "name": "Null Island"
* // },
* // "geometry": {
* // "type": "Point",
* // "coordinates": [0, 0]
* // }
* // }
* });
*/
MapboxDatasets.prototype.readFeature = function(id, dataset, callback) {
invariant(typeof id === 'string', 'id must be a string');
invariant(typeof dataset === 'string', 'dataset must be a string');
return this.client({
path: API_DATASET_FEATURE,
params: {
owner: this.owner,
dataset: dataset,
id: id
},
callback: callback
});
};
/**
* Delete an existing feature from a dataset.
* This request requires an access token with the datasets:write scope.
*
* @param {string} id the `id` of the feature to delete
* @param {string} dataset the id for an existing dataset
* @param {Function} callback called with (err)
* @returns {Promise} response
* @example
* var MapboxClient = require('mapbox');
* var client = new MapboxClient('ACCESSTOKEN');
* client.deleteFeature('feature-id', 'dataset-id', function(err, feature) {
* if (!err) console.log('deleted!');
* });
*/
MapboxDatasets.prototype.deleteFeature = function(id, dataset, callback) {
invariant(typeof id === 'string', 'id must be a string');
invariant(typeof dataset === 'string', 'dataset must be a string');
return this.client({
path: API_DATASET_FEATURE,
params: {
owner: this.owner,
dataset: dataset,
id: id
},
method: 'delete',
callback: callback
})};