diff --git a/src/BaseClient.ts b/src/BaseClient.ts index 588d574..c5fa27a 100644 --- a/src/BaseClient.ts +++ b/src/BaseClient.ts @@ -1,8 +1,8 @@ import type * as types from "./types"; export interface BaseClientOptions { - fetchFunction: typeof fetch; - headers: Record; + fetchFunction?: typeof fetch; + headers?: Record; } export interface ProviderInfo { @@ -26,82 +26,82 @@ export abstract class BaseClient { abstract banFromCommunity( payload: types.BanFromCommunity, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract blockCommunity( payload: { block: boolean; community_id: number }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ community_view: types.CommunityView }>; abstract blockInstance( payload: { block: boolean; instance_id: number }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract blockPerson( payload: { block: boolean; person_id: number }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ person_view: types.PersonView }>; abstract createComment( payload: types.CreateComment, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ comment_view: types.CommentView }>; abstract createCommentReport( payload: { comment_id: number; reason: string }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract createPost( payload: types.CreatePost, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: types.PostView }>; abstract createPostReport( payload: { post_id: number; reason: string }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract createPrivateMessage( payload: { content: string; recipient_id: number }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ private_message_view: types.PrivateMessageView }>; abstract createPrivateMessageReport( payload: { private_message_id: number; reason: string }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract deleteComment( payload: { comment_id: number; deleted: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ comment_view: types.CommentView }>; abstract deleteImage( payload: { delete_token: string; url: string }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract deletePost( payload: { deleted: boolean; post_id: number }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: types.PostView }>; abstract distinguishComment( payload: { comment_id: number; distinguished: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ comment_view: types.CommentView }>; abstract editComment( payload: types.EditComment, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ comment_view: types.CommentView }>; abstract editPost( payload: types.EditPost, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: types.PostView }>; abstract featurePost( @@ -110,26 +110,26 @@ export abstract class BaseClient { featured: boolean; post_id: number; }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: types.PostView }>; abstract followCommunity( payload: { community_id: number; follow: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ community_view: types.CommunityView }>; abstract getCaptcha( - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract getComments( payload: types.GetComments, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract getCommunity( payload: types.GetCommunity, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract getFederatedInstances(options?: RequestOptions): Promise<{ @@ -138,108 +138,108 @@ export abstract class BaseClient { abstract getModlog( payload: types.GetModlog, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract getNotifications( payload: types.GetPersonMentions, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract getPersonDetails( payload: { person_id: number } | { username: string }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract getPersonMentions( payload: types.GetPersonMentions, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract getPost( payload: types.GetPost, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: types.PostView }>; abstract getPosts( payload: types.GetPosts, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract getPrivateMessages( payload: types.GetPrivateMessages, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract getRandomCommunity( payload: { type_: types.ListingType }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ community_view: types.CommunityView }>; abstract getReplies( payload: types.GetReplies, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract getSite(options?: RequestOptions): Promise; abstract getSiteMetadata( payload: { url: string }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract getUnreadCount( - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract likeComment( payload: { comment_id: number; score: number }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ comment_view: types.CommentView }>; abstract likePost( payload: { post_id: number; score: number }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: types.PostView }>; abstract listCommentReports( payload: types.PageParams & { unresolved_only?: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract listCommunities( payload: types.ListCommunities, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract listPersonContent( payload: types.ListPersonContent, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract listPersonLiked( payload: types.PageParams & { type: types.LikeType }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract listPersonSaved( payload: types.PageParams & { person_id: number }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract listPostReports( payload: types.PageParams & { unresolved_only?: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract listReports( payload: types.ListReports, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract lockPost( payload: { locked: boolean; post_id: number }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: types.PostView }>; abstract login( @@ -248,7 +248,7 @@ export abstract class BaseClient { totp_2fa_token?: string; username_or_email: string; }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ jwt?: string }>; abstract logout(options?: RequestOptions): Promise; @@ -257,78 +257,78 @@ export abstract class BaseClient { abstract markCommentReplyAsRead( payload: { comment_reply_id: number; read: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract markPersonMentionAsRead( payload: { person_mention_id: number; read: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract markPostAsRead( payload: { post_ids: number[]; read: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract markPrivateMessageAsRead( payload: { private_message_id: number; read: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract register( payload: types.Register, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract removeComment( payload: { comment_id: number; reason?: string; removed: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ comment_view: types.CommentView }>; abstract removePost( payload: { post_id: number; reason?: string; removed: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: types.PostView }>; abstract resolveCommentReport( payload: { report_id: number; resolved: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract resolveObject( payload: { q: string; }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract resolvePostReport( payload: { report_id: number; resolved: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract saveComment( payload: { comment_id: number; save: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ comment_view: types.CommentView }>; abstract savePost( payload: { post_id: number; save: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: types.PostView }>; abstract saveUserSettings( payload: { show_nsfw: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract search( payload: types.Search, - options?: RequestOptions, + options?: RequestOptions ): Promise; abstract uploadImage( payload: { file: File }, - options?: RequestOptions, + options?: RequestOptions ): Promise; } diff --git a/src/providers/piefed/index.ts b/src/providers/piefed/index.ts index 066a839..6e95f91 100644 --- a/src/providers/piefed/index.ts +++ b/src/providers/piefed/index.ts @@ -50,12 +50,12 @@ export class UnsafePiefedClient implements BaseClient { #url: string; constructor(url: string, options: BaseClientOptions) { - this.#customFetch = options.fetchFunction; + this.#customFetch = options.fetchFunction ?? globalThis.fetch; this.#url = url; - const headers = options.headers.Authorization + const headers = options.headers?.Authorization ? { - Authorization: options.headers.Authorization, + Authorization: options.headers?.Authorization, } : undefined; @@ -73,7 +73,7 @@ export class UnsafePiefedClient implements BaseClient { async banFromCommunity( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { await this.#client.POST("/community/moderate/ban", { ...options, @@ -84,7 +84,7 @@ export class UnsafePiefedClient implements BaseClient { async blockCommunity( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.POST("/community/block", { ...options, @@ -98,7 +98,7 @@ export class UnsafePiefedClient implements BaseClient { async blockInstance( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { await this.#client.POST("/site/block", { ...options, @@ -108,7 +108,7 @@ export class UnsafePiefedClient implements BaseClient { async blockPerson( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.POST("/user/block", { ...options, @@ -123,7 +123,7 @@ export class UnsafePiefedClient implements BaseClient { async createComment( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.POST("/comment", { ...options, @@ -140,7 +140,7 @@ export class UnsafePiefedClient implements BaseClient { async createCommentReport( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { await this.#client.POST("/comment/report", { ...options, @@ -150,7 +150,7 @@ export class UnsafePiefedClient implements BaseClient { async createPost( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ) { const response = await this.#client.POST("/post", { ...options, @@ -167,7 +167,7 @@ export class UnsafePiefedClient implements BaseClient { async createPostReport( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { await this.#client.POST("/post/report", { ...options, @@ -177,7 +177,7 @@ export class UnsafePiefedClient implements BaseClient { async createPrivateMessage( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.POST("/private_message", { ...options, @@ -186,7 +186,7 @@ export class UnsafePiefedClient implements BaseClient { return { private_message_view: compat.toPrivateMessageView( - response.data!.private_message_view, + response.data!.private_message_view ), }; } @@ -195,13 +195,13 @@ export class UnsafePiefedClient implements BaseClient { ..._params: Parameters ): ReturnType { throw new UnsupportedError( - "Create private message report is not supported by piefed", + "Create private message report is not supported by piefed" ); } async deleteComment( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.POST("/comment/delete", { ...options, @@ -221,7 +221,7 @@ export class UnsafePiefedClient implements BaseClient { async deletePost( payload: { deleted: boolean; post_id: number }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: PostView }> { const response = await this.#client.POST("/post/delete", { ...options, @@ -237,13 +237,13 @@ export class UnsafePiefedClient implements BaseClient { ..._params: Parameters ): ReturnType { throw new UnsupportedError( - "Distinguish comment is not supported by piefed", + "Distinguish comment is not supported by piefed" ); } async editComment( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.PUT("/comment", { ...options, @@ -260,7 +260,7 @@ export class UnsafePiefedClient implements BaseClient { async editPost( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ) { const response = await this.#client.PUT("/post", { ...options, @@ -277,7 +277,7 @@ export class UnsafePiefedClient implements BaseClient { async featurePost( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: PostView }> { const response = await this.#client.POST("/post/feature", { ...options, @@ -291,7 +291,7 @@ export class UnsafePiefedClient implements BaseClient { async followCommunity( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.POST("/community/follow", { ...options, @@ -311,15 +311,15 @@ export class UnsafePiefedClient implements BaseClient { async getComments( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { if (payload.mode && payload.mode !== "piefed") throw new InvalidPayloadError( - `Connected to piefed, ${payload.mode} is not supported`, + `Connected to piefed, ${payload.mode} is not supported` ); const query = cleanThreadiverseParams( - compat.fromPageParams(payload), + compat.fromPageParams(payload) ) satisfies components["schemas"]["GetComments"]; const response = await this.#client.GET("/comment/list", { @@ -336,7 +336,7 @@ export class UnsafePiefedClient implements BaseClient { async getCommunity( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.GET("/community", { ...options, @@ -377,7 +377,7 @@ export class UnsafePiefedClient implements BaseClient { ].sort( (a, b) => Date.parse(getInboxItemPublished(b)) - - Date.parse(getInboxItemPublished(a)), + Date.parse(getInboxItemPublished(a)) ); return { @@ -388,7 +388,7 @@ export class UnsafePiefedClient implements BaseClient { async getPersonDetails( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.GET("/user", { ...options, @@ -405,7 +405,7 @@ export class UnsafePiefedClient implements BaseClient { async getPersonMentions( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.GET("/user/mentions", { ...options, @@ -421,7 +421,7 @@ export class UnsafePiefedClient implements BaseClient { async getPost( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ) { const query = payload satisfies components["schemas"]["GetPost"]; @@ -438,15 +438,15 @@ export class UnsafePiefedClient implements BaseClient { async getPosts( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { if (payload.mode && payload.mode !== "piefed") throw new InvalidPayloadError( - `Connected to piefed, ${payload.mode} is not supported`, + `Connected to piefed, ${payload.mode} is not supported` ); const query = cleanThreadiverseParams( - compat.fromPageParams(payload), + compat.fromPageParams(payload) ) satisfies components["schemas"]["GetPosts"]; const response = await this.#client.GET("/post/list", { @@ -463,7 +463,7 @@ export class UnsafePiefedClient implements BaseClient { async getPrivateMessages( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.GET("/private_message/list", { ...options, @@ -481,13 +481,13 @@ export class UnsafePiefedClient implements BaseClient { ..._params: Parameters ): ReturnType { throw new UnsupportedError( - "Get random community is not supported by piefed", + "Get random community is not supported by piefed" ); } async getReplies( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.GET("/user/replies", { ...options, @@ -512,14 +512,14 @@ export class UnsafePiefedClient implements BaseClient { ? { ...response.data!.my_user, community_blocks: response.data!.my_user?.community_blocks.map( - ({ community }) => compat.toCommunity(community), + ({ community }) => compat.toCommunity(community) ), follows: response.data!.my_user.follows.map((f) => ({ community: compat.toCommunity(f.community), follower: compat.toPerson(f.follower), })), instance_blocks: response.data!.my_user?.instance_blocks.map( - ({ instance }) => instance, + ({ instance }) => instance ), local_user_view: { ...response.data!.my_user.local_user_view, @@ -529,14 +529,14 @@ export class UnsafePiefedClient implements BaseClient { response.data!.my_user.local_user_view.local_user.show_nsfw, }, person: compat.toPerson( - response.data!.my_user.local_user_view.person, + response.data!.my_user.local_user_view.person ), }, moderates: response.data!.my_user.moderates.map( - compat.toCommunityModeratorView, + compat.toCommunityModeratorView ), person_blocks: response.data!.my_user?.person_blocks.map( - ({ target }) => compat.toPerson(target), + ({ target }) => compat.toPerson(target) ), } : undefined, @@ -563,7 +563,7 @@ export class UnsafePiefedClient implements BaseClient { async likeComment( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.POST("/comment/like", { ...options, @@ -579,7 +579,7 @@ export class UnsafePiefedClient implements BaseClient { async likePost( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ) { const response = await this.#client.POST("/post/like", { ...options, @@ -597,13 +597,13 @@ export class UnsafePiefedClient implements BaseClient { ..._params: Parameters ): ReturnType { throw new UnsupportedError( - "List comment reports is not supported by piefed", + "List comment reports is not supported by piefed" ); } async listCommunities( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.GET("/community/list", { ...options, @@ -619,7 +619,7 @@ export class UnsafePiefedClient implements BaseClient { async listPersonContent( payload: ListPersonContent, - options?: RequestOptions, + options?: RequestOptions ): Promise { switch (payload.type) { case "All": @@ -632,8 +632,8 @@ export class UnsafePiefedClient implements BaseClient { [...posts.data, ...comments.data].sort( (a, b) => getPostCommentItemCreatedDate(b) - - getPostCommentItemCreatedDate(a), - ), + getPostCommentItemCreatedDate(a) + ) ); return { @@ -659,7 +659,7 @@ export class UnsafePiefedClient implements BaseClient { async listPersonSaved( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.GET("/user", { ...options, @@ -679,7 +679,7 @@ export class UnsafePiefedClient implements BaseClient { ].sort( (a, b) => getPostCommentItemCreatedDate(b) - - getPostCommentItemCreatedDate(a), + getPostCommentItemCreatedDate(a) ); case "Comments": return response.data!.comments.map(compat.toCommentView); @@ -708,7 +708,7 @@ export class UnsafePiefedClient implements BaseClient { async lockPost( payload: { locked: boolean; post_id: number }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: PostView }> { const response = await this.#client.POST("/post/lock", { ...options, @@ -722,7 +722,7 @@ export class UnsafePiefedClient implements BaseClient { async login( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ) { const response = await this.#client.POST("/user/login", { ...options, @@ -741,7 +741,7 @@ export class UnsafePiefedClient implements BaseClient { async markCommentReplyAsRead( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { await this.#client.POST("/comment/mark_as_read", { ...options, @@ -751,7 +751,7 @@ export class UnsafePiefedClient implements BaseClient { async markPersonMentionAsRead( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { await this.#client.POST("/comment/mark_as_read", { ...options, @@ -764,7 +764,7 @@ export class UnsafePiefedClient implements BaseClient { async markPostAsRead( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { await this.#client.POST("/post/mark_as_read", { ...options, @@ -774,7 +774,7 @@ export class UnsafePiefedClient implements BaseClient { async markPrivateMessageAsRead( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { await this.#client.POST("/private_message/mark_as_read", { ...options, @@ -790,7 +790,7 @@ export class UnsafePiefedClient implements BaseClient { async removeComment( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.POST("/comment/remove", { ...options, @@ -804,7 +804,7 @@ export class UnsafePiefedClient implements BaseClient { async removePost( payload: { post_id: number; removed: boolean }, - options?: RequestOptions, + options?: RequestOptions ): Promise<{ post_view: PostView }> { const response = await this.#client.POST("/post/remove", { ...options, @@ -820,13 +820,13 @@ export class UnsafePiefedClient implements BaseClient { ..._params: Parameters ): ReturnType { throw new UnsupportedError( - "Resolve comment report is not supported by piefed", + "Resolve comment report is not supported by piefed" ); } async resolveObject( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.GET("/resolve_object", { ...options, @@ -858,13 +858,13 @@ export class UnsafePiefedClient implements BaseClient { ..._params: Parameters ): ReturnType { throw new UnsupportedError( - "Resolve post report is not supported by piefed", + "Resolve post report is not supported by piefed" ); } async saveComment( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.PUT("/comment/save", { ...options, @@ -878,7 +878,7 @@ export class UnsafePiefedClient implements BaseClient { async savePost( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { const response = await this.#client.PUT("/post/save", { ...options, @@ -898,7 +898,7 @@ export class UnsafePiefedClient implements BaseClient { async search( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ): ReturnType { if (payload.type_ === "Comments") { throw new UnsupportedError("Comment search is not supported by piefed"); @@ -922,7 +922,7 @@ export class UnsafePiefedClient implements BaseClient { async uploadImage( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ) { const formData = new FormData(); formData.append("file", payload.file); @@ -940,7 +940,7 @@ export class UnsafePiefedClient implements BaseClient { body: formData, headers: this.#headers, method: "POST", - }, + } ); await validateResponse(response); @@ -954,7 +954,7 @@ export class UnsafePiefedClient implements BaseClient { async #listPersonComments( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ) { const response = await this.#client.GET("/comment/list", { ...options, @@ -970,7 +970,7 @@ export class UnsafePiefedClient implements BaseClient { async #listPersonPosts( payload: Parameters[0], - options?: RequestOptions, + options?: RequestOptions ) { const response = await this.#client.GET("/post/list", { ...options,