Skip to content

Commit

Permalink
Merge pull request #19 from Automattic/remove/endpoints
Browse files Browse the repository at this point in the history
Remove/endpoints
  • Loading branch information
TooTallNate authored and jsnajdr committed Jan 27, 2020
1 parent 15ab853 commit 71ceadf
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 256 deletions.
54 changes: 28 additions & 26 deletions packages/wpcom.js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

var Me = require('./lib/me');
var Site = require('./lib/site');
var ends = require('./lib/endpoint');
var debug = require('debug')('wpcom');

/**
Expand Down Expand Up @@ -58,41 +57,44 @@ WPCOM.prototype.freshlyPressed = function(params, fn){
/**
* Request to WordPress REST API
*
* @param {String} type endpoint type
* @param {Object} vars to build endpoint
* @param {Object} params
* @param {String||Object} params
* @param {Object} [query]
* @param {Object} [body]
* @param {Function} fn
* @api private
*/

WPCOM.prototype.sendRequest = function (type, vars, params, fn){
debug('sendRequest("%s")', type);

// params.query || callback function
if ('function' == typeof params.query) {
fn = params.query;
params.query = {};
WPCOM.prototype.sendRequest = function (params, query, body, fn){
// `params` can be just the path (String)
if ('string' == typeof params) {
params = { path: params };
}

if (!fn) fn = function(err){ if (err) throw err; };
debug('sendRequest("%s")', params.path);

// endpoint config object
var end = ends(type);
// set `method` request param
params.method = (params.method || 'get').toUpperCase();

// request method
params.method = (params.method || end.method || 'GET').toUpperCase();

// build endpoint url
var endpoint = end.path;
if (vars) {
for (var k in vars) {
var rg = new RegExp("%" + k + "%");
endpoint = endpoint.replace(rg, vars[k]);
}
// `query` is optional
if ('function' == typeof query) {
fn = query;
query = null;
}

// `body` is optional
if ('function' == typeof body) {
fn = body;
body = null;
}
params.path = endpoint;
debug('endpoint: `%s`', endpoint);

// pass `query` and/or `body` to request params
if (query) params.query = query;
if (body) params.body = body;

// callback `fn` function is optional
if (!fn) fn = function(err){ if (err) throw err; };

// request method
this.request(params, fn);
};

Expand Down
6 changes: 0 additions & 6 deletions packages/wpcom.js/lib/endpoint/freshly-pressed.json

This file was deleted.

60 changes: 0 additions & 60 deletions packages/wpcom.js/lib/endpoint/index.js

This file was deleted.

26 changes: 0 additions & 26 deletions packages/wpcom.js/lib/endpoint/me.json

This file was deleted.

22 changes: 0 additions & 22 deletions packages/wpcom.js/lib/endpoint/media.json

This file was deleted.

31 changes: 0 additions & 31 deletions packages/wpcom.js/lib/endpoint/post.json

This file was deleted.

20 changes: 0 additions & 20 deletions packages/wpcom.js/lib/endpoint/site.json

This file was deleted.

10 changes: 5 additions & 5 deletions packages/wpcom.js/lib/me.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function Me(wpcom){
*/

Me.prototype.get = function(query, fn){
this.wpcom.sendRequest('me.get', null, { query: query }, fn);
this.wpcom.sendRequest('/me', query, null, fn);
};

/**
Expand All @@ -38,7 +38,7 @@ Me.prototype.get = function(query, fn){
*/

Me.prototype.sites = function(query, fn){
this.wpcom.sendRequest('me.sites', null, { query: query }, fn);
this.wpcom.sendRequest('/me/sites', query, null, fn);
};

/**
Expand All @@ -50,7 +50,7 @@ Me.prototype.sites = function(query, fn){
*/

Me.prototype.likes = function(query, fn){
this.wpcom.sendRequest('me.likes', null, { query: query }, fn);
this.wpcom.sendRequest('/me/likes', query, null, fn);
};

/**
Expand All @@ -62,7 +62,7 @@ Me.prototype.likes = function(query, fn){
*/

Me.prototype.groups = function(query, fn){
this.wpcom.sendRequest('me.groups', null, { query: query }, fn);
this.wpcom.sendRequest('/me/groups', query, null, fn);
};

/**
Expand All @@ -74,7 +74,7 @@ Me.prototype.groups = function(query, fn){
*/

Me.prototype.connections = function(query, fn){
this.wpcom.sendRequest('me.connections', null, { query: query }, fn);
this.wpcom.sendRequest('/me/connections', query, null, fn);
};

/**
Expand Down
20 changes: 10 additions & 10 deletions packages/wpcom.js/lib/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ function Media(id, sid, wpcom){
/**
* Get media
*
* @param {Object} [params]
* @param {Object} [query]
* @param {Function} fn
* @api public
*/

Media.prototype.get = function(params, fn){
var set = { site: this._sid, media_id: this._id };
this.wpcom.sendRequest('media.get', set, params, fn);
Media.prototype.get = function(query, fn){
var path = '/sites/' + this._sid + '/media/' + this._id;
this.wpcom.sendRequest(path, query, null, fn);
};

/**
Expand All @@ -48,8 +48,8 @@ Media.prototype.get = function(params, fn){
*/

Media.prototype.add = function(body, fn){
var set = { site: this._sid };
this.wpcom.sendRequest('media.add', set, { body: body }, fn);
var path = '/sites/' + this._sid + '/media/new';
this.wpcom.sendRequest({ path: path, method: 'post' }, null, body, fn);
};

/**
Expand All @@ -61,8 +61,8 @@ Media.prototype.add = function(body, fn){
*/

Media.prototype.update = function(body, fn){
var set = { site: this._sid, media_id: this._id };
this.wpcom.sendRequest('media.update', set, { body: body }, fn);
var path = '/sites/' + this._sid + '/media/' + this._id;
this.wpcom.sendRequest({ path: path, method: 'post' }, null, body, fn);
};

/**
Expand All @@ -73,8 +73,8 @@ Media.prototype.update = function(body, fn){
*/

Media.prototype.delete = function(fn){
var set = { site: this._sid, media_id: this._id };
this.wpcom.sendRequest('media.delete', set, fn);
var path = '/sites/' + this._sid + '/media/' + this._id + '/delete';
this.wpcom.sendRequest({ path: path, method: 'post' }, null, body, fn);
};

/**
Expand Down
Loading

0 comments on commit 71ceadf

Please sign in to comment.