Skip to content

feat(utils): add persistent cache and backend error utilities#64

Open
nicomiguelino wants to merge 2 commits into
mainfrom
feat/persistent-cache-utils
Open

feat(utils): add persistent cache and backend error utilities#64
nicomiguelino wants to merge 2 commits into
mainfrom
feat/persistent-cache-utils

Conversation

@nicomiguelino

Copy link
Copy Markdown
Contributor

Summary

  • Add createPersistentCache(), a localStorage-backed, namespaced last-known-good value cache
  • Add BackendServerError and shouldSkipBackendError() to gate falling back to cached data on transient backend failures
  • Document both utilities with usage examples in README
  • Bump package version to 1.3.0

Test plan

  • npm test — new unit tests for createPersistentCache and shouldSkipBackendError pass

- 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) with read/write APIs and unit tests.
  • Add BackendServerError and shouldSkipBackendError() with unit tests to gate cache fallback based on display_errors.
  • Document the new utilities in README.md and bump package version to 1.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.

Comment thread src/utils/persistent-cache.test.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.localStorage can 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 a try/finally to 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()`

Comment thread src/utils/persistent-cache.ts
Comment thread src/utils/persistent-cache.test.ts Outdated
Comment thread src/utils/backend-error.ts Outdated
Comment thread README.md Outdated
Comment thread README.md Outdated
- 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 8 changed files in this pull request and generated no new comments.

@nicomiguelino
nicomiguelino marked this pull request as ready for review July 27, 2026 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants