@infrascan/shared-types 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @infrascan/shared-types
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#82](https://github.com/infrascan/infrascan/pull/82) [`e28a6d9`](https://github.com/infrascan/infrascan/commit/e28a6d91eb36fa83e9a40a667eb39a15b2a45ccb) Thanks [@lfarrel6](https://github.com/lfarrel6)! - Introduce new readable/writable interface for @infrascan/core graph interface to make types nicer to work with. Previously, the graph returned union types to bridge the Graph's internal model of the state with its exposed API for adding children/parents/edges. This resulted in a lot of properties with signatures like `parent?: Node | string`.
8
+
9
+ This has been updated to wrap the Node and Edge types in `Readable` and `Writable` types based on the context they're being used in. This is inspired by the Kysely `Selectable`/`Insertable` pattern. When a Node is `Readable` its edges, parent and children will resolve to other Node objects. When `Writable`, all other Nodes are expected to be referenced using a string type containing their ID.
10
+
11
+ Introduce @infrascan/node-reducer-plugin which reduces graph complexity for dynamicially provisioned infrastructure.
12
+
3
13
  ## 0.4.0
4
14
 
5
15
  ### Minor Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infrascan/shared-types",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Shared types for multiple infrascan packages",
5
5
  "main": "src/index.ts",
6
6
  "scripts": {
package/src/graph.ts CHANGED
@@ -7,50 +7,83 @@ export interface SelectedNode {
7
7
  parent?: string;
8
8
  type?: string;
9
9
  rawState?: any;
10
- };
10
+ }
11
11
 
12
12
  export interface SelectedEdgeTarget {
13
- name: string,
14
- target: string
13
+ name: string;
14
+ target: string;
15
15
  }
16
16
 
17
17
  export interface SelectedEdge {
18
18
  source: string;
19
19
  target: string;
20
- metadata?: Record<string, unknown>
20
+ metadata?: Record<string, unknown>;
21
+ }
22
+
23
+ // Create Read/Write types inspired by kysely
24
+ export type ElementType<ReadType, WriteType> = {
25
+ readonly _read: ReadType;
26
+ readonly _write: WriteType;
27
+ };
28
+
29
+ export type ElementRecord<K extends keyof any, R, W> = ElementType<
30
+ Record<K, Readable<R>>,
31
+ Record<K, Writable<W>>
32
+ >;
33
+
34
+ export type ReadType<T> = T extends ElementType<infer R, any> ? Readable<R> : T;
35
+ export type Readable<T> = {
36
+ [K in keyof T]: ReadType<T[K]>;
37
+ };
38
+
39
+ export type WriteType<T> = T extends ElementType<any, infer W>
40
+ ? Writable<W>
41
+ : T;
42
+ export type Writable<T> = {
43
+ [K in keyof T]: WriteType<T[K]>;
21
44
  };
22
45
 
23
46
  export interface Node {
24
- id: string,
25
- name: string,
47
+ id: string;
48
+ name: string;
26
49
  metadata: Record<string, unknown>;
27
- service?: string,
28
- type?: string,
29
- parent?: string | Node,
30
- children?: Record<string, Node>,
31
- incomingEdges: Record<string, Edge<Node>>,
32
- outgoingEdges: Record<string, Edge<Node>>,
50
+ service?: string;
51
+ type?: string;
52
+ parent?: ElementType<Node, string>;
53
+ children?: ElementRecord<string, Node, string>;
54
+ incomingEdges: ElementRecord<string, Edge, string>;
55
+ outgoingEdges: ElementRecord<string, Edge, string>;
33
56
  }
34
57
 
35
- export interface Edge<T extends string | Node> {
36
- id: string,
37
- name?: string,
38
- metadata: Record<string, unknown>,
39
- source: T,
40
- target: T
58
+ export interface Edge {
59
+ id: string;
60
+ name?: string;
61
+ metadata: Record<string, unknown>;
62
+ source: ElementType<Node, string>;
63
+ target: ElementType<Node, string>;
41
64
  }
42
65
 
43
66
  export interface Graph {
44
- nodes: Node[],
45
- edges: Edge<Node>[],
46
- addNode: (node: Pick<Node, "id" | "name" | "metadata" | "parent" | "service" | "type">) => void,
47
- addEdge: (edge: Pick<Edge<string>, "name" | "source" | "target" | "metadata">) => void,
48
- addChild: (parent: string, child: string) => void,
49
- getNode: (id: string) => Node | undefined,
50
- mapNodesById: (id: string, mapperFn: (node: Node) => Node[]) => void,
51
- removeEdge: (id: string) => Edge<Node>,
52
- removeNode: (id: string) => Node,
53
- serialize: () => string
54
- };
67
+ nodes: Readable<Node>[];
68
+ edges: Readable<Edge>[];
69
+ addNode: (
70
+ node: Pick<
71
+ Writable<Node>,
72
+ "id" | "name" | "metadata" | "parent" | "service" | "type"
73
+ >,
74
+ ) => void;
75
+ addEdge: (
76
+ edge: Pick<Writable<Edge>, "name" | "source" | "target" | "metadata">,
77
+ ) => void;
78
+ addChild: (parent: string, child: string) => void;
79
+ getNode: (id: string) => Readable<Node> | undefined;
80
+ mapNodesById: (
81
+ id: string,
82
+ mapperFn: (node: Readable<Node>) => Writable<Node>[],
83
+ ) => void;
84
+ removeEdge: (id: string) => Readable<Edge>;
85
+ removeNode: (id: string) => Readable<Node>;
86
+ serialize: () => string;
87
+ }
55
88
 
56
- export type GraphSerializer<T> = (graph: Graph) => T;
89
+ export type GraphSerializer<T> = (graph: Graph) => T;
package/tsconfig.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "extends": "tsconfig/base.json",
2
+ "extends": "@infrascan/tsconfig/base.json",
3
3
  "compilerOptions": {
4
4
  "baseUrl": "src",
5
5
  "noEmit": true,