Fix fetch (missing request headers etc.)

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak
2024-05-23 18:16:10 +02:00
parent 8ea94e548b
commit 6daa4672d2
23 changed files with 79 additions and 40 deletions

View File

@@ -31,6 +31,8 @@ import { isDefaultHeader } from 'soapbox/utils/accounts';
import copy from 'soapbox/utils/copy';
import { MASTODON, parseVersion } from 'soapbox/utils/features';
import type { PlfeResponse } from 'soapbox/api';
const messages = defineMessages({
edit_profile: { id: 'account.edit_profile', defaultMessage: 'Edit profile' },
linkVerifiedOn: { id: 'account.link_verified_on', defaultMessage: 'Ownership of this link was checked on {date}' },
@@ -93,7 +95,7 @@ const Header: React.FC<IHeader> = ({ account }) => {
const createAndNavigateToChat = useMutation({
mutationFn: (accountId: string) => getOrCreateChatByAccountId(accountId),
onError: (error: { response: Response }) => {
onError: (error: { response: PlfeResponse }) => {
const data = error.response?.json as any;
toast.error(data?.error);
},

View File

@@ -15,6 +15,8 @@ import ConsumersList from './consumers-list';
import LoginForm from './login-form';
import OtpAuthForm from './otp-auth-form';
import type { PlfeResponse } from 'soapbox/api';
const LoginPage = () => {
const dispatch = useAppDispatch();
@@ -49,7 +51,7 @@ const LoginPage = () => {
} else {
setShouldRedirect(true);
}
}).catch((error: { response: Response }) => {
}).catch((error: { response: PlfeResponse }) => {
const data: any = error.response?.json;
if (data?.error === 'mfa_required') {
setMfaAuthNeeded(true);

View File

@@ -15,6 +15,8 @@ import Blankslate from './blankslate';
import EmptyResultsBlankslate from './empty-results-blankslate';
import Results from './results';
import type { PlfeResponse } from 'soapbox/api';
const messages = defineMessages({
placeholder: { id: 'chat_search.placeholder', defaultMessage: 'Type a name' },
});
@@ -44,7 +46,7 @@ const ChatSearch = (props: IChatSearch) => {
const handleClickOnSearchResult = useMutation({
mutationFn: (accountId: string) => getOrCreateChatByAccountId(accountId),
onError: (error: { response: Response }) => {
onError: (error: { response: PlfeResponse }) => {
const data = error.response?.json as any;
toast.error(data?.error);
},

View File

@@ -12,6 +12,7 @@ import toast from 'soapbox/toast';
import ChatComposer from './chat-composer';
import ChatMessageList from './chat-message-list';
import type { PlfeResponse } from 'soapbox/api';
import type { Attachment } from 'soapbox/types/entities';
const fileKeyGen = (): number => Math.floor((Math.random() * 0x10000));
@@ -69,7 +70,7 @@ const Chat: React.FC<ChatInterface> = ({ chat, inputRef, className }) => {
onSuccess: () => {
setErrorMessage(undefined);
},
onError: (error: { response: Response & { json: any } }, _variables, context) => {
onError: (error: { response: PlfeResponse }, _variables, context) => {
const message = error.response?.json?.error;
setErrorMessage(message || intl.formatMessage(messages.failedToSend));
setContent(context.prevContent as string);

View File

@@ -11,6 +11,7 @@ import toast from 'soapbox/toast';
import ColumnForbidden from '../ui/components/column-forbidden';
import type { PlfeResponse } from 'soapbox/api';
import type { Account as AccountEntity } from 'soapbox/schemas';
type RouteParams = { groupId: string };
@@ -80,7 +81,7 @@ const GroupMembershipRequests: React.FC<IGroupMembershipRequests> = ({ params })
const handleAuthorize = async (account: AccountEntity) =>
authorize(account.id)
.then(() => Promise.resolve())
.catch((error: { response: Response }) => {
.catch((error: { response: PlfeResponse }) => {
refetch();
let message = intl.formatMessage(messages.authorizeFail, { name: account.username });
@@ -95,7 +96,7 @@ const GroupMembershipRequests: React.FC<IGroupMembershipRequests> = ({ params })
const handleReject = async (account: AccountEntity) =>
reject(account.id)
.then(() => Promise.resolve())
.catch((error: { response: Response }) => {
.catch((error: { response: PlfeResponse }) => {
refetch();
let message = intl.formatMessage(messages.rejectFail, { name: account.username });

View File

@@ -10,6 +10,8 @@ import toast from 'soapbox/toast';
import { isDefaultAvatar } from 'soapbox/utils/accounts';
import resizeImage from 'soapbox/utils/resize-image';
import type { PlfeResponse } from 'soapbox/api';
const messages = defineMessages({
error: { id: 'onboarding.error', defaultMessage: 'An unexpected error occurred. Please try again or skip this step.' },
});
@@ -48,7 +50,7 @@ const AvatarSelectionStep = ({ onNext }: { onNext: () => void }) => {
setDisabled(false);
setSubmitting(false);
onNext();
}).catch((error: { response: Response }) => {
}).catch((error: { response: PlfeResponse }) => {
setSubmitting(false);
setDisabled(false);
setSelectedFile(null);

View File

@@ -7,6 +7,8 @@ import { Button, FormGroup, Stack, Textarea } from 'soapbox/components/ui';
import { useAppDispatch, useOwnAccount } from 'soapbox/hooks';
import toast from 'soapbox/toast';
import type { PlfeResponse } from 'soapbox/api';
const messages = defineMessages({
bioPlaceholder: { id: 'onboarding.bio.placeholder', defaultMessage: 'Tell the world a little about yourself…' },
error: { id: 'onboarding.error', defaultMessage: 'An unexpected error occurred. Please try again or skip this step.' },
@@ -30,7 +32,7 @@ const BioStep = ({ onNext }: { onNext: () => void }) => {
.then(() => {
setSubmitting(false);
onNext();
}).catch((error: { response: Response }) => {
}).catch((error: { response: PlfeResponse }) => {
setSubmitting(false);
if (error.response?.status === 422) {

View File

@@ -11,6 +11,8 @@ import toast from 'soapbox/toast';
import { isDefaultHeader } from 'soapbox/utils/accounts';
import resizeImage from 'soapbox/utils/resize-image';
import type { PlfeResponse } from 'soapbox/api';
const messages = defineMessages({
header: { id: 'account.header.alt', defaultMessage: 'Profile header' },
error: { id: 'onboarding.error', defaultMessage: 'An unexpected error occurred. Please try again or skip this step.' },
@@ -51,7 +53,7 @@ const CoverPhotoSelectionStep = ({ onNext }: { onNext: () => void }) => {
setDisabled(false);
setSubmitting(false);
onNext();
}).catch((error: { response: Response }) => {
}).catch((error: { response: PlfeResponse }) => {
setSubmitting(false);
setDisabled(false);
setSelectedFile(null);

View File

@@ -7,6 +7,8 @@ import { Button, FormGroup, Input, Stack } from 'soapbox/components/ui';
import { useAppDispatch, useOwnAccount } from 'soapbox/hooks';
import toast from 'soapbox/toast';
import type { PlfeResponse } from 'soapbox/api';
const messages = defineMessages({
usernamePlaceholder: { id: 'onboarding.display_name.placeholder', defaultMessage: 'Eg. John Smith' },
error: { id: 'onboarding.error', defaultMessage: 'An unexpected error occurred. Please try again or skip this step.' },
@@ -41,7 +43,7 @@ const DisplayNameStep = ({ onNext }: { onNext: () => void }) => {
.then(() => {
setSubmitting(false);
onNext();
}).catch((error: { response: Response }) => {
}).catch((error: { response: PlfeResponse }) => {
setSubmitting(false);
if (error.response?.status === 422) {

View File

@@ -11,6 +11,8 @@ import ConfirmationStep from './steps/confirmation-step';
import DetailsStep from './steps/details-step';
import PrivacyStep from './steps/privacy-step';
import type { PlfeResponse } from 'soapbox/api';
const messages = defineMessages({
next: { id: 'manage_group.next', defaultMessage: 'Next' },
create: { id: 'manage_group.create', defaultMessage: 'Create Group' },
@@ -64,7 +66,7 @@ const CreateGroupModal: React.FC<ICreateGroupModal> = ({ onClose }) => {
setCurrentStep(Steps.THREE);
setGroup(group);
},
onError(error: { response?: Response }) {
onError(error: { response?: PlfeResponse }) {
const msg = z.object({ error: z.string() }).safeParse(error?.response?.json);
if (msg.success) {
toast.error(msg.data.error);

View File

@@ -13,6 +13,8 @@ import { useAppDispatch, useFeatures, useOwnAccount, useRegistrationStatus } fro
import ProfileDropdown from './profile-dropdown';
import type { PlfeResponse } from 'soapbox/api';
const messages = defineMessages({
login: { id: 'navbar.login.action', defaultMessage: 'Log in' },
username: { id: 'navbar.login.username.placeholder', defaultMessage: 'Email or username' },
@@ -50,7 +52,7 @@ const Navbar = () => {
.then(() => dispatch(fetchInstance()))
);
})
.catch((error: { response: Response }) => {
.catch((error: { response: PlfeResponse }) => {
setLoading(false);
const data: any = error.response?.json;