This document provides reference documentation for all public APIs in the Nuxt Ads application, organized by layer and module. For working code examples, see the test files and source implementations.
Type definitions for the core ad entity.
Implementation: domain/ads/types.ts
Union type: GoogleAdSense | AmazonBanner | ImageAd
Use the type field to narrow to specific variants. Supports exhaustive pattern matching.
Google AdSense format with slot and layout configuration.
Key Fields: type, slot, layoutKey, format
Amazon affiliate product with image and pricing.
Key Fields: type, href, imageUrl, price, height, width
Generic image-based ad with link.
Key Fields: type, href, imageUrl, height, width
Validation logic for ad domain rules.
Implementation: domain/ads/validators.ts
Validates raw ad data and returns typed Ad or error.
Test Examples: tests/unit/domain/validators.test.ts
Functional error handling pattern.
Implementation: domain/shared/result.ts
Discriminated union for explicit error handling.
- Success:
{ isOk: true; value: T } - Error:
{ isErr: true; error: E }
Test Examples: tests/unit/domain/validators.test.ts
Domain-specific error types.
Implementation: domain/shared/errors.ts
Thrown when data validation fails.
Main use case for fetching random ads.
Implementation: application/use-cases/FetchRandomAd.ts
Orchestrates ad fetching with repository and validation.
Methods:
constructor(repository: IAdRepository)async execute(filters?: Record<string, string>): Promise<Result<Ad, Error>>
Test Examples: tests/unit/application/FetchRandomAd.test.ts
Port (interface) for ad data access.
Implementation: application/ports/IAdRepository.ts
Methods:
fetchRandom(filters?: Record<string, string>): Promise<Result<Ad, Error>>
Implementations:
Port (interface) for configuration access.
Implementation: application/ports/IConfigProvider.ts
Methods:
getAdsServerUrl(): stringgetAdClient(): string
Implementations:
Adapter for Laravel backend API.
Implementation: infrastructure/repositories/AdRepository.ts
Constructor: new AdRepository(configProvider: IConfigProvider)
Features:
- ✅ Automatic API response validation
- ✅ Type normalization (MochahostBanner → Mochahost)
- ✅ Error handling with Result pattern
- ✅ Query parameter support
Test Examples: tests/unit/infrastructure/AdRepository.test.ts
Adapter for Nuxt runtime configuration.
Implementation: infrastructure/config/NuxtConfigProvider.ts
Reads from Nuxt runtime config (NUXT_PUBLIC_ADS_SERVER, NUXT_PUBLIC_AD_CLIENT).
Security validators for parameters and configuration.
Implementation: infrastructure/security/validators.ts
validateQueryParameters(filters: Record<string, string>): Result<Record<string, string>, ValidationError>
Validates query parameters against whitelist.
Whitelist: at, pk, random, category, sb
Validation:
- Whitelist check (not in set = rejected)
- Type check (must be string)
- Length check (max 100 chars, DOS protection)
- Whitespace trimming
Test Examples: tests/unit/infrastructure/security.test.ts
validateRuntimeConfig(config: { adsServer?: string; adClient?: string }): Result<void, ValidationError>
Validates runtime configuration on startup.
Validation:
- Both
adsServerandadClientrequired adsServermust be valid URLadClientmust be non-empty string
Test Examples: tests/unit/infrastructure/security.test.ts
Safe postMessage API for iframe communication.
Implementation: infrastructure/security/messaging.ts
Interface with required type field: { type: string; [key: string]: any }
Validates incoming postMessage events.
Security Checks:
- ✅ Origin validation (explicit list, no wildcards)
- ✅ Structure validation (must be object)
- ✅ Type field requirement
- ✅ Fail-closed approach
Test Examples: tests/unit/infrastructure/messaging.test.ts
Safely sends postMessage to parent window.
Security Features:
- ✅ Message validation before sending
- ✅ Auto-detects parent origin in iframe scenarios
- ✅ Configurable target origin (defaults to current origin for same-origin)
- ✅ Error handling with logging
- ✅ No message broadcast (parent only)
Test Examples: tests/unit/infrastructure/messaging.test.ts
Vue composable controller for presentation logic.
Implementation: composables/useAdController.ts
Reactive composable that orchestrates use cases for Vue components.
Returned Properties:
isLoading: Ref<boolean>- Loading statead: Ref<Ad | null>- Current ad or nullerror: Ref<Error | null>- Error object or nullfetchAd: (filters?: Record<string, string>) => Promise<void>- Fetch function
Features:
- ✅ Automatic query parameter validation (Phase 5)
- ✅ Configuration validation on first use (Phase 5)
- ✅ Dependency injection for testability
- ✅ Comprehensive error handling
- ✅ Result pattern for explicit error handling
Test Examples: tests/unit/RandomAd.test.ts
Main ad display component.
Implementation: components/RandomAd.vue
- ✅ Renders different ad types (GoogleAdSense, AmazonBanner, ImageAd)
- ✅ Shows shuffle button when
sb=1query param present - ✅ Auto-calculates image dimensions and resizes parent iframe
- ✅ Validates all query parameters (Phase 5)
- ✅ Uses safe messaging API (Phase 5)
- ✅ Shows loading indicator during fetch
Developers embed ads via ads.js script - no direct component usage needed.
Test Examples: tests/unit/RandomAd.test.ts
Domain Layer (Pure)
├── Ad (discriminated union)
├── ValidationError
└── Result<T, E>
↑ depends on
│
Application Layer (Orchestration)
├── FetchRandomAdUseCase
├── IAdRepository
└── IConfigProvider
↑ implements
│
Infrastructure Layer (Adapters)
├── AdRepository
├── NuxtConfigProvider
├── validateQueryParameters()
├── validateRuntimeConfig()
├── validateMessageEvent()
└── sendSafeMessage()
↑ uses
│
Presentation Layer (UI)
├── useAdController() (composable)
└── RandomAd.vue (component)
See tests/unit/RandomAd.test.ts for complete working examples.
Key Steps:
- Use
useAdController()composable in Vue component - Call
fetchAd(filters)with optional query parameters - Check
ad.valuefor result orerror.valuefor errors
See tests/unit/infrastructure/security.test.ts for working examples.
Key Steps:
- Call
validateQueryParameters(filters) - Check
isOkorisErron result - Use validated
valueif successful
See tests/unit/infrastructure/messaging.test.ts for working examples.
Key Steps:
- Import
sendSafeMessageorvalidateMessageEvent - Create message with required
typefield - Send/validate with appropriate function
All public APIs have comprehensive unit tests. Run the full test suite:
pnpm test- Execute all testspnpm test tests/unit/domain/- Domain layer testspnpm test tests/unit/application/- Application layer testspnpm test tests/unit/infrastructure/- Infrastructure layer testspnpm test tests/unit/components/- Component tests
Test Coverage: ✅ Comprehensive unit tests
- ARCHITECTURE.md - Overall architecture and design patterns
- SECURITY.md - Security best practices and threat models
Last Updated: February 5, 2026 Status: Complete (Refactored Option A - No Code Duplication)