Skip to content

feat(Campaign): make context creation extensible via overridable template method #314

@timowestnosto

Description

@timowestnosto

Problem
NostoCampaign currently calls getContext(...) directly when preparing template data. This prevents subclasses or future variations (e.g., adding custom context enrichment, feature flags, A/B metadata) from extending or modifying the context before rendering.

Goal
Introduce a template method that centralizes context construction so it can be overridden or monkey‑patched without duplicating render logic.

Proposal

  1. Add a method (e.g. createContext(raw: JSONResult): object) in NostoCampaign.
  2. Replace direct getContext(...) usage with this.createContext(...).
  3. Document the extension point in TypeDoc.

Acceptance Criteria

  • Campaign still renders identically (no behavioral changes).
  • Tests pass (no snapshot/content change).
  • Subclasses can override createContext to inject/transform values.
  • TypeDoc shows the new method.
  • No lint/type errors.

Non-Goals

  • No refactor of getContext itself.
  • No changes to other components yet.

Risks

  • Overridden implementations might skip required normalization. Mitigate via doc note: “Call super.createContext(raw)” or reuse getContext.

Suggested Patch (illustrative)

// ...existing code...
import { getContext } from "../../templating/context"
// ...existing code...

export class NostoCampaign extends NostoElement {
  // ...existing code...

  /**
   * Extension point: override to enrich or modify the template context
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  createContext(raw: JSONResult) {
    return getContext(raw)
  }

  async #renderRecommendation(rec: Recommendation) {
    // ...existing code...
    // before:
    // const ctx = getContext(base)
    const ctx = this.createContext(base)
    // ...existing code...
  }
  
  // ...existing code...
}

Follow-up Tasks

  • Add a brief test with a subclass overriding createContext (optional but recommended).
  • Update README / developer docs (extension section) if such section exists.

Labels Suggested:
type: enhancement, component: campaign, extensibility, good first issue

Let me know if you want the test scaffold added in the same issue.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions