StaffCoder
TypeScript
Vanilla TypeScriptTypeScript 7.0
6 modules · 24 challengesSign up
Module 5 · Modules & Code Organisation

Storefront Config Module

easyCentralising a global behind a typed config module with defaultsGeneric key-typed accessors and named error classes—No data yet

Problem statement

A small storefront page needs one config module that reads a raw settings object off window and hands typed, defaulted, frozen values to every consumer on the page — so nothing else on the page has to know the global exists, and a fake key never comes back as undefined, using Vanilla TypeScript and standard DOM APIs.

Demo

The page, styles, seeded product data, DOM helpers, price label consumer, promo banner consumer, API client consumer, and bootstrap entry are already scaffolded and wired. Only the logic inside src/js/config.ts is missing — implement it so every test passes. Do not edit any other file.

What to implement

  • Read the settings object the page put on the window before the bundle ran, and use whatever values it holds.
  • For any setting the page did not include, fall back to the documented default value.
  • Show a featured product card with the product name and its price prefixed by the currency symbol chosen by the current setting.
  • Show a promo banner strip only when the current setting turns the promo on; keep it hidden otherwise.
  • Show the current API base URL in the small footer.
  • Prevent the rest of the page from changing any value it reads back — once handed out, the settings snapshot cannot be mutated.
  • Raise the named error when the page asks for a setting name that is not one of the known settings.
  • Everything shown must come from the injected settings and their documented defaults — nothing should be hardcoded.

More in Modules & Code Organisation

See the module
Storefront Utilities PackageSplit a monolithic entry into three named-export modules — format (formatPrice, formatSavingsPercent), validate (isValidEmail), and dom (setText, setDisabled) — and wire them from a thin entry inside a single DOMContentLoaded handler. No file may assign to window, importing any helper in isolation must not touch the document, and the helper import graph must have no cycles.—No data yeteasyAnalytics Chart LoaderImplement createChartLoader(importFn) and the default loadChart so a heavy chart module is only fetched via import('./chart') when the user clicks Load. The loader dedupes concurrent calls to a single in-flight promise, caches the resolved module for every future call, and does not cache failures so Retry starts clean. The entry never references ./chart statically.—No data yeteasyShared Storefront Bootstrap Write bootstrap.ts, homeEntry.ts, and productEntry.ts so nothing runs at import time. initBootstrap() runs its work exactly once no matter how many entries invoke it; each entry's init() writes only to its own view and registers only its own click listener; and any init() is safe to call multiple times without duplicating listeners or double-writing text. Idempotency lives in module-scope let flags; no file assigns to window.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.