# onesignal/onesignal-website-sdk architecture

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

- Interactive diagram: https://gitdiagram.com/onesignal/onesignal-website-sdk
- Repository: https://github.com/onesignal/onesignal-website-sdk

## Overview

##### Main Components
- SDK Client & Initialization: Represented by the code in src/entries (pageSdkInit, sdk.ts, etc.) which bootstraps the SDK on a webpage.
- Core Functionality Layer: The src/core directory encapsulates modules for model management, caching, executors, operations, and request services. This is the heart of the SDK’s business logic.
- Service Worker Layer: The src/sw directory holds the ServiceWorker code and related helpers. This is responsible for push notification handling, background tasks, and interacting with notifications.
- OneSignal Integration: The src/onesignal directory defines the primary public API, event handling, and namespace integration for user-facing interactions with OneSignal.
- Page & UI Components: Under src/page and its subdirectories (e.g., bell, errors, slidedown), the user interface and error handling aspects are implemented.
- Shared Utilities and Managers: The src/shared folder provides underlying utilities, API wrappers (in src/shared/api), shared managers (e.g., PermissionManager, SessionManager), helper functions, and error handling common to both the client SDK and service worker.
- Testing Infrastructure: The __test__ directory shows a robust test infrastructure, including unit tests for APIs, core modules, notifications, and push/subscription flows.
- DevOps/Build & Preview: Top-level items like Dockerfile, docker-compose.yml, and the preview folder indicate how the project is containerized and previewed before deployment.

##### Relationships and Interactions
- The Client-Side SDK (initialized via the code in src/entries) interacts with the Core and Shared layers to manage subscriptions, caching, and API requests.
- Registration of the Service Worker (indicated by OneSignalSDKWorker.js and code in src/sw) is triggered from the client, establishing communication between the webpage and the background processes (service worker).
- The OneSignal integration and public API in src/onesignal interface with the developer’s page code and call down into the lower layers (core, shared, page managers) to coordinate behavior (subscriptions, event handling, push processing).
- API communication flows through modules in src/shared/api and src/core/requestService, which handle the routing and management of data both internally (e.g., caching or model updates) and externally to OneSignal’s servers.

##### Architectural Patterns and Design Principles
- Modularity: The separation into clear directories (core, onesignal, page, shared, sw) suggests well-defined modules with single responsibilities.
- Layered Architecture: There is a clear distinction between the client initialization layer, the core business logic, and the service worker background processes.
- Test-Driven Design: Given the extensive suite in the __test__ directory, unit tests are central, suggesting that components can be individually mapped, tested, and maintained.
- Separation of Concerns: UI components, network/API services, business logic, and service worker code are kept separate, promoting maintainability and clarity.

##### Relevant Technologies, Frameworks, and Tools
- TypeScript: Evident by extensive .ts and .d.ts files throughout the src folders.
- Vite: The presence of vite.config.ts and vitest.config.ts indicates a modern build and testing environment.
- Docker: Dockerfile and docker-compose.yml support containerized development/testing.
- Testing Libraries: The __test__ structure along with scripts for unit tests hint at a robust testing framework.
- ESLint and Prettier: Code quality and formatting are enforced via configuration files (.eslintrc.cjs, .prettierrc.cjs).

## Diagram (Mermaid)

```mermaid
graph TD
    %% Client-Side SDK Section
    subgraph "Client-Side SDK"
        SDKClient["SDK Client & Initialization"]:::client
        UIComponents["Page & UI Components"]:::clientUI
    end

    %% Core Business Logic Section
    subgraph "Core Business Logic"
        CoreLayer["Core Functionality Layer"]:::core
        OneSignalIntegration["OneSignal Integration"]:::onesignal
        SharedUtilities["Shared Utilities & Managers"]:::shared
    end

    %% Service Worker Section
    subgraph "Service Worker"
        ServiceWorker["Service Worker Layer"]:::sw
    end

    %% Testing & DevOps Section
    subgraph "Testing & DevOps"
        TestingInfra["Testing Infrastructure"]:::tests
        DevOpsBuild["DevOps/Build & Preview"]:::devops
    end

    %% External Communication
    ExternalServers["External OneSignal Servers"]:::external

    %% Relationships and Data Flow
    SDKClient -->|"initializes"| CoreLayer
    SDKClient -->|"registers"| ServiceWorker
    SDKClient -->|"triggers UI"| UIComponents

    OneSignalIntegration -->|"invokes logic in"| CoreLayer
    OneSignalIntegration -->|"interacts with"| UIComponents

    CoreLayer -->|"delegates to"| SharedUtilities
    ServiceWorker -->|"communicates with"| SharedUtilities

    CoreLayer -->|"API-calls"| ExternalServers

    %% Testing Validation
    TestingInfra -->|"verifies"| CoreLayer
    TestingInfra -->|"verifies"| OneSignalIntegration

    %% Build & Deployment Flow
    DevOpsBuild -->|"builds/deploys"| SDKClient
    DevOpsBuild -->|"builds/deploys"| ServiceWorker

    %% Click Events
    click SDKClient "https://github.com/onesignal/onesignal-website-sdk/blob/main/src/entries/sdk.ts"
    click UIComponents "https://github.com/onesignal/onesignal-website-sdk/tree/main/src/page"
    click CoreLayer "https://github.com/onesignal/onesignal-website-sdk/tree/main/src/core"
    click OneSignalIntegration "https://github.com/onesignal/onesignal-website-sdk/tree/main/src/onesignal"
    click SharedUtilities "https://github.com/onesignal/onesignal-website-sdk/tree/main/src/shared"
    click ServiceWorker "https://github.com/onesignal/onesignal-website-sdk/tree/main/src/sw"
    click TestingInfra "https://github.com/onesignal/onesignal-website-sdk/tree/main/__test__"
    click DevOpsBuild "https://github.com/onesignal/onesignal-website-sdk/tree/main/Dockerfile"

    %% Style Definitions
    classDef client fill:#f9c,stroke:#333,stroke-width:2px;
    classDef clientUI fill:#ff9,stroke:#333,stroke-width:2px;
    classDef core fill:#9f9,stroke:#333,stroke-width:2px;
    classDef onesignal fill:#9ff,stroke:#333,stroke-width:2px;
    classDef shared fill:#fc9,stroke:#333,stroke-width:2px;
    classDef sw fill:#cff,stroke:#333,stroke-width:2px;
    classDef tests fill:#f99,stroke:#333,stroke-width:2px;
    classDef devops fill:#99f,stroke:#333,stroke-width:2px;
    classDef external fill:#eee,stroke:#333,stroke-width:2px,stroke-dasharray: 5, 5;
```
