Skip to content

Commit

Permalink
Merge pull request #100 from cyle/cyle/links-compatibility
Browse files Browse the repository at this point in the history
Add compatibility with Tumblr API links objects being passed through to these low level functions
  • Loading branch information
cyle authored Jul 28, 2020
2 parents 95f65fe + 43228fa commit 80bf8a4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 31 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,19 @@ In the unlikely event that we add a bunch of methods to the API docs and don't u
// GET methods
client.addGetMethods({
// creates client.userInfo(params, callback)
userInfo: '/user/info',
userInfo: '/v2/user/info',
// client.blogInfo(blogIdentifier, params, callback)
blogInfo: '/blog/:blogIdentifier/info',
blogInfo: '/v2/blog/:blogIdentifier/info',
// Creates client.taggedPosts(tag, params, callback)
taggedPosts: ['/tagged', ['tag']],
taggedPosts: ['/v2/tagged', ['tag']],
});

// POST methods
client.addPostMethods({
// client.deletePost(blogIdentifier, id, params, callback)
deletePost: ['/blog/:blogIdentifier/post/delete', ['id']],
deletePost: ['/v2/blog/:blogIdentifier/post/delete', ['id']],
// Creates client.likePost(tag, id, reblog_key, params, callback)
likePost: ['/user/like', ['id', 'reblog_key']],
likePost: ['/v2/user/like', ['id', 'reblog_key']],
});
```

Expand Down
73 changes: 50 additions & 23 deletions lib/tumblr.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const qs = require('query-string');
const request = require('request');
const URL = require('url').URL;

const get = require('lodash/get');
const set = require('lodash/set');
Expand All @@ -26,8 +27,8 @@ const isArray = require('lodash/isArray');
const isPlainObject = require('lodash/isPlainObject');
const omit = require('lodash/omit');

const CLIENT_VERSION = '1.0.0';
const API_BASE_URL = 'https://api.tumblr.com/v2';
const CLIENT_VERSION = '3.0.0';
const API_BASE_URL = 'https://api.tumblr.com'; // deliberately no trailing slash

const API_METHODS = {
GET: {
Expand All @@ -44,7 +45,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
blogInfo: '/blog/:blogIdentifier/info',
blogInfo: '/v2/blog/:blogIdentifier/info',

/**
* Gets the avatar URL for a blog
Expand All @@ -60,7 +61,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
blogAvatar: '/blog/:blogIdentifier/avatar/:size',
blogAvatar: '/v2/blog/:blogIdentifier/avatar/:size',

/**
* Gets the likes for a blog
Expand All @@ -75,7 +76,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
blogLikes: '/blog/:blogIdentifier/likes',
blogLikes: '/v2/blog/:blogIdentifier/likes',

/**
* Gets the followers for a blog
Expand All @@ -90,7 +91,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
blogFollowers: '/blog/:blogIdentifier/followers',
blogFollowers: '/v2/blog/:blogIdentifier/followers',

/**
* Gets a list of posts for a blog
Expand All @@ -104,7 +105,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
blogPosts: '/blog/:blogIdentifier/posts/:type',
blogPosts: '/v2/blog/:blogIdentifier/posts/:type',

/**
* Gets the queue for a blog
Expand All @@ -119,7 +120,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
blogQueue: '/blog/:blogIdentifier/posts/queue',
blogQueue: '/v2/blog/:blogIdentifier/posts/queue',

/**
* Gets the drafts for a blog
Expand All @@ -134,7 +135,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
blogDrafts: '/blog/:blogIdentifier/posts/draft',
blogDrafts: '/v2/blog/:blogIdentifier/posts/draft',

/**
* Gets the submissions for a blog
Expand All @@ -149,7 +150,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
blogSubmissions: '/blog/:blogIdentifier/posts/submission',
blogSubmissions: '/v2/blog/:blogIdentifier/posts/submission',

/**
* Gets information about the authenticating user and their blogs
Expand All @@ -163,7 +164,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
userInfo: '/user/info',
userInfo: '/v2/user/info',

/**
* Gets the dashboard posts for the authenticating user
Expand All @@ -177,7 +178,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
userDashboard: '/user/dashboard',
userDashboard: '/v2/user/dashboard',

/**
* Gets the blogs the authenticating user follows
Expand All @@ -191,7 +192,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
userFollowing: '/user/following',
userFollowing: '/v2/user/following',

/**
* Gets the likes for the authenticating user
Expand All @@ -205,7 +206,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
userLikes: '/user/likes',
userLikes: '/v2/user/likes',

/**
* Gets posts tagged with the specified tag
Expand All @@ -220,7 +221,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
taggedPosts: ['/tagged', ['tag']],
taggedPosts: ['/v2/tagged', ['tag']],
},

POST: {
Expand All @@ -238,7 +239,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
createPost: '/blog/:blogIdentifier/post',
createPost: '/v2/blog/:blogIdentifier/post',

/**
* Edits a given post
Expand All @@ -253,7 +254,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
editPost: '/blog/:blogIdentifier/post/edit',
editPost: '/v2/blog/:blogIdentifier/post/edit',

/**
* Edits a given post
Expand All @@ -268,7 +269,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
reblogPost: '/blog/:blogIdentifier/post/reblog',
reblogPost: '/v2/blog/:blogIdentifier/post/reblog',

/**
* Edits a given post
Expand All @@ -284,7 +285,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
deletePost: ['/blog/:blogIdentifier/post/delete', ['id']],
deletePost: ['/v2/blog/:blogIdentifier/post/delete', ['id']],

/**
* Follows a blog as the authenticating user
Expand All @@ -299,7 +300,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
followBlog: ['/user/follow', ['url']],
followBlog: ['/v2/user/follow', ['url']],

/**
* Unfollows a blog as the authenticating user
Expand All @@ -314,7 +315,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
unfollowBlog: ['/user/unfollow', ['url']],
unfollowBlog: ['/v2/user/unfollow', ['url']],

/**
* Likes a post as the authenticating user
Expand All @@ -330,7 +331,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
likePost: ['/user/like', ['id', 'reblog_key']],
likePost: ['/v2/user/like', ['id', 'reblog_key']],

/**
* Unlikes a post as the authenticating user
Expand All @@ -346,7 +347,7 @@ const API_METHODS = {
*
* @memberof TumblrClient
*/
unlikePost: ['/user/unlike', ['id', 'reblog_key']],
unlikePost: ['/v2/user/unlike', ['id', 'reblog_key']],
},
};

Expand Down Expand Up @@ -478,6 +479,18 @@ function getRequest(requestGet, credentials, baseUrl, apiPath, requestOptions, p
params.api_key = credentials.consumer_key;
}

// if the apiPath already has query params, use them
let existingQueryIndex = apiPath.indexOf('?');
if (existingQueryIndex !== -1) {
let existingParams = qs.parse(apiPath.substr(existingQueryIndex));

// extend the existing params with the given params
extend(existingParams, params);

// reset the given apiPath to remove those query params for clean reassembly
apiPath = apiPath.substring(0, existingQueryIndex);
}

return requestGet(extend({
url: baseUrl + apiPath + '?' + qs.stringify(params),
oauth: credentials,
Expand Down Expand Up @@ -716,6 +729,20 @@ function TumblrClient(options) {
this.version = CLIENT_VERSION;
this.credentials = get(options, 'credentials', omit(options, 'baseUrl', 'request'));
this.baseUrl = get(options, 'baseUrl', API_BASE_URL);

// if someone is providing a custom baseUrl with a path, show a message
// to help them debug if they run into errors.
if (this.baseUrl !== API_BASE_URL && this.baseUrl !== '') {
const baseUrl = new URL(this.baseUrl);
if (baseUrl.pathname !== '/') {
/* eslint-disable no-console */
console.warn('WARNING! Path detected in your custom baseUrl!');
console.warn('As of version 3.0.0, tumblr.js no longer includes a path in the baseUrl.');
console.warn('If you encounter errors, please try to omit the path.');
/* eslint-enable no-console */
}
}

this.request = get(options, 'request', request);
this.requestOptions = {
followRedirect: false,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tumblr.js",
"version": "2.0.2",
"version": "3.0.0",
"description": "Official JavaScript client for the Tumblr API",
"main": "./lib/tumblr",
"types": "./lib/tumblr.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions test/tumblr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const DUMMY_CREDENTIALS = {
token: 'Toad',
token_secret: 'Princess Toadstool',
};
const DUMMY_API_URL = 'https://t.umblr.com/v2';
const DUMMY_API_URL = 'https://t.umblr.com';

const URL_PARAM_REGEX = /\/:([^\/]+)/g;

Expand Down Expand Up @@ -171,7 +171,7 @@ describe('tumblr.js', function() {
describe('default options', function() {
it('uses the default Tumblr API base URL', function() {
const client = tumblr.createClient();
assert.equal(client.baseUrl, 'https://api.tumblr.com/v2');
assert.equal(client.baseUrl, 'https://api.tumblr.com');
});

it('uses default request library', function() {
Expand Down

0 comments on commit 80bf8a4

Please sign in to comment.