shrub/pkg/npm/api/metadata/lib.ts

81 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-02-25 06:54:00 +03:00
import { AppName, Path, Poke, uxToHex, PatpNoSig } from "../lib";
import { Association, Metadata, MetadataUpdate, MetadataUpdateAdd, MetadataUpdateRemove } from './types';
2021-04-22 17:17:39 +03:00
export const METADATA_UPDATE_VERSION = 1;
export const metadataAction = <T extends MetadataUpdate>(data: T, version: number = METADATA_UPDATE_VERSION): Poke<T> => ({
2021-02-25 06:54:00 +03:00
app: 'metadata-push-hook',
2021-04-22 17:17:39 +03:00
mark: `metadata-update-${version}`,
2021-02-25 06:54:00 +03:00
json: data
});
export const add = (
ship: PatpNoSig,
appName: AppName,
resource: Path,
group: Path,
title: string,
description: string,
dateCreated: string,
color: string,
moduleName: string,
): Poke<MetadataUpdateAdd> => metadataAction({
add: {
group,
resource: {
resource,
2021-02-25 18:13:32 +03:00
app: appName
2021-02-25 06:54:00 +03:00
},
metadata: {
title,
description,
color,
'date-created': dateCreated,
creator: `~${ship}`,
2021-04-22 17:17:39 +03:00
config: { graph: moduleName },
2021-02-25 06:54:00 +03:00
picture: '',
2021-04-22 17:17:39 +03:00
hidden: false,
2021-02-25 06:54:00 +03:00
preview: false,
vip: ''
}
}
});
export { add as metadataAdd };
export const remove = (
appName: AppName,
resource: string,
group: string
): Poke<MetadataUpdateRemove> => metadataAction({
remove: {
group,
resource: {
resource,
2021-02-25 18:13:32 +03:00
app: appName
2021-02-25 06:54:00 +03:00
}
}
});
export { remove as metadataRemove };
export const update = (
association: Association,
newMetadata: Partial<Metadata>
): Poke<MetadataUpdateAdd> => {
const metadata = { ...association.metadata, ...newMetadata };
metadata.color = uxToHex(metadata.color);
return metadataAction({
add: {
group: association.group,
resource: {
resource: association.resource,
2021-02-25 18:13:32 +03:00
app: association.app
2021-02-25 06:54:00 +03:00
},
metadata
}
});
}
2021-06-09 03:48:28 +03:00
export { update as metadataUpdate };