diff --git a/d2js/js/src/index.d.ts b/d2js/js/src/index.d.ts index d320b1774..182eb6a23 100644 --- a/d2js/js/src/index.d.ts +++ b/d2js/js/src/index.d.ts @@ -50,31 +50,109 @@ declare module "index" { center?: boolean; } - interface Request { - fs?: { index: string }; - options?: Options; + export interface CompileRequest { + fs?: { + index: string; + }; + options: Options; + } + + // Replace the properties below with the actual structure of the worker’s responses. + export interface CompileResult { + compiled: string; + } + + export interface RenderResult { + svg: string; + } + + export interface EncodedResult { + encoded: string; + } + + export interface DecodedResult { + decoded: string; + } + + export type WorkerMessage = + | { type: "ready" } + | { type: "error"; error: string } + | { + type: "result"; + data: CompileResult | RenderResult | EncodedResult | DecodedResult; + }; + + export interface D2Worker { + on(event: "message", listener: (data: WorkerMessage) => void): void; + on(event: "error", listener: (error: Error) => void): void; + onmessage?: (e: { data: WorkerMessage }) => void; + onerror?: (error: Error) => void; + postMessage(message: { type: string; data: object }): void; } export class D2 { - ready: Promise; - currentResolve?: (value: any) => void; - currentReject?: (reason?: any) => void; - worker: any; + readonly ready: Promise; + worker: D2Worker; + currentResolve?: ( + result: + | CompileResult + | RenderResult + | EncodedResult + | DecodedResult, + ) => void; + currentReject?: (reason: Error) => void; constructor(); + /** + * Sets up the message handler for the worker. + */ setupMessageHandler(): Promise; + /** + * Initializes the worker and related resources. + */ init(): Promise; - sendMessage(type: string, data: any): Promise; + /** + * Sends a message to the worker. + * @param type The type of message. + * @param data The message payload. + */ + sendMessage( + type: string, + data: object, + ): Promise< + CompileResult | RenderResult | EncodedResult | DecodedResult + >; - compile(input: string | Request, options?: Options): Promise; + /** + * Compiles the provided input. + * @param input A string representing the source or a CompileRequest. + * @param options Optional compilation options. + */ + compile( + input: string | CompileRequest, + options?: Options, + ): Promise; - render(diagram: string, options?: Options): Promise; + /** + * Renders the given diagram. + * @param diagram A diagram definition in string form. + * @param options Optional rendering options. + */ + render(diagram: string, options?: Options): Promise; - encode(script: string): Promise; + /** + * Encodes the provided script. + * @param script The script to encode. + */ + encode(script: string): Promise; - decode(encoded: string): Promise; + /** + * Decodes the provided encoded string. + * @param encoded The encoded data. + */ + decode(encoded: string): Promise; } }