Download admin-bro/src/backend/utils/request-parser/request-parser.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.93 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/admin-bro/src/backend/utils/request-parser/request-parser.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/admin-bro/src/backend/utils/request-parser/request-parser.ts
-
curl -L -o request-parser.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/admin-bro/src/backend/utils/request-parser/request-parser.ts
1.93 kB
| import { ActionRequest } from '../../actions/index.js' | |
| import { BaseResource } from '../../adapters/index.js' | |
| import { | |
| FORM_VALUE_NULL, | |
| FORM_VALUE_EMPTY_OBJECT, | |
| FORM_VALUE_EMPTY_ARRAY, | |
| } from '../../../frontend/hooks/use-record/params-to-form-data.js' | |
| /** | |
| * Takes the original ActionRequest and convert string values to a corresponding | |
| * types. It | |
| * | |
| * @param {ActionRequest} originalRequest | |
| * @param {BaseResource} resource | |
| * @returns {ActionRequest} | |
| * | |
| * @private | |
| */ | |
| export const requestParser = ( | |
| originalRequest: ActionRequest, | |
| resource: BaseResource, | |
| ): ActionRequest => { | |
| const { payload: originalPayload } = originalRequest | |
| const payload = Object.entries(originalPayload || {}).reduce((memo, [path, formValue]) => { | |
| const property = resource._decorated?.getPropertyByKey(path) | |
| let value = formValue | |
| if (formValue === FORM_VALUE_NULL) { value = null } | |
| if (formValue === FORM_VALUE_EMPTY_OBJECT) { value = {} } | |
| if (formValue === FORM_VALUE_EMPTY_ARRAY) { value = [] } | |
| if (property) { | |
| if (property.type() === 'boolean') { | |
| if (value === 'true') { | |
| memo[path] = true | |
| return memo | |
| } | |
| if (value === 'false') { | |
| memo[path] = false | |
| return memo | |
| } | |
| if (value === '') { | |
| memo[path] = false | |
| return memo | |
| } | |
| } | |
| if (['date', 'datetime'].includes(property.type())) { | |
| if (value === '' || value === null) { | |
| memo[path] = null | |
| return memo | |
| } | |
| } | |
| if (property.type() === 'string') { | |
| const availableValues = property.availableValues() | |
| if (availableValues && !availableValues.includes(value) && value === '') { | |
| memo[path] = null | |
| return memo | |
| } | |
| } | |
| } | |
| memo[path] = value | |
| return memo | |
| }, {}) | |
| return { | |
| ...originalRequest, | |
| payload, | |
| } | |
| } | |
| export default requestParser | |