2024-10-05 23:17:20 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { MetaType } from '@/lib/graphql/generated/graphql.ts';
|
2025-05-17 23:17:36 +02:00
|
|
|
import { AllowedMetadataValueTypes, AppMetadataKeys, Metadata } from '@/modules/metadata/Metadata.types.ts';
|
2025-06-24 22:22:53 +02:00
|
|
|
import { APP_METADATA } from '@/modules/metadata/Metadata.constants.ts';
|
2024-10-05 23:17:20 +02:00
|
|
|
|
|
|
|
|
export const convertValueFromMetadata = <T extends AllowedMetadataValueTypes = AllowedMetadataValueTypes>(
|
2025-05-17 23:17:36 +02:00
|
|
|
key: string,
|
2024-10-05 23:17:20 +02:00
|
|
|
value: string,
|
2025-06-25 00:38:01 +02:00
|
|
|
defaultValue: any,
|
|
|
|
|
): T => APP_METADATA[key as AppMetadataKeys].convert(value, defaultValue);
|
2024-10-05 23:17:20 +02:00
|
|
|
|
2024-11-04 18:00:35 +01:00
|
|
|
export const convertFromGqlMeta = (
|
|
|
|
|
gqlMetadata?: MetaType[],
|
|
|
|
|
filter: (key: string) => boolean = () => true,
|
|
|
|
|
): Metadata | undefined => {
|
2024-10-05 23:17:20 +02:00
|
|
|
if (!gqlMetadata) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const metadata: Metadata = {};
|
|
|
|
|
gqlMetadata.forEach(({ key, value }) => {
|
2024-11-04 18:00:35 +01:00
|
|
|
if (filter(key)) {
|
|
|
|
|
metadata[key] = value;
|
|
|
|
|
}
|
2024-10-05 23:17:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return metadata;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const convertToGqlMeta = (metadata?: Metadata): MetaType[] | undefined => {
|
|
|
|
|
if (!metadata) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Object.entries(metadata).map(([key, value]) => ({ key, value }));
|
|
|
|
|
};
|