# Models and API The `contree-client` npm package is pure ESM with zero runtime dependencies and full TypeScript declarations. There is no adapter zoo: the platform `fetch` is the transport, and connection pooling, TLS and gzip are the platform's job. ```shell npm install contree-client ``` ## Naming convention Public JavaScript names use camelCase. Serialization keeps the exact API wire spelling: | Surface | Style | Example | |---|---|---| | methods and functions | camelCase | `spawnInstance`, `waitOperation`, `ensureFile` | | model fields | camelCase | `operation.resultImageUuid` | | option-object keys | camelCase | `{ preserveEnv: true, pageSize: 100 }` | Required arguments are positional, every optional knob rides in a trailing options object. ## The client ```js import { ContreeClient, RetryPolicy } from "contree-client"; const client = new ContreeClient(token, { baseUrl: "https://contree.example.com", project: "my-project", timeout: 300, // seconds, like the Python default retry: new RetryPolicy(), // opt-in, identical semantics identity: "my-app/1.2.3", // leads the User-Agent (Node only) fetch: customFetch, // BYO transport: proxies, TLS, mocks }); ``` The token may be `null`, just like the project: the client then sends no `Authorization` header, which is what the endpoints that need no authentication expect. `fetch` is the single customization point — wrap the platform fetch to add an undici dispatcher (TLS options, proxies) in Node or a mock in tests: ```js import { Agent } from "undici"; const dispatcher = new Agent({ connect: { ca: internalCa } }); const client = new ContreeClient(token, { fetch: (url, options) => fetch(url, { ...options, dispatcher }), }); ``` `ContreeClient.fromProfile()` (Node only) reads the Contree profile files (`$CONTREE_HOME/{cli,auth}.ini`, shared with the Python client), including the active profile from `[DEFAULT]` and `CONTREE_PROFILE`. For config-less environments (CI, containers) `profiles.fromEnv()` builds a profile from `CONTREE_TOKEN` / `NEBIUS_API_KEY`, `CONTREE_URL` and `CONTREE_PROJECT` — a non-null result means the environment fully described one. ## Return shapes The mapping from the Python client is one-to-one: - buffered calls resolve to typed models (`whoami()`, `getOperationStatus()`, `listImages()`, ...); - `checkFileExists()` / `checkImageFile()` / `checkImageArchive()` resolve to booleans (`HEAD`, 404 means `false`); - `inspectImageDownload()` resolves to a `Uint8Array`; `inspectImageDownloadStream()` and `inspectImageArchive()` are `AsyncGenerator`; - `iterOperationEvents()` / `followOperationEvents()` yield typed `OperationEvent`s — reconnection with `Last-Event-Id`, deadlines and in-band `sse_error` handling match the Python `follow_operation_events`; - `iterImages()` / `iterOperations()` / `iterFiles()` paginate lazily with the same `pageSize` / `limit` knobs. Helpers carry over unchanged: `waitOperation(id, { timeout, followEvents })`, `resolveImage(ref)`, `ensureFile(content)` (sha256 dedup; Blobs hash chunk-by-chunk in Node). ## Models Generated classes expose `fromWire()` / `toWire()` instead of `from_dict()` / `to_dict()`. The tri-state field semantics are the Python ones: an unset optional field is `undefined` and omitted on the wire, an explicit `null` is sent as JSON null. Timestamps become `Date` where the spec says so. - `StreamRepr`, `EventDataStream`, and `ClosableStreamRepr` expose codec helpers: `asBytes()`, `asText()`, `StreamRepr.fromBytes()`, `StreamRepr.fromText()`; module-level `decodeChunk()` / `decodeStream()` decode payloads that may be typed or raw. - `OperationResponse.metadata` is discriminated by `kind`: `OperationInstanceMetadata` or `ImageImportMetadata`. - `OperationEvent.data` is discriminated by `event.type` (`EventDataStream`, `EventDataExit`, `EventDataCompletion`, ...); unknown payloads stay plain objects. - `OperationStatus` constants, `TERMINAL_STATUSES` / `ACTIVE_STATUSES` sets and `isTerminalStatus()` mirror the Python enum and frozen sets. `GrepMatch.asText()` formats one grep row. `GrepResult.asText()` formats all rows in response order, including context separators. `DirectoryList.asText()` returns an `ls -l`-style table with UTC timestamps; `asText(true)` returns paths only. Nonempty grep and directory output ends with a newline. Check `truncated` separately on grep results. These three models and all three stream models implement `toString()`, so `String(model)` uses the default text format. Formatting preserves `toWire()` and all fields. `ClosableStreamRepr` treats omitted encoding as ASCII; its constructors leave `close` unset so the server default applies. ## Errors ```text ContreeError ├── APIConnectionError └── APIStatusError (status, error, traceback, retryAfter) ├── BadRequestError 400 ├── AuthenticationError 401 ├── PermissionDeniedError 403 ├── NotFoundError 404 ├── ConflictError 409 ├── GoneError 410 ├── UnprocessableEntityError 422 ├── TooEarlyError 425 ├── RateLimitError 429 └── ServerError >=500 ``` `ContreeClient.request()` maps fetch and buffered body-read failures to `APIConnectionError`. Its standard `cause` property keeps the original error for diagnostics. `timedOut` is true for timeout failures. HTTP responses with status 400 or higher map to `APIStatusError` or its matching subclass. Local validation and response parsing use native JavaScript errors. Stream status failures use `RangeError`. Other stream failures remain native. ## Platform notes - Browsers forbid the `User-Agent` header — it is only attached in Node; `client.userAgent()` reports the composed value everywhere. - `fromProfile()` and `ReadableStream` request bodies are Node-only. - `getNetworkPolicyTag()` reads a JSON body from HTTP 302. Native browser fetch hides this body. Use Node.js or a custom fetch that exposes redirect responses. - A truncated compressed download can end short because fetch hides the raw compressed bytes. The complete generated surface — every method with its options and every model with its fields — lives on the [API reference](reference.rst) page, rendered from the same OpenAPI IR as the code; the package's `.d.ts` files carry the same information for your editor.