lemmy-js-client/src/http.ts

688 lines
16 KiB
TypeScript
Raw Normal View History

import fetch from "node-fetch";
import {
CommentReportResponse,
2020-12-20 17:33:14 +00:00
CommentResponse,
CreateComment,
CreateCommentLike,
CreateCommentReport,
2020-12-20 17:33:14 +00:00
DeleteComment,
EditComment,
GetComments,
GetCommentsResponse,
ListCommentReports,
ListCommentReportsResponse,
2020-12-20 17:33:14 +00:00
MarkCommentAsRead,
RemoveComment,
ResolveCommentReport,
2020-12-20 17:33:14 +00:00
SaveComment,
} from "./interfaces/api/comment";
2020-12-20 17:33:14 +00:00
import {
AddModToCommunity,
AddModToCommunityResponse,
BanFromCommunity,
BanFromCommunityResponse,
BlockCommunity,
BlockCommunityResponse,
2020-12-20 17:33:14 +00:00
CommunityResponse,
CreateCommunity,
DeleteCommunity,
EditCommunity,
FollowCommunity,
GetCommunity,
GetCommunityResponse,
ListCommunities,
ListCommunitiesResponse,
RemoveCommunity,
TransferCommunity,
} from "./interfaces/api/community";
import {
AddAdmin,
AddAdminResponse,
BanPerson,
BanPersonResponse,
BlockPerson,
BlockPersonResponse,
ChangePassword,
CreatePrivateMessage,
DeleteAccount,
DeletePrivateMessage,
EditPrivateMessage,
GetCaptchaResponse,
GetPersonDetails,
GetPersonDetailsResponse,
GetPersonMentions,
GetPersonMentionsResponse,
GetPrivateMessages,
GetReplies,
GetRepliesResponse,
GetReportCount,
GetReportCountResponse,
GetUnreadCount,
GetUnreadCountResponse,
Login,
LoginResponse,
MarkAllAsRead,
MarkPersonMentionAsRead,
MarkPrivateMessageAsRead,
PasswordChange,
PasswordReset,
PasswordResetResponse,
PersonMentionResponse,
PrivateMessageResponse,
PrivateMessagesResponse,
Register,
SaveUserSettings,
} from "./interfaces/api/person";
2020-12-20 17:33:14 +00:00
import {
CreatePost,
CreatePostLike,
CreatePostReport,
2020-12-20 17:33:14 +00:00
DeletePost,
EditPost,
GetPost,
GetPostResponse,
GetPosts,
GetPostsResponse,
GetSiteMetadata,
GetSiteMetadataResponse,
ListPostReports,
ListPostReportsResponse,
2020-12-20 17:33:14 +00:00
LockPost,
PostReportResponse,
2020-12-20 17:33:14 +00:00
PostResponse,
RemovePost,
ResolvePostReport,
2020-12-20 17:33:14 +00:00
SavePost,
StickyPost,
} from "./interfaces/api/post";
2020-12-20 17:33:14 +00:00
import {
CreateSite,
EditSite,
GetModlog,
GetModlogResponse,
GetSite,
GetSiteConfig,
GetSiteConfigResponse,
2020-12-20 17:33:14 +00:00
GetSiteResponse,
ResolveObject,
ResolveObjectResponse,
2020-12-20 17:33:14 +00:00
SaveSiteConfig,
Search,
SearchResponse,
2020-12-20 17:33:14 +00:00
SiteResponse,
TransferSite,
} from "./interfaces/api/site";
import { VERSION } from "./interfaces/others";
enum HttpType {
Get = "GET",
Post = "POST",
Put = "PUT",
}
/**
* Helps build lemmy HTTP requests.
*/
export class LemmyHttp {
private apiUrl: string;
private headers: { [key: string]: string } = {};
2021-01-24 17:37:04 +00:00
/**
* Generates a new instance of LemmyHttp.
* @param baseUrl the base url, without the vX version: https://lemmy.ml -> goes to https://lemmy.ml/api/vX
* @param headers optional headers. Should contain `x-real-ip` and `x-forwarded-for` .
2021-01-24 17:37:04 +00:00
*/
constructor(baseUrl: string, headers?: { [key: string]: string }) {
this.apiUrl = `${baseUrl}/api/${VERSION}`;
if (headers) {
this.headers = headers;
}
}
/**
* Gets the site, and your user data.
*/
2020-12-20 17:33:14 +00:00
async getSite(form: GetSite): Promise<GetSiteResponse> {
return this.wrapper(HttpType.Get, "/site", form);
}
/**
* Create your site.
*/
2020-12-20 17:33:14 +00:00
async createSite(form: CreateSite): Promise<SiteResponse> {
return this.wrapper(HttpType.Post, "/site", form);
}
/**
* Edit your site.
*/
2020-12-20 17:33:14 +00:00
async editSite(form: EditSite): Promise<SiteResponse> {
return this.wrapper(HttpType.Put, "/site", form);
}
/**
* Transfer your site to another user.
*/
2020-12-20 17:33:14 +00:00
async transferSite(form: TransferSite): Promise<GetSiteResponse> {
return this.wrapper(HttpType.Post, "/site/transfer", form);
}
/**
* Get your site configuration.
*/
async getSiteConfig(form: GetSiteConfig): Promise<GetSiteConfigResponse> {
return this.wrapper(HttpType.Get, "/site/config", form);
}
/**
* Save your site config.
*/
2020-12-20 17:33:14 +00:00
async saveSiteConfig(form: SaveSiteConfig): Promise<GetSiteConfigResponse> {
return this.wrapper(HttpType.Put, "/site/config", form);
}
/**
* Get the modlog.
*/
2020-12-20 17:33:14 +00:00
async getModlog(form: GetModlog): Promise<GetModlogResponse> {
return this.wrapper(HttpType.Get, "/modlog", form);
}
/**
* Search lemmy.
*/
2020-12-20 17:33:14 +00:00
async search(form: Search): Promise<SearchResponse> {
return this.wrapper(HttpType.Get, "/search", form);
}
/**
* Fetch a non-local / federated object.
*/
async resolveObject(form: ResolveObject): Promise<ResolveObjectResponse> {
return this.wrapper(HttpType.Get, "/resolve_object", form);
}
/**
* Create a new community.
*/
2020-12-20 17:33:14 +00:00
async createCommunity(form: CreateCommunity): Promise<CommunityResponse> {
return this.wrapper(HttpType.Post, "/community", form);
}
/**
* Get / fetch a community.
*/
2020-12-20 17:33:14 +00:00
async getCommunity(form: GetCommunity): Promise<GetCommunityResponse> {
return this.wrapper(HttpType.Get, "/community", form);
}
/**
* Edit a community.
*/
2020-12-20 17:33:14 +00:00
async editCommunity(form: EditCommunity): Promise<CommunityResponse> {
return this.wrapper(HttpType.Put, "/community", form);
}
/**
* List communities, with various filters.
*/
async listCommunities(
2020-12-20 17:33:14 +00:00
form: ListCommunities
): Promise<ListCommunitiesResponse> {
return this.wrapper(HttpType.Get, "/community/list", form);
}
/**
* Follow / subscribe to a community.
*/
2020-12-20 17:33:14 +00:00
async followCommunity(form: FollowCommunity): Promise<CommunityResponse> {
return this.wrapper(HttpType.Post, "/community/follow", form);
}
/**
* Block a community.
*/
async blockCommunity(form: BlockCommunity): Promise<BlockCommunityResponse> {
return this.wrapper(HttpType.Post, "/community/block", form);
}
/**
* Delete a community.
*/
2020-12-20 17:33:14 +00:00
async deleteCommunity(form: DeleteCommunity): Promise<CommunityResponse> {
return this.wrapper(HttpType.Post, "/community/delete", form);
}
/**
* A moderator remove for a community.
*/
2020-12-20 17:33:14 +00:00
async removeCommunity(form: RemoveCommunity): Promise<CommunityResponse> {
return this.wrapper(HttpType.Post, "/community/remove", form);
}
/**
* Transfer your community to an existing moderator.
*/
async transferCommunity(
2020-12-20 17:33:14 +00:00
form: TransferCommunity
): Promise<GetCommunityResponse> {
return this.wrapper(HttpType.Post, "/community/transfer", form);
}
/**
* Ban a user from a community.
*/
async banFromCommunity(
2020-12-20 17:33:14 +00:00
form: BanFromCommunity
): Promise<BanFromCommunityResponse> {
return this.wrapper(HttpType.Post, "/community/ban_user", form);
}
/**
* Add a moderator to your community.
*/
async addModToCommunity(
2020-12-20 17:33:14 +00:00
form: AddModToCommunity
): Promise<AddModToCommunityResponse> {
return this.wrapper(HttpType.Post, "/community/mod", form);
}
/**
* Create a post.
*/
2020-12-20 17:33:14 +00:00
async createPost(form: CreatePost): Promise<PostResponse> {
return this.wrapper(HttpType.Post, "/post", form);
}
/**
* Get / fetch a post.
*/
2020-12-20 17:33:14 +00:00
async getPost(form: GetPost): Promise<GetPostResponse> {
return this.wrapper(HttpType.Get, "/post", form);
}
/**
* Edit a post.
*/
2020-12-20 17:33:14 +00:00
async editPost(form: EditPost): Promise<PostResponse> {
return this.wrapper(HttpType.Put, "/post", form);
}
/**
* Delete a post.
*/
2020-12-20 17:33:14 +00:00
async deletePost(form: DeletePost): Promise<PostResponse> {
return this.wrapper(HttpType.Post, "/post/delete", form);
}
/**
* A moderator remove for a post.
*/
2020-12-20 17:33:14 +00:00
async removePost(form: RemovePost): Promise<PostResponse> {
return this.wrapper(HttpType.Post, "/post/remove", form);
}
/**
* A moderator can lock a post ( IE disable new comments ).
*/
2020-12-20 17:33:14 +00:00
async lockPost(form: LockPost): Promise<PostResponse> {
return this.wrapper(HttpType.Post, "/post/lock", form);
}
/**
* A moderator can sticky a post ( IE stick it to the top of a community ).
*/
2020-12-20 17:33:14 +00:00
async stickyPost(form: StickyPost): Promise<PostResponse> {
return this.wrapper(HttpType.Post, "/post/sticky", form);
}
/**
* Get / fetch posts, with various filters.
*/
2020-12-20 17:33:14 +00:00
async getPosts(form: GetPosts): Promise<GetPostsResponse> {
return this.wrapper(HttpType.Get, "/post/list", form);
}
/**
* Like / vote on a post.
*/
2020-12-20 17:33:14 +00:00
async likePost(form: CreatePostLike): Promise<PostResponse> {
return this.wrapper(HttpType.Post, "/post/like", form);
}
/**
* Save a post.
*/
2020-12-20 17:33:14 +00:00
async savePost(form: SavePost): Promise<PostResponse> {
return this.wrapper(HttpType.Put, "/post/save", form);
}
/**
* Report a post.
*/
async createPostReport(form: CreatePostReport): Promise<PostReportResponse> {
return this.wrapper(HttpType.Post, "/post/report", form);
}
/**
* Resolve a post report. Only a mod can do this.
*/
async resolvePostReport(
form: ResolvePostReport
): Promise<PostReportResponse> {
return this.wrapper(HttpType.Put, "/post/report/resolve", form);
}
/**
* List post reports.
*/
async listPostReports(
form: ListPostReports
): Promise<ListPostReportsResponse> {
return this.wrapper(HttpType.Get, "/post/report/list", form);
}
/**
* Fetch metadata for any given site.
*/
async getSiteMetadata(
form: GetSiteMetadata
): Promise<GetSiteMetadataResponse> {
return this.wrapper(HttpType.Get, "/post/site_metadata", form);
}
/**
* Create a comment.
*/
2020-12-20 17:33:14 +00:00
async createComment(form: CreateComment): Promise<CommentResponse> {
return this.wrapper(HttpType.Post, "/comment", form);
}
/**
* Edit a comment.
*/
2020-12-20 17:33:14 +00:00
async editComment(form: EditComment): Promise<CommentResponse> {
return this.wrapper(HttpType.Put, "/comment", form);
}
/**
* Delete a comment.
*/
2020-12-20 17:33:14 +00:00
async deleteComment(form: DeleteComment): Promise<CommentResponse> {
return this.wrapper(HttpType.Post, "/comment/delete", form);
}
/**
* A moderator remove for a comment.
*/
2020-12-20 17:33:14 +00:00
async removeComment(form: RemoveComment): Promise<CommentResponse> {
return this.wrapper(HttpType.Post, "/comment/remove", form);
}
/**
* Mark a comment as read.
*/
2020-12-20 17:33:14 +00:00
async markCommentAsRead(form: MarkCommentAsRead): Promise<CommentResponse> {
return this.wrapper(HttpType.Post, "/comment/mark_as_read", form);
}
/**
* Like / vote on a comment.
*/
2020-12-20 17:33:14 +00:00
async likeComment(form: CreateCommentLike): Promise<CommentResponse> {
return this.wrapper(HttpType.Post, "/comment/like", form);
}
/**
* Save a comment.
*/
2020-12-20 17:33:14 +00:00
async saveComment(form: SaveComment): Promise<CommentResponse> {
return this.wrapper(HttpType.Put, "/comment/save", form);
}
/**
* Get / fetch comments.
*/
2020-12-20 17:33:14 +00:00
async getComments(form: GetComments): Promise<GetCommentsResponse> {
return this.wrapper(HttpType.Get, "/comment/list", form);
}
/**
* Report a comment.
*/
async createCommentReport(
form: CreateCommentReport
): Promise<CommentReportResponse> {
return this.wrapper(HttpType.Post, "/comment/report", form);
}
/**
* Resolve a comment report. Only a mod can do this.
*/
async resolveCommentReport(
form: ResolveCommentReport
): Promise<CommentReportResponse> {
return this.wrapper(HttpType.Put, "/comment/report/resolve", form);
}
/**
* List comment reports.
*/
async listCommentReports(
form: ListCommentReports
): Promise<ListCommentReportsResponse> {
return this.wrapper(HttpType.Get, "/comment/report/list", form);
}
/**
* Get / fetch private messages.
*/
async getPrivateMessages(
2020-12-20 17:33:14 +00:00
form: GetPrivateMessages
): Promise<PrivateMessagesResponse> {
return this.wrapper(HttpType.Get, "/private_message/list", form);
}
/**
* Create a private message.
*/
async createPrivateMessage(
2020-12-20 17:33:14 +00:00
form: CreatePrivateMessage
): Promise<PrivateMessageResponse> {
return this.wrapper(HttpType.Post, "/private_message", form);
}
/**
* Edit a private message.
*/
async editPrivateMessage(
2020-12-20 17:33:14 +00:00
form: EditPrivateMessage
): Promise<PrivateMessageResponse> {
return this.wrapper(HttpType.Put, "/private_message", form);
}
/**
* Delete a private message.
*/
async deletePrivateMessage(
2020-12-20 17:33:14 +00:00
form: DeletePrivateMessage
): Promise<PrivateMessageResponse> {
return this.wrapper(HttpType.Post, "/private_message/delete", form);
}
/**
* Mark a private message as read.
*/
async markPrivateMessageAsRead(
2020-12-20 17:33:14 +00:00
form: MarkPrivateMessageAsRead
): Promise<PrivateMessageResponse> {
return this.wrapper(HttpType.Post, "/private_message/mark_as_read", form);
}
/**
* Register a new user.
*/
2020-12-20 17:33:14 +00:00
async register(form: Register): Promise<LoginResponse> {
return this.wrapper(HttpType.Post, "/user/register", form);
}
/**
* Log into lemmy.
*/
2020-12-20 17:33:14 +00:00
async login(form: Login): Promise<LoginResponse> {
return this.wrapper(HttpType.Post, "/user/login", form);
}
/**
* Get the details for a person.
*/
async getPersonDetails(
form: GetPersonDetails
): Promise<GetPersonDetailsResponse> {
return this.wrapper(HttpType.Get, "/user", form);
}
/**
* Get mentions for your user.
*/
async getPersonMentions(
form: GetPersonMentions
): Promise<GetPersonMentionsResponse> {
return this.wrapper(HttpType.Get, "/user/mention", form);
}
/**
* Mark a person mention as read.
*/
async markPersonMentionAsRead(
form: MarkPersonMentionAsRead
): Promise<PersonMentionResponse> {
return this.wrapper(HttpType.Post, "/user/mention/mark_as_read", form);
}
/**
* Get comment replies.
*/
2020-12-20 17:33:14 +00:00
async getReplies(form: GetReplies): Promise<GetRepliesResponse> {
return this.wrapper(HttpType.Get, "/user/replies", form);
}
/**
* Ban a person from your site.
*/
async banPerson(form: BanPerson): Promise<BanPersonResponse> {
return this.wrapper(HttpType.Post, "/user/ban", form);
}
/**
* Block a person.
*/
async blockPerson(form: BlockPerson): Promise<BlockPersonResponse> {
return this.wrapper(HttpType.Post, "/user/block", form);
}
/**
* Fetch a Captcha.
*/
async getCaptcha(): Promise<GetCaptchaResponse> {
return this.wrapper(HttpType.Get, "/user/get_captcha", {});
}
/**
* Delete your account.
*/
2020-12-20 17:33:14 +00:00
async deleteAccount(form: DeleteAccount): Promise<LoginResponse> {
return this.wrapper(HttpType.Post, "/user/delete_account", form);
}
/**
* Reset your password.
*/
2020-12-20 17:33:14 +00:00
async passwordReset(form: PasswordReset): Promise<PasswordResetResponse> {
return this.wrapper(HttpType.Post, "/user/password_reset", form);
}
/**
* Change your password from an email / token based reset.
*/
2020-12-20 17:33:14 +00:00
async passwordChange(form: PasswordChange): Promise<LoginResponse> {
return this.wrapper(HttpType.Post, "/user/password_change", form);
}
/**
* Mark all replies as read.
*/
async markAllAsRead(form: MarkAllAsRead): Promise<GetRepliesResponse> {
return this.wrapper(HttpType.Post, "/user/mark_all_as_read", form);
}
/**
* Save your user settings.
*/
2021-03-12 19:03:44 +00:00
async saveUserSettings(form: SaveUserSettings): Promise<LoginResponse> {
return this.wrapper(HttpType.Put, "/user/save_user_settings", form);
}
/**
* Change your user password.
*/
2021-04-01 21:35:37 +00:00
async changePassword(form: ChangePassword): Promise<LoginResponse> {
return this.wrapper(HttpType.Put, "/user/change_password", form);
2021-04-01 21:35:37 +00:00
}
/**
* Get counts for your reports
*/
async getReportCount(form: GetReportCount): Promise<GetReportCountResponse> {
return this.wrapper(HttpType.Get, "/user/report_count", form);
}
/**
* Get your unread counts
*/
async getUnreadCount(form: GetUnreadCount): Promise<GetUnreadCountResponse> {
return this.wrapper(HttpType.Get, "/user/unread_count", form);
}
/**
* Add an admin to your site.
*/
2020-12-20 17:33:14 +00:00
async addAdmin(form: AddAdmin): Promise<AddAdminResponse> {
return this.wrapper(HttpType.Post, "/admin/add", form);
}
private buildFullUrl(endpoint: string): string {
return `${this.apiUrl}${endpoint}`;
}
private async wrapper<ResponseType, MessageType>(
type_: HttpType,
endpoint: string,
form: MessageType
): Promise<ResponseType> {
if (type_ == HttpType.Get) {
2020-08-20 03:51:57 +00:00
let getUrl = `${this.buildFullUrl(endpoint)}?${encodeGetParams(form)}`;
return fetch(getUrl, {
method: "GET",
headers: this.headers,
2021-09-28 16:08:22 +00:00
}).then(d => d.json() as Promise<ResponseType>);
} else {
return fetch(this.buildFullUrl(endpoint), {
method: type_,
headers: {
"Content-Type": "application/json",
...this.headers,
},
body: JSON.stringify(form),
2021-09-28 16:08:22 +00:00
}).then(d => d.json() as Promise<ResponseType>);
}
}
}
function encodeGetParams(p: any): string {
return Object.entries(p)
.map(kv => kv.map(encodeURIComponent).join("="))
.join("&");
}