feat(utils): add persistent cache and backend error utilities#64
feat(utils): add persistent cache and backend error utilities#64nicomiguelino wants to merge 2 commits into
Conversation
- add createPersistentCache() for localStorage-backed last-known-good caching - add BackendServerError and shouldSkipBackendError() to gate cache fallback - document new utilities and usage patterns in README - bump package version to 1.3.0
There was a problem hiding this comment.
Pull request overview
Adds two new utilities to the @screenly/edge-apps utils surface area: a localStorage-backed “last-known-good” persistent cache and a small error utility (BackendServerError + shouldSkipBackendError) to control when cached fallback is allowed. This fits the library’s existing goal of providing reusable Edge App platform/helpers via the src/utils barrel.
Changes:
- Add
createPersistentCache(namespace)withread/writeAPIs and unit tests. - Add
BackendServerErrorandshouldSkipBackendError()with unit tests to gate cache fallback based ondisplay_errors. - Document the new utilities in
README.mdand bump package version to1.3.0.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/utils/persistent-cache.ts | Introduces a namespaced localStorage persistent cache utility with safe (non-throwing) read/write behavior. |
| src/utils/persistent-cache.test.ts | Adds unit tests for cache behavior, namespacing, invalid JSON, and missing localStorage. |
| src/utils/index.ts | Re-exports the new backend error and persistent cache utilities from the utils barrel. |
| src/utils/backend-error.ts | Adds BackendServerError and shouldSkipBackendError() for “transient backend failure” classification. |
| src/utils/backend-error.test.ts | Adds unit tests validating shouldSkipBackendError() behavior. |
| README.md | Documents usage patterns for persistent cache + backend transient error gating and updates the exported types example. |
| package.json | Bumps package version to 1.3.0. |
| package-lock.json | Updates lockfile version metadata to 1.3.0. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 8 changed files in this pull request and generated 5 comments.
Comments suppressed due to low confidence (2)
src/utils/persistent-cache.test.ts:47
- Deleting/restoring
globalThis.localStoragecan throw in some environments (non-configurable globals) and doesn't use the repo's existing stubbing pattern (vi.stubGlobal/vi.unstubAllGlobals). Consider stubbing via Vitest with atry/finallyto ensure cleanup even if an assertion fails.
const originalLocalStorage = globalThis.localStorage
// @ts-expect-error simulating an environment without localStorage
delete globalThis.localStorage
const cache = createPersistentCache('test-app')
expect(cache.read('credentials')).toBeNull()
expect(() => cache.write('credentials', { accessToken: 'x' })).not.toThrow()
globalThis.localStorage = originalLocalStorage
})
README.md:188
- If the section above is promoted to a
##heading, these subsections should be###(not####) to keep the heading hierarchy consistent.
#### `BackendServerError` and `shouldSkipBackendError()`
- restore localStorage via vi.stubGlobal/unstubAllGlobals instead of delete - skip persisting a write when JSON.stringify returns undefined - set BackendServerError.name in constructor for clearer logs/stack traces - promote persistent cache/backend error docs to a top-level README section with consistent heading levels
Summary
createPersistentCache(), alocalStorage-backed, namespaced last-known-good value cacheBackendServerErrorandshouldSkipBackendError()to gate falling back to cached data on transient backend failuresTest plan
npm test— new unit tests forcreatePersistentCacheandshouldSkipBackendErrorpass