Skip to content

Commit

Permalink
Styles API Support. Fixes #67
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Mar 24, 2016
1 parent 3f0efec commit f3d0a49
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@
// place, so that we could concievably update this for API layout
// revisions.
module.exports.DEFAULT_ENDPOINT = 'https://api.mapbox.com';

module.exports.API_GEOCODING_FORWARD = '/geocoding/v5/{dataset}/{query}.json{?proximity,country,types}';
module.exports.API_GEOCODING_REVERSE = '/geocoding/v5/{dataset}/{longitude},{latitude}.json{?types}';

module.exports.API_DIRECTIONS = '/v4/directions/{profile}/{encodedWaypoints}.json{?alternatives,instructions,geometry,steps}';
module.exports.API_DISTANCE = '/distances/v1/mapbox/{profile}';

module.exports.API_SURFACE = '/v4/surface/{mapid}.json{?layer,fields,points,geojson,interpolate,encoded_polyline}';

module.exports.API_UPLOADS = '/uploads/v1/{owner}';
module.exports.API_UPLOAD = '/uploads/v1/{owner}/{upload}';
module.exports.API_UPLOAD_CREDENTIALS = '/uploads/v1/{owner}/credentials';

module.exports.API_MATCHING = '/matching/v4/{profile}.json';

module.exports.API_DATASET_DATASETS = '/datasets/v1/{owner}';
module.exports.API_DATASET_DATASET = '/datasets/v1/{owner}/{dataset}';
module.exports.API_DATASET_FEATURES = '/datasets/v1/{owner}/{dataset}/features{?reverse,limit,start}';
module.exports.API_DATASET_FEATURE = '/datasets/v1/{owner}/{dataset}/features/{id}';

module.exports.API_TILESTATS_STATISTICS = '/tilestats/v1/{owner}/{tileset}';
module.exports.API_TILESTATS_LAYER = '/tilestats/v1/{owner}/{tileset}/{layer}';
module.exports.API_TILESTATS_ATTRIBUTE = '/tilestats/v1/{owner}/{tileset}/{layer}/{attribute}';

module.exports.API_STATIC = '/v4/{mapid}{+overlay}/{+xyz}/{width}x{height}{+retina}{.format}';

module.exports.API_STYLES_LIST = '/styles/v1/{owner}';
module.exports.API_STYLES_CREATE = '/styles/v1/{owner}';
78 changes: 78 additions & 0 deletions lib/services/styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

var invariant = require('invariant'),
hat = require('hat'),
makeService = require('../make_service'),
constants = require('../constants');

var Styles = module.exports = makeService('MapboxStyles');

/**
* To retrieve a listing of styles for a particular account.
*
* @param {Function} callback called with (err, datasets)
* @returns {undefined} nothing, calls callback
* @example
* var MapboxClient = require('mapbox');
* var client = new MapboxClient('ACCESSTOKEN');
* client.listStyles(function(err, styles) {
* console.log(datasets);
* // [{ version: 8,
* // name: 'Light',
* // center: [ -77.0469979435026, 38.898634927602814 ],
* // zoom: 12.511766533145998,
* // bearing: 0,
* // pitch: 0,
* // created: '2016-02-09T14:26:15.059Z',
* // id: 'STYLEID',
* // modified: '2016-02-09T14:28:31.253Z',
* // owner: '{username}' },
* // { version: 8,
* // name: 'Dark',
* // created: '2015-08-28T18:05:22.517Z',
* // id: 'STYILEID',
* // modified: '2015-08-28T18:05:22.517Z',
* // owner: '{username}' }]
* });
*/
Styles.prototype.listStyles = function(callback) {
return this.client({
path: constants.API_STYLES_LIST,
params: {
owner: this.owner
},
callback: callback
}).entity();
};

/**
* Create a style style, given the style as a JSON object.
*
* @param {Object} style Mapbox GL Style Spec object
* @param {Function} callback called with (err, datasets)
* @returns {undefined} nothing, calls callback
* @example
* var MapboxClient = require('mapbox');
* var client = new MapboxClient('ACCESSTOKEN');
* var style = {
* 'version': 8,
* 'name': 'My Awesome Style',
* 'metadata': {},
* 'sources': {},
* 'layers': [],
* 'glyphs': 'mapbox://fonts/{owner}/{fontstack}/{range}.pbf'
* };
* client.createStyle(style, function(err, createdStyle) {
* console.log(createdStyle);
* });
*/
Styles.prototype.createStyle = function(style, callback) {
return this.client({
path: constants.API_STYLES_CREATE,
params: {
owner: this.owner
},
entity: style,
callback: callback
}).entity();
};
77 changes: 77 additions & 0 deletions test/styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* eslint no-shadow: 0 */
'use strict';

var test = require('tap').test;
var MapboxClient = require('../lib/services/styles');
var hat = require('hat');

var newStyleFixture = {
'version': 8,
'name': 'My Awesome Style',
'metadata': {},
'sources': {},
'layers': [],
'glyphs': 'mapbox://fonts/{owner}/{fontstack}/{range}.pbf'
};

test('StyleClient', function(styleClient) {

if (process.browser) {
styleClient.pass('skipping style api in browser');
return styleClient.end();
}

styleClient.test('#listStyles', function(listStyles) {

listStyles.test('simple list', function(assert) {
var client = new MapboxClient(process.env.MapboxAccessToken);
assert.ok(client, 'created style client');
client.listStyles(function(err, styles) {
assert.ifError(err, 'success');
assert.ok(Array.isArray(styles), 'lists styles');
styles.forEach(function(style) {
assert.ok(style.id, 'Each style has an id');
});
assert.end();
});
});

listStyles.end();
});

styleClient.test('#createStyle', function(createStyle) {

createStyle.test('create a style', function(assert) {
var client = new MapboxClient(process.env.MapboxAccessToken);
assert.ok(client, 'created style client');
newStyleFixture.glyphs = newStyleFixture.glyphs
.replace('{owner}', client.owner);
client.createStyle(newStyleFixture, function(err, style) {
assert.ifError(err, 'success');
assert.ok(style.id, 'returned style has a valid id');
assert.end();
});
});

createStyle.end();
});

styleClient.test('#createStyle', function(createStyle) {

createStyle.test('create a style', function(assert) {
var client = new MapboxClient(process.env.MapboxAccessToken);
assert.ok(client, 'created style client');
newStyleFixture.glyphs = newStyleFixture.glyphs
.replace('{owner}', client.owner);
client.createStyle(newStyleFixture, function(err, style) {
assert.ifError(err, 'success');
assert.ok(style.id, 'returned style has a valid id');
assert.end();
});
});

createStyle.end();
});

styleClient.end();
});

0 comments on commit f3d0a49

Please sign in to comment.