# openedx/frontend-platform architecture

> Architecture diagram of the GitHub repository openedx/frontend-platform, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openedx/frontend-platform
- Repository: https://github.com/openedx/frontend-platform

## Overview

##### Project Type and Purpose
- This is an open-source JavaScript/React framework package (@edx/frontend-platform) designed to provide foundational services—analytics, logging, authentication, and internationalization—for Open edX micro-frontend applications.
- It’s not a standalone web app but a library/framework that consumer micro-frontends import and initialize via its API (initialize(), getConfig(), mergeConfig(), etc.).

##### File Structure Analysis
- Top-level:
- src/ – core platform implementation
- example/ – demo micro-frontend application using the platform
- docs/ – architecture decisions, how-tos, templates, and published docs
- __mocks__/, jest.config.js – testing support
- babel.config.js, webpack.dev.config.js – build tool configurations
- .github/workflows/ – CI/CD pipelines (lint, test, release, publish)
- Under src/:
- analytics/, auth/, i18n/, logging/ – each has an interface module and default implementations
- initialize.js – orchestrates the application startup lifecycle (phases: config load, pub/sub init, logging, auth, analytics, i18n, ready)
- config.js, constants.js, pubSub.js, utils.js – miscellaneous utilities used by all services
- react/ – React-specific components and hooks for integrating platform services into React apps
- scripts/ – client-side scripts (e.g., GoogleAnalyticsLoader)
- testing/ – helpers for mocking and initializing a mock app in tests
- Build and deployment:
- example runs on webpack-dev-server (public/index.html)
- package.json scripts manage build, test, and publish to npm

##### README Insights
- Describes the “Service architecture”: strict modular separation of interfaces vs. implementations, dependency injection pattern (each service receives its dependencies via constructor).
- Describes “Application initialization”: the initialize() function with ordered lifecycle hooks that applications can tap into.
- Highlights that default implementations (New Relic, Segment, Axios/JWT, React Intl) co-exist but remain decoupled from interfaces.

