Fix FormData

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-05-12 19:43:57 +02:00
parent 47b01ca078
commit ec918d497d
5 changed files with 16 additions and 9 deletions

View file

@ -129,6 +129,7 @@
"localforage": "^1.10.0",
"lodash": "^4.7.11",
"mini-css-extract-plugin": "^2.6.0",
"object-to-formdata": "^4.5.1",
"path-browserify": "^1.0.1",
"postcss": "^8.4.29",
"process": "^0.11.10",

View file

@ -1,3 +1,5 @@
import { serialize } from 'object-to-formdata';
import { selectAccount } from 'soapbox/selectors';
import { setSentryAccount } from 'soapbox/sentry';
import KVStore from 'soapbox/storage/kv-store';
@ -70,14 +72,11 @@ const patchMe = (params: Record<string, any>, isFormData = false) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch(patchMeRequest());
const headers: HeadersInit = isFormData ? {
'Content-Type': 'multipart/form-data',
} : {};
const headers: HeadersInit = isFormData ? { 'Content-Type': '' } : {};
let body: FormData | string;
if (isFormData) {
body = new FormData();
Object.entries(params).forEach(([key, value]) => (body as FormData).append(key, value));
body = serialize(params, { indices: true });
} else {
body = JSON.stringify(params);
}

View file

@ -1,3 +1,5 @@
import { serialize } from 'object-to-formdata';
import { Entities } from 'soapbox/entity-store/entities';
import { useCreateEntity } from 'soapbox/entity-store/hooks';
import { useApi } from 'soapbox/hooks/useApi';
@ -17,9 +19,7 @@ function useCreateGroup() {
const api = useApi();
const { createEntity, ...rest } = useCreateEntity([Entities.GROUPS, 'search', ''], (params: CreateGroupParams) => {
const formData = new FormData();
Object.entries(params).forEach(([key, value]) => formData.append(key, value));
const formData = serialize(params, { indices: true });
return api('/api/v1/groups', {
method: 'POST',

View file

@ -1,3 +1,5 @@
import { serialize } from 'object-to-formdata';
import { Entities } from 'soapbox/entity-store/entities';
import { useCreateEntity } from 'soapbox/entity-store/hooks';
import { useApi } from 'soapbox/hooks/useApi';
@ -16,7 +18,7 @@ function useUpdateGroup(groupId: string) {
const api = useApi();
const { createEntity, ...rest } = useCreateEntity([Entities.GROUPS], (params: UpdateGroupParams) => {
const formData = new FormData();
const formData = serialize(params, { indices: true });
Object.entries(params).forEach(([key, value]) => formData.append(key, value));

View file

@ -6595,6 +6595,11 @@ object-keys@^1.1.1:
resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.1.1.tgz#1c47f272df277f3b1daf061677d9c82e2322c60e"
integrity sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==
object-to-formdata@^4.5.1:
version "4.5.1"
resolved "https://registry.yarnpkg.com/object-to-formdata/-/object-to-formdata-4.5.1.tgz#b6955a9c505b58df15852fee5f844b418b3eb6fe"
integrity sha512-QiM9D0NiU5jV6J6tjE1g7b4Z2tcUnKs1OPUi4iMb2zH+7jwlcUrASghgkFk9GtzqNNq8rTQJtT8AzjBAvLoNMw==
object.assign@^4.1.4:
version "4.1.4"
resolved "https://registry.yarnpkg.com/object.assign/-/object.assign-4.1.4.tgz#9673c7c7c351ab8c4d0b516f4343ebf4dfb7799f"