# eyelevelai/groundx-typescript architecture

> Architecture diagram of the GitHub repository eyelevelai/groundx-typescript, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/eyelevelai/groundx-typescript
- Repository: https://github.com/eyelevelai/groundx-typescript

## Overview

##### Project type and purpose
- This is an open-source TypeScript SDK (client library) for interacting with the GroundX REST API.
- The library exports a single entry point (GroundXClient) plus a set of resource-specific clients, request/response types, core HTTP-fetch logic, and error classes.

##### File structure
- Top-level files: package.json, tsconfig.json, README.md, reference.md, jest.config.js, CI workflow.
- src/ contains:
- index.ts & version.ts: library entry point exports
- Client.ts & Wrapper.ts: public API surface (GroundXClient) wraps internal logic
- api/: resource clients and type definitions organized by resource (buckets, customer, documents, groups, health, search)
- core/: HTTP-fetcher abstraction (Fetcher, retries, timeout, request/response parsing, stream wrappers)
- errors/: custom error classes (GroundXError, GroundXTimeoutError)
- environments.ts: runtime feature flags
- tests/: unit tests for core fetcher logic and custom integration tests

##### Key README insights
- GroundXClient instantiation with apiKey, request options (headers, retries, timeout, abort signals).
- Public API methods (e.g., ingest, search, list buckets) delegate to resource-specific clients.
- Automatic retries, raw responses, runtime compatibility, custom fetch injection.

