Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

4.9 KB
Raw Blame
import { CliError } from "./errors";
import {
  getPublicApiOperation,
  publicApiOperationRequiresServerSupport,
  type PublicApiCommand,
} from "@webstudio-is/protocol";
import type { RuntimeOperationId } from "@webstudio-is/project-build/contracts";
import type { BuilderNamespace } from "@webstudio-is/project-build/contracts";
import {
  serializeProjectSessionMeta,
  type ProjectSessionEnvelope,
  type ProjectSessionDiagnostic,
} from "@webstudio-is/project-build/project-session";
import {
  assertCliEditingContractVersion,
  assertCliServerOperationSupported,
  createCliProjectSession,
  getCliServerApiContract,
  requiresCliEditingContract,
  type CliServerApiContract,
} from "./project-session";
import type { ApiConnection } from "./api-connection";

export type ProjectSessionApiCommand = PublicApiCommand;

type CreateProjectSession = typeof createCliProjectSession;

const uniqueNamespaces = (
  namespaces: readonly BuilderNamespace[]
): readonly BuilderNamespace[] => [...new Set(namespaces)];

const getSessionError = (envelope: {
  diagnostics: readonly ProjectSessionDiagnostic[];
}) => envelope.diagnostics.find((diagnostic) => diagnostic.level === "error");

const createProjectSessionApiError = (
  diagnostic: Pick<ProjectSessionDiagnostic, "code" | "message" | "issues">
) => {
  const error = new Error(diagnostic.message) as Error & {
    code?: string;
    issues?: ProjectSessionDiagnostic["issues"];
  };
  if (diagnostic.code !== undefined) {
    error.name = diagnostic.code;
    error.code = diagnostic.code;
  }
  if (diagnostic.issues !== undefined) {
    error.issues = diagnostic.issues;
  }
  return error;
};

const withProjectId = (
  input: unknown,
  projectId: string
): Record<string, unknown> => ({
  ...(typeof input === "object" && input !== null
    ? (input as Record<string, unknown>)
    : {}),
  projectId,
});

export const isProjectSessionEnvelope = (
  value: unknown
): value is ProjectSessionEnvelope =>
  typeof value === "object" &&
  value !== null &&
  "operationId" in value &&
  "projectId" in value &&
  "state" in value &&
  "namespaces" in value &&
  "diagnostics" in value;

export const getProjectSessionMeta = serializeProjectSessionMeta;

export const executeProjectSessionApiOperation = async ({
  command,
  input,
  connection,
  createProjectSession = createCliProjectSession,
  getServerApiContract = getCliServerApiContract,
  apiContract,
  dryRun = false,
  refresh = false,
}: {
  command: ProjectSessionApiCommand;
  input: unknown;
  connection: ApiConnection;
  createProjectSession?: CreateProjectSession;
  getServerApiContract?: typeof getCliServerApiContract;
  apiContract?: CliServerApiContract;
  dryRun?: boolean;
  refresh?: boolean;
}) => {
  const operation = getPublicApiOperation(command);
  const runtimeOperationId = operation.runtimeOperationId as
    | RuntimeOperationId
    | undefined;
  if (
    dryRun === true &&
    (runtimeOperationId === undefined || operation.method !== "mutation")
  ) {
    throw new CliError(
      "INVALID_ARGUMENT",
      `${command} does not support --dry-run. Use --dry-run only with local-capable mutation tools; omit it for read or server-only tools.`
    );
  }
  const requiresServerSupport =
    runtimeOperationId === undefined ||
    publicApiOperationRequiresServerSupport(operation);
  const requiresEditingContract = requiresCliEditingContract(operation, dryRun);
  const contract =
    requiresServerSupport || requiresEditingContract
      ? (apiContract ?? (await getServerApiContract(connection)))
      : undefined;
  if (contract !== undefined) {
    if (requiresServerSupport) {
      assertCliServerOperationSupported(operation.id, contract);
    }
    if (requiresEditingContract) {
      assertCliEditingContractVersion(contract);
    }
  }
  const session = createProjectSession({ connection });
  await session.initialize();
  if (refresh && runtimeOperationId !== undefined) {
    await session.refresh(
      uniqueNamespaces([
        ...operation.readNamespaces,
        ...operation.writeNamespaces,
      ])
    );
  }
  const envelope =
    runtimeOperationId === undefined
      ? await session.executeServerOperation(
          {
            id: operation.id,
            method: operation.method,
            invalidatesNamespaces: operation.invalidatesNamespaces,
            refetchInvalidatedNamespaces:
              operation.invalidatesNamespaces.length > 0,
          },
          input
        )
      : operation.method === "query"
        ? await session.read(
            runtimeOperationId,
            command === "audit"
              ? input
              : withProjectId(input, connection.projectId),
            { permit: operation.permit }
          )
        : await session.mutate(
            runtimeOperationId,
            withProjectId(input, connection.projectId),
            { permit: operation.permit, dryRun }
          );
  const error = getSessionError(envelope);
  if (error !== undefined) {
    throw createProjectSessionApiError(error);
  }
  return envelope;
};