/** * Palantir Foundry Operations API. * Long-running operations tracking. * @see https://www.palantir.com/docs/foundry/api/v2/operations-v2-resources/ */ import type { PalantirClient } from "./client.js"; import type { Operation } from "./types.js"; import { PalantirApiError } from "./errors.js"; export class OperationsNamespace { constructor(private client: PalantirClient) {} /** Get an operation by RID. */ async get(operationRid: string): Promise { return this.client.get(`/api/v2/operations/${operationRid}`); } /** Wait for an operation to complete (polling). */ async waitFor( operationRid: string, options?: { pollIntervalMs?: number; timeoutMs?: number } ): Promise { const pollInterval = options?.pollIntervalMs ?? 2000; const timeout = options?.timeoutMs ?? 300_000; const start = Date.now(); let operation = await this.get(operationRid); while (operation.status !== "SUCCEEDED" && operation.status !== "FAILED") { if (Date.now() - start > timeout) { throw new PalantirApiError( { errorCode: "TIMEOUT", errorName: "OperationTimedOut", errorInstanceId: operationRid, parameters: { lastStatus: operation.status, timeoutMs: timeout }, }, 408 ); } await new Promise((resolve) => setTimeout(resolve, pollInterval)); operation = await this.get(operationRid); } if (operation.status === "FAILED") { throw new PalantirApiError( { errorCode: "INTERNAL", errorName: "OperationFailed", errorInstanceId: operationRid, parameters: { operation }, }, 500 ); } return operation; } }