How schibsted/account-sdk-browser works
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.