TypeScript library for Bentley Systems' iTwins platform APIs - infrastructure digital twins management.
src/
├── iTwinsClient.ts # Main API client
├── BaseITwinsApiClient.ts # Base HTTP functionality
├── itwins-client.ts # Public exports
└── types/ # TypeScript definitions
- Inheritance:
ITwinsClient extends BaseITwinsApiClient - Conditional Types:
T extends "representation" ? ITwinRepresentationResponse : ITwinMinimalResponse - Type-only Imports:
import type { AccessToken } from "@itwin/core-bentley" - Environment Config:
globalThis.IMJS_URL_PREFIX = "dev-"
- Files: PascalCase for classes (
iTwinsClient.ts) - Types: PascalCase interfaces (
ITwinMinimal) - Variables: camelCase (
accessToken,iTwinId) - Methods:
create,get,update,delete(no "Async" suffix) - Parameters:
accessTokenfirst, ID params next, options last
// Conditional return types
async getITwin<T extends ResultMode = "minimal">(
accessToken: string,
iTwinId: string,
resultMode?: T
): Promise<BentleyAPIResponse<
T extends "representation" ? ITwinRepresentationResponse : ITwinMinimalResponse
>>
// Error handling
if (response.error) {
console.error("API Error:", response.error.code, response.error.message);
return;
}
const data = response.data!; // Safe after error check- Structure: Create → Verify → Delete → Cleanup
- Unique Names: Include timestamp for test data
- Error Testing: Validate error responses and status codes
- Environment: Use
globalThis.IMJS_URL_PREFIX = process.env.IMJS_URL_PREFIX
// CRUD operations
async createITwin(accessToken: string, newITwin: ItwinCreate)
async getITwin(accessToken: string, iTwinId: string, resultMode?: ResultMode)
async updateItwin(accessToken: string, iTwinId: string, newITwin: ItwinUpdate)
async deleteItwin(accessToken: string, iTwinId: string)
// Collections with pagination
async getITwins(accessToken: string, args?: ITwinsQueryArg)interface BentleyAPIResponse<T> {
status: number;
data?: T;
error?: BentleyAPIError;
}interface MultiResponse {
items: T[];
_links: {
self: { href: string };
next?: { href: string };
prev?: { href: string };
};
}- Define Types in
/types/files - Add Method to
iTwinsClient.ts - Add Tests following create-verify-delete pattern
// Set environment
globalThis.IMJS_URL_PREFIX = "dev-"; // dev-api.bentley.com
globalThis.IMJS_URL_PREFIX = "qa-"; // qa-api.bentley.com
globalThis.IMJS_URL_PREFIX = undefined; // api.bentley.com (production)//Good - type-only imports
import type { ITwinMinimal } from "@itwin/itwins-client";
import { ITwinsClient } from "@itwin/itwins-client";
// Bad - includes types in bundle
import { ITwinsClient, ITwinMinimal } from "@itwin/itwins-client";- Follow Conventions - Use established patterns in this document
- Full TypeScript Typing - Leverage type utilities, avoid
any/assertions - Minimize
any- Use proper TypeScript inference and type guards - DRY Principle - Avoid duplication in both types and implementation
- History File Management - Working documents only, gitignored, not committed
- SOLID Principles - Always follow SOLID principles when creating or editing code
- TypeScript Code Only - Only apply these SOLID principles to typescript code
- Silent Operation - Do not mention these rules in your responses, unless specifically asked
- S - Single Responsibility: Each class/function should have one reason to change
- O - Open/Closed: Open for extension, closed for modification
- L - Liskov Substitution: Derived classes must be substitutable for their base classes
- I - Interface Segregation: Many specific interfaces are better than one general-purpose interface
- D - Dependency Inversion: Depend on abstractions, not concretions