UNPKG

@devbookhq/sdk

Version:

SDK for managing Devbook sessions from JavaScript/TypeScript

63 lines (62 loc) 2.45 kB
import { components } from '../api'; import Logger from '../utils/logger'; import { codeSnippetService } from './codeSnippet'; import { filesystemService } from './filesystem'; import { processService } from './process'; import { terminalService } from './terminal'; type SubscriptionHandler = (result: any) => void; type Service = typeof processService | typeof codeSnippetService | typeof filesystemService | typeof terminalService; export type CloseHandler = () => void; export type DisconnectHandler = () => void; export type ReconnectHandler = () => void; export interface SessionConnectionOpts { id: string; apiKey?: string; onClose?: CloseHandler; onDisconnect?: DisconnectHandler; onReconnect?: ReconnectHandler; debug?: boolean; editEnabled?: boolean; __debug_hostname?: string; __debug_port?: number; __debug_devEnv?: 'remote' | 'local'; } declare abstract class SessionConnection { private readonly opts; protected readonly logger: Logger; protected session?: components['schemas']['Session']; protected isOpen: boolean; private readonly rpc; private subscribers; constructor(opts: SessionConnectionOpts); /** * Get the hostname for the session or for the specified session's port. * * `getHostname` method requires `this` context - you may need to bind it. * * @param port specify if you want to connect to a specific port of the session * @returns hostname of the session or session's port */ getHostname(port?: number): string | undefined; /** * Close the connection to the session * * `close` method requires `this` context - you may need to bind it. */ close(): Promise<void>; /** * Open a connection to a new session * * `open` method requires `this` context - you may need to bind it. */ open(): Promise<void>; call(service: Service, method: string, params?: any[]): Promise<unknown>; handleSubscriptions<T extends (ReturnType<SessionConnection['subscribe']> | undefined)[]>(...subs: T): Promise<{ [P in keyof T]: Awaited<T[P]>; }>; unsubscribe(subID: string): Promise<void>; subscribe(service: Service, handler: SubscriptionHandler, method: string, ...params: any[]): Promise<string>; private handleNotification; private refresh; } export default SessionConnection;