Skip to content

Commit

Permalink
feat: builds a way for multipart/form-data reqs to retain non-string …
Browse files Browse the repository at this point in the history
…values
  • Loading branch information
jmikrut committed Dec 31, 2021
1 parent cd0e172 commit 4efc2cf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/admin/components/elements/SaveDraft/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const SaveDraft: React.FC = () => {

const canSaveDraft = modified;

console.log({ modified });

const saveDraft = useCallback(() => {
const search = `?locale=${locale}&depth=0&fallback-locale=null&draft=true`;
let action;
Expand Down
13 changes: 12 additions & 1 deletion src/admin/components/forms/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,24 @@ const Form: React.FC<Props> = (props) => {
const createFormData = useCallback((overrides: any = {}) => {
const data = reduceFieldsToValues(contextRef.current.fields);

const file = data?.file;

if (file) {
delete data.file;
}

const dataWithOverrides = {
...data,
...overrides,
};

const dataToSerialize = {
_payload: JSON.stringify(dataWithOverrides),
file,
};

// nullAsUndefineds is important to allow uploads and relationship fields to clear themselves
const formData = serialize(dataWithOverrides, { indices: true, nullsAsUndefineds: false });
const formData = serialize(dataToSerialize, { indices: true, nullsAsUndefineds: false });
return formData;
}, [contextRef]);

Expand Down
4 changes: 0 additions & 4 deletions src/admin/components/forms/field-types/RichText/RichText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,6 @@ const RichText: React.FC<Props> = (props) => {

if (!valueToRender) valueToRender = defaultValue;

if (path === 'defaultRichText') {
console.log(valueToRender);
}

return (
<div
className={classes}
Expand Down
14 changes: 14 additions & 0 deletions src/express/middleware/convertPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Request, Response, NextFunction } from 'express';

export default (req: Request, _: Response, next: NextFunction): void => {
if (req.body?._payload) {
const payloadJSON = JSON.parse(req.body._payload);

req.body = {
...req.body,
...payloadJSON,
};
}

next();
};
2 changes: 2 additions & 0 deletions src/express/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import identifyAPI from './identifyAPI';
import { Payload } from '../..';
import { PayloadRequest } from '../types';
import corsHeaders from './corsHeaders';
import convertPayload from './convertPayload';

const middleware = (payload: Payload): any => {
const rateLimitOptions: {
Expand Down Expand Up @@ -39,6 +40,7 @@ const middleware = (payload: Payload): any => {
parseNested: true,
...payload.config.upload,
}),
convertPayload,
corsHeaders(payload.config),
authenticate(payload.config),
...(payload.config.express.middleware || []),
Expand Down

0 comments on commit 4efc2cf

Please sign in to comment.