2020-08-20 03:51:57 +00:00
|
|
|
import fetch from 'node-fetch';
|
2020-08-20 02:25:21 +00:00
|
|
|
import {
|
2020-12-20 17:33:14 +00:00
|
|
|
CommentResponse,
|
|
|
|
CreateComment,
|
|
|
|
CreateCommentLike,
|
|
|
|
DeleteComment,
|
|
|
|
EditComment,
|
|
|
|
GetComments,
|
|
|
|
GetCommentsResponse,
|
|
|
|
MarkCommentAsRead,
|
|
|
|
RemoveComment,
|
|
|
|
SaveComment,
|
|
|
|
} from './interfaces/api/comment';
|
|
|
|
import {
|
|
|
|
AddModToCommunity,
|
|
|
|
AddModToCommunityResponse,
|
|
|
|
BanFromCommunity,
|
|
|
|
BanFromCommunityResponse,
|
|
|
|
CommunityJoin,
|
|
|
|
CommunityJoinResponse,
|
|
|
|
CommunityResponse,
|
|
|
|
CreateCommunity,
|
|
|
|
DeleteCommunity,
|
|
|
|
EditCommunity,
|
|
|
|
FollowCommunity,
|
|
|
|
GetCommunity,
|
|
|
|
GetCommunityResponse,
|
|
|
|
GetFollowedCommunities,
|
|
|
|
GetFollowedCommunitiesResponse,
|
|
|
|
ListCommunities,
|
|
|
|
ListCommunitiesResponse,
|
|
|
|
RemoveCommunity,
|
|
|
|
TransferCommunity,
|
|
|
|
} from './interfaces/api/community';
|
|
|
|
import {
|
|
|
|
CreatePost,
|
|
|
|
CreatePostLike,
|
|
|
|
DeletePost,
|
|
|
|
EditPost,
|
|
|
|
GetPost,
|
|
|
|
GetPostResponse,
|
|
|
|
GetPosts,
|
|
|
|
GetPostsResponse,
|
|
|
|
LockPost,
|
|
|
|
PostJoin,
|
|
|
|
PostJoinResponse,
|
|
|
|
PostResponse,
|
|
|
|
RemovePost,
|
|
|
|
SavePost,
|
|
|
|
StickyPost,
|
|
|
|
} from './interfaces/api/post';
|
|
|
|
import {
|
|
|
|
CreateSite,
|
|
|
|
EditSite,
|
|
|
|
GetModlog,
|
|
|
|
GetModlogResponse,
|
|
|
|
GetSite,
|
2020-08-20 02:25:21 +00:00
|
|
|
GetSiteConfig,
|
|
|
|
GetSiteConfigResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
GetSiteResponse,
|
2020-08-20 02:25:21 +00:00
|
|
|
ListCategoriesResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
SaveSiteConfig,
|
|
|
|
Search,
|
2020-08-20 02:25:21 +00:00
|
|
|
SearchResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
SiteResponse,
|
|
|
|
TransferSite,
|
|
|
|
} from './interfaces/api/site';
|
|
|
|
import {
|
|
|
|
AddAdmin,
|
2020-08-20 02:25:21 +00:00
|
|
|
AddAdminResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
BanUser,
|
|
|
|
BanUserResponse,
|
|
|
|
CreatePrivateMessage,
|
|
|
|
DeleteAccount,
|
|
|
|
DeletePrivateMessage,
|
|
|
|
EditPrivateMessage,
|
2020-08-20 02:25:21 +00:00
|
|
|
GetCaptchaResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
GetPrivateMessages,
|
|
|
|
GetReplies,
|
|
|
|
GetRepliesResponse,
|
|
|
|
GetUserDetails,
|
|
|
|
GetUserDetailsResponse,
|
|
|
|
GetUserMentions,
|
2020-08-20 02:25:21 +00:00
|
|
|
GetUserMentionsResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
Login,
|
|
|
|
LoginResponse,
|
|
|
|
MarkAllAsRead,
|
|
|
|
MarkPrivateMessageAsRead,
|
|
|
|
MarkUserMentionAsRead,
|
|
|
|
PasswordChange,
|
|
|
|
PasswordReset,
|
|
|
|
PasswordResetResponse,
|
2020-08-20 02:25:21 +00:00
|
|
|
PrivateMessageResponse,
|
|
|
|
PrivateMessagesResponse,
|
2020-12-20 17:33:14 +00:00
|
|
|
Register,
|
|
|
|
SaveUserSettings,
|
|
|
|
UserJoin,
|
|
|
|
UserJoinResponse,
|
|
|
|
UserMentionResponse,
|
|
|
|
} from './interfaces/api/user';
|
2020-08-20 02:25:21 +00:00
|
|
|
|
|
|
|
enum HttpType {
|
|
|
|
Get = 'GET',
|
|
|
|
Post = 'POST',
|
|
|
|
Put = 'PUT',
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LemmyHttp {
|
|
|
|
private baseUrl: string;
|
2020-11-12 19:42:31 +00:00
|
|
|
private headers: { [key: string]: string } = {};
|
2020-08-20 02:25:21 +00:00
|
|
|
|
2020-11-12 19:42:31 +00:00
|
|
|
constructor(baseUrl: string, headers?: { [key: string]: string }) {
|
2020-08-20 02:25:21 +00:00
|
|
|
this.baseUrl = baseUrl;
|
2020-11-12 19:42:31 +00:00
|
|
|
|
|
|
|
if (headers) {
|
|
|
|
this.headers = headers;
|
|
|
|
}
|
2020-08-20 02:25:21 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async getSite(form: GetSite): Promise<GetSiteResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/site', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async createSite(form: CreateSite): Promise<SiteResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/site', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async editSite(form: EditSite): Promise<SiteResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/site', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async transferSite(form: TransferSite): Promise<GetSiteResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/site/transfer', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getSiteConfig(form: GetSiteConfig): Promise<GetSiteConfigResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/site/config', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async saveSiteConfig(form: SaveSiteConfig): Promise<GetSiteConfigResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/site/config', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async listCategories(): Promise<ListCategoriesResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/categories', {});
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async getModlog(form: GetModlog): Promise<GetModlogResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/modlog', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async search(form: Search): Promise<SearchResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/search', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async createCommunity(form: CreateCommunity): Promise<CommunityResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/community', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async getCommunity(form: GetCommunity): Promise<GetCommunityResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/community', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async editCommunity(form: EditCommunity): Promise<CommunityResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/community', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async listCommunities(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: ListCommunities
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<ListCommunitiesResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/community/list', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async followCommunity(form: FollowCommunity): Promise<CommunityResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/community/follow', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async deleteCommunity(form: DeleteCommunity): Promise<CommunityResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/community/delete', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async removeCommunity(form: RemoveCommunity): Promise<CommunityResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/community/remove', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async transferCommunity(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: TransferCommunity
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<GetCommunityResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/community/transfer', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async banFromCommunity(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: BanFromCommunity
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<BanFromCommunityResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/community/ban_user', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async addModToCommunity(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: AddModToCommunity
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<AddModToCommunityResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/community/mod', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async createPost(form: CreatePost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async getPost(form: GetPost): Promise<GetPostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/post', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async editPost(form: EditPost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/post', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async deletePost(form: DeletePost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post/delete', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async removePost(form: RemovePost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post/remove', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async lockPost(form: LockPost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post/lock', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async stickyPost(form: StickyPost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post/sticky', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async getPosts(form: GetPosts): Promise<GetPostsResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/post/list', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async likePost(form: CreatePostLike): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post/like', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async savePost(form: SavePost): Promise<PostResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/post/save', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async createComment(form: CreateComment): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/comment', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async editComment(form: EditComment): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/comment', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async deleteComment(form: DeleteComment): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/comment/delete', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async removeComment(form: RemoveComment): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/comment/remove', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async markCommentAsRead(form: MarkCommentAsRead): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/comment/mark_as_read', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async likeComment(form: CreateCommentLike): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/comment/like', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async saveComment(form: SaveComment): Promise<CommentResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/comment/save', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async getComments(form: GetComments): Promise<GetCommentsResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/comment/list', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPrivateMessages(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: GetPrivateMessages
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<PrivateMessagesResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/private_message/list', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async createPrivateMessage(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: CreatePrivateMessage
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<PrivateMessageResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/private_message', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async editPrivateMessage(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: EditPrivateMessage
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<PrivateMessageResponse> {
|
|
|
|
return this.wrapper(HttpType.Put, '/private_message', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deletePrivateMessage(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: DeletePrivateMessage
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<PrivateMessageResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/private_message/delete', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async markPrivateMessageAsRead(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: MarkPrivateMessageAsRead
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<PrivateMessageResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/private_message/mark_as_read', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async register(form: Register): Promise<LoginResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/register', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async login(form: Login): Promise<LoginResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/login', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async getUserDetails(form: GetUserDetails): Promise<GetUserDetailsResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/user', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getUserMentions(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: GetUserMentions
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<GetUserMentionsResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/user/mention', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async markUserMentionAsRead(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: MarkUserMentionAsRead
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<UserMentionResponse> {
|
|
|
|
return this.wrapper(HttpType.Post, '/user/mention/mark_as_read', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async getReplies(form: GetReplies): Promise<GetRepliesResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Get, '/user/replies', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getFollowedCommunities(
|
2020-12-20 17:33:14 +00:00
|
|
|
form: GetFollowedCommunities
|
2020-08-20 02:25:21 +00:00
|
|
|
): Promise<GetFollowedCommunitiesResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/user/followed_communities', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async userJoin(form: UserJoin): Promise<UserJoinResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/join', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async postJoin(form: PostJoin): Promise<PostJoinResponse> {
|
2020-09-13 13:49:35 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/post/join', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async communityJoin(form: CommunityJoin): Promise<CommunityJoinResponse> {
|
2020-09-13 13:49:35 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/community/join', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async banUser(form: BanUser): Promise<BanUserResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/ban', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getCaptcha(): Promise<GetCaptchaResponse> {
|
|
|
|
return this.wrapper(HttpType.Get, '/user/get_captcha', {});
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async deleteAccount(form: DeleteAccount): Promise<LoginResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/delete_account', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async passwordReset(form: PasswordReset): Promise<PasswordResetResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/password_reset', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async passwordChange(form: PasswordChange): Promise<LoginResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/password_change', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async markAllAsRead(form: MarkAllAsRead): Promise<LoginResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/user/mark_all_as_read', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async saveUserSettings(form: SaveUserSettings): Promise<LoginResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Put, '/user/save_user_settings', form);
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:33:14 +00:00
|
|
|
async addAdmin(form: AddAdmin): Promise<AddAdminResponse> {
|
2020-08-20 02:25:21 +00:00
|
|
|
return this.wrapper(HttpType.Post, '/admin/add', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildFullUrl(endpoint: string): string {
|
|
|
|
return `${this.baseUrl}${endpoint}`;
|
|
|
|
}
|
|
|
|
|
2020-12-20 19:21:26 +00:00
|
|
|
private async wrapper<ResponseType, MessageType>(
|
2020-08-20 02:25:21 +00:00
|
|
|
type_: HttpType,
|
|
|
|
endpoint: string,
|
|
|
|
form: MessageType
|
2020-12-20 19:21:26 +00:00
|
|
|
): Promise<ResponseType> {
|
2020-08-20 02:25:21 +00:00
|
|
|
if (type_ == HttpType.Get) {
|
2020-08-20 03:51:57 +00:00
|
|
|
let getUrl = `${this.buildFullUrl(endpoint)}?${encodeGetParams(form)}`;
|
2020-08-20 02:25:21 +00:00
|
|
|
return fetch(getUrl, {
|
|
|
|
method: 'GET',
|
2020-11-12 19:42:31 +00:00
|
|
|
headers: this.headers,
|
2020-08-20 02:25:21 +00:00
|
|
|
}).then(d => d.json());
|
|
|
|
} else {
|
|
|
|
return fetch(this.buildFullUrl(endpoint), {
|
|
|
|
method: type_,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2020-11-12 19:42:31 +00:00
|
|
|
...this.headers,
|
2020-08-20 02:25:21 +00:00
|
|
|
},
|
|
|
|
body: JSON.stringify(form),
|
|
|
|
}).then(d => d.json());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function encodeGetParams(p: any): string {
|
|
|
|
return Object.entries(p)
|
|
|
|
.map(kv => kv.map(encodeURIComponent).join('='))
|
|
|
|
.join('&');
|
|
|
|
}
|