Skip to content

Commit

Permalink
Reorganize types to match jsdoc, export Graph type
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaines committed Sep 12, 2023
1 parent e7d877e commit 7b86801
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 145 deletions.
186 changes: 93 additions & 93 deletions packages/viz/test/types/graph-objects.ts
Original file line number Diff line number Diff line change
@@ -1,106 +1,106 @@
import { instance } from "@viz-js/viz";
import { type Graph } from "@viz-js/viz";

instance().then(viz => {
viz.render({});
let graph: Graph;

viz.render({
edges: [
{ tail: "a", head: "b" }
]
});
graph = {};

viz.render({
directed: false,
strict: false,
name: "G",
graphAttributes: {
label: "Test"
},
edgeAttributes: {
color: "green"
},
nodeAttributes: {
shape: "circle"
},
nodes: [
{ name: "a", attributes: { label: "A" } }
],
edges: [
{ tail: "a", head: "b", attributes: { label: "test" } }
],
subgraphs: [
{
name: "cluster1",
graphAttributes: {
color: "green"
},
edgeAttributes: {
color: "blue"
},
nodeAttributes: {
color: "red"
},
subgraphs: [
{
nodes: [
{ name: "b" }
]
}
]
}
]
});
graph = {
edges: [
{ tail: "a", head: "b" }
]
};

viz.render({
graphAttributes: {
width: 2,
abc: true,
label: { html: "<b>test</b>" }
},
nodes: [
{
name: "a",
attributes: {
width: 2,
abc: true,
label: { html: "<b>test</b>" }
graph = {
directed: false,
strict: false,
name: "G",
graphAttributes: {
label: "Test"
},
edgeAttributes: {
color: "green"
},
nodeAttributes: {
shape: "circle"
},
nodes: [
{ name: "a", attributes: { label: "A" } }
],
edges: [
{ tail: "a", head: "b", attributes: { label: "test" } }
],
subgraphs: [
{
name: "cluster1",
graphAttributes: {
color: "green"
},
edgeAttributes: {
color: "blue"
},
nodeAttributes: {
color: "red"
},
subgraphs: [
{
nodes: [
{ name: "b" }
]
}
]
}
]
};

graph = {
graphAttributes: {
width: 2,
abc: true,
label: { html: "<b>test</b>" }
},
nodes: [
{
name: "a",
attributes: {
width: 2,
abc: true,
label: { html: "<b>test</b>" }
}
]
});
}
]
};

graph = {
graphAttributes: {
// @ts-expect-error
blah: null
}
};

viz.render({
graphAttributes: {
graph = {
graphAttributes: {
// @ts-expect-error
label: { stuff: "abc" }
}
};

graph = {
subgraphs: [
{
// @ts-expect-error
blah: null
directed: false
}
});
]
};

viz.render({
graphAttributes: {
graph = {
subgraphs: [
{
// @ts-expect-error
label: { stuff: "abc" }
strict: true
}
});

viz.render({
subgraphs: [
{
// @ts-expect-error
directed: false
}
]
});

viz.render({
subgraphs: [
{
// @ts-expect-error
strict: true
}
]
});
]
};

// @ts-expect-error
viz.render({ a: "b" });
});
// @ts-expect-error
graph = { a: "b" };
117 changes: 65 additions & 52 deletions packages/viz/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,97 @@
type HTMLString = { html: string };
export function instance(): Promise<Viz>;

type AttributeValue = string | number | boolean | HTMLString;
export const graphvizVersion: string;

type Attributes = { [key: string]: AttributeValue };
export const formats: string[];

type Node = {
name: string,
attributes?: Attributes
export const engines: string[];

export {};

export type Graph = {
name?: string;
strict?: boolean;
directed?: boolean;
graphAttributes?: Attributes;
nodeAttributes?: Attributes;
edgeAttributes?: Attributes;
nodes?: Node[];
edges?: Edge[];
subgraphs?: Subgraph[];
};

type Edge = {
tail: string,
head: string,
attributes?: Attributes
type Attributes = {
[name: string]: AttributeValue;
};

type Graph = {
name?: string,
graphAttributes?: Attributes,
nodeAttributes?: Attributes,
edgeAttributes?: Attributes,
nodes?: Node[],
edges?: Edge[],
subgraphs?: Graph[]
type AttributeValue = string | number | boolean | HTMLString;

type HTMLString = {
html: string;
};

type Header = {
strict?: boolean,
directed?: boolean
type Node = {
name: string;
attributes?: Attributes;
};

export type RenderInputObject = Header & Graph;
type Edge = {
tail: string;
head: string;
attributes?: Attributes;
};

type RenderInput = string | RenderInputObject;
type Subgraph = {
name?: string;
graphAttributes?: Attributes;
nodeAttributes?: Attributes;
edgeAttributes?: Attributes;
nodes?: Node[];
edges?: Edge[];
subgraphs?: Subgraph[];
};

export type RenderOptions = {
format?: string;
engine?: string;
yInvert?: boolean;
reduce?: boolean;
graphAttributes?: Attributes;
nodeAttributes?: Attributes;
edgeAttributes?: Attributes;
format?: string;
engine?: string;
yInvert?: boolean;
reduce?: boolean;
graphAttributes?: Attributes;
nodeAttributes?: Attributes;
edgeAttributes?: Attributes;
};

export type RenderError = {
level?: "error" | "warning";
message: string;
};
export type RenderResult = SuccessResult | FailureResult;

type SuccessResult = {
status: "success";
output: string;
errors: RenderError[];
status: "success";
output: string;
errors: RenderError[];
};

type FailureResult = {
status: "failure";
output: undefined;
errors: RenderError[];
status: "failure";
output: undefined;
errors: RenderError[];
};

export type RenderResult = SuccessResult | FailureResult;
export type RenderError = {
level?: "error" | "warning";
message: string;
};

declare class Viz {
get graphvizVersion(): string;

get formats(): string[];

get engines(): string[];

render(input: RenderInput, options?: RenderOptions): RenderResult;
renderString(input: RenderInput, options?: RenderOptions): string;
renderSVGElement(input: RenderInput, options?: RenderOptions): SVGSVGElement;
renderJSON(input: RenderInput, options?: RenderOptions): object;
}
export {};
render(input: string | Graph, options?: RenderOptions): RenderResult;

export function instance(): Promise<Viz>;
renderString(src: any, options?: RenderOptions): string;

export const graphvizVersion: string;
renderSVGElement(src: any, options?: RenderOptions): SVGSVGElement;

export const formats: string[];

export const engines: string[];
renderJSON(src: any, options?: RenderOptions): object;
}

0 comments on commit 7b86801

Please sign in to comment.