diff --git a/src/http.ts b/src/http.ts index 722cd03..5f0e6c7 100644 --- a/src/http.ts +++ b/src/http.ts @@ -1835,47 +1835,176 @@ export class LemmyHttp { ); } + /** + * Upload new user avatar. + * + * `HTTP.Post /account/avatar` + */ + async uploadUserAvatar( + image: UploadImage, + options?: RequestOptions, + ): Promise { + return this.#upload("/account/avatar", image, options); + } + + /** + * Delete the user avatar. + * + * `HTTP.Delete /account/avatar` + */ + async deleteUserAvatar(options?: RequestOptions): Promise { + return this.#wrapper( + HttpType.Delete, + "/account/avatar", + {}, + options, + ); + } + + /** + * Upload new user banner. + * + * `HTTP.Post /account/banner` + */ + async uploadUserBanner( + image: UploadImage, + options?: RequestOptions, + ): Promise { + return this.#upload("/account/banner", image, options); + } + + /** + * Delete the user banner. + * + * `HTTP.Delete /account/banner` + */ + async deleteUserBanner(options?: RequestOptions): Promise { + return this.#wrapper( + HttpType.Delete, + "/account/banner", + {}, + options, + ); + } + + /** + * Upload new community icon. + * + * `HTTP.Post /community/icon` + */ + async uploadCommunityIcon( + image: UploadImage, + options?: RequestOptions, + ): Promise { + return this.#upload("/community/icon", image, options); + } + + /** + * Delete the community icon. + * + * `HTTP.Delete /community/icon` + */ + async deleteCommunityIcon( + options?: RequestOptions, + ): Promise { + return this.#wrapper( + HttpType.Delete, + "/community/icon", + {}, + options, + ); + } + + /** + * Upload new community banner. + * + * `HTTP.Post /community/banner` + */ + async uploadCommunityBanner( + image: UploadImage, + options?: RequestOptions, + ): Promise { + return this.#upload("/community/banner", image, options); + } + + /** + * Delete the community banner. + * + * `HTTP.Delete /community/banner` + */ + async deleteCommunityBanner( + options?: RequestOptions, + ): Promise { + return this.#wrapper( + HttpType.Delete, + "/community/banner", + {}, + options, + ); + } + + /** + * Upload new site icon. + * + * `HTTP.Post /site/icon` + */ + async uploadSiteIcon( + image: UploadImage, + options?: RequestOptions, + ): Promise { + return this.#upload("/site/icon", image, options); + } + + /** + * Delete the site icon. + * + * `HTTP.Delete /site/icon` + */ + async deleteSiteIcon(options?: RequestOptions): Promise { + return this.#wrapper( + HttpType.Delete, + "/site/icon", + {}, + options, + ); + } + + /** + * Upload new site banner. + * + * `HTTP.Post /site/banner` + */ + async uploadSiteBanner( + image: UploadImage, + options?: RequestOptions, + ): Promise { + return this.#upload("/site/banner", image, options); + } + + /** + * Delete the site banner. + * + * `HTTP.Delete /site/banner` + */ + async deleteSiteBanner(options?: RequestOptions): Promise { + return this.#wrapper( + HttpType.Delete, + "/site/banner", + {}, + options, + ); + } + /** * Upload an image to the server. * * `HTTP.Post /image` */ async uploadImage( - { image }: UploadImage, + image: UploadImage, options?: RequestOptions, ): Promise { - const formData = createFormData(image); - - const response = await this.#fetchFunction(this.#buildFullUrl("/image"), { - ...options, - method: HttpType.Post, - body: formData as unknown as BodyInit, - headers: this.#headers, - }); - return response.json(); - } - - /** - * Upload new user avatar. - * - * `HTTP.Post /account/avatar` - */ - async userUploadAvatar( - { image }: UploadImage, - options?: RequestOptions, - ): Promise { - const formData = createFormData(image); - - const response = await this.#fetchFunction( - this.#buildFullUrl("/account/avatar"), - { - ...options, - method: HttpType.Post, - body: formData as unknown as BodyInit, - headers: this.#headers, - }, - ); - return response.json(); + return this.#upload("/image", image, options); } /** @@ -1910,6 +2039,22 @@ export class LemmyHttp { return `${this.#apiUrl}${endpoint}`; } + async #upload( + path: string, + { image }: UploadImage, + options?: RequestOptions, + ): Promise { + const formData = createFormData(image); + + const response = await this.#fetchFunction(this.#buildFullUrl(path), { + ...options, + method: HttpType.Post, + body: formData as unknown as BodyInit, + headers: this.#headers, + }); + return response.json(); + } + async #wrapper( type_: HttpType, endpoint: string,