diff --git a/package.json b/package.json index 8d37899..c69d1c3 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "inferno-server": "^8.1.1", "isomorphic-cookie": "^1.2.4", "jwt-decode": "^3.1.2", - "lemmy-js-client": "0.17.2-rc.20", + "lemmy-js-client": "0.17.2-rc.23", "lodash": "^4.17.21", "markdown-it": "^13.0.1", "markdown-it-container": "^3.0.0", diff --git a/src/shared/components/app/navbar.tsx b/src/shared/components/app/navbar.tsx index a77345a..6b0625b 100644 --- a/src/shared/components/app/navbar.tsx +++ b/src/shared/components/app/navbar.tsx @@ -8,11 +8,7 @@ import { } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { UserService } from "../../services"; -import { - HttpService, - RequestState, - apiWrapper, -} from "../../services/HttpService"; +import { HttpService, RequestState } from "../../services/HttpService"; import { amAdmin, canCreateCommunity, @@ -421,28 +417,29 @@ export class Navbar extends Component { if (auth) { this.setState({ unreadInboxCountRes: { state: "loading" } }); this.setState({ - unreadInboxCountRes: await apiWrapper( - HttpService.client.getUnreadCount({ auth }) - ), + unreadInboxCountRes: await HttpService.wrappedClient.getUnreadCount({ + auth, + }), }); if (this.moderatesSomething) { this.setState({ unreadReportCountRes: { state: "loading" } }); this.setState({ - unreadReportCountRes: await apiWrapper( - HttpService.client.getReportCount({ auth }) - ), + unreadReportCountRes: await HttpService.wrappedClient.getReportCount({ + auth, + }), }); } if (amAdmin()) { this.setState({ unreadApplicationCountRes: { state: "loading" } }); this.setState({ - unreadApplicationCountRes: await apiWrapper( - HttpService.client.getUnreadRegistrationApplicationCount({ - auth, - }) - ), + unreadApplicationCountRes: + await HttpService.wrappedClient.getUnreadRegistrationApplicationCount( + { + auth, + } + ), }); } } diff --git a/src/shared/components/community/communities.tsx b/src/shared/components/community/communities.tsx index 8e36bab..957af84 100644 --- a/src/shared/components/community/communities.tsx +++ b/src/shared/components/community/communities.tsx @@ -12,7 +12,6 @@ import { InitialFetchRequest } from "../../interfaces"; import { HttpService, RequestState, - apiWrapper, apiWrapperIso, } from "../../services/HttpService"; import { @@ -315,14 +314,11 @@ export class Communities extends Component { communityId: number; follow: boolean; }) { - const res = await apiWrapper( - HttpService.client.followCommunity({ - community_id: data.communityId, - follow: data.follow, - auth: myAuthRequired(), - }) - ); - + const res = await HttpService.wrappedClient.followCommunity({ + community_id: data.communityId, + follow: data.follow, + auth: myAuthRequired(), + }); data.i.findAndUpdateCommunity(res); } @@ -332,15 +328,13 @@ export class Communities extends Component { const { listingType, page } = this.getCommunitiesQueryParams(); this.setState({ - listCommunitiesResponse: await apiWrapper( - HttpService.client.listCommunities({ - type_: listingType, - sort: "TopMonth", - limit: communityLimit, - page, - auth: myAuth(), - }) - ), + listCommunitiesResponse: await HttpService.wrappedClient.listCommunities({ + type_: listingType, + sort: "TopMonth", + limit: communityLimit, + page, + auth: myAuth(), + }), }); window.scrollTo(0, 0); diff --git a/src/shared/components/community/community.tsx b/src/shared/components/community/community.tsx index 7b3e846..bb2b648 100644 --- a/src/shared/components/community/community.tsx +++ b/src/shared/components/community/community.tsx @@ -61,7 +61,6 @@ import { UserService } from "../../services"; import { HttpService, RequestState, - apiWrapper, apiWrapperIso, } from "../../services/HttpService"; import { @@ -224,12 +223,10 @@ export class Community extends Component< async fetchCommunity() { this.setState({ communityRes: { state: "loading" } }); this.setState({ - communityRes: await apiWrapper( - HttpService.client.getCommunity({ - name: this.props.match.params.name, - auth: myAuth(), - }) - ), + communityRes: await HttpService.wrappedClient.getCommunity({ + name: this.props.match.params.name, + auth: myAuth(), + }), }); } @@ -585,32 +582,28 @@ export class Community extends Component< if (dataType === DataType.Post) { this.setState({ postsRes: { state: "loading" } }); this.setState({ - postsRes: await apiWrapper( - HttpService.client.getPosts({ - page, - limit: fetchLimit, - sort, - type_: "All", - community_name: name, - saved_only: false, - auth: myAuth(), - }) - ), + postsRes: await HttpService.wrappedClient.getPosts({ + page, + limit: fetchLimit, + sort, + type_: "All", + community_name: name, + saved_only: false, + auth: myAuth(), + }), }); } else { this.setState({ commentsRes: { state: "loading" } }); this.setState({ - commentsRes: await apiWrapper( - HttpService.client.getComments({ - page, - limit: fetchLimit, - sort: postToCommentSortType(sort), - type_: "All", - community_name: name, - saved_only: false, - auth: myAuth(), - }) - ), + commentsRes: await HttpService.wrappedClient.getComments({ + page, + limit: fetchLimit, + sort: postToCommentSortType(sort), + type_: "All", + community_name: name, + saved_only: false, + auth: myAuth(), + }), }); } @@ -619,23 +612,20 @@ export class Community extends Component< } async handleDeleteCommunity(form: DeleteCommunity) { - const deleteCommunityRes = await apiWrapper( - HttpService.client.deleteCommunity(form) + const deleteCommunityRes = await HttpService.wrappedClient.deleteCommunity( + form ); - this.updateCommunity(deleteCommunityRes); } async handleAddModToCommunity(form: AddModToCommunity) { - const addModRes = await apiWrapper( - HttpService.client.addModToCommunity(form) - ); + const addModRes = await HttpService.wrappedClient.addModToCommunity(form); this.updateModerators(addModRes); } async handleFollow(form: FollowCommunity) { - const followCommunityRes = await apiWrapper( - HttpService.client.followCommunity(form) + const followCommunityRes = await HttpService.wrappedClient.followCommunity( + form ); this.updateCommunity(followCommunityRes); @@ -650,68 +640,60 @@ export class Community extends Component< } async handlePurgeCommunity(form: PurgeCommunity) { - const purgeCommunityRes = await apiWrapper( - HttpService.client.purgeCommunity(form) + const purgeCommunityRes = await HttpService.wrappedClient.purgeCommunity( + form ); this.purgeItem(purgeCommunityRes); } async handlePurgePerson(form: PurgePerson) { - const purgePersonRes = await apiWrapper( - HttpService.client.purgePerson(form) - ); + const purgePersonRes = await HttpService.wrappedClient.purgePerson(form); this.purgeItem(purgePersonRes); } async handlePurgeComment(form: PurgeComment) { - const purgeCommentRes = await apiWrapper( - HttpService.client.purgeComment(form) - ); + const purgeCommentRes = await HttpService.wrappedClient.purgeComment(form); this.purgeItem(purgeCommentRes); } async handlePurgePost(form: PurgePost) { - const purgeRes = await apiWrapper(HttpService.client.purgePost(form)); + const purgeRes = await HttpService.wrappedClient.purgePost(form); this.purgeItem(purgeRes); } async handleBlockCommunity(form: BlockCommunity) { - const blockCommunityRes = await apiWrapper( - HttpService.client.blockCommunity(form) + const blockCommunityRes = await HttpService.wrappedClient.blockCommunity( + form ); - if (blockCommunityRes.state == "success") { updateCommunityBlock(blockCommunityRes.data); } } async handleBlockPerson(form: BlockPerson) { - const blockPersonRes = await apiWrapper( - HttpService.client.blockPerson(form) - ); - + const blockPersonRes = await HttpService.wrappedClient.blockPerson(form); if (blockPersonRes.state == "success") { updatePersonBlock(blockPersonRes.data); } } async handleRemoveCommunity(form: RemoveCommunity) { - const removeCommunityRes = await apiWrapper( - HttpService.client.removeCommunity(form) + const removeCommunityRes = await HttpService.wrappedClient.removeCommunity( + form ); this.updateCommunity(removeCommunityRes); } async handleEditCommunity(form: EditCommunity) { - const res = await apiWrapper(HttpService.client.editCommunity(form)); + const res = await HttpService.wrappedClient.editCommunity(form); this.updateCommunity(res); return res; } async handleCreateComment(form: CreateComment) { - const createCommentRes = await apiWrapper( - HttpService.client.createComment(form) + const createCommentRes = await HttpService.wrappedClient.createComment( + form ); this.createAndUpdateComments(createCommentRes); @@ -719,100 +701,89 @@ export class Community extends Component< } async handleEditComment(form: EditComment) { - const editCommentRes = await apiWrapper( - HttpService.client.editComment(form) - ); - + const editCommentRes = await HttpService.wrappedClient.editComment(form); this.findAndUpdateComment(editCommentRes); return editCommentRes; } async handleDeleteComment(form: DeleteComment) { - const deleteCommentRes = await apiWrapper( - HttpService.client.deleteComment(form) + const deleteCommentRes = await HttpService.wrappedClient.deleteComment( + form ); - this.findAndUpdateComment(deleteCommentRes); } async handleDeletePost(form: DeletePost) { - const deleteRes = await apiWrapper(HttpService.client.deletePost(form)); + const deleteRes = await HttpService.wrappedClient.deletePost(form); this.findAndUpdatePost(deleteRes); } async handleRemovePost(form: RemovePost) { - const removeRes = await apiWrapper(HttpService.client.removePost(form)); + const removeRes = await HttpService.wrappedClient.removePost(form); this.findAndUpdatePost(removeRes); } async handleRemoveComment(form: RemoveComment) { - const removeCommentRes = await apiWrapper( - HttpService.client.removeComment(form) + const removeCommentRes = await HttpService.wrappedClient.removeComment( + form ); - this.findAndUpdateComment(removeCommentRes); } async handleSaveComment(form: SaveComment) { - const saveCommentRes = await apiWrapper( - HttpService.client.saveComment(form) - ); + const saveCommentRes = await HttpService.wrappedClient.saveComment(form); this.findAndUpdateComment(saveCommentRes); } async handleSavePost(form: SavePost) { - const saveRes = await apiWrapper(HttpService.client.savePost(form)); + const saveRes = await HttpService.wrappedClient.savePost(form); this.findAndUpdatePost(saveRes); } async handleFeaturePost(form: FeaturePost) { - const featureRes = await apiWrapper(HttpService.client.featurePost(form)); + const featureRes = await HttpService.wrappedClient.featurePost(form); this.findAndUpdatePost(featureRes); } async handleCommentVote(form: CreateCommentLike) { - const voteRes = await apiWrapper(HttpService.client.likeComment(form)); + const voteRes = await HttpService.wrappedClient.likeComment(form); this.findAndUpdateComment(voteRes); } async handlePostVote(form: CreatePostLike) { - const voteRes = await apiWrapper(HttpService.client.likePost(form)); + const voteRes = await HttpService.wrappedClient.likePost(form); this.findAndUpdatePost(voteRes); } async handleCommentReport(form: CreateCommentReport) { - const reportRes = await apiWrapper( - HttpService.client.createCommentReport(form) - ); + const reportRes = await HttpService.wrappedClient.createCommentReport(form); if (reportRes.state == "success") { toast(i18n.t("report_created")); } } async handlePostReport(form: CreatePostReport) { - const reportRes = await apiWrapper( - HttpService.client.createPostReport(form) - ); + const reportRes = await HttpService.wrappedClient.createPostReport(form); if (reportRes.state == "success") { toast(i18n.t("report_created")); } } async handleLockPost(form: LockPost) { - const lockRes = await apiWrapper(HttpService.client.lockPost(form)); + const lockRes = await HttpService.wrappedClient.lockPost(form); this.findAndUpdatePost(lockRes); } async handleDistinguishComment(form: DistinguishComment) { - const distinguishRes = await apiWrapper( - HttpService.client.distinguishComment(form) + const distinguishRes = await HttpService.wrappedClient.distinguishComment( + form ); this.findAndUpdateComment(distinguishRes); } async handleAddAdmin(form: AddAdmin) { - const addAdminRes = await apiWrapper(HttpService.client.addAdmin(form)); + const addAdminRes = await HttpService.wrappedClient.addAdmin(form); if (addAdminRes.state == "success") { this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s)); @@ -820,32 +791,31 @@ export class Community extends Component< } async handleTransferCommunity(form: TransferCommunity) { - const transferCommunityRes = await apiWrapper( - HttpService.client.transferCommunity(form) - ); + const transferCommunityRes = + await HttpService.wrappedClient.transferCommunity(form); toast(i18n.t("transfer_community")); this.updateCommunityFull(transferCommunityRes); } async handleCommentReplyRead(form: MarkCommentReplyAsRead) { - const readRes = await apiWrapper( - HttpService.client.markCommentReplyAsRead(form) + const readRes = await HttpService.wrappedClient.markCommentReplyAsRead( + form ); this.findAndUpdateCommentReply(readRes); } async handlePersonMentionRead(form: MarkPersonMentionAsRead) { // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it. - await apiWrapper(HttpService.client.markPersonMentionAsRead(form)); + await HttpService.wrappedClient.markPersonMentionAsRead(form); } async handleBanFromCommunity(form: BanFromCommunity) { - const banRes = await apiWrapper(HttpService.client.banFromCommunity(form)); + const banRes = await HttpService.wrappedClient.banFromCommunity(form); this.updateBanFromCommunity(banRes); } async handleBanPerson(form: BanPerson) { - const banRes = await apiWrapper(HttpService.client.banPerson(form)); + const banRes = await HttpService.wrappedClient.banPerson(form); this.updateBan(banRes); } diff --git a/src/shared/components/community/create-community.tsx b/src/shared/components/community/create-community.tsx index 0e70372..5749314 100644 --- a/src/shared/components/community/create-community.tsx +++ b/src/shared/components/community/create-community.tsx @@ -4,7 +4,7 @@ import { GetSiteResponse, } from "lemmy-js-client"; import { i18n } from "../../i18next"; -import { HttpService, apiWrapper } from "../../services/HttpService"; +import { HttpService } from "../../services/HttpService"; import { enableNsfw, setIsoData } from "../../utils"; import { HtmlTags } from "../common/html-tags"; import { CommunityForm } from "./community-form"; @@ -53,7 +53,7 @@ export class CreateCommunity extends Component { } async handleCommunityCreate(form: CreateCommunityI) { - const res = await apiWrapper(HttpService.client.createCommunity(form)); + const res = await HttpService.wrappedClient.createCommunity(form); if (res.state === "success") { const name = res.data.community_view.community.name; this.props.history.replace(`/c/${name}`); diff --git a/src/shared/components/home/admin-settings.tsx b/src/shared/components/home/admin-settings.tsx index 18ab050..4b6077f 100644 --- a/src/shared/components/home/admin-settings.tsx +++ b/src/shared/components/home/admin-settings.tsx @@ -15,7 +15,6 @@ import { InitialFetchRequest } from "../../interfaces"; import { HttpService, RequestState, - apiWrapper, apiWrapperIso, } from "../../services/HttpService"; import { @@ -88,16 +87,12 @@ export class AdminSettings extends Component { const auth = myAuthRequired(); this.setState({ - bannedRes: await apiWrapper( - HttpService.client.getBannedPersons({ - auth, - }) - ), - instancesRes: await apiWrapper( - HttpService.client.getFederatedInstances({ - auth, - }) - ), + bannedRes: await HttpService.wrappedClient.getBannedPersons({ + auth, + }), + instancesRes: await HttpService.wrappedClient.getFederatedInstances({ + auth, + }), }); } @@ -251,7 +246,7 @@ export class AdminSettings extends Component { } async handleEditSite(form: EditSite) { - const editRes = await apiWrapper(HttpService.client.editSite(form)); + const editRes = await HttpService.wrappedClient.editSite(form); if (editRes.state === "success") { toast(i18n.t("site_saved")); @@ -267,9 +262,9 @@ export class AdminSettings extends Component { async handleLeaveAdminTeam(i: AdminSettings) { i.setState({ leaveAdminTeamRes: { state: "loading" } }); this.setState({ - leaveAdminTeamRes: await apiWrapper( - HttpService.client.leaveAdmin({ auth: myAuthRequired() }) - ), + leaveAdminTeamRes: await HttpService.wrappedClient.leaveAdmin({ + auth: myAuthRequired(), + }), }); if (this.state.leaveAdminTeamRes.state === "success") { @@ -279,21 +274,21 @@ export class AdminSettings extends Component { } async handleEditEmoji(form: EditCustomEmoji) { - const res = await apiWrapper(HttpService.client.editCustomEmoji(form)); + const res = await HttpService.wrappedClient.editCustomEmoji(form); if (res.state === "success") { updateEmojiDataModel(res.data.custom_emoji); } } async handleDeleteEmoji(form: DeleteCustomEmoji) { - const res = await apiWrapper(HttpService.client.deleteCustomEmoji(form)); + const res = await HttpService.wrappedClient.deleteCustomEmoji(form); if (res.state === "success") { removeFromEmojiDataModel(res.data.id); } } async handleCreateEmoji(form: CreateCustomEmoji) { - const res = await apiWrapper(HttpService.client.createCustomEmoji(form)); + const res = await HttpService.wrappedClient.createCustomEmoji(form); if (res.state === "success") { updateEmojiDataModel(res.data.custom_emoji); } diff --git a/src/shared/components/home/home.tsx b/src/shared/components/home/home.tsx index 0f699f7..bebcba3 100644 --- a/src/shared/components/home/home.tsx +++ b/src/shared/components/home/home.tsx @@ -54,7 +54,6 @@ import { } from "../../interfaces"; import { UserService } from "../../services"; import { - apiWrapper, apiWrapperIso, HttpService, RequestState, @@ -745,14 +744,12 @@ export class Home extends Component { async fetchTrendingCommunities() { this.setState({ trendingCommunitiesRes: { state: "loading" } }); this.setState({ - trendingCommunitiesRes: await apiWrapper( - HttpService.client.listCommunities({ - type_: "Local", - sort: "Hot", - limit: trendingFetchLimit, - auth: myAuth(), - }) - ), + trendingCommunitiesRes: await HttpService.wrappedClient.listCommunities({ + type_: "Local", + sort: "Hot", + limit: trendingFetchLimit, + auth: myAuth(), + }), }); } @@ -763,30 +760,26 @@ export class Home extends Component { if (dataType === DataType.Post) { this.setState({ postsRes: { state: "loading" } }); this.setState({ - postsRes: await apiWrapper( - HttpService.client.getPosts({ - page, - limit: fetchLimit, - sort, - saved_only: false, - type_: listingType, - auth, - }) - ), + postsRes: await HttpService.wrappedClient.getPosts({ + page, + limit: fetchLimit, + sort, + saved_only: false, + type_: listingType, + auth, + }), }); } else { this.setState({ commentsRes: { state: "loading" } }); this.setState({ - commentsRes: await apiWrapper( - HttpService.client.getComments({ - page, - limit: fetchLimit, - sort: postToCommentSortType(sort), - saved_only: false, - type_: listingType, - auth, - }) - ), + commentsRes: await HttpService.wrappedClient.getComments({ + page, + limit: fetchLimit, + sort: postToCommentSortType(sort), + saved_only: false, + type_: listingType, + auth, + }), }); } @@ -832,143 +825,124 @@ export class Home extends Component { async handleAddModToCommunity(form: AddModToCommunity) { // TODO not sure what to do here - await apiWrapper(HttpService.client.addModToCommunity(form)); + await HttpService.wrappedClient.addModToCommunity(form); } async handlePurgePerson(form: PurgePerson) { - const purgePersonRes = await apiWrapper( - HttpService.client.purgePerson(form) - ); + const purgePersonRes = await HttpService.wrappedClient.purgePerson(form); this.purgeItem(purgePersonRes); } async handlePurgeComment(form: PurgeComment) { - const purgeCommentRes = await apiWrapper( - HttpService.client.purgeComment(form) - ); + const purgeCommentRes = await HttpService.wrappedClient.purgeComment(form); this.purgeItem(purgeCommentRes); } async handlePurgePost(form: PurgePost) { - const purgeRes = await apiWrapper(HttpService.client.purgePost(form)); + const purgeRes = await HttpService.wrappedClient.purgePost(form); this.purgeItem(purgeRes); } async handleBlockPerson(form: BlockPerson) { - const blockPersonRes = await apiWrapper( - HttpService.client.blockPerson(form) - ); - + const blockPersonRes = await HttpService.wrappedClient.blockPerson(form); if (blockPersonRes.state == "success") { updatePersonBlock(blockPersonRes.data); } } async handleCreateComment(form: CreateComment) { - const createCommentRes = await apiWrapper( - HttpService.client.createComment(form) + const createCommentRes = await HttpService.wrappedClient.createComment( + form ); - this.createAndUpdateComments(createCommentRes); return createCommentRes; } async handleEditComment(form: EditComment) { - const editCommentRes = await apiWrapper( - HttpService.client.editComment(form) - ); - + const editCommentRes = await HttpService.wrappedClient.editComment(form); this.findAndUpdateComment(editCommentRes); return editCommentRes; } async handleDeleteComment(form: DeleteComment) { - const deleteCommentRes = await apiWrapper( - HttpService.client.deleteComment(form) + const deleteCommentRes = await HttpService.wrappedClient.deleteComment( + form ); - this.findAndUpdateComment(deleteCommentRes); } async handleDeletePost(form: DeletePost) { - const deleteRes = await apiWrapper(HttpService.client.deletePost(form)); + const deleteRes = await HttpService.wrappedClient.deletePost(form); this.findAndUpdatePost(deleteRes); } async handleRemovePost(form: RemovePost) { - const removeRes = await apiWrapper(HttpService.client.removePost(form)); + const removeRes = await HttpService.wrappedClient.removePost(form); this.findAndUpdatePost(removeRes); } async handleRemoveComment(form: RemoveComment) { - const removeCommentRes = await apiWrapper( - HttpService.client.removeComment(form) + const removeCommentRes = await HttpService.wrappedClient.removeComment( + form ); - this.findAndUpdateComment(removeCommentRes); } async handleSaveComment(form: SaveComment) { - const saveCommentRes = await apiWrapper( - HttpService.client.saveComment(form) - ); + const saveCommentRes = await HttpService.wrappedClient.saveComment(form); this.findAndUpdateComment(saveCommentRes); } async handleSavePost(form: SavePost) { - const saveRes = await apiWrapper(HttpService.client.savePost(form)); + const saveRes = await HttpService.wrappedClient.savePost(form); this.findAndUpdatePost(saveRes); } async handleFeaturePost(form: FeaturePost) { - const featureRes = await apiWrapper(HttpService.client.featurePost(form)); + const featureRes = await HttpService.wrappedClient.featurePost(form); this.findAndUpdatePost(featureRes); } async handleCommentVote(form: CreateCommentLike) { - const voteRes = await apiWrapper(HttpService.client.likeComment(form)); + const voteRes = await HttpService.wrappedClient.likeComment(form); this.findAndUpdateComment(voteRes); } async handlePostVote(form: CreatePostLike) { - const voteRes = await apiWrapper(HttpService.client.likePost(form)); + const voteRes = await HttpService.wrappedClient.likePost(form); this.findAndUpdatePost(voteRes); } async handleCommentReport(form: CreateCommentReport) { - const reportRes = await apiWrapper( - HttpService.client.createCommentReport(form) - ); + const reportRes = await HttpService.wrappedClient.createCommentReport(form); if (reportRes.state == "success") { toast(i18n.t("report_created")); } } async handlePostReport(form: CreatePostReport) { - const reportRes = await apiWrapper( - HttpService.client.createPostReport(form) - ); + const reportRes = await HttpService.wrappedClient.createPostReport(form); if (reportRes.state == "success") { toast(i18n.t("report_created")); } } async handleLockPost(form: LockPost) { - const lockRes = await apiWrapper(HttpService.client.lockPost(form)); + const lockRes = await HttpService.wrappedClient.lockPost(form); this.findAndUpdatePost(lockRes); } async handleDistinguishComment(form: DistinguishComment) { - const distinguishRes = await apiWrapper( - HttpService.client.distinguishComment(form) + const distinguishRes = await HttpService.wrappedClient.distinguishComment( + form ); this.findAndUpdateComment(distinguishRes); } async handleAddAdmin(form: AddAdmin) { - const addAdminRes = await apiWrapper(HttpService.client.addAdmin(form)); + const addAdminRes = await HttpService.wrappedClient.addAdmin(form); if (addAdminRes.state == "success") { this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s)); @@ -976,29 +950,29 @@ export class Home extends Component { } async handleTransferCommunity(form: TransferCommunity) { - await apiWrapper(HttpService.client.transferCommunity(form)); + await HttpService.wrappedClient.transferCommunity(form); toast(i18n.t("transfer_community")); } async handleCommentReplyRead(form: MarkCommentReplyAsRead) { - const readRes = await apiWrapper( - HttpService.client.markCommentReplyAsRead(form) + const readRes = await HttpService.wrappedClient.markCommentReplyAsRead( + form ); this.findAndUpdateCommentReply(readRes); } async handlePersonMentionRead(form: MarkPersonMentionAsRead) { // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it. - await apiWrapper(HttpService.client.markPersonMentionAsRead(form)); + await HttpService.wrappedClient.markPersonMentionAsRead(form); } async handleBanFromCommunity(form: BanFromCommunity) { - const banRes = await apiWrapper(HttpService.client.banFromCommunity(form)); + const banRes = await HttpService.wrappedClient.banFromCommunity(form); this.updateBanFromCommunity(banRes); } async handleBanPerson(form: BanPerson) { - const banRes = await apiWrapper(HttpService.client.banPerson(form)); + const banRes = await HttpService.wrappedClient.banPerson(form); this.updateBan(banRes); } diff --git a/src/shared/components/home/instances.tsx b/src/shared/components/home/instances.tsx index a23e53b..dcfb85b 100644 --- a/src/shared/components/home/instances.tsx +++ b/src/shared/components/home/instances.tsx @@ -9,7 +9,6 @@ import { InitialFetchRequest } from "../../interfaces"; import { HttpService, RequestState, - apiWrapper, apiWrapperIso, } from "../../services/HttpService"; import { isInitialRoute, relTags, setIsoData } from "../../utils"; @@ -54,9 +53,7 @@ export class Instances extends Component { }); this.setState({ - instancesRes: await apiWrapper( - HttpService.client.getFederatedInstances({}) - ), + instancesRes: await HttpService.wrappedClient.getFederatedInstances({}), }); } diff --git a/src/shared/components/home/login.tsx b/src/shared/components/home/login.tsx index 6bdcd4f..9234830 100644 --- a/src/shared/components/home/login.tsx +++ b/src/shared/components/home/login.tsx @@ -2,11 +2,7 @@ import { Component, linkEvent } from "inferno"; import { GetSiteResponse, LoginResponse } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { UserService } from "../../services"; -import { - HttpService, - RequestState, - apiWrapper, -} from "../../services/HttpService"; +import { HttpService, RequestState } from "../../services/HttpService"; import { isBrowser, setIsoData, toast, validEmail } from "../../utils"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; @@ -164,14 +160,11 @@ export class Login extends Component { if (username_or_email && password) { i.setState({ loginRes: { state: "loading" } }); - const loginRes = await apiWrapper( - HttpService.client.login({ - username_or_email, - password, - totp_2fa_token, - }) - ); - + const loginRes = await HttpService.wrappedClient.login({ + username_or_email, + password, + totp_2fa_token, + }); switch (loginRes.state) { case "failed": { if (loginRes.msg === "missing_totp_token") { @@ -212,7 +205,7 @@ export class Login extends Component { event.preventDefault(); let email = i.state.form.username_or_email; if (email) { - const res = await apiWrapper(HttpService.client.passwordReset({ email })); + const res = await HttpService.wrappedClient.passwordReset({ email }); if (res.state == "success") { toast(i18n.t("reset_password_mail_sent")); } diff --git a/src/shared/components/home/setup.tsx b/src/shared/components/home/setup.tsx index 5ecd6e3..84edc27 100644 --- a/src/shared/components/home/setup.tsx +++ b/src/shared/components/home/setup.tsx @@ -8,11 +8,7 @@ import { } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { UserService } from "../../services"; -import { - HttpService, - RequestState, - apiWrapper, -} from "../../services/HttpService"; +import { HttpService, RequestState } from "../../services/HttpService"; import { setIsoData } from "../../utils"; import { Spinner } from "../common/icon"; import { SiteForm } from "./site-form"; @@ -180,7 +176,7 @@ export class Setup extends Component { answer: cForm.answer, }; i.setState({ - registerRes: await apiWrapper(HttpService.client.register(form)), + registerRes: await HttpService.wrappedClient.register(form), }); if (this.state.registerRes.state == "success") { @@ -195,7 +191,7 @@ export class Setup extends Component { } async handleCreateSite(form: CreateSite) { - const createRes = await apiWrapper(HttpService.client.createSite(form)); + const createRes = await HttpService.wrappedClient.createSite(form); if (createRes.state === "success") { this.context.router.history.replace("/"); } diff --git a/src/shared/components/home/signup.tsx b/src/shared/components/home/signup.tsx index e4cff6b..3b5a14e 100644 --- a/src/shared/components/home/signup.tsx +++ b/src/shared/components/home/signup.tsx @@ -11,11 +11,7 @@ import { } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { UserService } from "../../services"; -import { - HttpService, - RequestState, - apiWrapper, -} from "../../services/HttpService"; +import { HttpService, RequestState } from "../../services/HttpService"; import { isBrowser, joinLemmyUrl, @@ -102,7 +98,7 @@ export class Signup extends Component { async fetchCaptcha() { this.setState({ captchaRes: { state: "loading" } }); this.setState({ - captchaRes: await apiWrapper(HttpService.client.getCaptcha({})), + captchaRes: await HttpService.wrappedClient.getCaptcha({}), }); this.setState(s => { @@ -441,20 +437,17 @@ export class Signup extends Component { if (cForm.username && cForm.password && cForm.password_verify) { i.setState({ registerRes: { state: "loading" } }); - const registerRes = await apiWrapper( - HttpService.client.register({ - username: cForm.username, - password: cForm.password, - password_verify: cForm.password_verify, - email: cForm.email, - show_nsfw: cForm.show_nsfw, - captcha_uuid: cForm.captcha_uuid, - captcha_answer: cForm.captcha_answer, - honeypot: cForm.honeypot, - answer: cForm.answer, - }) - ); - + const registerRes = await HttpService.wrappedClient.register({ + username: cForm.username, + password: cForm.password, + password_verify: cForm.password_verify, + email: cForm.email, + show_nsfw: cForm.show_nsfw, + captcha_uuid: cForm.captcha_uuid, + captcha_answer: cForm.captcha_answer, + honeypot: cForm.honeypot, + answer: cForm.answer, + }); switch (registerRes.state) { case "failed": { toast(registerRes.msg, "danger"); diff --git a/src/shared/components/modlog.tsx b/src/shared/components/modlog.tsx index 9206af8..0d3d872 100644 --- a/src/shared/components/modlog.tsx +++ b/src/shared/components/modlog.tsx @@ -33,7 +33,6 @@ import { InitialFetchRequest } from "../interfaces"; import { HttpService, RequestState, - apiWrapper, apiWrapperIso, } from "../services/HttpService"; import { @@ -951,31 +950,27 @@ export class Modlog extends Component< this.setState({ res: { state: "loading" } }); this.setState({ - res: await apiWrapper( - HttpService.client.getModlog({ - community_id: communityId, - page, - limit: fetchLimit, - type_: actionType, - other_person_id: userId ?? undefined, - mod_person_id: !this.isoData.site_res.site_view.local_site - .hide_modlog_mod_names - ? modId ?? undefined - : undefined, - auth, - }) - ), + res: await HttpService.wrappedClient.getModlog({ + community_id: communityId, + page, + limit: fetchLimit, + type_: actionType, + other_person_id: userId ?? undefined, + mod_person_id: !this.isoData.site_res.site_view.local_site + .hide_modlog_mod_names + ? modId ?? undefined + : undefined, + auth, + }), }); if (communityId) { this.setState({ communityRes: { state: "loading" } }); this.setState({ - communityRes: await apiWrapper( - HttpService.client.getCommunity({ - id: communityId, - auth, - }) - ), + communityRes: await HttpService.wrappedClient.getCommunity({ + id: communityId, + auth, + }), }); } } diff --git a/src/shared/components/person/inbox.tsx b/src/shared/components/person/inbox.tsx index 3226269..f2f6fe6 100644 --- a/src/shared/components/person/inbox.tsx +++ b/src/shared/components/person/inbox.tsx @@ -53,7 +53,6 @@ import { UserService } from "../../services"; import { HttpService, RequestState, - apiWrapper, apiWrapperIso, } from "../../services/HttpService"; import { @@ -724,40 +723,34 @@ export class Inbox extends Component { this.setState({ repliesRes: { state: "loading" } }); this.setState({ - repliesRes: await apiWrapper( - HttpService.client.getReplies({ - sort, - unread_only, - page, - limit, - auth, - }) - ), + repliesRes: await HttpService.wrappedClient.getReplies({ + sort, + unread_only, + page, + limit, + auth, + }), }); this.setState({ mentionsRes: { state: "loading" } }); this.setState({ - mentionsRes: await apiWrapper( - HttpService.client.getPersonMentions({ - sort, - unread_only, - page, - limit, - auth, - }) - ), + mentionsRes: await HttpService.wrappedClient.getPersonMentions({ + sort, + unread_only, + page, + limit, + auth, + }), }); this.setState({ messagesRes: { state: "loading" } }); this.setState({ - messagesRes: await apiWrapper( - HttpService.client.getPrivateMessages({ - unread_only, - page, - limit, - auth, - }) - ), + messagesRes: await HttpService.wrappedClient.getPrivateMessages({ + unread_only, + page, + limit, + auth, + }), }); } @@ -770,9 +763,9 @@ export class Inbox extends Component { i.setState({ markAllAsReadRes: { state: "loading" } }); i.setState({ - markAllAsReadRes: await apiWrapper( - HttpService.client.markAllAsRead({ auth: myAuthRequired() }) - ), + markAllAsReadRes: await HttpService.wrappedClient.markAllAsRead({ + auth: myAuthRequired(), + }), }); if (i.state.markAllAsReadRes.state == "success") { @@ -786,40 +779,33 @@ export class Inbox extends Component { async handleAddModToCommunity(form: AddModToCommunity) { // TODO not sure what to do here - apiWrapper(HttpService.client.addModToCommunity(form)); + HttpService.wrappedClient.addModToCommunity(form); } async handlePurgePerson(form: PurgePerson) { - const purgePersonRes = await apiWrapper( - HttpService.client.purgePerson(form) - ); + const purgePersonRes = await HttpService.wrappedClient.purgePerson(form); this.purgeItem(purgePersonRes); } async handlePurgeComment(form: PurgeComment) { - const purgeCommentRes = await apiWrapper( - HttpService.client.purgeComment(form) - ); + const purgeCommentRes = await HttpService.wrappedClient.purgeComment(form); this.purgeItem(purgeCommentRes); } async handlePurgePost(form: PurgePost) { - const purgeRes = await apiWrapper(HttpService.client.purgePost(form)); + const purgeRes = await HttpService.wrappedClient.purgePost(form); this.purgeItem(purgeRes); } async handleBlockPerson(form: BlockPerson) { - const blockPersonRes = await apiWrapper( - HttpService.client.blockPerson(form) - ); - + const blockPersonRes = await HttpService.wrappedClient.blockPerson(form); if (blockPersonRes.state == "success") { updatePersonBlock(blockPersonRes.data); } } async handleCreateComment(form: CreateComment) { - const res = await apiWrapper(HttpService.client.createComment(form)); + const res = await HttpService.wrappedClient.createComment(form); if (res.state === "success") { toast(i18n.t("reply_sent")); @@ -830,7 +816,7 @@ export class Inbox extends Component { } async handleEditComment(form: EditComment) { - const res = await apiWrapper(HttpService.client.editComment(form)); + const res = await HttpService.wrappedClient.editComment(form); if (res.state === "success") { toast(i18n.t("edit")); @@ -843,7 +829,7 @@ export class Inbox extends Component { } async handleDeleteComment(form: DeleteComment) { - const res = await apiWrapper(HttpService.client.deleteComment(form)); + const res = await HttpService.wrappedClient.deleteComment(form); if (res.state == "success") { toast(i18n.t("deleted")); this.findAndUpdateComment(res); @@ -851,7 +837,7 @@ export class Inbox extends Component { } async handleRemoveComment(form: RemoveComment) { - const res = await apiWrapper(HttpService.client.removeComment(form)); + const res = await HttpService.wrappedClient.removeComment(form); if (res.state == "success") { toast(i18n.t("remove_comment")); this.findAndUpdateComment(res); @@ -859,90 +845,82 @@ export class Inbox extends Component { } async handleSaveComment(form: SaveComment) { - const res = await apiWrapper(HttpService.client.saveComment(form)); + const res = await HttpService.wrappedClient.saveComment(form); this.findAndUpdateComment(res); } async handleCommentVote(form: CreateCommentLike) { - const res = await apiWrapper(HttpService.client.likeComment(form)); + const res = await HttpService.wrappedClient.likeComment(form); this.findAndUpdateComment(res); } async handleCommentReport(form: CreateCommentReport) { - const reportRes = await apiWrapper( - HttpService.client.createCommentReport(form) - ); + const reportRes = await HttpService.wrappedClient.createCommentReport(form); this.reportToast(reportRes); } async handleDistinguishComment(form: DistinguishComment) { - const res = await apiWrapper(HttpService.client.distinguishComment(form)); + const res = await HttpService.wrappedClient.distinguishComment(form); this.findAndUpdateComment(res); } async handleAddAdmin(form: AddAdmin) { - const addAdminRes = await apiWrapper(HttpService.client.addAdmin(form)); + const addAdminRes = await HttpService.wrappedClient.addAdmin(form); - if (addAdminRes.state == "success") { + if (addAdminRes.state === "success") { this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s)); } } async handleTransferCommunity(form: TransferCommunity) { - await apiWrapper(HttpService.client.transferCommunity(form)); + await HttpService.wrappedClient.transferCommunity(form); toast(i18n.t("transfer_community")); } async handleCommentReplyRead(form: MarkCommentReplyAsRead) { - const res = await apiWrapper( - HttpService.client.markCommentReplyAsRead(form) - ); + const res = await HttpService.wrappedClient.markCommentReplyAsRead(form); this.findAndUpdateCommentReply(res); } async handlePersonMentionRead(form: MarkPersonMentionAsRead) { - const res = await apiWrapper( - HttpService.client.markPersonMentionAsRead(form) - ); + const res = await HttpService.wrappedClient.markPersonMentionAsRead(form); this.findAndUpdateMention(res); } async handleBanFromCommunity(form: BanFromCommunity) { - const banRes = await apiWrapper(HttpService.client.banFromCommunity(form)); + const banRes = await HttpService.wrappedClient.banFromCommunity(form); this.updateBanFromCommunity(banRes); } async handleBanPerson(form: BanPerson) { - const banRes = await apiWrapper(HttpService.client.banPerson(form)); + const banRes = await HttpService.wrappedClient.banPerson(form); this.updateBan(banRes); } async handleDeleteMessage(form: DeletePrivateMessage) { - const res = await apiWrapper(HttpService.client.deletePrivateMessage(form)); + const res = await HttpService.wrappedClient.deletePrivateMessage(form); this.findAndUpdateMessage(res); } async handleEditMessage(form: EditPrivateMessage) { - const res = await apiWrapper(HttpService.client.editPrivateMessage(form)); + const res = await HttpService.wrappedClient.editPrivateMessage(form); this.findAndUpdateMessage(res); } async handleMarkMessageAsRead(form: MarkPrivateMessageAsRead) { - const res = await apiWrapper( - HttpService.client.markPrivateMessageAsRead(form) - ); + const res = await HttpService.wrappedClient.markPrivateMessageAsRead(form); this.findAndUpdateMessage(res); } async handleMessageReport(form: CreatePrivateMessageReport) { - const res = await apiWrapper( - HttpService.client.createPrivateMessageReport(form) + const res = await HttpService.wrappedClient.createPrivateMessageReport( + form ); this.reportToast(res); } async handleCreateMessage(form: CreatePrivateMessage) { - const res = await apiWrapper(HttpService.client.createPrivateMessage(form)); + const res = await HttpService.wrappedClient.createPrivateMessage(form); this.setState(s => { if (s.messagesRes.state == "success" && res.state == "success") { s.messagesRes.data.private_messages.unshift( @@ -956,7 +934,7 @@ export class Inbox extends Component { findAndUpdateMessage(res: RequestState) { this.setState(s => { - if (s.messagesRes.state == "success" && res.state == "success") { + if (s.messagesRes.state === "success" && res.state === "success") { s.messagesRes.data.private_messages = editPrivateMessage( res.data.private_message_view, s.messagesRes.data.private_messages diff --git a/src/shared/components/person/password-change.tsx b/src/shared/components/person/password-change.tsx index 50f0671..7ed432b 100644 --- a/src/shared/components/person/password-change.tsx +++ b/src/shared/components/person/password-change.tsx @@ -2,7 +2,7 @@ import { Component, linkEvent } from "inferno"; import { GetSiteResponse, LoginResponse } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { HttpService, UserService } from "../../services"; -import { RequestState, apiWrapper } from "../../services/HttpService"; +import { RequestState } from "../../services/HttpService"; import { capitalizeFirstLetter, setIsoData } from "../../utils"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; @@ -124,13 +124,12 @@ export class PasswordChange extends Component { if (password && password_verify) { i.setState({ - passwordChangeRes: await apiWrapper( - HttpService.client.passwordChangeAfterReset({ + passwordChangeRes: + await HttpService.wrappedClient.passwordChangeAfterReset({ token: i.state.form.token, password, password_verify, - }) - ), + }), }); if (i.state.passwordChangeRes.state == "success") { diff --git a/src/shared/components/person/profile.tsx b/src/shared/components/person/profile.tsx index 5f1d985..f0bea81 100644 --- a/src/shared/components/person/profile.tsx +++ b/src/shared/components/person/profile.tsx @@ -52,7 +52,6 @@ import { UserService } from "../../services"; import { HttpService, RequestState, - apiWrapper, apiWrapperIso, } from "../../services/HttpService"; import { @@ -232,16 +231,14 @@ export class Profile extends Component< this.setState({ personRes: { state: "empty" } }); this.setState({ - personRes: await apiWrapper( - HttpService.client.getPersonDetails({ - username: this.props.match.params.username, - sort, - saved_only: view === PersonDetailsView.Saved, - page, - limit: fetchLimit, - auth: myAuth(), - }) - ), + personRes: await HttpService.wrappedClient.getPersonDetails({ + username: this.props.match.params.username, + sort, + saved_only: view === PersonDetailsView.Saved, + page, + limit: fetchLimit, + auth: myAuth(), + }), }); restoreScrollPosition(this.context); this.setPersonBlock(); @@ -770,17 +767,14 @@ export class Profile extends Component< i.setState({ removeData: false }); } - const res = await apiWrapper( - HttpService.client.banPerson({ - person_id: person.id, - ban, - remove_data: removeData, - reason: banReason, - expires: futureDaysToUnixTime(banExpireDays), - auth: myAuthRequired(), - }) - ); - + const res = await HttpService.wrappedClient.banPerson({ + person_id: person.id, + ban, + remove_data: removeData, + reason: banReason, + expires: futureDaysToUnixTime(banExpireDays), + auth: myAuthRequired(), + }); // TODO this.updateBan(res); i.setState({ showBanDialog: false }); @@ -788,14 +782,11 @@ export class Profile extends Component< } async toggleBlockPerson(recipientId: number, block: boolean) { - const res = await apiWrapper( - HttpService.client.blockPerson({ - person_id: recipientId, - block, - auth: myAuthRequired(), - }) - ); - + const res = await HttpService.wrappedClient.blockPerson({ + person_id: recipientId, + block, + auth: myAuthRequired(), + }); if (res.state == "success") { updatePersonBlock(res.data); } @@ -811,41 +802,34 @@ export class Profile extends Component< async handleAddModToCommunity(form: AddModToCommunity) { // TODO not sure what to do here - await apiWrapper(HttpService.client.addModToCommunity(form)); + await HttpService.wrappedClient.addModToCommunity(form); } async handlePurgePerson(form: PurgePerson) { - const purgePersonRes = await apiWrapper( - HttpService.client.purgePerson(form) - ); + const purgePersonRes = await HttpService.wrappedClient.purgePerson(form); this.purgeItem(purgePersonRes); } async handlePurgeComment(form: PurgeComment) { - const purgeCommentRes = await apiWrapper( - HttpService.client.purgeComment(form) - ); + const purgeCommentRes = await HttpService.wrappedClient.purgeComment(form); this.purgeItem(purgeCommentRes); } async handlePurgePost(form: PurgePost) { - const purgeRes = await apiWrapper(HttpService.client.purgePost(form)); + const purgeRes = await HttpService.wrappedClient.purgePost(form); this.purgeItem(purgeRes); } async handleBlockPersonAlt(form: BlockPerson) { - const blockPersonRes = await apiWrapper( - HttpService.client.blockPerson(form) - ); - - if (blockPersonRes.state == "success") { + const blockPersonRes = await HttpService.wrappedClient.blockPerson(form); + if (blockPersonRes.state === "success") { updatePersonBlock(blockPersonRes.data); } } async handleCreateComment(form: CreateComment) { - const createCommentRes = await apiWrapper( - HttpService.client.createComment(form) + const createCommentRes = await HttpService.wrappedClient.createComment( + form ); this.createAndUpdateComments(createCommentRes); @@ -853,100 +837,89 @@ export class Profile extends Component< } async handleEditComment(form: EditComment) { - const editCommentRes = await apiWrapper( - HttpService.client.editComment(form) - ); - + const editCommentRes = await HttpService.wrappedClient.editComment(form); this.findAndUpdateComment(editCommentRes); return editCommentRes; } async handleDeleteComment(form: DeleteComment) { - const deleteCommentRes = await apiWrapper( - HttpService.client.deleteComment(form) + const deleteCommentRes = await HttpService.wrappedClient.deleteComment( + form ); - this.findAndUpdateComment(deleteCommentRes); } async handleDeletePost(form: DeletePost) { - const deleteRes = await apiWrapper(HttpService.client.deletePost(form)); + const deleteRes = await HttpService.wrappedClient.deletePost(form); this.findAndUpdatePost(deleteRes); } async handleRemovePost(form: RemovePost) { - const removeRes = await apiWrapper(HttpService.client.removePost(form)); + const removeRes = await HttpService.wrappedClient.removePost(form); this.findAndUpdatePost(removeRes); } async handleRemoveComment(form: RemoveComment) { - const removeCommentRes = await apiWrapper( - HttpService.client.removeComment(form) + const removeCommentRes = await HttpService.wrappedClient.removeComment( + form ); - this.findAndUpdateComment(removeCommentRes); } async handleSaveComment(form: SaveComment) { - const saveCommentRes = await apiWrapper( - HttpService.client.saveComment(form) - ); + const saveCommentRes = await HttpService.wrappedClient.saveComment(form); this.findAndUpdateComment(saveCommentRes); } async handleSavePost(form: SavePost) { - const saveRes = await apiWrapper(HttpService.client.savePost(form)); + const saveRes = await HttpService.wrappedClient.savePost(form); this.findAndUpdatePost(saveRes); } async handleFeaturePost(form: FeaturePost) { - const featureRes = await apiWrapper(HttpService.client.featurePost(form)); + const featureRes = await HttpService.wrappedClient.featurePost(form); this.findAndUpdatePost(featureRes); } async handleCommentVote(form: CreateCommentLike) { - const voteRes = await apiWrapper(HttpService.client.likeComment(form)); + const voteRes = await HttpService.wrappedClient.likeComment(form); this.findAndUpdateComment(voteRes); } async handlePostVote(form: CreatePostLike) { - const voteRes = await apiWrapper(HttpService.client.likePost(form)); + const voteRes = await HttpService.wrappedClient.likePost(form); this.findAndUpdatePost(voteRes); } async handleCommentReport(form: CreateCommentReport) { - const reportRes = await apiWrapper( - HttpService.client.createCommentReport(form) - ); - if (reportRes.state == "success") { + const reportRes = await HttpService.wrappedClient.createCommentReport(form); + if (reportRes.state === "success") { toast(i18n.t("report_created")); } } async handlePostReport(form: CreatePostReport) { - const reportRes = await apiWrapper( - HttpService.client.createPostReport(form) - ); - if (reportRes.state == "success") { + const reportRes = await HttpService.wrappedClient.createPostReport(form); + if (reportRes.state === "success") { toast(i18n.t("report_created")); } } async handleLockPost(form: LockPost) { - const lockRes = await apiWrapper(HttpService.client.lockPost(form)); + const lockRes = await HttpService.wrappedClient.lockPost(form); this.findAndUpdatePost(lockRes); } async handleDistinguishComment(form: DistinguishComment) { - const distinguishRes = await apiWrapper( - HttpService.client.distinguishComment(form) + const distinguishRes = await HttpService.wrappedClient.distinguishComment( + form ); this.findAndUpdateComment(distinguishRes); } async handleAddAdmin(form: AddAdmin) { - const addAdminRes = await apiWrapper(HttpService.client.addAdmin(form)); + const addAdminRes = await HttpService.wrappedClient.addAdmin(form); if (addAdminRes.state == "success") { this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s)); @@ -954,45 +927,45 @@ export class Profile extends Component< } async handleTransferCommunity(form: TransferCommunity) { - await apiWrapper(HttpService.client.transferCommunity(form)); + await HttpService.wrappedClient.transferCommunity(form); toast(i18n.t("transfer_community")); } async handleCommentReplyRead(form: MarkCommentReplyAsRead) { - const readRes = await apiWrapper( - HttpService.client.markCommentReplyAsRead(form) + const readRes = await HttpService.wrappedClient.markCommentReplyAsRead( + form ); this.findAndUpdateCommentReply(readRes); } async handlePersonMentionRead(form: MarkPersonMentionAsRead) { // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it. - await apiWrapper(HttpService.client.markPersonMentionAsRead(form)); + await HttpService.wrappedClient.markPersonMentionAsRead(form); } async handleBanFromCommunity(form: BanFromCommunity) { - const banRes = await apiWrapper(HttpService.client.banFromCommunity(form)); + const banRes = await HttpService.wrappedClient.banFromCommunity(form); this.updateBanFromCommunity(banRes); } async handleBanPerson(form: BanPerson) { - const banRes = await apiWrapper(HttpService.client.banPerson(form)); + const banRes = await HttpService.wrappedClient.banPerson(form); this.updateBan(banRes); } updateBanFromCommunity(banRes: RequestState) { // Maybe not necessary - if (banRes.state == "success") { + if (banRes.state === "success") { this.setState(s => { if (s.personRes.state == "success") { s.personRes.data.posts - .filter(c => c.creator.id == banRes.data.person_view.person.id) + .filter(c => c.creator.id === banRes.data.person_view.person.id) .forEach( c => (c.creator_banned_from_community = banRes.data.banned) ); s.personRes.data.comments - .filter(c => c.creator.id == banRes.data.person_view.person.id) + .filter(c => c.creator.id === banRes.data.person_view.person.id) .forEach( c => (c.creator_banned_from_community = banRes.data.banned) ); diff --git a/src/shared/components/person/registration-applications.tsx b/src/shared/components/person/registration-applications.tsx index ccd26fb..668a7dc 100644 --- a/src/shared/components/person/registration-applications.tsx +++ b/src/shared/components/person/registration-applications.tsx @@ -12,7 +12,6 @@ import { UserService } from "../../services"; import { HttpService, RequestState, - apiWrapper, apiWrapperIso, } from "../../services/HttpService"; import { @@ -211,22 +210,18 @@ export class RegistrationApplications extends Component< appsRes: { state: "loading" }, }); this.setState({ - appsRes: await apiWrapper( - HttpService.client.listRegistrationApplications({ - unread_only: unread_only, - page: this.state.page, - limit: fetchLimit, - auth: myAuthRequired(), - }) - ), + appsRes: await HttpService.wrappedClient.listRegistrationApplications({ + unread_only: unread_only, + page: this.state.page, + limit: fetchLimit, + auth: myAuthRequired(), + }), }); } async handleApproveApplication(form: ApproveRegistrationApplication) { - const approveRes = await apiWrapper( - HttpService.client.approveRegistrationApplication(form) - ); - + const approveRes = + await HttpService.wrappedClient.approveRegistrationApplication(form); this.setState(s => { if (s.appsRes.state == "success" && approveRes.state == "success") { s.appsRes.data.registration_applications = editRegistrationApplication( diff --git a/src/shared/components/person/reports.tsx b/src/shared/components/person/reports.tsx index 2706c4f..84e6f29 100644 --- a/src/shared/components/person/reports.tsx +++ b/src/shared/components/person/reports.tsx @@ -20,11 +20,7 @@ import { import { i18n } from "../../i18next"; import { InitialFetchRequest } from "../../interfaces"; import { HttpService, UserService } from "../../services"; -import { - RequestState, - apiWrapper, - apiWrapperIso, -} from "../../services/HttpService"; +import { RequestState, apiWrapperIso } from "../../services/HttpService"; import { amAdmin, editCommentReport, @@ -532,36 +528,33 @@ export class Reports extends Component { }; this.setState({ - commentReportsRes: await apiWrapper( - HttpService.client.listCommentReports(form) - ), - postReportsRes: await apiWrapper( - HttpService.client.listPostReports(form) + commentReportsRes: await HttpService.wrappedClient.listCommentReports( + form ), + postReportsRes: await HttpService.wrappedClient.listPostReports(form), }); if (amAdmin()) { this.setState({ - messageReportsRes: await apiWrapper( - HttpService.client.listPrivateMessageReports(form) - ), + messageReportsRes: + await HttpService.wrappedClient.listPrivateMessageReports(form), }); } } async handleResolveCommentReport(form: ResolveCommentReport) { - const res = await apiWrapper(HttpService.client.resolveCommentReport(form)); + const res = await HttpService.wrappedClient.resolveCommentReport(form); this.findAndUpdateCommentReport(res); } async handleResolvePostReport(form: ResolvePostReport) { - const res = await apiWrapper(HttpService.client.resolvePostReport(form)); + const res = await HttpService.wrappedClient.resolvePostReport(form); this.findAndUpdatePostReport(res); } async handleResolvePrivateMessageReport(form: ResolvePrivateMessageReport) { - const res = await apiWrapper( - HttpService.client.resolvePrivateMessageReport(form) + const res = await HttpService.wrappedClient.resolvePrivateMessageReport( + form ); this.findAndUpdatePrivateMessageReport(res); } diff --git a/src/shared/components/person/settings.tsx b/src/shared/components/person/settings.tsx index aabb364..9f56522 100644 --- a/src/shared/components/person/settings.tsx +++ b/src/shared/components/person/settings.tsx @@ -13,11 +13,7 @@ import { } from "lemmy-js-client"; import { i18n, languages } from "../../i18next"; import { UserService } from "../../services"; -import { - HttpService, - RequestState, - apiWrapper, -} from "../../services/HttpService"; +import { HttpService, RequestState } from "../../services/HttpService"; import { Choice, capitalizeFirstLetter, @@ -931,38 +927,31 @@ export class Settings extends Component { async handleBlockPerson({ value }: Choice) { if (value !== "0") { - const res = await apiWrapper( - HttpService.client.blockPerson({ - person_id: Number(value), - block: true, - auth: myAuthRequired(), - }) - ); - + const res = await HttpService.wrappedClient.blockPerson({ + person_id: Number(value), + block: true, + auth: myAuthRequired(), + }); this.personBlock(res); } } async handleUnblockPerson(i: { ctx: Settings; recipientId: number }) { - const res = await apiWrapper( - HttpService.client.blockPerson({ - person_id: i.recipientId, - block: false, - auth: myAuthRequired(), - }) - ); + const res = await HttpService.wrappedClient.blockPerson({ + person_id: i.recipientId, + block: false, + auth: myAuthRequired(), + }); i.ctx.personBlock(res); } async handleBlockCommunity({ value }: Choice) { if (value !== "0") { - const res = await apiWrapper( - HttpService.client.blockCommunity({ - community_id: Number(value), - block: true, - auth: myAuthRequired(), - }) - ); + const res = await HttpService.wrappedClient.blockCommunity({ + community_id: Number(value), + block: true, + auth: myAuthRequired(), + }); this.communityBlock(res); } } @@ -970,13 +959,11 @@ export class Settings extends Component { async handleUnblockCommunity(i: { ctx: Settings; communityId: number }) { const auth = myAuth(); if (auth) { - const res = await apiWrapper( - HttpService.client.blockCommunity({ - community_id: i.communityId, - block: false, - auth: myAuthRequired(), - }) - ); + const res = await HttpService.wrappedClient.blockCommunity({ + community_id: i.communityId, + block: false, + auth: myAuthRequired(), + }); i.ctx.communityBlock(res); } } @@ -1150,13 +1137,10 @@ export class Settings extends Component { event.preventDefault(); i.setState({ saveRes: { state: "loading" } }); - const saveRes = await apiWrapper( - HttpService.client.saveUserSettings({ - ...i.state.saveUserSettingsForm, - auth: myAuthRequired(), - }) - ); - + const saveRes = await HttpService.wrappedClient.saveUserSettings({ + ...i.state.saveUserSettingsForm, + auth: myAuthRequired(), + }); if (saveRes.state === "success") { UserService.Instance.login(saveRes.data); location.reload(); @@ -1174,15 +1158,12 @@ export class Settings extends Component { if (new_password && old_password && new_password_verify) { i.setState({ changePasswordRes: { state: "loading" } }); - const changePasswordRes = await apiWrapper( - HttpService.client.changePassword({ - new_password, - new_password_verify, - old_password, - auth: myAuthRequired(), - }) - ); - + const changePasswordRes = await HttpService.wrappedClient.changePassword({ + new_password, + new_password_verify, + old_password, + auth: myAuthRequired(), + }); if (changePasswordRes.state === "success") { UserService.Instance.login(changePasswordRes.data); window.scrollTo(0, 0); @@ -1205,13 +1186,10 @@ export class Settings extends Component { const password = i.state.deleteAccountForm.password; if (password) { i.setState({ deleteAccountRes: { state: "loading" } }); - const deleteAccountRes = await apiWrapper( - HttpService.client.deleteAccount({ - password, - auth: myAuthRequired(), - }) - ); - + const deleteAccountRes = await HttpService.wrappedClient.deleteAccount({ + password, + auth: myAuthRequired(), + }); if (deleteAccountRes.state === "success") { UserService.Instance.logout(); this.context.router.history.replace("/"); diff --git a/src/shared/components/person/verify-email.tsx b/src/shared/components/person/verify-email.tsx index 2c96f49..51cea95 100644 --- a/src/shared/components/person/verify-email.tsx +++ b/src/shared/components/person/verify-email.tsx @@ -7,11 +7,7 @@ import { wsUserOp, } from "lemmy-js-client"; import { i18n } from "../../i18next"; -import { - HttpService, - RequestState, - apiWrapper, -} from "../../services/HttpService"; +import { HttpService, RequestState } from "../../services/HttpService"; import { setIsoData, toast } from "../../utils"; import { HtmlTags } from "../common/html-tags"; import { Spinner } from "../common/icon"; @@ -39,11 +35,9 @@ export class VerifyEmail extends Component { }); this.setState({ - verifyRes: await apiWrapper( - HttpService.client.verifyEmail({ - token: this.props.match.params.token, - }) - ), + verifyRes: await HttpService.wrappedClient.verifyEmail({ + token: this.props.match.params.token, + }), }); if (this.state.verifyRes.state == "success") { diff --git a/src/shared/components/post/create-post.tsx b/src/shared/components/post/create-post.tsx index b56a7e8..b469df8 100644 --- a/src/shared/components/post/create-post.tsx +++ b/src/shared/components/post/create-post.tsx @@ -8,7 +8,7 @@ import { } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { InitialFetchRequest, PostFormParams } from "../../interfaces"; -import { HttpService, apiWrapper } from "../../services/HttpService"; +import { HttpService } from "../../services/HttpService"; import { Choice, QueryParams, @@ -87,13 +87,10 @@ export class CreatePost extends Component< const auth = myAuth(); if (communityId) { - const res = await apiWrapper( - HttpService.client.getCommunity({ - id: communityId, - auth, - }) - ); - + const res = await HttpService.wrappedClient.getCommunity({ + id: communityId, + auth, + }); if (res.state == "success") { this.setState({ selectedCommunityChoice: { @@ -197,7 +194,7 @@ export class CreatePost extends Component< } async handlePostCreate(form: CreatePostI) { - const res = await apiWrapper(HttpService.client.createPost(form)); + const res = await HttpService.wrappedClient.createPost(form); if (res.state === "success") { const postId = res.data.post_view.post.id; diff --git a/src/shared/components/post/post-form.tsx b/src/shared/components/post/post-form.tsx index 4cd3e18..6ae7b10 100644 --- a/src/shared/components/post/post-form.tsx +++ b/src/shared/components/post/post-form.tsx @@ -13,11 +13,7 @@ import { import { i18n } from "../../i18next"; import { PostFormParams } from "../../interfaces"; import { UserService } from "../../services"; -import { - HttpService, - RequestState, - apiWrapper, -} from "../../services/HttpService"; +import { HttpService, RequestState } from "../../services/HttpService"; import { Choice, archiveTodayUrl, @@ -547,9 +543,7 @@ export class PostForm extends Component { if (url && validURL(url)) { this.setState({ metadataRes: { state: "loading" } }); this.setState({ - metadataRes: await apiWrapper( - HttpService.client.getSiteMetadata({ url }) - ), + metadataRes: await HttpService.wrappedClient.getSiteMetadata({ url }), }); } } @@ -564,18 +558,16 @@ export class PostForm extends Component { if (q && q !== "") { this.setState({ suggestedPostsRes: { state: "loading" } }); this.setState({ - suggestedPostsRes: await apiWrapper( - HttpService.client.search({ - q, - type_: "Posts", - sort: "TopAll", - listing_type: "All", - community_id: this.state.form.community_id, - page: 1, - limit: trendingFetchLimit, - auth: myAuth(), - }) - ), + suggestedPostsRes: await HttpService.wrappedClient.search({ + q, + type_: "Posts", + sort: "TopAll", + listing_type: "All", + community_id: this.state.form.community_id, + page: 1, + limit: trendingFetchLimit, + auth: myAuth(), + }), }); } } diff --git a/src/shared/components/post/post-listing.tsx b/src/shared/components/post/post-listing.tsx index c9efb37..858c0ff 100644 --- a/src/shared/components/post/post-listing.tsx +++ b/src/shared/components/post/post-listing.tsx @@ -27,7 +27,7 @@ import { getExternalHost, getHttpBase } from "../../env"; import { i18n } from "../../i18next"; import { BanType, PostFormParams, PurgeType, VoteType } from "../../interfaces"; import { UserService } from "../../services"; -import { HttpService, apiWrapper } from "../../services/HttpService"; +import { HttpService } from "../../services/HttpService"; import { amAdmin, amCommunityCreator, @@ -1447,7 +1447,7 @@ export class PostListing extends Component { } async handleEditPost(form: EditPost) { - const res = await apiWrapper(HttpService.client.editPost(form)); + const res = await HttpService.wrappedClient.editPost(form); if (res.state === "success") { this.setState({ showEdit: false }); diff --git a/src/shared/components/post/post.tsx b/src/shared/components/post/post.tsx index be6a717..e78a593 100644 --- a/src/shared/components/post/post.tsx +++ b/src/shared/components/post/post.tsx @@ -58,7 +58,6 @@ import { } from "../../interfaces"; import { UserService } from "../../services"; import { - apiWrapper, apiWrapperIso, HttpService, RequestState, @@ -195,24 +194,20 @@ export class Post extends Component { const auth = myAuth(); this.setState({ - postRes: await apiWrapper( - HttpService.client.getPost({ - id: this.state.postId, - comment_id: this.state.commentId, - auth, - }) - ), - commentsRes: await apiWrapper( - HttpService.client.getComments({ - post_id: this.state.postId, - parent_id: this.state.commentId, - max_depth: commentTreeMaxDepth, - sort: this.state.commentSort, - type_: "All", - saved_only: false, - auth, - }) - ), + postRes: await HttpService.wrappedClient.getPost({ + id: this.state.postId, + comment_id: this.state.commentId, + auth, + }), + commentsRes: await HttpService.wrappedClient.getComments({ + post_id: this.state.postId, + parent_id: this.state.commentId, + max_depth: commentTreeMaxDepth, + sort: this.state.commentSort, + type_: "All", + saved_only: false, + auth, + }), }); setupTippy(); @@ -683,29 +678,25 @@ export class Post extends Component { } async handleDeleteCommunityClick(form: DeleteCommunity) { - const deleteCommunityRes = await apiWrapper( - HttpService.client.deleteCommunity(form) + const deleteCommunityRes = await HttpService.wrappedClient.deleteCommunity( + form ); - this.updateCommunity(deleteCommunityRes); } async handleAddModToCommunity(form: AddModToCommunity) { - const addModRes = await apiWrapper( - HttpService.client.addModToCommunity(form) - ); - + const addModRes = await HttpService.wrappedClient.addModToCommunity(form); this.updateModerators(addModRes); } async handleFollow(form: FollowCommunity) { - const followCommunityRes = await apiWrapper( - HttpService.client.followCommunity(form) + const followCommunityRes = await HttpService.wrappedClient.followCommunity( + form ); this.updateCommunity(followCommunityRes); // Update myUserInfo - if (followCommunityRes.state == "success") { + if (followCommunityRes.state === "success") { const communityId = followCommunityRes.data.community_view.community.id; const mui = UserService.Instance.myUserInfo; if (mui) { @@ -715,36 +706,31 @@ export class Post extends Component { } async handlePurgeCommunity(form: PurgeCommunity) { - const purgeCommunityRes = await apiWrapper( - HttpService.client.purgeCommunity(form) + const purgeCommunityRes = await HttpService.wrappedClient.purgeCommunity( + form ); this.purgeItem(purgeCommunityRes); } async handlePurgePerson(form: PurgePerson) { - const purgePersonRes = await apiWrapper( - HttpService.client.purgePerson(form) - ); + const purgePersonRes = await HttpService.wrappedClient.purgePerson(form); this.purgeItem(purgePersonRes); } async handlePurgeComment(form: PurgeComment) { - const purgeCommentRes = await apiWrapper( - HttpService.client.purgeComment(form) - ); + const purgeCommentRes = await HttpService.wrappedClient.purgeComment(form); this.purgeItem(purgeCommentRes); } async handlePurgePost(form: PurgePost) { - const purgeRes = await apiWrapper(HttpService.client.purgePost(form)); + const purgeRes = await HttpService.wrappedClient.purgePost(form); this.purgeItem(purgeRes); } async handleBlockCommunity(form: BlockCommunity) { - const blockCommunityRes = await apiWrapper( - HttpService.client.blockCommunity(form) + const blockCommunityRes = await HttpService.wrappedClient.blockCommunity( + form ); - // TODO Probably isn't necessary this.setState(s => { if ( @@ -762,153 +748,133 @@ export class Post extends Component { } async handleBlockPerson(form: BlockPerson) { - const blockPersonRes = await apiWrapper( - HttpService.client.blockPerson(form) - ); - + const blockPersonRes = await HttpService.wrappedClient.blockPerson(form); if (blockPersonRes.state == "success") { updatePersonBlock(blockPersonRes.data); } } async handleModRemoveCommunity(form: RemoveCommunity) { - const removeCommunityRes = await apiWrapper( - HttpService.client.removeCommunity(form) + const removeCommunityRes = await HttpService.wrappedClient.removeCommunity( + form ); this.updateCommunity(removeCommunityRes); } async handleEditCommunity(form: EditCommunity) { - const res = await apiWrapper(HttpService.client.editCommunity(form)); + const res = await HttpService.wrappedClient.editCommunity(form); this.updateCommunity(res); return res; } async handleCreateComment(form: CreateComment) { - const createCommentRes = await apiWrapper( - HttpService.client.createComment(form) + const createCommentRes = await HttpService.wrappedClient.createComment( + form ); - this.createAndUpdateComments(createCommentRes); return createCommentRes; } async handleEditComment(form: EditComment) { - const editCommentRes = await apiWrapper( - HttpService.client.editComment(form) - ); - + const editCommentRes = await HttpService.wrappedClient.editComment(form); this.findAndUpdateComment(editCommentRes); return editCommentRes; } async handleDeleteComment(form: DeleteComment) { - const deleteCommentRes = await apiWrapper( - HttpService.client.deleteComment(form) + const deleteCommentRes = await HttpService.wrappedClient.deleteComment( + form ); - this.findAndUpdateComment(deleteCommentRes); } async handleDeletePost(form: DeletePost) { - const deleteRes = await apiWrapper(HttpService.client.deletePost(form)); + const deleteRes = await HttpService.wrappedClient.deletePost(form); this.updatePost(deleteRes); } async handleRemovePost(form: RemovePost) { - const removeRes = await apiWrapper(HttpService.client.removePost(form)); + const removeRes = await HttpService.wrappedClient.removePost(form); this.updatePost(removeRes); } async handleRemoveComment(form: RemoveComment) { - const removeCommentRes = await apiWrapper( - HttpService.client.removeComment(form) + const removeCommentRes = await HttpService.wrappedClient.removeComment( + form ); - this.findAndUpdateComment(removeCommentRes); } async handleSaveComment(form: SaveComment) { - const saveCommentRes = await apiWrapper( - HttpService.client.saveComment(form) - ); + const saveCommentRes = await HttpService.wrappedClient.saveComment(form); this.findAndUpdateComment(saveCommentRes); } async handleSavePost(form: SavePost) { - const saveRes = await apiWrapper(HttpService.client.savePost(form)); + const saveRes = await HttpService.wrappedClient.savePost(form); this.updatePost(saveRes); } async handleFeaturePost(form: FeaturePost) { - const featureRes = await apiWrapper(HttpService.client.featurePost(form)); + const featureRes = await HttpService.wrappedClient.featurePost(form); this.updatePost(featureRes); } async handleCommentVote(form: CreateCommentLike) { - const voteRes = await apiWrapper(HttpService.client.likeComment(form)); + const voteRes = await HttpService.wrappedClient.likeComment(form); this.findAndUpdateComment(voteRes); } async handlePostVote(form: CreatePostLike) { - const voteRes = await apiWrapper(HttpService.client.likePost(form)); + const voteRes = await HttpService.wrappedClient.likePost(form); this.updatePost(voteRes); } async handleCommentReport(form: CreateCommentReport) { - const reportRes = await apiWrapper( - HttpService.client.createCommentReport(form) - ); + const reportRes = await HttpService.wrappedClient.createCommentReport(form); if (reportRes.state == "success") { toast(i18n.t("report_created")); } } async handlePostReport(form: CreatePostReport) { - const reportRes = await apiWrapper( - HttpService.client.createPostReport(form) - ); + const reportRes = await HttpService.wrappedClient.createPostReport(form); if (reportRes.state == "success") { toast(i18n.t("report_created")); } } async handleLockPost(form: LockPost) { - const lockRes = await apiWrapper(HttpService.client.lockPost(form)); + const lockRes = await HttpService.wrappedClient.lockPost(form); this.updatePost(lockRes); } async handleDistinguishComment(form: DistinguishComment) { - const distinguishRes = await apiWrapper( - HttpService.client.distinguishComment(form) + const distinguishRes = await HttpService.wrappedClient.distinguishComment( + form ); this.findAndUpdateComment(distinguishRes); } async handleAddAdmin(form: AddAdmin) { - const addAdminRes = await apiWrapper(HttpService.client.addAdmin(form)); + const addAdminRes = await HttpService.wrappedClient.addAdmin(form); - if (addAdminRes.state == "success") { + if (addAdminRes.state === "success") { this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s)); } } async handleTransferCommunity(form: TransferCommunity) { - const transferCommunityRes = await apiWrapper( - HttpService.client.transferCommunity(form) - ); - + const transferCommunityRes = + await HttpService.wrappedClient.transferCommunity(form); this.updateCommunityFull(transferCommunityRes); } async handleFetchChildren(form: GetComments) { - const moreCommentsRes = await apiWrapper( - HttpService.client.getComments(form) - ); - + const moreCommentsRes = await HttpService.wrappedClient.getComments(form); if ( this.state.commentsRes.state == "success" && moreCommentsRes.state == "success" @@ -923,24 +889,24 @@ export class Post extends Component { } async handleCommentReplyRead(form: MarkCommentReplyAsRead) { - const readRes = await apiWrapper( - HttpService.client.markCommentReplyAsRead(form) + const readRes = await HttpService.wrappedClient.markCommentReplyAsRead( + form ); this.findAndUpdateCommentReply(readRes); } async handlePersonMentionRead(form: MarkPersonMentionAsRead) { // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it. - await apiWrapper(HttpService.client.markPersonMentionAsRead(form)); + await HttpService.wrappedClient.markPersonMentionAsRead(form); } async handleBanFromCommunity(form: BanFromCommunity) { - const banRes = await apiWrapper(HttpService.client.banFromCommunity(form)); + const banRes = await HttpService.wrappedClient.banFromCommunity(form); this.updateBan(banRes); } async handleBanPerson(form: BanPerson) { - const banRes = await apiWrapper(HttpService.client.banPerson(form)); + const banRes = await HttpService.wrappedClient.banPerson(form); this.updateBan(banRes); } diff --git a/src/shared/components/private_message/create-private-message.tsx b/src/shared/components/private_message/create-private-message.tsx index 8c5dc4a..585fde5 100644 --- a/src/shared/components/private_message/create-private-message.tsx +++ b/src/shared/components/private_message/create-private-message.tsx @@ -11,7 +11,6 @@ import { InitialFetchRequest } from "../../interfaces"; import { HttpService, RequestState, - apiWrapper, apiWrapperIso, } from "../../services/HttpService"; import { @@ -72,14 +71,12 @@ export class CreatePrivateMessage extends Component< }); this.setState({ - recipientRes: await apiWrapper( - HttpService.client.getPersonDetails({ - person_id: this.state.recipientId, - sort: "New", - saved_only: false, - auth: myAuth(), - }) - ), + recipientRes: await HttpService.wrappedClient.getPersonDetails({ + person_id: this.state.recipientId, + sort: "New", + saved_only: false, + auth: myAuth(), + }), }); } @@ -147,7 +144,7 @@ export class CreatePrivateMessage extends Component< } async handlePrivateMessageCreate(form: CreatePrivateMessageI) { - const res = await apiWrapper(HttpService.client.createPrivateMessage(form)); + const res = await HttpService.wrappedClient.createPrivateMessage(form); if (res.state == "success") { toast(i18n.t("message_sent")); diff --git a/src/shared/components/search.tsx b/src/shared/components/search.tsx index e761a01..413f2c2 100644 --- a/src/shared/components/search.tsx +++ b/src/shared/components/search.tsx @@ -25,7 +25,6 @@ import { CommentViewType, InitialFetchRequest } from "../interfaces"; import { HttpService, RequestState, - apiWrapper, apiWrapperIso, } from "../services/HttpService"; import { @@ -322,14 +321,12 @@ export class Search extends Component { async fetchCommunities() { this.setState({ communitiesRes: { state: "loading" } }); this.setState({ - communitiesRes: await apiWrapper( - HttpService.client.listCommunities({ - type_: defaultListingType, - sort: defaultSortType, - limit: fetchLimit, - auth: myAuth(), - }) - ), + communitiesRes: await HttpService.wrappedClient.listCommunities({ + type_: defaultListingType, + sort: defaultSortType, + limit: fetchLimit, + auth: myAuth(), + }), }); } @@ -879,19 +876,17 @@ export class Search extends Component { if (q && q !== "") { this.setState({ searchRes: { state: "loading" } }); this.setState({ - searchRes: await apiWrapper( - HttpService.client.search({ - q, - community_id: communityId ?? undefined, - creator_id: creatorId ?? undefined, - type_: type, - sort, - listing_type: listingType, - page, - limit: fetchLimit, - auth, - }) - ), + searchRes: await HttpService.wrappedClient.search({ + q, + community_id: communityId ?? undefined, + creator_id: creatorId ?? undefined, + type_: type, + sort, + listing_type: listingType, + page, + limit: fetchLimit, + auth, + }), }); window.scrollTo(0, 0); restoreScrollPosition(this.context); @@ -899,12 +894,10 @@ export class Search extends Component { if (auth) { this.setState({ resolveObjectRes: { state: "loading" } }); this.setState({ - resolveObjectRes: await apiWrapper( - HttpService.client.resolveObject({ - q, - auth, - }) - ), + resolveObjectRes: await HttpService.wrappedClient.resolveObject({ + q, + auth, + }), }); } } diff --git a/src/shared/services/HttpService.ts b/src/shared/services/HttpService.ts index 807472e..269ef78 100644 --- a/src/shared/services/HttpService.ts +++ b/src/shared/services/HttpService.ts @@ -32,6 +32,16 @@ export type RequestState = | FailedRequestState | SuccessRequestState; +type WrappedLemmyHttp = { + [K in keyof LemmyHttp]: LemmyHttp[K] extends (...args: any[]) => any + ? ReturnType extends Promise + ? (...args: Parameters) => Promise> + : ( + ...args: Parameters + ) => Promise> + : LemmyHttp[K]; +}; + export async function apiWrapper( req: Promise ): Promise> { @@ -51,6 +61,40 @@ export async function apiWrapper( } } +class WrappedLemmyHttpClient { + #client: LemmyHttp; + + constructor(client: LemmyHttp) { + this.#client = client; + + for (const key of Object.getOwnPropertyNames( + Object.getPrototypeOf(this.#client) + )) { + WrappedLemmyHttpClient.prototype[key] = async (...args) => { + try { + const res = await this.#client[key](...args); + + return { + data: res, + state: "success", + }; + } catch (error) { + console.error(`API error: ${error}`); + toast(i18n.t(error), "danger"); + return { + state: "failed", + msg: error, + }; + } + }; + } + } +} + +export function getWrappedClient(client: LemmyHttp) { + return new WrappedLemmyHttpClient(client) as unknown as WrappedLemmyHttp; // unfortunately, this verbose cast is necessary +} + /** * A Special type of apiWrapper, used only for the iso routes. * @@ -75,18 +119,24 @@ export function apiWrapperIso( } export class HttpService { - private static _instance: HttpService; - private client: LemmyHttp; + static #_instance: HttpService; + #client: LemmyHttp; + #wrappedClient: WrappedLemmyHttp; private constructor() { - this.client = new LemmyHttp(getHttpBase()); + this.#client = new LemmyHttp(getHttpBase()); + this.#wrappedClient = getWrappedClient(this.#client); } - private static get Instance() { - return this._instance || (this._instance = new this()); + static get #Instance() { + return this.#_instance ?? (this.#_instance = new this()); } public static get client() { - return this.Instance.client; + return this.#Instance.#client; + } + + public static get wrappedClient() { + return this.#Instance.#wrappedClient; } } diff --git a/webpack.config.js b/webpack.config.js index 130b1f2..67b10a9 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -94,7 +94,7 @@ const createClientConfig = (_env, mode) => { plugins: [ ...base.plugins, new ServiceWorkerPlugin({ - enableInDevelopment: mode !== "development", + enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct workbox: { modifyURLPrefix: { "/": "/static/", diff --git a/yarn.lock b/yarn.lock index 795bb23..114861a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5341,10 +5341,10 @@ leac@^0.6.0: resolved "https://registry.yarnpkg.com/leac/-/leac-0.6.0.tgz#dcf136e382e666bd2475f44a1096061b70dc0912" integrity sha512-y+SqErxb8h7nE/fiEX07jsbuhrpO9lL8eca7/Y1nuWV2moNlXhyd59iDGcRf6moVyDMbmTNzL40SUyrFU/yDpg== -lemmy-js-client@0.17.2-rc.20: - version "0.17.2-rc.20" - resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.2-rc.20.tgz#64138cb48fa57f096ee50b33ae18feec10b5f9d7" - integrity sha512-Y8jnGSCNNc65LaFOwE/YqQU+o2Q/PrEz2RW7ASo/zjPWxpoBT4tm1lPveXhTsHlhhvMaFvmK1LyzvIdq2AO5ZQ== +lemmy-js-client@0.17.2-rc.23: + version "0.17.2-rc.23" + resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.2-rc.23.tgz#7e3e2d2ba82b721a9dc092e687874eebeb080637" + integrity sha512-MXUg2s9CZbH5+G53RvfM9M/UnPtH+LnnMc05sWRNVkKDnpl1nHudIrbRTqOOJNikTf1sJLxGvUOi8TTDJ3OSdQ== dependencies: cross-fetch "^3.1.5" form-data "^4.0.0"