Skip to content

Commit

Permalink
Makes tumblr API access not raise an error
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSuckerberg committed Mar 12, 2024
1 parent 86f2074 commit ccd05e2
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"typescriptreact"
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
}
9 changes: 0 additions & 9 deletions src/functions/AccessTumblrApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,5 @@ export async function accessTumblrAPI(

const response: TumblrAPIResponse = await request.json();

//TODO: Don't make this throw an error, instead return the response and let the caller handle it
if (!request.ok) {
throw new Error(
`${response.meta.status}: ${response.meta.msg} - ${JSON.stringify(
response.errors
)} (Query URL: ${method} ${url} ${method === "GET" ? "" : JSON.stringify(params)})`
);
}

return response;
}
21 changes: 14 additions & 7 deletions src/functions/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ export async function getBlogBlocks(
offset = 0
): Promise<TumblrFollowerBlog[]> {
// Tumblr API only allows a maximum of 20 blogs per request
if (limit > 20) limit = 20;
return await (
await accessTumblrAPI(token, `blog/${blogIdentifier}/blocks`, {
limit: limit.toString(),
offset: offset.toString(),
})
).response.blocked_tumblelogs;
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;
}

/**
Expand Down
70 changes: 45 additions & 25 deletions src/functions/BlogInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ 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 }> {
return (
await accessTumblrAPI(
token,
`blog/${blogIdentifier}/info`,
responseFields ? { fields: responseFields.join(",") } : undefined
)
).response.blog;
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;
}

/**
Expand All @@ -60,13 +64,20 @@ export async function getBlogFollowers(
offset = 0
): Promise<TumblrFollowingBlog[]> {
// Tumblr API only allows a maximum of 20 blogs per request
if (limit > 20) limit = 20;
return (
await accessTumblrAPI(token, `blog/${blogIdentifier}/followers`, {
limit: limit.toString(),
offset: offset.toString(),
})
).response.users;
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;
}

/**
Expand All @@ -86,12 +97,17 @@ export async function getBlogFollowing(
): Promise<TumblrFollowerBlog[]> {
// Tumblr API only allows a maximum of 20 blogs per request
if (limit > 20) limit = 20;
return (
await accessTumblrAPI(token, `blog/${blogIdentifier}/following`, {
limit: limit.toString(),
offset: offset.toString(),
})
).response.blogs;

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;
}

/**
Expand Down Expand Up @@ -125,9 +141,13 @@ export async function getBlogFollowedBy(
blogIdentifier: string,
followedBy: string
): Promise<boolean> {
return (
await accessTumblrAPI(token, `blog/${blogIdentifier}/followed_by`, {
query: followedBy,
})
).response.followed_by;
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;
}
24 changes: 14 additions & 10 deletions src/functions/Follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ 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<TumblrFollowingBlog> {
return (
await accessTumblrAPI(
token,
`user/follow`,
{
url: url,
},
"POST"
)
).response.blog;
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;
}

/**
Expand Down
128 changes: 74 additions & 54 deletions src/functions/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ export async function CreatePost(
blogIdentifier: string,
postDetails: NewPostDetails
): Promise<string | undefined> {
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;
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;
}

/**
Expand All @@ -37,15 +41,19 @@ export async function EditPost(
postId: string,
postDetails: NewPostDetails
): Promise<string | undefined> {
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;
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;
}

/**
Expand All @@ -67,17 +75,21 @@ export async function FetchPostNeue(
postId: string,
postFormat: "npf" | "legacy" = "npf"
): Promise<TumblrPost> {
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;
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;
}

/**
Expand Down Expand Up @@ -184,17 +196,21 @@ export async function FetchPosts<PostType extends TumblrPost = TumblrPost>(
type: type,
};

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[];
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[];
}

export async function GetNotes(
Expand All @@ -205,18 +221,22 @@ export async function GetNotes(
mode: "all" | "likes" | "conversation" | "rollup" | "reblogs_with_tags" = "all",
basicAuth = false
) {
return (
await accessTumblrAPI(
token,
`blog/${blogIdentifier}/notes`,
{
id: id.toString(),
mode,
before_timestamp: beforeTimestamp.toString(),
},
"GET",
undefined,
basicAuth
)
).response as TumblrNoteResponse;
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;
}
31 changes: 21 additions & 10 deletions src/functions/UserInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { TumblrFollowerBlog } from "../interfaces";
import { accessTumblrAPI } from "./AccessTumblrApi";

export async function getUserInfo(token: string): Promise<TumblrUserInfo> {
return await (
await accessTumblrAPI(token, "user/info")
).response.user;
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;
}

export async function getUserFollowing(
Expand All @@ -14,11 +18,18 @@ export async function getUserFollowing(
offset = 0
): Promise<TumblrFollowerBlog[]> {
// Tumblr API only allows a maximum of 20 blogs per request
if (limit > 20) limit = 20;
return await (
await accessTumblrAPI(token, "user/following", {
limit: limit.toString(),
offset: offset.toString(),
})
).response.blogs;
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;
}

0 comments on commit ccd05e2

Please sign in to comment.