|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Yandex Cloud Node.js SDK (`@yandex-cloud/nodejs-sdk`). Provides typed gRPC clients for all Yandex Cloud services, generated from protobuf definitions in the `cloudapi` git submodule. |
| 8 | + |
| 9 | +## Commands |
| 10 | + |
| 11 | +- **Build:** `npm run build` (compiles TypeScript to `dist/`) |
| 12 | +- **Lint:** `npm run lint` (ESLint on `src` and `config`) |
| 13 | +- **Test:** `npm test` (runs Jest; auto-generates services first) |
| 14 | +- **Single test:** `cross-env NODE_OPTIONS="--max-old-space-size=8192" node --experimental-vm-modules node_modules/jest/bin/jest.js -c config/jest.ts --passWithNoTests 'path/to/test'` |
| 15 | +- **Typecheck:** `npm run typecheck` (checks both src and examples) |
| 16 | +- **Generate services from protos:** `npm run cloudapi:generate-services` |
| 17 | +- **Update cloudapi submodule:** `npm run cloudapi:update` |
| 18 | + |
| 19 | +Commit messages must follow [Conventional Commits](https://www.conventionalcommits.org/) (enforced by commitlint via husky hook). Semantic-release publishes from `master`, `beta`, and `alpha` branches. |
| 20 | + |
| 21 | +## Architecture |
| 22 | + |
| 23 | +### Code generation pipeline |
| 24 | + |
| 25 | +The `cloudapi` submodule contains `.proto` files from `github.com/yandex-cloud/cloudapi`. The `scripts/generate_services` script: |
| 26 | +1. Runs `ts-proto` via `grpc_tools_node_protoc` to generate TypeScript types/clients into `src/generated/` |
| 27 | +2. Detects service directories and creates re-export `index.ts` files in `src/clients/<service-name>/` |
| 28 | +3. Updates `package.json` exports map so each service is importable as `@yandex-cloud/nodejs-sdk/<service-name>` |
| 29 | + |
| 30 | +**Do not edit files in `src/generated/` or `src/clients/*/index.ts` manually** — they are overwritten by code generation. Client directories may contain an `export-alias.json` for custom export names. |
| 31 | + |
| 32 | +### Session and client creation |
| 33 | + |
| 34 | +`Session` (`src/session.ts`) is the main entry point. It handles authentication (OAuth, IAM token, service account JSON, or metadata service) and creates gRPC channels with credentials. Clients are obtained via `session.client(ServiceClient)`, which resolves endpoints from `src/service-endpoints-map.json`. |
| 35 | + |
| 36 | +### Middleware stack |
| 37 | + |
| 38 | +The client factory (`src/utils/client-factory.ts`) chains three `nice-grpc` middlewares: |
| 39 | +1. **errorMetadataMiddleware** — wraps errors into `ApiError` with request/trace IDs from gRPC metadata |
| 40 | +2. **retryMiddleware** — exponential backoff retry for idempotent/configured calls |
| 41 | +3. **deadlineMiddleware** — from `nice-grpc-client-middleware-deadline` |
| 42 | + |
| 43 | +### Service endpoint resolution |
| 44 | + |
| 45 | +`src/service-endpoints.ts` maps a gRPC service's `serviceName` to its API endpoint using `src/service-endpoints-map.json`. The map is updated by the generation script via `scripts/check-endpoints.ts` which queries `https://api.cloud.yandex.net/endpoints`. |
| 46 | + |
| 47 | +### Key types |
| 48 | + |
| 49 | +- `SessionConfig` — union of credential configs (OAuth, IAM token, service account, generic/metadata) |
| 50 | +- `WrappedServiceClientType<S>` — nice-grpc `RawClient` with retry + deadline call options |
| 51 | +- `ClientCallArgs` — `RetryOptions & DeadlineOptions`, passed per-call |
| 52 | +- `ApiError` — extends `Error` with gRPC metadata (`requestId`, `serverTraceId`) |
0 commit comments