Files
freeCodeCamp/client/src/utils/challenge-request-helpers.ts
Oliver Eyton-Williams 4ff00922da refactor: fix hidden eslint errors (#49365)
* refactor: explicit types for validate

* refactor: explicit return types for ui-components

* refactor: use exec instead of match

* refactor: add lots more boundary types

* refactor: more eslint warnings

* refactor: more explicit exports

* refactor: more explicit types

* refactor: even more explicit types

* fix: relax type contrainsts for superblock-order

* refactor: final boundaries

* refactor: avoid using 'object' type

* fix: use named import for captureException

This uses TypeScript (which works) instead of import/namespace
(which doesn't) to check if captureException exists in sentry/gatsby
(it does)
2023-02-13 07:13:50 -08:00

59 lines
1.2 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,
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;
}