sdk icon indicating copy to clipboard operation
sdk copied to clipboard

`createIntegrationEntity` should take Typescript Generic

Open zemberdotnet opened this issue 3 years ago • 1 comments

zemberdotnet avatar May 31 '22 14:05 zemberdotnet

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: {},
  },
});

zemberdotnet avatar Jun 14 '22 18:06 zemberdotnet