Files
freeCodeCamp/client/src/utils/challenge-request-helpers.ts
Oliver Eyton-Williams 6e787d3336 feat(api): add /user/get-session-user (#50557)
Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
2023-07-11 16:28:56 +00:00

61 lines
1.4 KiB
TypeScript

import { ChallengeFiles } from '../redux/prop-types';
/*
* Express's body-parser has a default size limit of 102400 bytes for a request body.
* These helper functions make sure the request body isn't too big when saving or submitting multifile cert projects
*/
export const MAX_BODY_SIZE = 102400;
interface StandardizeRequestBodyArgs {
id: string;
challengeFiles: ChallengeFiles;
challengeType: number;
}
interface File {
contents: string;
ext: string;
history: string[];
key: string;
name: string;
}
interface Body {
id: string;
files?: File[];
challengeType: number;
}
export function standardizeRequestBody({
id,
challengeFiles = [],
challengeType
}: StandardizeRequestBodyArgs): Body {
return {
id,
files: challengeFiles?.map(({ fileKey, contents, ext, name, history }) => {
return {
contents,
ext,
history, // TODO(Post-MVP): stop sending history, if possible. The client
// already gets it from the curriculum, so it should not be necessary to
// save it in the db.
key: fileKey,
name
};
}),
challengeType
};
}
export function getStringSizeInBytes(str = ''): number {
const stringSizeInBytes = new Blob([JSON.stringify(str)]).size;
return stringSizeInBytes;
}
export function bodySizeFits(bodySizeInBytes: number): boolean {
return bodySizeInBytes <= MAX_BODY_SIZE;
}