Improve metadata value conversion

The current approach has issues with numbers of type long
This commit is contained in:
schroda
2025-05-17 23:17:36 +02:00
parent 822e47084e
commit bf90d95266
3 changed files with 97 additions and 90 deletions

View File

@@ -7,24 +7,29 @@
*/
import { MetaType } from '@/lib/graphql/generated/graphql.ts';
import { AllowedMetadataValueTypes, Metadata } from '@/modules/metadata/Metadata.types.ts';
import { AllowedMetadataValueTypes, AppMetadataKeys, Metadata } from '@/modules/metadata/Metadata.types.ts';
import { APP_METADATA_KEY_TO_TYPE } from '@/modules/metadata/Metadata.constants.ts';
export const convertValueFromMetadata = <T extends AllowedMetadataValueTypes = AllowedMetadataValueTypes>(
key: string,
value: string,
): T => {
if (!Number.isNaN(+value)) {
const typeOfKey = APP_METADATA_KEY_TO_TYPE[key as AppMetadataKeys];
const isAutoType = typeOfKey === 'auto';
if ((isAutoType && !Number.isNaN(+value)) || typeOfKey === 'number') {
return +value as T;
}
if (value === 'true' || value === 'false') {
if ((isAutoType && (value === 'true' || value === 'false')) || typeOfKey === 'boolean') {
return (value === 'true') as T;
}
if (value === 'undefined') {
if (isAutoType && value === 'undefined') {
return undefined as T;
}
if (value === 'null') {
if (isAutoType && value === 'null') {
return null as T;
}