sdk
sdk copied to clipboard
`createIntegrationEntity` should take Typescript Generic
Sketching ideas.
import { validateRawData } from './rawData';
import { ResourceTagList, ResourceTagMap } from './tagging';
enum EntityClasses {
DEVICE = 'Device',
}
export interface GraphObject {
_key: string;
_type: string;
_class: string[];
}
export interface Entity extends GraphObject {
active: boolean;
[k: string]:
| string
| boolean
| number
| (string | boolean | number)[]
| null
| undefined;
}
export interface Device extends Entity {
deviceId: string | null;
make?: string;
}
/**
* A type representing entity data from a provider.
*/
type ProviderSourceData = {
/**
* Some providers include a collection of `tags` that will be stored on the
* generated entity as `tag.propertyName`, `propertyName` when the tag is
* registered in `tagProperties` or is known to be a common tag property name,
* and the tag values will be collected in the generated entity as `tags` (a
* `string[]`);
*/
tags?: ResourceTagList | ResourceTagMap;
[key: string]: any;
};
type GeneratedEntity<T extends Entity> = T & { _rawData: any };
interface NewEntityDataInput<T> {
entityData: {
assign: T;
source: ProviderSourceData;
};
}
export function createIntegrationEntity<T extends Entity>(
input: NewEntityDataInput<T>,
): GeneratedEntity<T> {
const generatedEntity: GeneratedEntity<T> = {
...input.entityData.assign,
_rawData: input.entityData.source,
};
validateRawData(generatedEntity);
return generatedEntity;
}
const d: Device = {
deviceId: null,
active: false,
_key: '',
_type: '',
_class: [],
};
const x = createIntegrationEntity<Device>({
entityData: {
assign: {
_class: ['Device'],
_key: 'a',
_type: 'b',
active: false,
deviceId: 'ss',
lol: 'x',
},
source: {},
},
});