60 lines · 2.0 KB
/** Session details the embedding page hands to the workbench (and on to this extension) via a command. */
export type EditorSessionInfo = {
  token: string;
  forgeOrigin: string;
  owner: string;
  repo: string;
  ref: string;
  refKind: "branch" | "tag" | "commit";
  sha: string;
  writable: boolean;
  protected: boolean;
  user: string;
  containers: boolean;
  /** Set by "Open in container" on the forge: start a container and open a terminal after activation. */
  startContainer?: boolean;
  /** File to open on start, e.g. from `/edit/<ref>?path=` or pressing `.` on a file page. */
  openPath?: string | null;
};

export type TreeEntry = { path: string; type: string; sha: string };

/** Token-only calls to the forge's editor API. */
export class ForgeApi {
  constructor(private session: EditorSessionInfo) {}

  update(session: EditorSessionInfo): void {
    this.session = session;
  }

  get token(): string {
    return this.session.token;
  }

  roomUrl(): string {
    const url = new URL("/api/editor/room", this.session.forgeOrigin);
    url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
    return url.toString();
  }

  async request<T>(path: string, init: { method?: string; body?: unknown } = {}): Promise<T> {