##### Main components
a. Entry point: GroundXClient (src/Client.ts)
b. Resource modules: api/resources/* (Client classes & requests)
c. Core HTTP layer: core/fetcher (Fetcher, makeRequest, retries, stream wrappers)
d. Types module: api/types (TypeScript interfaces for requests/responses)
e. Error handling: errors/GroundXError, GroundXTimeoutError
f. External systems:
- GroundX REST API (HTTP endpoint)
- Underlying HTTP client (node-fetch, global fetch, or user-supplied fetcher)

##### Relationships and interactions
- GroundXClient exposes methods that call Wrapper.ts
- Wrapper constructs request payloads and delegates to api/resources/* Client classes
- Resource clients build typed requests (using api/types) and call core/fetcher.makeRequest()
- core/fetcher resolves which Fetch implementation to use (Node, Undici, browser fetch), applies headers, timeout, retries
- HTTP request is sent over the network to the GroundX API endpoint
- Response body is parsed; on 2xx returns typed response, on error status codes throws GroundXError (or subclass)
- Consumers can call .asRaw() or pass abort signals/custom headers

##### Architectural patterns and design principles
- Layered architecture:
- Public API layer (Client.ts, Wrapper.ts)
- Resource client layer (api/resources)
- HTTP abstraction layer (core/fetcher)
- Type definitions layer (api/types)
- Error handling layer (errors)
- Dependency injection for Fetch client to support multiple runtimes
- Single responsibility: each module handles a specific concern
- Typed interfaces for strong compile-time guarantees

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Public API Layer
    subgraph "Public API Layer"
        direction TB
        Index["index.ts"]:::public
        ClientTS["Client.ts"]:::public
        WrapperTS["Wrapper.ts"]:::public
        VersionTS["version.ts"]:::public
    end

    %% Resource Module Layer
    subgraph "Resource Clients"   
        direction TB
        ResIndex["src/api/resources/index.ts"]:::resources
        Buckets["BucketsClient\n(Client.ts)"]:::resources
        Customer["CustomerClient\n(Client.ts)"]:::resources
        Documents["DocumentsClient\n(Client.ts)"]:::resources
        Groups["GroupsClient\n(Client.ts)"]:::resources
        Health["HealthClient\n(Client.ts)"]:::resources
        Search["SearchClient\n(Client.ts)"]:::resources
    end

    %% Types Layer
    subgraph "Type Definitions"
        direction TB
        TypesIndex["src/api/types/index.ts"]:::types
        AllTypes["*.ts"]:::types
    end

    %% Infrastructure Layer
    subgraph "Core HTTP Abstraction"
        direction TB
        CoreIndex["src/core/index.ts"]:::infra
        Fetcher["Fetcher.ts"]:::infra
        MakeReq["makeRequest.ts"]:::infra
        Retries["requestWithRetries.ts"]:::infra
        Stream["stream-wrappers/*"]:::infra
    end

    %% Error Handling Layer
    subgraph "Error Handling"
        direction TB
        ErrorsIndex["src/errors/index.ts"]:::error
        GroundXError["GroundXError.ts"]:::error
        TimeoutError["GroundXTimeoutError.ts"]:::error
        APIErrorsIndex["src/api/errors/index.ts"]:::error
        BadRequest["BadRequestError.ts"]:::error
        Unauthorized["UnauthorizedError.ts"]:::error
    end

    %% Environment & Config
    subgraph "Environment & Config"
        direction TB
        Env["environments.ts"]:::config
        TSConfig["tsconfig.json"]:::config
        Package["package.json"]:::config
        Jest["jest.config.js"]:::config
    end

    %% External Systems
    subgraph "External Systems"
        direction TB
        RestAPI["GroundX REST API"]:::external
        UnderlyingFetch["Underling Fetch Implementations\n(node-fetch, global fetch,\ncustom)"]:::external
    end

    %% Connections
    Index -->|exports| ClientTS
    Index -->|exports| WrapperTS
    ClientTS -->|wraps logic| WrapperTS
    WrapperTS -->|delegates| ResIndex
    ResIndex --> Buckets
    ResIndex --> Customer
    ResIndex --> Documents
    ResIndex --> Groups
    ResIndex --> Health
    ResIndex --> Search
    Buckets -->|calls| MakeReq
    Customer -->|calls| MakeReq
    Documents -->|calls| MakeReq
    Groups -->|calls| MakeReq
    Health -->|calls| MakeReq
    Search -->|calls| MakeReq
    Buckets -->|uses| TypesIndex
    Customer -->|uses| TypesIndex
    Documents -->|uses| TypesIndex
    Groups -->|uses| TypesIndex
    Health -->|uses| TypesIndex
    Search -->|uses| TypesIndex
    Buckets -->|throws| GroundXError
    Customer -->|throws| GroundXError
    Documents -->|throws| GroundXError
    Groups -->|throws| GroundXError
    Health -->|throws| GroundXError
    Search -->|throws| GroundXError
    MakeReq -->|uses| Retries
    MakeReq -->|uses| Fetcher
    MakeReq -->|uses| Stream
    Fetcher -->|injects| UnderlyingFetch
    Retries -->|loops| MakeReq
    MakeReq -->|sends HTTP| RestAPI
    RestAPI -->|response| MakeReq
    MakeReq -->|parses| TypesIndex
    MakeReq -->|error| TimeoutError
    Env --- ClientTS

    %% Click Events
    click Index "https://github.com/eyelevelai/groundx-typescript/blob/main/src/index.ts"
    click ClientTS "https://github.com/eyelevelai/groundx-typescript/blob/main/src/Client.ts"
    click WrapperTS "https://github.com/eyelevelai/groundx-typescript/blob/main/src/Wrapper.ts"
    click VersionTS "https://github.com/eyelevelai/groundx-typescript/blob/main/src/version.ts"
    click ResIndex "https://github.com/eyelevelai/groundx-typescript/blob/main/src/api/resources/index.ts"
    click Buckets "https://github.com/eyelevelai/groundx-typescript/blob/main/src/api/resources/buckets/client/Client.ts"
    click Customer "https://github.com/eyelevelai/groundx-typescript/blob/main/src/api/resources/customer/client/Client.ts"
    click Documents "https://github.com/eyelevelai/groundx-typescript/blob/main/src/api/resources/documents/client/Client.ts"
    click Groups "https://github.com/eyelevelai/groundx-typescript/blob/main/src/api/resources/groups/client/Client.ts"
    click Health "https://github.com/eyelevelai/groundx-typescript/blob/main/src/api/resources/health/client/Client.ts"
    click Search "https://github.com/eyelevelai/groundx-typescript/blob/main/src/api/resources/search/client/Client.ts"
    click TypesIndex "https://github.com/eyelevelai/groundx-typescript/blob/main/src/api/types/index.ts"
    click CoreIndex "https://github.com/eyelevelai/groundx-typescript/blob/main/src/core/fetcher/index.ts"
    click Fetcher "https://github.com/eyelevelai/groundx-typescript/blob/main/src/core/fetcher/Fetcher.ts"
    click MakeReq "https://github.com/eyelevelai/groundx-typescript/blob/main/src/core/fetcher/makeRequest.ts"
    click Retries "https://github.com/eyelevelai/groundx-typescript/blob/main/src/core/fetcher/requestWithRetries.ts"
    click Stream "https://github.com/eyelevelai/groundx-typescript/tree/main/src/core/fetcher/stream-wrappers/"
    click ErrorsIndex "https://github.com/eyelevelai/groundx-typescript/blob/main/src/errors/index.ts"
    click GroundXError "https://github.com/eyelevelai/groundx-typescript/blob/main/src/errors/GroundXError.ts"
    click TimeoutError "https://github.com/eyelevelai/groundx-typescript/blob/main/src/errors/GroundXTimeoutError.ts"
    click APIErrorsIndex "https://github.com/eyelevelai/groundx-typescript/blob/main/src/api/errors/index.ts"
    click BadRequest "https://github.com/eyelevelai/groundx-typescript/blob/main/src/api/errors/BadRequestError.ts"
    click Unauthorized "https://github.com/eyelevelai/groundx-typescript/blob/main/src/api/errors/UnauthorizedError.ts"
    click Env "https://github.com/eyelevelai/groundx-typescript/blob/main/src/environments.ts"
    click TSConfig "https://github.com/eyelevelai/groundx-typescript/blob/main/tsconfig.json"
    click Package "https://github.com/eyelevelai/groundx-typescript/blob/main/package.json"
    click Jest "https://github.com/eyelevelai/groundx-typescript/blob/main/jest.config.js"

    %% Styles
    classDef public fill:#bbdefb,stroke:#1e88e5,color:#0d47a1;
    classDef resources fill:#c8e6c9,stroke:#43a047,color:#1b5e20;
    classDef types fill:#e1bee7,stroke:#8e24aa,color:#4a148c;
    classDef infra fill:#ffe0b2,stroke:#fb8c00,color:#e65100;
    classDef error fill:#ffcdd2,stroke:#e53935,color:#b71c1c;
    classDef config fill:#f0f4c3,stroke:#c0ca33,color:#827717;
    classDef external fill:#eceff1,stroke:#78909c,color:#455a64;
```
