# schibsted/account-sdk-browser architecture

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

- Interactive diagram: https://gitdiagram.com/schibsted/account-sdk-browser
- Repository: https://github.com/schibsted/account-sdk-browser

## Overview

##### Project Type and Purpose
- This is a JavaScript/TypeScript SDK published to NPM (“@schibsted/account-sdk-browser”).
- It is a client-side library for browsers, providing Identity, Monetization and Payment APIs.
- It communicates over HTTP to remote Session Service endpoints (OAuth2/OIDC flows, product entitlements, paylinks).

##### File Structure Analysis
- Top-level modules:
- identity.js/.d.ts, monetization.js/.d.ts, payment.js/.d.ts, index.js/.d.ts
- RESTClient.js/.d.ts (HTTP transport abstraction)
- SDKError.js/.d.ts, cache.js/.d.ts, config.js/.d.ts, utils.js/.d.ts
- `src/` – Source code in modern ESNext + type definitions
- `src/es5/` – Pre-built ES5 bundles for legacy browsers
- `__tests__/` & `__mocks__/` – Unit and integration tests
- `scripts/` – Build helper (genversion.js), `build.sh` for packing es5 and main bundles
- CI Config: GitHub workflows (npm-publish, github-pages, PR check), Jest config, ESLint, jsdoc

##### Core Components
a. SDK Entry Points
- Identity
- Monetization
- Payment
These classes are the public interface imported by consumers.
b. Transport Layer
- RESTClient: wraps `fetch`/XHR, handles cookies, query-string building, error mapping via SDKError
- Config: holds clientId, redirectUri, env, sessionDomain
c. Utilities
- cache (in-memory/sessionStorage for tokens)
- validate (parameter validation)
- url builder (generates auth URLs, paylink URLs)
- popup helper (opens fallback window or redirect)
d. External Services
- Session Service (OAuth2/OIDC endpoints on sessionDomain)
- Monetization API (access checks)
- Payment API (paylink creation, purchase endpoints)

##### Relationships and Interactions
- Identity, Monetization, Payment each depend on Config + RESTClient
- RESTClient issues HTTP calls → Session Service API → receives JSON responses or 401 errors
- Identity emits events (login/logout) via EventEmitter pattern
- Popup helper uses `window.open` + `postMessage` to coordinate flows
- cache & SDKError are cross-cutting: RESTClient transforms errors to SDKError; cache used by Identity/Monetization

##### Architectural Patterns & Design Principles
- Layered architecture:
- Public SDK layer (Identity, Monetization, Payment)
- Transport layer (RESTClient, config)
- Utility layer (validate, url, cache, popup)
- Single responsibility: each class/module does one job
- Event-driven for Identity session changes
- Polyfill/ES5-fallback – separate bundles in `src/es5`

##### Detail Level
- Core modules in `src/` (including .d.ts to indicate type definitions)
- Test layer only if necessary (can be omitted for simplicity)
- Optional: annotate build script → bundles → published package

Component’s responsibility and interaction is explicitly shown so that any new engineer can grasp the end-to-end flow—from import to Session Service communication to event handling.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Public API Layer"
        Entry["SDK Entry Point\n(index.js/.d.ts)"]:::public
        Identity["Identity\n(identity.js/.d.ts)"]:::public
        Monetization["Monetization\n(monetization.js/.d.ts)"]:::public
        Payment["Payment\n(payment.js/.d.ts)"]:::public
    end

    subgraph "Transport Layer"
        RESTClient["RESTClient\n(RESTClient.js/.d.ts)"]:::transport
        Config["Config\n(config.js/.d.ts)"]:::transport
    end

    subgraph "Utility Layer"
        SDKError["SDKError\n(SDKError.js/.d.ts)"]:::util
        Cache["Cache\n(cache.js/.d.ts)"]:::util
        Validate["Validate\n(validate.js/.d.ts)"]:::util
        URLBuilder["URL Builder\n(url.js/.d.ts)"]:::util
        Popup["Popup Helper\n(popup.js/.d.ts)"]:::util
    end

    subgraph "External Services"
        SessionService[(Session Service)]:::external
        MonetAPI[(Monetization API)]:::external
        PaymentAPI[(Payment API)]:::external
    end

    subgraph "Build & Bundles"
        BuildSH["build.sh"]:::build
        GenVersion["genversion.js"]:::build
        ES5Bundles["ES5 Bundles\n(src/es5)"]:::build
    end

    %% Public to Transport
    Entry --> Identity
    Entry --> Monetization
    Entry --> Payment

    Identity --> RESTClient
    Identity --> Config
    Monetization --> RESTClient
    Monetization --> Config
    Payment --> RESTClient
    Payment --> Config

    %% Transport to External & Utilities
    RESTClient --> SessionService
    RESTClient --> MonetAPI
    RESTClient --> PaymentAPI
    RESTClient --> SDKError

    %% Public to Utilities
    Identity --> Popup
    Identity --> Cache
    Identity --> URLBuilder

    %% Build workflows
    BuildSH --> ES5Bundles
    GenVersion --> ES5Bundles
    ES5Bundles -.-> Entry

    %% Click Events
    click Identity "https://github.com/schibsted/account-sdk-browser/blob/master/src/identity.js"
    click Monetization "https://github.com/schibsted/account-sdk-browser/blob/master/src/monetization.js"
    click Payment "https://github.com/schibsted/account-sdk-browser/blob/master/src/payment.js"
    click Entry "https://github.com/schibsted/account-sdk-browser/blob/master/src/index.js"
    click RESTClient "https://github.com/schibsted/account-sdk-browser/blob/master/src/RESTClient.js"
    click Config "https://github.com/schibsted/account-sdk-browser/blob/master/src/config.js"
    click SDKError "https://github.com/schibsted/account-sdk-browser/blob/master/src/SDKError.js"
    click Cache "https://github.com/schibsted/account-sdk-browser/blob/master/src/cache.js"
    click Validate "https://github.com/schibsted/account-sdk-browser/blob/master/src/validate.js"
    click URLBuilder "https://github.com/schibsted/account-sdk-browser/blob/master/src/url.js"
    click Popup "https://github.com/schibsted/account-sdk-browser/blob/master/src/popup.js"
    click BuildSH "https://github.com/schibsted/account-sdk-browser/blob/master/build.sh"
    click GenVersion "https://github.com/schibsted/account-sdk-browser/blob/master/scripts/genversion.js"

    %% Styles
    classDef public fill:#BBDEFB,stroke:#0D47A1
    classDef transport fill:#C8E6C9,stroke:#1B5E20
    classDef util fill:#FFF9C4,stroke:#F57F17
    classDef external fill:#CFD8DC,stroke:#455A64
    classDef build fill:#E1BEE7,stroke:#6A1B9A
```
