Improve metadata value conversion
The current approach has issues with numbers of type long
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user