# tcgdex/javascript-sdk architecture

> Architecture diagram of the GitHub repository tcgdex/javascript-sdk, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tcgdex/javascript-sdk
- Repository: https://github.com/tcgdex/javascript-sdk

## Overview

##### Project Type & Purpose
- This project is an SDK designed to provide a convenient wrapper for the TCGdex API. It abstracts the details of API calls into a user-friendly fetch method for various endpoints (cards, sets, series, and additional endpoints).
- The SDK supports both browser and Node.js environments and is built using TypeScript (with JavaScript compatibility provided through transpilation). The purpose is to simplify how developers interact with the TCGdex API.

##### Main Components
a. Client/Integration Interface:
- Entry point of the SDK that instantiates the “TCGdex” class.
- Differentiate between browser and Node.js integration:
- Browser Entry: Represented by the “tcgdex.browser.ts” file.
- Node.js Entry: Represented by the “tcgdex.node.ts” file.
- Unified API: “tcgdex.ts” that abstracts the environment differences.

b. Core SDK Module – “TCGdex”:
- The central component that is instantiated by the user. It handles configuration (e.g., language settings) and exposes the fetch method.

c. Endpoints Module:
- “src/endpoints” directory that contains:
- Base Endpoint class (“Endpoint.ts”) defining shared properties and behaviors for endpoints.
- Specialized implementations like “SimpleEndpoint.ts” and “StringEndpoint.ts” which provide customized logic for different types of API endpoints.
- That these components are responsible for constructing requests and processing responses.

d. Models Layer:
- “src/models” directory; include tools for mapping API responses to domain-specific objects (e.g., “Card.ts”, “Serie.ts”, “Set.ts” and their Resume counterparts).
- That these models serve as data representations that the SDK uses internally to provide structured and type-safe responses.

e. Utility Functions & Interfaces:
- “src/utils.ts” and “src/interfaces.d.ts” which provide supporting functionality and type definitions throughout the SDK.

##### Data Flow & Interaction
- Finally, illustrate the use of “Models” to transform the response data into structured objects before returning the final data to the “Client Application.”
- Optionally, indicate where utility functions come into play in supporting these operations (e.g., formatting, error handling).

##### Architectural Patterns & Design Principles
- Separation of Concerns: Different layers for API interaction (Endpoints), data modeling (Models), and public SDK API (TCGdex).
- Abstraction that allows the same API call (tcgdex.fetch) to function across diverse environments by leveraging environment-specific entry points.
- Mention modularity which facilitates testing (as seen in the __tests__ directory) and clearer maintenance of domain logic.

## Diagram (Mermaid)

```mermaid
flowchart TD
    CA["Client Application"]:::client

    subgraph "Client/Integration Interface"
        BrowserEntry["Browser-specific Entry"]:::integration
        NodeEntry["Node-specific Entry"]:::integration
    end

    UnifiedSDK["TCGdex SDK (Unified Interface)"]:::sdk

    subgraph "Endpoints Service"
        EndpointBase["Base Endpoint"]:::endpoint
        EndpointSimple["SimpleEndpoint"]:::endpoint
        EndpointString["StringEndpoint"]:::endpoint
    end

    ExternalAPI["TCGdex API (External Service)"]:::external

    subgraph "Models Layer"
        CardModel["Card Model"]:::mod
        CardResume["Card Resume"]:::mod
        SerieModel["Serie Model"]:::mod
        SerieResume["Serie Resume"]:::mod
        SetModel["Set Model"]:::mod
        SetResume["Set Resume"]:::mod
        GenericModel["Generic Model/Others"]:::mod
    end

    subgraph "Utilities & Interfaces"
        Util["Utilities"]:::util
        Intf["Interfaces & Types"]:::util
    end

    %% Data Flow
    CA --> BrowserEntry
    CA --> NodeEntry
    BrowserEntry --> UnifiedSDK
    NodeEntry --> UnifiedSDK
    UnifiedSDK -->|"fetch"| EndpointBase
    EndpointBase -->|"API_call"| ExternalAPI
    ExternalAPI -->|"response"| EndpointBase
    EndpointBase -->|"process"| CardModel
    EndpointBase -->|"process"| CardResume
    EndpointBase -->|"process"| SerieModel
    EndpointBase -->|"process"| SerieResume
    EndpointBase -->|"process"| SetModel
    EndpointBase -->|"process"| SetResume
    EndpointBase -->|"process"| GenericModel
    CardModel -->|"return_data"| CA
    CardResume -->|"return_data"| CA
    SerieModel -->|"return_data"| CA
    SerieResume -->|"return_data"| CA
    SetModel -->|"return_data"| CA
    SetResume -->|"return_data"| CA
    GenericModel -->|"return_data"| CA

    %% Support Utilities
    EndpointBase ---|"util_support"| Util
    CardModel ---|"uses"| Intf
    CardResume ---|"uses"| Intf
    SerieModel ---|"uses"| Intf
    SerieResume ---|"uses"| Intf
    SetModel ---|"uses"| Intf
    SetResume ---|"uses"| Intf
    GenericModel ---|"uses"| Intf

    %% Click Events
    click BrowserEntry "https://github.com/tcgdex/javascript-sdk/blob/master/src/tcgdex.browser.ts"
    click NodeEntry "https://github.com/tcgdex/javascript-sdk/blob/master/src/tcgdex.node.ts"
    click UnifiedSDK "https://github.com/tcgdex/javascript-sdk/blob/master/src/tcgdex.ts"
    click EndpointBase "https://github.com/tcgdex/javascript-sdk/blob/master/src/endpoints/Endpoint.ts"
    click EndpointSimple "https://github.com/tcgdex/javascript-sdk/blob/master/src/endpoints/SimpleEndpoint.ts"
    click EndpointString "https://github.com/tcgdex/javascript-sdk/blob/master/src/models/StringEndpoint.ts"
    click CardModel "https://github.com/tcgdex/javascript-sdk/blob/master/src/models/Card.ts"
    click CardResume "https://github.com/tcgdex/javascript-sdk/blob/master/src/models/CardResume.ts"
    click SerieModel "https://github.com/tcgdex/javascript-sdk/blob/master/src/models/Serie.ts"
    click SerieResume "https://github.com/tcgdex/javascript-sdk/blob/master/src/models/SerieResume.ts"
    click SetModel "https://github.com/tcgdex/javascript-sdk/blob/master/src/models/Set.ts"
    click SetResume "https://github.com/tcgdex/javascript-sdk/blob/master/src/models/SetResume.ts"
    click GenericModel "https://github.com/tcgdex/javascript-sdk/blob/master/src/models/Model.ts"
    click Util "https://github.com/tcgdex/javascript-sdk/blob/master/src/utils.ts"
    click Intf "https://github.com/tcgdex/javascript-sdk/blob/master/src/interfaces.d.ts"

    %% Styles
    classDef client fill:#FFCCCC,stroke:#333,stroke-width:2px;
    classDef integration fill:#FFDAB9,stroke:#333,stroke-width:2px;
    classDef sdk fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef endpoint fill:#98FB98,stroke:#333,stroke-width:2px;
    classDef mod fill:#E6E6FA,stroke:#333,stroke-width:2px;
    classDef util fill:#FFFACD,stroke:#333,stroke-width:2px;
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:2px;
```
