Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createIntegrationEntity should take Typescript Generic #728

Open
zemberdotnet opened this issue May 31, 2022 · 1 comment
Open

createIntegrationEntity should take Typescript Generic #728

zemberdotnet opened this issue May 31, 2022 · 1 comment
Labels

Comments

@zemberdotnet
Copy link
Member

No description provided.

@zemberdotnet
Copy link
Member Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant