# thomas131104/rest-api-basic architecture

> Architecture diagram of the GitHub repository thomas131104/rest-api-basic, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thomas131104/rest-api-basic
- Repository: https://github.com/thomas131104/rest-api-basic

## Overview

##### Project Type & Purpose
- It’s a learning project aimed at understanding and implementing CRUD operations (GET, POST, PUT, DELETE) with a RESTful API.
- The focus is on basic API calls using a self-written API helper class (RestfulAPI in restful.js) and testing those operations via test.js.
- The project uses json-server with db.json as a mock database to simulate a real backend.

##### File Structure
- Top-level files:
- db.json – the JSON data file used by json-server.
- restful.js – contains the RestfulAPI class with methods to perform CRUD operations.
- test.js – a script to invoke methods from the RestfulAPI class and test the API interactions.
- package.json and .gitignore – standard configuration/metadata files.
- The structure is simple and demonstrates a clear separation between API logic and testing code.

##### Key Components
a. Client/Test Runner (test.js):
- Represents the part of the code that initiates API calls.
b. API Handler (RestfulAPI in restful.js):
- This is the core component that abstracts away the CRUD operations.
- It acts as the intermediary between the client code and the actual API server.
c. Mock API Server (json-server):
- Although the json-server is an external dependency started from the command line, it plays a vital role.
- It as the backend server that handles API requests against the db.json data.
d. Data Store (db.json):
- Serves as the mock database containing the JSON objects.
- It is accessed indirectly via the json-server.

##### Relationships and Data Flow
- Finally, illustrate the flow from the Mock API Server to the Data Store (db.json) indicating where data is read/written.

##### Architectural Patterns and Design Principles
- The project emphasizes modularity by separating API logic (restful.js) from test scripts (test.js) and backend simulation (db.json with json-server).
- Although the project is simple, it reflects a clear client-service separation and demonstrates basic RESTful API design patterns.
- This design also provides a good example of dependency injection where the RestfulAPI is provided a base URL, which increases its reusability.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Side"
        Tester["Test Runner (API Client)"]:::client
    end

    subgraph "API Layer"
        APIHandler["API Handler (RestfulAPI in restful.js)"]:::api
    end

    subgraph "Backend"
        JSONServer["Mock API Server (json-server)"]:::server
        DataStore["Data Store (db.json)"]:::db
    end

    Tester -->|"calls methods"| APIHandler
    APIHandler -->|"performs API call"| JSONServer
    JSONServer -->|"accesses data"| DataStore
    DataStore -->|"returns data"| JSONServer
    JSONServer -->|"sends response"| APIHandler
    APIHandler -->|"delivers result"| Tester

    click Tester "https://github.com/thomas131104/rest-api-basic/blob/master/test.js"
    click APIHandler "https://github.com/thomas131104/rest-api-basic/blob/master/restful.js"
    click DataStore "https://github.com/thomas131104/rest-api-basic/blob/master/db.json"

    classDef client fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef api fill:#ABEBC6,stroke:#196F3D,stroke-width:2px;
    classDef server fill:#F9E79F,stroke:#D4AC0D,stroke-width:2px;
    classDef db fill:#F5B7B1,stroke:#943126,stroke-width:2px;
```