##### Components
a. Main Components
1. Platform Core
- Initialization Engine (initialize.js)
- Config Manager (config.js, env.config.js)
- Pub/Sub Bus (pubSub.js)
- Utilities (utils.js, constants.js)
2. Service Interfaces
- AnalyticsService Interface
- LoggingService Interface
- AuthService Interface
- I18nService Interface
3. Default Service Implementations
- SegmentAnalyticsService (analytics/)
- NewRelicLoggingService (logging/)
- AxiosJwtAuthService / AxiosCsrfTokenService (auth/)
- ReactIntl I18n Implementation (i18n/)
4. Consumer Application
- Example micro-frontend (example/)
- React components/hooks under src/react/
5. External Systems
- Segment API
- New Relic API
- Auth backend (via Axios)
- Translation source (e.g., Transifex)
- npm registry (for package publish)
- GitHub/GitHub Actions (CI/CD)
b. Relationships & Data Flow
- Initialization Engine invokes lifecycle phases in order; at each phase, it:
1. Loads environment config → Config Manager
2. Initializes Pub/Sub → Pub/Sub Bus
3. Instantiates LoggingService (injects config & pub/sub) → Logging API
4. Instantiates AuthService (injects LoggingService & config) → Auth API
5. Instantiates AnalyticsService (injects LoggingService & pub/sub) → Analytics API
6. Instantiates I18nService (injects config & messages) → I18n API
7. Signals “application ready” → Consumer App subscribes to pub/sub for any post-init hooks
- Consumer App imports service APIs (via the interface layer) and calls methods (e.g., auth.login(), analytics.track(), logging.error(), intl.formatMessage()).
- Default implementations make HTTP calls or library calls to external systems (Segment, New Relic, backend, React Intl).
c. Architectural Patterns & Principles
- Dependency Injection: service implementations receive only the interfaces they depend on.
- Convention over Configuration: sensible defaults drive initialization unless overridden.
- Modular Separation: interfaces are separate from implementations; new implementations can be swapped in without changing consumers.
- Lifecycle Hooks: ordered phases allow custom code to run at precise times.
d. Key Technologies & Libraries
- React (hooks, context)
- Axios (HTTP client for auth)
- React Intl (i18n)
- Segment & New Relic SDKs
- Webpack & Babel (build)
- Jest (testing)
- GitHub Actions (CI/CD)
- npm (package management)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Build & CI/CD Layer
    subgraph "Build & CI/CD"
        GH["GitHub Actions"]:::external
        NPM["npm Registry"]:::external
    end

    GH -->|"publish package"| NPM

    %% Code Layer
    subgraph "Code"
        direction TB

        subgraph "Platform Core" 
            direction TB
            Init["Initialization Engine"]:::core
            Config["Config Manager"]:::core
            PubSub["Pub/Sub Bus"]:::core
            Utils["Utilities"]:::core
        end

        subgraph "Service Interfaces"
            direction TB
            AnalInt["AnalyticsService Interface"]:::interface
            LogInt["LoggingService Interface"]:::interface
            AuthInt["AuthService Interface"]:::interface
            I18nInt["I18nService Interface"]:::interface
        end

        subgraph "Default Implementations"
            direction TB
            SegmentSvc["SegmentAnalyticsService"]:::implementation
            NewRelicSvc["NewRelicLoggingService"]:::implementation
            AxiosJwt["AxiosJwtAuthService"]:::implementation
            AxiosCsrf["AxiosCsrfTokenService"]:::implementation
            ReactIntlSvc["ReactIntl I18n"]:::implementation
        end

        subgraph "Consumer Application"
            direction TB
            ExampleApp["Example Micro-frontend"]:::consumer
            ReactInt["React Integration (hooks/components)"]:::consumer
        end
    end

    %% Runtime Layer
    subgraph "Runtime & External APIs"
        direction TB
        SegmentAPI["Segment API"]:::external
        NewRelicAPI["New Relic API"]:::external
        AuthBackend["Auth Backend"]:::external
        TransSource["Translation Source"]:::external
    end

    %% Initialization Sequence
    Init -->|"1. load env config"| Config
    Init -->|"2. init pub/sub"| PubSub
    Init -->|"3. instantiate LoggingService"| LogInt
    LogInt -->|"implements"| NewRelicSvc
    NewRelicSvc -->|"logging HTTP"| NewRelicAPI

    Init -->|"4. instantiate AuthService"| AuthInt
    AuthInt -->|"implements"| AxiosJwt
    AuthInt -->|"implements"| AxiosCsrf
    AxiosJwt -->|"auth HTTP"| AuthBackend
    AxiosCsrf -->|"csrf HTTP"| AuthBackend

    Init -->|"5. instantiate AnalyticsService"| AnalInt
    AnalInt -->|"implements"| SegmentSvc
    SegmentSvc -->|"analytics HTTP"| SegmentAPI

    Init -->|"6. instantiate I18nService"| I18nInt
    I18nInt -->|"implements"| ReactIntlSvc
    ReactIntlSvc -->|"load messages"| TransSource

    Init -->|"7. signal ready"| ExampleApp

    %% Consumer usage
    ExampleApp -->|"calls initialize()"| Init
    ExampleApp -->|"uses services"| AnalInt
    ExampleApp -->|"uses services"| LogInt
    ExampleApp -->|"uses services"| AuthInt
    ExampleApp -->|"uses services"| I18nInt
    ExampleApp -->|"renders"| ReactInt

    %% Click Events
    click Init "https://github.com/openedx/frontend-platform/blob/master/src/initialize.js"
    click Config "https://github.com/openedx/frontend-platform/blob/master/src/config.js"
    click PubSub "https://github.com/openedx/frontend-platform/blob/master/src/pubSub.js"
    click Utils "https://github.com/openedx/frontend-platform/blob/master/src/utils.js"
    click AnalInt "https://github.com/openedx/frontend-platform/blob/master/src/analytics/interface.js"
    click LogInt "https://github.com/openedx/frontend-platform/blob/master/src/logging/interface.js"
    click AuthInt "https://github.com/openedx/frontend-platform/blob/master/src/auth/interface.js"
    click I18nInt "https://github.com/openedx/frontend-platform/blob/master/src/i18n/index.js"
    click SegmentSvc "https://github.com/openedx/frontend-platform/blob/master/src/analytics/SegmentAnalyticsService.js"
    click NewRelicSvc "https://github.com/openedx/frontend-platform/blob/master/src/logging/NewRelicLoggingService.js"
    click AxiosJwt "https://github.com/openedx/frontend-platform/blob/master/src/auth/AxiosJwtAuthService.js"
    click AxiosCsrf "https://github.com/openedx/frontend-platform/blob/master/src/auth/AxiosCsrfTokenService.js"
    click ReactIntlSvc "https://github.com/openedx/frontend-platform/blob/master/src/i18n/lib.js"
    click ExampleApp "https://github.com/openedx/frontend-platform/tree/master/example/"
    click ReactInt "https://github.com/openedx/frontend-platform/tree/master/src/react/"

    %% Styles
    classDef core fill:#AED6F1,stroke:#1B4F72,stroke-width:1px;
    classDef interface fill:#A9DFBF,stroke:#145A32,stroke-width:1px;
    classDef implementation fill:#F5B041,stroke:#7D6608,stroke-width:1px;
    classDef consumer fill:#D2B4DE,stroke:#4A235A,stroke-width:1px;
    classDef external fill:#D5D8DC,stroke:#1C2833,stroke-width:1px;
```
