From f2ce55c630e6d4e8c76fa8383d70790b9d502002 Mon Sep 17 00:00:00 2001 From: Mark Suckerberg Date: Mon, 11 Mar 2024 22:42:19 -0500 Subject: [PATCH] Finishes the revert --- src/functions/Block.ts | 21 +++---- src/functions/BlogInfo.ts | 70 ++++++++------------- src/functions/Follow.ts | 24 +++---- src/functions/Post.ts | 128 ++++++++++++++++---------------------- src/functions/UserInfo.ts | 31 +++------ 5 files changed, 106 insertions(+), 168 deletions(-) diff --git a/src/functions/Block.ts b/src/functions/Block.ts index 2c20002..8b7e80f 100644 --- a/src/functions/Block.ts +++ b/src/functions/Block.ts @@ -17,20 +17,13 @@ export async function getBlogBlocks( offset = 0 ): Promise { // Tumblr API only allows a maximum of 20 blogs per request - if (limit > 20) { - limit = 20; - } - - const response = await accessTumblrAPI(token, `blog/${blogIdentifier}/blocks`, { - limit: limit.toString(), - offset: offset.toString(), - }); - - if (response.meta.status !== 200) { - throw new Error(`Failed to get blocked blogs: ${response.meta.msg}`); - } - - return response.response.blocked_tumblelogs; + if (limit > 20) limit = 20; + return await ( + await accessTumblrAPI(token, `blog/${blogIdentifier}/blocks`, { + limit: limit.toString(), + offset: offset.toString(), + }) + ).response.blocked_tumblelogs; } /** diff --git a/src/functions/BlogInfo.ts b/src/functions/BlogInfo.ts index bfab0b0..65ed6d5 100644 --- a/src/functions/BlogInfo.ts +++ b/src/functions/BlogInfo.ts @@ -35,17 +35,13 @@ export async function getBlogInfo( blogIdentifier: string, responseFields?: BlogInfoResponseFields //TODO: Find a way to make this dynamically return the correct type ): Promise<{ [field: string]: string; thing: string }> { - const response = await accessTumblrAPI( - token, - `blog/${blogIdentifier}/info`, - responseFields ? { fields: responseFields.join(",") } : undefined - ); - - if (response.meta.status !== 200) { - throw new Error(`Failed to get blog info: ${response.meta.msg}`); - } - - return response.response.blog; + return ( + await accessTumblrAPI( + token, + `blog/${blogIdentifier}/info`, + responseFields ? { fields: responseFields.join(",") } : undefined + ) + ).response.blog; } /** @@ -64,20 +60,13 @@ export async function getBlogFollowers( offset = 0 ): Promise { // Tumblr API only allows a maximum of 20 blogs per request - if (limit > 20) { - limit = 20; - } - - const response = await accessTumblrAPI(token, `blog/${blogIdentifier}/followers`, { - limit: limit.toString(), - offset: offset.toString(), - }); - - if (response.meta.status !== 200) { - throw new Error(`Failed to get blog followers: ${response.meta.msg}`); - } - - return response.response.users; + if (limit > 20) limit = 20; + return ( + await accessTumblrAPI(token, `blog/${blogIdentifier}/followers`, { + limit: limit.toString(), + offset: offset.toString(), + }) + ).response.users; } /** @@ -97,17 +86,12 @@ export async function getBlogFollowing( ): Promise { // Tumblr API only allows a maximum of 20 blogs per request if (limit > 20) limit = 20; - - const response = await accessTumblrAPI(token, `blog/${blogIdentifier}/following`, { - limit: limit.toString(), - offset: offset.toString(), - }); - - if (response.meta.status !== 200) { - throw new Error(`Failed to get following blogs: ${response.meta.msg}`); - } - - return response.response.blogs; + return ( + await accessTumblrAPI(token, `blog/${blogIdentifier}/following`, { + limit: limit.toString(), + offset: offset.toString(), + }) + ).response.blogs; } /** @@ -141,13 +125,9 @@ export async function getBlogFollowedBy( blogIdentifier: string, followedBy: string ): Promise { - const response = await accessTumblrAPI(token, `blog/${blogIdentifier}/followed_by`, { - query: followedBy, - }); - - if (response.meta.status !== 200) { - throw new Error(`Failed to get blog followed by: ${response.meta.msg}`); - } - - return response.response.followed_by; + return ( + await accessTumblrAPI(token, `blog/${blogIdentifier}/followed_by`, { + query: followedBy, + }) + ).response.followed_by; } diff --git a/src/functions/Follow.ts b/src/functions/Follow.ts index ea3bd3f..8a04379 100644 --- a/src/functions/Follow.ts +++ b/src/functions/Follow.ts @@ -9,20 +9,16 @@ import { accessTumblrAPI } from "./AccessTumblrApi"; * @link https://www.tumblr.com/docs/en/api/v2#userfollow--follow-a-blog */ export async function followBlog(token: string, url: string): Promise { - const response = await accessTumblrAPI( - token, - `user/follow`, - { - url: url, - }, - "POST" - ); - - if (response.meta.status !== 200) { - throw new Error(`Failed to follow blog: ${response.meta.msg}`); - } - - return response.response.blog; + return ( + await accessTumblrAPI( + token, + `user/follow`, + { + url: url, + }, + "POST" + ) + ).response.blog; } /** diff --git a/src/functions/Post.ts b/src/functions/Post.ts index d4ebab5..7a03d14 100644 --- a/src/functions/Post.ts +++ b/src/functions/Post.ts @@ -13,19 +13,15 @@ export async function CreatePost( blogIdentifier: string, postDetails: NewPostDetails ): Promise { - const response = await accessTumblrAPI( - token, - `blog/${blogIdentifier}/posts`, - //TODO: This is a hacky way to do this. Find a better way to make this into a string array. - JSON.parse(JSON.stringify(postDetails)), - "POST" - ); - - if (response.meta.status !== 201) { - throw new Error(`Failed to create post: ${response.meta.msg}`); - } - - return response.response.id; + return ( + await accessTumblrAPI( + token, + `blog/${blogIdentifier}/posts`, + //TODO: This is a hacky way to do this. Find a better way to make this into a string array. + JSON.parse(JSON.stringify(postDetails)), + "POST" + ) + ).response.id; } /** @@ -41,19 +37,15 @@ export async function EditPost( postId: string, postDetails: NewPostDetails ): Promise { - const response = await accessTumblrAPI( - token, - `blog/${blogIdentifier}/posts/${postId}`, - //TODO: This is a hacky way to do this. Find a better way to make this into a string array. - JSON.parse(JSON.stringify(postDetails)), - "PUT" - ); - - if (response.meta.status !== 200) { - throw new Error(`Failed to edit post: ${response.meta.msg}`); - } - - return response.response.id; + return ( + await accessTumblrAPI( + token, + `blog/${blogIdentifier}/posts/${postId}`, + //TODO: This is a hacky way to do this. Find a better way to make this into a string array. + JSON.parse(JSON.stringify(postDetails)), + "PUT" + ) + ).response.id; } /** @@ -75,21 +67,17 @@ export async function FetchPostNeue( postId: string, postFormat: "npf" | "legacy" = "npf" ): Promise { - const response = await accessTumblrAPI( - token, - `blog/${blogIdentifier}/posts/${postId}`, - { - post_format: postFormat, - }, - "GET", - "https://www.tumblr.com/api/v2/" // Yes, getting posts needs to be done on a different API endpoint for some reason. Ask Tumblr why. - ); - - if (response.meta.status !== 200) { - throw new Error(`Failed to fetch post: ${response.meta.msg}`); - } - - return response.response; + return ( + await accessTumblrAPI( + token, + `blog/${blogIdentifier}/posts/${postId}`, + { + post_format: postFormat, + }, + "GET", + "https://www.tumblr.com/api/v2/" // Yes, getting posts needs to be done on a different API endpoint for some reason. Ask Tumblr why. + ) + ).response; } /** @@ -196,21 +184,17 @@ export async function FetchPosts( type: type, }; - const response = await accessTumblrAPI( - token, - `blog/${blogIdentifier}/posts`, - //TODO: This is a hacky way to do this. Find a better way to make this into a string array. - JSON.parse(JSON.stringify(args)), - "GET", - "https://www.tumblr.com/api/v2/", // Yes, getting posts needs to be done on a different API endpoint for some reason. Ask Tumblr why. - basicAuth - ); - - if (response.meta.status !== 200) { - throw new Error(`Failed to fetch posts: ${response.meta.msg}`); - } - - return response.response.posts as PostType[]; + return ( + await accessTumblrAPI( + token, + `blog/${blogIdentifier}/posts`, + //TODO: This is a hacky way to do this. Find a better way to make this into a string array. + JSON.parse(JSON.stringify(args)), + "GET", + "https://www.tumblr.com/api/v2/", // Yes, getting posts needs to be done on a different API endpoint for some reason. Ask Tumblr why. + basicAuth + ) + ).response.posts as PostType[]; } export async function GetNotes( @@ -221,22 +205,18 @@ export async function GetNotes( mode: "all" | "likes" | "conversation" | "rollup" | "reblogs_with_tags" = "all", basicAuth = false ) { - const response = await accessTumblrAPI( - token, - `blog/${blogIdentifier}/notes`, - { - id: id.toString(), - mode, - before_timestamp: beforeTimestamp.toString(), - }, - "GET", - undefined, - basicAuth - ); - - if (response.meta.status !== 200) { - throw new Error(`Failed to fetch notes: ${response.meta.msg}`); - } - - return response.response as TumblrNoteResponse; + return ( + await accessTumblrAPI( + token, + `blog/${blogIdentifier}/notes`, + { + id: id.toString(), + mode, + before_timestamp: beforeTimestamp.toString(), + }, + "GET", + undefined, + basicAuth + ) + ).response as TumblrNoteResponse; } diff --git a/src/functions/UserInfo.ts b/src/functions/UserInfo.ts index 87626ec..75bb685 100644 --- a/src/functions/UserInfo.ts +++ b/src/functions/UserInfo.ts @@ -3,13 +3,9 @@ import { TumblrFollowerBlog } from "../interfaces"; import { accessTumblrAPI } from "./AccessTumblrApi"; export async function getUserInfo(token: string): Promise { - const response = await accessTumblrAPI(token, "user/info"); - - if (response.meta.status !== 200) { - throw new Error(`Failed to get user info: ${response.meta.msg}`); - } - - return response.response.user; + return await ( + await accessTumblrAPI(token, "user/info") + ).response.user; } export async function getUserFollowing( @@ -18,18 +14,11 @@ export async function getUserFollowing( offset = 0 ): Promise { // Tumblr API only allows a maximum of 20 blogs per request - if (limit > 20) { - limit = 20; - } - - const response = await accessTumblrAPI(token, "user/following", { - limit: limit.toString(), - offset: offset.toString(), - }); - - if (response.meta.status !== 200) { - throw new Error(`Failed to get blogs following: ${response.meta.msg}`); - } - - return response.response.blogs; + if (limit > 20) limit = 20; + return await ( + await accessTumblrAPI(token, "user/following", { + limit: limit.toString(), + offset: offset.toString(), + }) + ).response.blogs; }