A small cache status service started behaving unstably after repeated requests with similar inputs. You need to restore the expected behavior for request validation, cache key selection, and result return.
Requirements
- The service runs in Node.js as an ESM module and exports
fetchStatus(input: unknown): Promise<StatusResponse>.
- The input is normalized first: a string with leading and trailing spaces is considered the same request as the same string without spaces.
- An empty string after normalization is considered invalid, the request is not sent, and the function returns an error in a consistent format.
- For a valid request, the service calls
loadStatus(key) and caches the result by the normalized key.
- The returned value must be an independent snapshot so that external code cannot accidentally modify the internal cache through the received object.
- If the data source returns a record for the wrong key or with
ok: false, this is considered an error.
- A network error must be returned as an error, not as a successful response.
Reproduction
- Pass a string with spaces, for example:
await fetchStatus(' pro ')
- Then call the function with the same logical request without spaces.
- Change the
items field on the received result and call the function again for the same key.
- Substitute the source response so that it returns a record with someone else's
key or ok: false.
Expected behavior
- Whitespace input should be normalized and use the same cache key as the trimmed string.
- Invalid input after normalization should immediately return an error in the response object.
- A foreign key and
ok: false must not be accepted as a successful result.
- Changing the object returned by
fetchStatus must not change the next response for the same key.