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

100 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-08-30 03:54:09 +03:00
import { Path, Poke, uxToHex, PatpNoSig } from '../lib';
import { MdAppName, Association, Metadata, MetadataUpdate, MetadataUpdateAdd, MetadataUpdateRemove, MetadataEditField, MetadataUpdateEdit } from './types';
2021-02-25 06:54:00 +03:00
2021-07-21 05:26:33 +03:00
export const METADATA_UPDATE_VERSION = 2;
2021-04-22 17:17:39 +03:00
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,
2021-08-30 03:54:09 +03:00
appName: MdAppName,
2021-02-25 06:54:00 +03:00
resource: Path,
group: Path,
title: string,
description: string,
dateCreated: string,
color: string,
moduleName: string
2021-02-25 06:54:00 +03:00
): Poke<MetadataUpdateAdd> => metadataAction({
add: {
group,
resource: {
resource,
'app-name': 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 = (
2021-08-30 03:54:09 +03:00
appName: MdAppName,
2021-02-25 06:54:00 +03:00
resource: string,
group: string
2021-08-30 03:54:09 +03:00
): Poke<MetadataUpdateRemove> => metadataAction<MetadataUpdateRemove>({
2021-02-25 06:54:00 +03:00
remove: {
group,
resource: {
resource,
'app-name': appName
2021-02-25 06:54:00 +03:00
}
}
});
export { remove as metadataRemove };
export const edit = (
association: Association,
edit: MetadataEditField
2021-08-30 03:54:09 +03:00
): Poke<MetadataUpdateEdit> => metadataAction<MetadataUpdateEdit>({
edit: {
group: association.group,
resource: {
resource: association.resource,
'app-name': association['app-name']
},
edit
}
});
export { edit as metadataEdit };
/**
* @deprecated use {@link edit} instead
*/
2021-02-25 06:54:00 +03:00
export const update = (
association: Association,
newMetadata: Partial<Metadata>
): Poke<MetadataUpdateAdd> => {
const metadata = { ...association.metadata, ...newMetadata };
metadata.color = uxToHex(metadata.color);
2021-08-30 03:54:09 +03:00
return metadataAction<MetadataUpdateAdd>({
2021-02-25 06:54:00 +03:00
add: {
group: association.group,
resource: {
resource: association.resource,
'app-name': association['app-name']
2021-02-25 06:54:00 +03:00
},
metadata
}
});
};
2021-02-25 06:54:00 +03:00
2021-06-09 03:48:28 +03:00
export { update as metadataUpdate };