Use proper generics for messagetype and responsetype.

This commit is contained in:
Dessalines 2020-12-20 14:21:26 -05:00
parent ac11456767
commit 7ba95c72f3
3 changed files with 6 additions and 172 deletions

View file

@ -98,7 +98,6 @@ import {
UserJoinResponse,
UserMentionResponse,
} from './interfaces/api/user';
import { MessageType } from './interfaces/others';
enum HttpType {
Get = 'GET',
@ -386,11 +385,11 @@ export class LemmyHttp {
return `${this.baseUrl}${endpoint}`;
}
private async wrapper<Res>(
private async wrapper<ResponseType, MessageType>(
type_: HttpType,
endpoint: string,
form: MessageType
): Promise<Res> {
): Promise<ResponseType> {
if (type_ == HttpType.Get) {
let getUrl = `${this.buildFullUrl(endpoint)}?${encodeGetParams(form)}`;
return fetch(getUrl, {

View file

@ -1,88 +1,3 @@
import {
CommentResponse,
CreateComment,
DeleteComment,
EditComment,
MarkCommentAsRead,
RemoveComment,
SaveComment,
} from './api/comment';
import {
AddModToCommunity,
AddModToCommunityResponse,
BanFromCommunity,
BanFromCommunityResponse,
CommunityJoin,
CommunityJoinResponse,
CommunityResponse,
CreateCommunity,
DeleteCommunity,
FollowCommunity,
GetCommunity,
GetFollowedCommunities,
GetFollowedCommunitiesResponse,
ListCommunities,
ListCommunitiesResponse,
RemoveCommunity,
TransferCommunity,
} from './api/community';
import {
CreatePostLike,
DeletePost,
GetPost,
GetPosts,
GetPostsResponse,
LockPost,
PostJoin,
PostJoinResponse,
PostResponse,
RemovePost,
StickyPost,
} from './api/post';
import {
CreateSite,
EditSite,
GetModlog,
GetModlogResponse,
GetSite,
GetSiteConfig,
GetSiteConfigResponse,
GetSiteResponse,
ListCategoriesResponse,
Search,
SearchResponse,
SiteResponse,
TransferSite,
} from './api/site';
import {
AddAdmin,
AddAdminResponse,
BanUser,
BanUserResponse,
DeleteAccount,
DeletePrivateMessage,
EditPrivateMessage,
GetCaptchaResponse,
GetPrivateMessages,
GetReplies,
GetRepliesResponse,
GetUserDetails,
GetUserMentions,
GetUserMentionsResponse,
Login,
LoginResponse,
MarkPrivateMessageAsRead,
MarkUserMentionAsRead,
PasswordChange,
PasswordReset,
PrivateMessageResponse,
PrivateMessagesResponse,
Register,
SaveUserSettings,
UserMentionResponse,
} from './api/user';
import { PrivateMessage } from './source';
export enum UserOperation {
Login,
Register,
@ -173,92 +88,12 @@ export enum SearchType {
Url = 'Url',
}
export type MessageType =
| Login
| Register
| CreateCommunity
| GetCommunity
| DeleteCommunity
| RemoveCommunity
| FollowCommunity
| ListCommunities
| GetFollowedCommunities
| DeletePost
| RemovePost
| LockPost
| StickyPost
| GetPost
| GetPosts
| GetCommunity
| CreateComment
| EditComment
| DeleteComment
| RemoveComment
| MarkCommentAsRead
| SaveComment
| CreatePostLike
| BanFromCommunity
| AddAdmin
| AddModToCommunity
| TransferCommunity
| TransferSite
| SaveComment
| BanUser
| GetUserDetails
| GetReplies
| GetUserMentions
| MarkUserMentionAsRead
| GetModlog
| CreateSite
| EditSite
| GetSite
| Search
| SaveUserSettings
| DeleteAccount
| PasswordReset
| PasswordChange
| PrivateMessage
| EditPrivateMessage
| DeletePrivateMessage
| MarkPrivateMessageAsRead
| GetPrivateMessages
| GetSiteConfig
| PostJoin
| CommunityJoin;
type ResponseType =
| SiteResponse
| GetFollowedCommunitiesResponse
| ListCommunitiesResponse
| GetPostsResponse
| PostResponse
| GetRepliesResponse
| GetUserMentionsResponse
| ListCategoriesResponse
| CommunityResponse
| CommentResponse
| UserMentionResponse
| LoginResponse
| GetCaptchaResponse
| GetModlogResponse
| SearchResponse
| BanFromCommunityResponse
| AddModToCommunityResponse
| BanUserResponse
| AddAdminResponse
| PrivateMessageResponse
| PrivateMessagesResponse
| GetSiteConfigResponse
| GetSiteResponse
| PostJoinResponse
| CommunityJoinResponse;
export interface WebSocketResponse {
export interface WebSocketResponse<ResponseType> {
op: UserOperation;
data: ResponseType;
}
export interface WebSocketJsonResponse {
export interface WebSocketJsonResponse<ResponseType> {
op?: string;
data?: ResponseType;
error?: string;

View file

@ -66,7 +66,7 @@ import {
SaveUserSettings,
UserJoin,
} from './interfaces/api/user';
import { MessageType, UserOperation } from './interfaces/others';
import { UserOperation } from './interfaces/others';
export class LemmyWebsocket {
constructor() {}
@ -312,7 +312,7 @@ export class LemmyWebsocket {
}
}
function wrapper(op: UserOperation, data: MessageType) {
function wrapper<MessageType>(op: UserOperation, data: MessageType) {
let send = { op: UserOperation[op], data: data };
console.log(send);
return JSON.stringify(send);