Refactor TypeScript definitions for D2 module and update response types

This commit is contained in:
jolo-dev 2025-02-23 14:45:34 +01:00
parent 74e22772b8
commit 7cf6e2b42a

View file

@ -1,4 +1,4 @@
declare module "index" { declare module "@terrastruct/d2" {
interface Options { interface Options {
/** /**
* @default 0 * @default 0
@ -57,11 +57,6 @@ declare module "index" {
options: Options; options: Options;
} }
// Replace the properties below with the actual structure of the workers responses.
export interface CompileResult {
compiled: string;
}
export interface RenderResult { export interface RenderResult {
svg: string; svg: string;
} }
@ -79,9 +74,13 @@ declare module "index" {
| { type: "error"; error: string } | { type: "error"; error: string }
| { | {
type: "result"; type: "result";
data: CompileResult | RenderResult | EncodedResult | DecodedResult; data: string | EncodedResult | DecodedResult;
}; };
export interface CompileResult {
result: string;
}
export interface D2Worker { export interface D2Worker {
on(event: "message", listener: (data: WorkerMessage) => void): void; on(event: "message", listener: (data: WorkerMessage) => void): void;
on(event: "error", listener: (error: Error) => void): void; on(event: "error", listener: (error: Error) => void): void;
@ -95,7 +94,7 @@ declare module "index" {
worker: D2Worker; worker: D2Worker;
currentResolve?: ( currentResolve?: (
result: result:
| CompileResult | string
| RenderResult | RenderResult
| EncodedResult | EncodedResult
| DecodedResult, | DecodedResult,
@ -134,14 +133,14 @@ declare module "index" {
compile( compile(
input: string | CompileRequest, input: string | CompileRequest,
options?: Options, options?: Options,
): Promise<CompileResult>; ): Promise<string>;
/** /**
* Renders the given diagram. * Renders the given diagram.
* @param diagram A diagram definition in string form. * @param diagram A diagram definition in string form.
* @param options Optional rendering options. * @param options Optional rendering options.
*/ */
render(diagram: string, options?: Options): Promise<RenderResult>; render(diagram: string, options?: Options): Promise<string>;
/** /**
* Encodes the provided script. * Encodes the provided script.