From 3888ab8f1ae9f8f67568e6cf0e70e041a3c8f632 Mon Sep 17 00:00:00 2001 From: Xylight Date: Sat, 7 Feb 2026 20:33:53 -0800 Subject: [PATCH 1/6] refactor: switch LemmyClient to a proxy to remove boilerplate methods --- src/lib/api/client.svelte.ts | 12 +- src/lib/api/lemmy/adapter.ts | 546 ++++------------------------ src/lib/ui/layout/pages/Tabs.svelte | 2 - 3 files changed, 72 insertions(+), 488 deletions(-) diff --git a/src/lib/api/client.svelte.ts b/src/lib/api/client.svelte.ts index db9f26ce..85f9eafd 100644 --- a/src/lib/api/client.svelte.ts +++ b/src/lib/api/client.svelte.ts @@ -80,13 +80,11 @@ export function client({ // but not here, so that if jwt == '', it doesnt put a bearer const headers = jwt ? { authorization: `Bearer ${jwt}` } : {} - return new (clientType.name == 'piefed' ? PiefedClient : LemmyClient)( - instanceToURL(instanceURL), - { - fetchFunction: (input, init) => customFetch(func, input, init, jwt), - headers: headers, - }, - ) + const Client = clientType.name == 'piefed' ? PiefedClient : LemmyClient + return new Client(instanceToURL(instanceURL), { + fetchFunction: (input, init) => customFetch(func, input, init, jwt), + headers: headers, + }) as BaseClient } // here for parts where i forgor to switch diff --git a/src/lib/api/lemmy/adapter.ts b/src/lib/api/lemmy/adapter.ts index 4139f806..172b3967 100644 --- a/src/lib/api/lemmy/adapter.ts +++ b/src/lib/api/lemmy/adapter.ts @@ -2,12 +2,68 @@ import { LemmyHttp } from 'lemmy-js-client' import type { BaseClient, ClientType } from '../base' import { fromGetPosts, toListingType } from './rewrite' -export class LemmyClient implements BaseClient { - type: ClientType = { name: 'lemmy', baseUrl: '/api/v3' } +export const LemmyClientConstants = { + password: { minLength: 8, maxLength: 60 }, +} - static constants = { password: { minLength: 8, maxLength: 60 } } +export function createLemmyClient( + baseUrl: string, + args: { + fetchFunction: (input: any, init: any) => Promise + headers: any + }, +): BaseClient { + const client = new LemmyHttp(baseUrl, args) - #client: LemmyHttp + const overrides: Partial = { + type: { name: 'lemmy', baseUrl: '/api/v3' } as ClientType, + + async search(params) { + return await client.search({ + ...params, + listing_type: toListingType(params.listing_type), + } as any) + }, + + async listCommunities(params) { + return await client.listCommunities({ + ...params, + type_: toListingType(params.type_), + } as any) + }, + + async getPosts(params) { + return (await client.getPosts(fromGetPosts(params) as any)) as any + }, + + async getComments(params) { + return await client.getComments({ + ...params, + type_: toListingType(params.type_), + } as any) + }, + } + + return new Proxy(overrides as BaseClient, { + get: (target, prop, receiver) => { + const value = Reflect.get(target, prop, receiver) + if (value !== undefined) { + return value + } + + const clientValue = (client as any)[prop] + if (typeof clientValue === 'function') { + return clientValue.bind(client) + } + return clientValue + }, + }) +} + +export class LemmyClient { + static constants = LemmyClientConstants + + #proxy: BaseClient constructor( baseUrl: string, @@ -16,481 +72,13 @@ export class LemmyClient implements BaseClient { headers: any }, ) { - this.#client = new LemmyHttp(baseUrl, args) - } + this.#proxy = createLemmyClient(baseUrl, args) - async getSite( - ...params: Parameters - ): ReturnType { - return await this.#client.getSite(...params) - } - async generateTotpSecret( - ...params: Parameters - ): ReturnType { - return await this.#client.generateTotpSecret(...params) - } - async listLogins( - ...params: Parameters - ): ReturnType { - return await this.#client.listLogins(...params) - } - async listAllMedia( - ...params: Parameters - ): ReturnType { - return await this.#client.listAllMedia(...params) - } - async updateTotp( - ...params: Parameters - ): ReturnType { - return await this.#client.updateTotp(...params) - } - async getModlog( - ...params: Parameters - ): ReturnType { - return await this.#client.getModlog(...params) - } - async search( - params: Parameters[0], - ): ReturnType { - return await this.#client.search({ - ...params, - listing_type: toListingType(params.listing_type), - }) - } - async resolveObject( - ...params: Parameters - ): ReturnType { - return await this.#client.resolveObject(...params) - } - async createCommunity( - ...params: Parameters - ): ReturnType { - return await this.#client.createCommunity(...params) - } - async getCommunity( - ...params: Parameters - ): ReturnType { - return await this.#client.getCommunity(...params) - } - async editCommunity( - ...params: Parameters - ): ReturnType { - return await this.#client.editCommunity(...params) - } - async listCommunities( - params: Parameters[0], - ): ReturnType { - return await this.#client.listCommunities({ - ...params, - type_: toListingType(params.type_), - }) - } - async followCommunity( - ...params: Parameters - ): ReturnType { - return await this.#client.followCommunity(...params) - } - async blockCommunity( - ...params: Parameters - ): ReturnType { - return await this.#client.blockCommunity(...params) - } - async deleteCommunity( - ...params: Parameters - ): ReturnType { - return await this.#client.deleteCommunity(...params) - } - async hideCommunity( - ...params: Parameters - ): ReturnType { - return await this.#client.hideCommunity(...params) - } - async removeCommunity( - ...params: Parameters - ): ReturnType { - return await this.#client.removeCommunity(...params) - } - async banFromCommunity( - ...params: Parameters - ): ReturnType { - return await this.#client.banFromCommunity(...params) - } - async addModToCommunity( - ...params: Parameters - ): ReturnType { - return await this.#client.addModToCommunity(...params) - } - async createPost( - ...params: Parameters - ): ReturnType { - return await this.#client.createPost(...params) - } - async getPost( - ...params: Parameters - ): ReturnType { - return await this.#client.getPost(...params) - } - async editPost( - ...params: Parameters - ): ReturnType { - return await this.#client.editPost(...params) - } - async deletePost( - ...params: Parameters - ): ReturnType { - return await this.#client.deletePost(...params) - } - async removePost( - ...params: Parameters - ): ReturnType { - return await this.#client.removePost(...params) - } - async markPostAsRead( - ...params: Parameters - ): ReturnType { - return await this.#client.markPostAsRead(...params) - } - async hidePost( - ...params: Parameters - ): ReturnType { - return await this.#client.hidePost(...params) - } - async lockPost( - ...params: Parameters - ): ReturnType { - return await this.#client.lockPost(...params) - } - async featurePost( - ...params: Parameters - ): ReturnType { - return await this.#client.featurePost(...params) - } - async getPosts( - params: Parameters[0], - ): ReturnType { - return await this.#client.getPosts(fromGetPosts(params)) - } - async likePost( - ...params: Parameters - ): ReturnType { - return await this.#client.likePost(...params) - } - async listPostLikes( - ...params: Parameters - ): ReturnType { - return await this.#client.listPostLikes(...params) - } - async savePost( - ...params: Parameters - ): ReturnType { - return await this.#client.savePost(...params) - } - async createPostReport( - ...params: Parameters - ): ReturnType { - return await this.#client.createPostReport(...params) - } - async resolvePostReport( - ...params: Parameters - ): ReturnType { - return await this.#client.resolvePostReport(...params) - } - async listPostReports( - ...params: Parameters - ): ReturnType { - return await this.#client.listPostReports(...params) - } - async getSiteMetadata( - ...params: Parameters - ): ReturnType { - return await this.#client.getSiteMetadata(...params) - } - async createComment( - ...params: Parameters - ): ReturnType { - return await this.#client.createComment(...params) - } - async editComment( - ...params: Parameters - ): ReturnType { - return await this.#client.editComment(...params) - } - async deleteComment( - ...params: Parameters - ): ReturnType { - return await this.#client.deleteComment(...params) - } - async removeComment( - ...params: Parameters - ): ReturnType { - return await this.#client.removeComment(...params) - } - async markCommentReplyAsRead( - ...params: Parameters - ): ReturnType { - return await this.#client.markCommentReplyAsRead(...params) - } - async likeComment( - ...params: Parameters - ): ReturnType { - return await this.#client.likeComment(...params) - } - async listCommentLikes( - ...params: Parameters - ): ReturnType { - return await this.#client.listCommentLikes(...params) - } - async saveComment( - ...params: Parameters - ): ReturnType { - return await this.#client.saveComment(...params) - } - async distinguishComment( - ...params: Parameters - ): ReturnType { - return await this.#client.distinguishComment(...params) - } - async getComments( - params: Parameters[0], - ): ReturnType { - return await this.#client.getComments({ - ...params, - type_: toListingType(params.type_), - }) - } - async getComment( - ...params: Parameters - ): ReturnType { - return await this.#client.getComment(...params) - } - async createCommentReport( - ...params: Parameters - ): ReturnType { - return await this.#client.createCommentReport(...params) - } - async resolveCommentReport( - ...params: Parameters - ): ReturnType { - return await this.#client.resolveCommentReport(...params) - } - async listCommentReports( - ...params: Parameters - ): ReturnType { - return await this.#client.listCommentReports(...params) - } - async getPrivateMessages( - ...params: Parameters - ): ReturnType { - return await this.#client.getPrivateMessages(...params) - } - async createPrivateMessage( - ...params: Parameters - ): ReturnType { - return await this.#client.createPrivateMessage(...params) - } - async editPrivateMessage( - ...params: Parameters - ): ReturnType { - return await this.#client.editPrivateMessage(...params) - } - async deletePrivateMessage( - ...params: Parameters - ): ReturnType { - return await this.#client.deletePrivateMessage(...params) - } - async markPrivateMessageAsRead( - ...params: Parameters - ): ReturnType { - return await this.#client.markPrivateMessageAsRead(...params) - } - async createPrivateMessageReport( - ...params: Parameters - ): ReturnType { - return await this.#client.createPrivateMessageReport(...params) - } - async resolvePrivateMessageReport( - ...params: Parameters - ): ReturnType { - return await this.#client.resolvePrivateMessageReport(...params) - } - async listPrivateMessageReports( - ...params: Parameters - ): ReturnType { - return await this.#client.listPrivateMessageReports(...params) - } - async register( - ...params: Parameters - ): ReturnType { - return await this.#client.register(...params) - } - async login( - ...params: Parameters - ): ReturnType { - return await this.#client.login(...params) - } - async logout( - ...params: Parameters - ): ReturnType { - return await this.#client.logout(...params) - } - async getPersonDetails( - ...params: Parameters - ): ReturnType { - return await this.#client.getPersonDetails(...params) - } - async getPersonMentions( - ...params: Parameters - ): ReturnType { - return await this.#client.getPersonMentions(...params) - } - async markPersonMentionAsRead( - ...params: Parameters - ): ReturnType { - return await this.#client.markPersonMentionAsRead(...params) - } - async getReplies( - ...params: Parameters - ): ReturnType { - return await this.#client.getReplies(...params) - } - async banPerson( - ...params: Parameters - ): ReturnType { - return await this.#client.banPerson(...params) - } - async getBannedPersons( - ...params: Parameters - ): ReturnType { - return await this.#client.getBannedPersons(...params) - } - async blockPerson( - ...params: Parameters - ): ReturnType { - return await this.#client.blockPerson(...params) - } - async getCaptcha( - ...params: Parameters - ): ReturnType { - return await this.#client.getCaptcha(...params) - } - async deleteAccount( - ...params: Parameters - ): ReturnType { - return await this.#client.deleteAccount(...params) - } - async passwordReset( - ...params: Parameters - ): ReturnType { - return await this.#client.passwordReset(...params) - } - async passwordChangeAfterReset( - ...params: Parameters - ): ReturnType { - return await this.#client.passwordChangeAfterReset(...params) - } - async markAllAsRead( - ...params: Parameters - ): ReturnType { - return await this.#client.markAllAsRead(...params) - } - async saveUserSettings( - ...params: Parameters - ): ReturnType { - return await this.#client.saveUserSettings(...params) - } - async changePassword( - ...params: Parameters - ): ReturnType { - return await this.#client.changePassword(...params) - } - async getReportCount( - ...params: Parameters - ): ReturnType { - return await this.#client.getReportCount(...params) - } - async getUnreadCount( - ...params: Parameters - ): ReturnType { - return await this.#client.getUnreadCount(...params) - } - async verifyEmail( - ...params: Parameters - ): ReturnType { - return await this.#client.verifyEmail(...params) - } - async addAdmin( - ...params: Parameters - ): ReturnType { - return await this.#client.addAdmin(...params) - } - async getUnreadRegistrationApplicationCount( - ...params: Parameters - ): ReturnType { - return await this.#client.getUnreadRegistrationApplicationCount(...params) - } - async listRegistrationApplications( - ...params: Parameters - ): ReturnType { - return await this.#client.listRegistrationApplications(...params) - } - async approveRegistrationApplication( - ...params: Parameters - ): ReturnType { - return await this.#client.approveRegistrationApplication(...params) - } - async getRegistrationApplication( - ...params: Parameters - ): ReturnType { - return await this.#client.getRegistrationApplication(...params) - } - async purgePerson( - ...params: Parameters - ): ReturnType { - return await this.#client.purgePerson(...params) - } - async purgeCommunity( - ...params: Parameters - ): ReturnType { - return await this.#client.purgeCommunity(...params) - } - async purgePost( - ...params: Parameters - ): ReturnType { - return await this.#client.purgePost(...params) - } - async purgeComment( - ...params: Parameters - ): ReturnType { - return await this.#client.purgeComment(...params) - } - async getFederatedInstances( - ...params: Parameters - ): ReturnType { - return await this.#client.getFederatedInstances(...params) - } - async blockInstance( - ...params: Parameters - ): ReturnType { - return await this.#client.blockInstance(...params) - } - async uploadImage( - ...params: Parameters - ): ReturnType { - return await this.#client.uploadImage(...params) - } - async editSite( - ...params: Parameters - ): ReturnType { - return await this.#client.editSite(...params) - } - async deleteImage( - ...params: Parameters - ): ReturnType { - return await this.#client.deleteImage(...params) - } - async listMedia( - ...params: Parameters - ): ReturnType { - return await this.#client.listMedia(...params) + return new Proxy(this, { + get: (target, prop) => { + if (prop === 'constructor') return LemmyClient + return (target.#proxy as any)[prop] + }, + }) as any } } diff --git a/src/lib/ui/layout/pages/Tabs.svelte b/src/lib/ui/layout/pages/Tabs.svelte index 8f532a6d..080e10d0 100644 --- a/src/lib/ui/layout/pages/Tabs.svelte +++ b/src/lib/ui/layout/pages/Tabs.svelte @@ -1,5 +1,4 @@ -
+
{#if meta}
@@ -68,7 +64,6 @@
{/if} From 491985fb50885cd074b76fbebf25b2556f154566 Mon Sep 17 00:00:00 2001 From: Xylight Date: Sun, 8 Feb 2026 11:46:28 -0800 Subject: [PATCH 6/6] cleanup: remove useless 'transforms' src/lib/api/piefed/adapter.ts --- src/lib/api/piefed/adapter.ts | 70 +++++++++++++++++------------------ 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/src/lib/api/piefed/adapter.ts b/src/lib/api/piefed/adapter.ts index 323fb202..f47c3f99 100644 --- a/src/lib/api/piefed/adapter.ts +++ b/src/lib/api/piefed/adapter.ts @@ -92,32 +92,6 @@ function assertData(response: { data?: T; error?: unknown }): T { return response.data } -const transforms = { - postView: (r: any) => ({ post_view: toPostView(r.post_view) }), - - commentView: (r: any) => ({ - comment_view: toCommentView(r.comment_view), - recipient_ids: [], - }), - - communityView: (r: any) => ({ - ...r, - community_view: toCommunityView(r.community_view), - }), - - privateMessageView: (r: any) => ({ - ...r, - private_message_view: toPrivateMessageView(r.private_message_view), - }), - - moderators: (r: any) => ({ - moderators: r.moderators.map((i: any) => ({ - community: toCommunity(i.community), - moderator: toPerson(i.moderator), - })), - }), -} - const methods: MethodDefinitions = { getSite: { method: 'GET', @@ -177,7 +151,9 @@ const methods: MethodDefinitions = { method: 'POST', path: '/api/alpha/post', body: fromCreatePost, - transform: transforms.postView, + transform: (r: piefed['GetPostResponse']) => ({ + post_view: toPostView(r.post_view), + }), }, getPost: { @@ -198,19 +174,25 @@ const methods: MethodDefinitions = { editPost: { method: 'PUT', path: '/api/alpha/post', - transform: transforms.postView, + transform: (r: piefed['GetPostResponse']) => ({ + post_view: toPostView(r.post_view), + }), }, deletePost: { method: 'POST', path: '/api/alpha/post/delete', - transform: transforms.postView, + transform: (r: piefed['GetPostResponse']) => ({ + post_view: toPostView(r.post_view), + }), }, removePost: { method: 'POST', path: '/api/alpha/post/remove', - transform: transforms.postView, + transform: (r: piefed['GetPostResponse']) => ({ + post_view: toPostView(r.post_view), + }), }, getPosts: { @@ -227,7 +209,9 @@ const methods: MethodDefinitions = { likePost: { method: 'POST', path: '/api/alpha/post/like', - transform: transforms.postView, + transform: (r: piefed['GetPostResponse']) => ({ + post_view: toPostView(r.post_view), + }), }, markPostAsRead: { @@ -239,19 +223,25 @@ const methods: MethodDefinitions = { lockPost: { method: 'POST', path: '/api/alpha/post/lock', - transform: transforms.postView, + transform: (r: piefed['GetPostResponse']) => ({ + post_view: toPostView(r.post_view), + }), }, featurePost: { method: 'POST', path: '/api/alpha/post/feature', - transform: transforms.postView, + transform: (r: piefed['GetPostResponse']) => ({ + post_view: toPostView(r.post_view), + }), }, savePost: { method: 'PUT', path: '/api/alpha/post/save', - transform: transforms.postView, + transform: (r: piefed['GetPostResponse']) => ({ + post_view: toPostView(r.post_view), + }), }, listPostLikes: { @@ -350,7 +340,10 @@ const methods: MethodDefinitions = { method: 'POST', path: '/api/alpha/comment/like', body: (p) => ({ ...p, private: false }), - transform: transforms.commentView, + transform: (r: piefed['GetCommentResponse']) => ({ + comment_view: toCommentView(r.comment_view), + recipient_ids: [], + }), }, listCommentLikes: { @@ -548,7 +541,12 @@ const methods: MethodDefinitions = { addModToCommunity: { method: 'POST', path: '/api/alpha/community/mod', - transform: transforms.moderators, + transform: (r: piefed['ModCommunityResponse']) => ({ + moderators: r.moderators.map((i) => ({ + community: toCommunity(i.community), + moderator: toPerson(i.moderator), + })), + }), }, search: {