A small module loads a status badge for a text request, stores the result in memory, and returns it as an object for the UI.
Requirements
- Input is a query string.
- Empty strings and strings made only of spaces must be rejected before accessing the data source.
- The same normalized query must use a single cache key.
- The response must be structurally validated before being saved to the cache and before being returned externally.
- The returned value must not share mutable nested data with the internal cache.
- If the source fails, the function must return a safe result rather than throwing an unhandled exception into the UI.
Reproduction
- Run the verification script under Node.js 22 with TypeScript 5.8 and
strict=true.
- Pass a string with spaces, then a normal query.
- Repeat the query with the same meaning but a different number of spaces.
- Provide a source that returns an object with the wrong structure, and a source that throws an exception.
Example call:
await loadBadge(" sales ", fetchBadge)
await loadBadge("sales", fetchBadge)
Expected behavior
- Whitespace input is rejected without a network request.
"sales" and " sales " use the same cached result.
- An invalid payload is not stored as a successful result.
- The returned object can be read safely without affecting the cache.