import { env } from '$env/dynamic/public' import * as types from './types' export type ClientType = | { name: 'lemmy'; baseUrl: '/api/v3' } | { name: 'piefed'; baseUrl: '/api/alpha' } export const DEFAULT_CLIENT_TYPE: ClientType = env.PUBLIC_INSTANCE_TYPE == 'piefedalpha' ? { name: 'piefed', baseUrl: '/api/alpha', } : { name: 'lemmy', baseUrl: '/api/v3', } export abstract class BaseClient { abstract type: ClientType static constants: { password: { minLength: number maxLength: number } } static async fetchInfo( base: string | URL, ): Promise<{ type: ClientType; version: string } | null> { try { const res = await fetch(new URL('/nodeinfo/2.1', base)) if (!res.ok) return null const software: { name: string; version: string } = (await res.json()) .software // should probably extract this into not a static function but its ok switch (software.name) { case 'lemmy': { return { type: { baseUrl: '/api/v3', name: 'lemmy' }, version: software.version, } } case 'piefed': { return { type: { baseUrl: '/api/alpha', name: 'piefed' }, version: software.version, } } default: return null } } catch (err) { console.error(err) return null } } abstract getSite(): Promise abstract editSite(form: types.EditSite): Promise abstract generateTotpSecret(): Promise abstract listLogins(): Promise abstract listAllMedia(form: types.ListMedia): Promise abstract updateTotp(form: types.UpdateTotp): Promise abstract getModlog(form: types.GetModlog): Promise abstract search(form: types.Search): Promise abstract resolveObject( form: types.ResolveObject, ): Promise abstract createCommunity( form: types.CreateCommunity, ): Promise abstract getCommunity( form: types.GetCommunity, ): Promise abstract editCommunity( form: types.EditCommunity, ): Promise abstract listCommunities( form: types.ListCommunities, ): Promise abstract followCommunity( form: types.FollowCommunity, ): Promise abstract blockCommunity( form: types.BlockCommunity, ): Promise abstract deleteCommunity( form: types.DeleteCommunity, ): Promise abstract hideCommunity( form: types.HideCommunity, ): Promise abstract removeCommunity( form: types.RemoveCommunity, ): Promise abstract banFromCommunity( form: types.BanFromCommunity, ): Promise abstract addModToCommunity( form: types.AddModToCommunity, ): Promise abstract createPost(form: types.CreatePost): Promise abstract getPost(form: types.GetPost): Promise abstract editPost(form: types.EditPost): Promise abstract deletePost(form: types.DeletePost): Promise abstract removePost(form: types.RemovePost): Promise abstract markPostAsRead( form: types.MarkPostAsRead, ): Promise abstract hidePost(form: types.HidePost): Promise abstract lockPost(form: types.LockPost): Promise abstract featurePost(form: types.FeaturePost): Promise abstract getPosts(form: types.GetPosts): Promise abstract likePost(form: types.CreatePostLike): Promise abstract listPostLikes( form: types.ListPostLikes, ): Promise abstract savePost(form: types.SavePost): Promise // TODO should unify these reports probably abstract createPostReport( form: types.CreatePostReport, ): Promise abstract resolvePostReport( form: types.ResolvePostReport, ): Promise abstract listPostReports( form: types.ListPostReports, ): Promise abstract getSiteMetadata( form: types.GetSiteMetadata, ): Promise abstract createComment( form: types.CreateComment, ): Promise abstract editComment(form: types.EditComment): Promise abstract deleteComment( form: types.DeleteComment, ): Promise abstract removeComment( form: types.RemoveComment, ): Promise abstract markCommentReplyAsRead( form: types.MarkCommentReplyAsRead, ): Promise abstract likeComment( form: types.CreateCommentLike, ): Promise abstract listCommentLikes( form: types.ListCommentLikes, ): Promise // TODO unify saving with posts too // i have options for unifying these since I'm making the API client myself now abstract saveComment(form: types.SaveComment): Promise abstract distinguishComment( form: types.DistinguishComment, ): Promise abstract getComments( form: types.GetComments, ): Promise abstract getComment(form: types.GetComment): Promise abstract createCommentReport( form: types.CreateCommentReport, ): Promise abstract resolveCommentReport( form: types.ResolveCommentReport, ): Promise abstract listCommentReports( form: types.ListCommentReports, ): Promise abstract getPrivateMessages( form: types.GetPrivateMessages, ): Promise abstract createPrivateMessage( form: types.CreatePrivateMessage, ): Promise abstract editPrivateMessage( form: types.EditPrivateMessage, ): Promise abstract deletePrivateMessage( form: types.DeletePrivateMessage, ): Promise abstract markPrivateMessageAsRead( form: types.MarkPrivateMessageAsRead, ): Promise abstract createPrivateMessageReport( form: types.CreatePrivateMessageReport, ): Promise abstract resolvePrivateMessageReport( form: types.ResolvePrivateMessageReport, ): Promise abstract listPrivateMessageReports( form: types.ListPrivateMessageReports, ): Promise abstract register(form: types.Register): Promise abstract login(form: types.Login): Promise abstract logout(): Promise abstract getPersonDetails( form: types.GetPersonDetails, ): Promise // TODO unify these inbox things too abstract getPersonMentions( form: types.GetPersonMentions, ): Promise abstract markPersonMentionAsRead( form: types.MarkPersonMentionAsRead, ): Promise abstract getReplies(form: types.GetReplies): Promise abstract banPerson(form: types.BanPerson): Promise abstract getBannedPersons(): Promise abstract blockPerson( form: types.BlockPerson, ): Promise abstract getCaptcha(): Promise abstract deleteAccount( form: types.DeleteAccount, ): Promise abstract passwordReset( form: types.PasswordReset, ): Promise abstract passwordChangeAfterReset( form: types.PasswordChangeAfterReset, ): Promise abstract markAllAsRead(): Promise abstract saveUserSettings( form: types.SaveUserSettings, ): Promise abstract changePassword( form: types.ChangePassword, ): Promise abstract getReportCount( form: types.GetReportCount, ): Promise abstract getUnreadCount(): Promise abstract verifyEmail(form: types.VerifyEmail): Promise abstract addAdmin(form: types.AddAdmin): Promise abstract getUnreadRegistrationApplicationCount(): Promise abstract listRegistrationApplications( form: types.ListRegistrationApplications, ): Promise abstract approveRegistrationApplication( form: types.ApproveRegistrationApplication, ): Promise abstract getRegistrationApplication( form: types.GetRegistrationApplication, ): Promise abstract purgePerson(form: types.PurgePerson): Promise abstract purgeCommunity( form: types.PurgeCommunity, ): Promise abstract purgePost(form: types.PurgePost): Promise abstract purgeComment( form: types.PurgeComment, ): Promise abstract getFederatedInstances(): Promise abstract blockInstance( form: types.BlockInstance, ): Promise abstract uploadImage( form: types.UploadImage, ): Promise abstract deleteImage(form: types.DeleteImage): Promise abstract setFlair?(form: types.SetPersonFlair): Promise abstract getFeeds?(form: types.GetFeeds): Promise abstract getTopics?(form: types.GetTopics): Promise abstract assignFlair?(form: types.AssignFlair): Promise abstract listMedia(form: types.ListMedia): Promise abstract voteOnPoll?(form: types.PollVote): Promise abstract setNote?(form: types.SetNote): Promise } export type NullableFnArg = T extends ( ...args: infer A ) => any ? A : Fallback export type NullableFnReturn = T extends ( ...args: any[] ) => infer A ? A : Fallback