Conversation
|
Caution Review failedThe pull request is closed. WalkthroughFeature-flag support and profileId propagation were added. New public APIs fetch, cache, and clear feature flags; profileId can be set globally or per-call and is included in tracking/pageview payloads. Internal caching and API base resolution were introduced. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Client
participant Lib
participant Cache
participant API as "API Server"
Client->>Lib: getFeatureFlags(options, forceRefresh)
Lib->>Cache: check cachedFlags (pid, age)
alt cache valid and not forceRefresh
Cache-->>Lib: return cached flags
else cache miss/expired or forceRefresh
Lib->>API: POST /feature-flag/evaluate { pid, profileId? }
API-->>Lib: { flags: Record<string,bool> }
Lib->>Cache: store flags + timestamp
Cache-->>Lib: ack
end
Lib-->>Client: Promise<Record<string,bool>>
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
c10b737 to
d0fe207
Compare
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/Lib.ts (2)
439-445: Add timeout to fetch request.The fetch call has no timeout, which could cause the request to hang indefinitely on slow or unresponsive networks.
Consider adding a timeout using
AbortController:+ const controller = new AbortController() + const timeoutId = setTimeout(() => controller.abort(), 10000) // 10 second timeout + const response = await fetch(`${apiBase}/feature-flag/evaluate`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(body), + signal: controller.signal, }) + + clearTimeout(timeoutId)
452-458: Consider validating response structure.The code assumes the response has a
flagsproperty without validation. If the API returns an unexpected structure, accessingdata.flagscould fail.Consider adding basic validation:
const data = (await response.json()) as { flags: Record<string, boolean> } + if (!data || typeof data.flags !== 'object') { + console.warn('[Swetrix] Invalid response structure from feature flags API') + return this.cachedFlags?.flags || {} + } + // Update cache this.cachedFlags = {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/Lib.ts(9 hunks)src/index.ts(3 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/index.ts (1)
src/Lib.ts (4)
getFeatureFlags(416-465)FeatureFlagsOptions(113-119)getFeatureFlag(475-478)clearFeatureFlagsCache(483-485)
src/Lib.ts (1)
src/utils.ts (1)
isInBrowser(17-19)
🔇 Additional comments (8)
src/index.ts (2)
153-178: LGTM!The function correctly handles the null
LIB_INSTANCEcase by returning thedefaultValue, and the JSDoc is accurate.
180-188: LGTM!The cache-clearing function follows the established pattern and correctly handles the null
LIB_INSTANCEcase.src/Lib.ts (6)
36-40: LGTM!The
profileIdadditions provide a clean global-with-override pattern. The implementation correctly uses the nullish coalescing operator intrack(line 333) and allows spread override insubmitPageView(line 555).Also applies to: 55-56, 76-77
110-127: LGTM!The interfaces are well-defined.
FeatureFlagsOptionsprovides a clean API surface, andCachedFlagsproperly encapsulates the cache structure.
208-208: LGTM!The constants are well-defined. A 5-minute cache duration is reasonable for feature flags, balancing freshness with API load.
Also applies to: 210-211
467-478: LGTM!The method correctly delegates to
getFeatureFlagsand uses nullish coalescing to handle missing flag keys.
480-485: LGTM!The cache-clearing logic is straightforward and correct.
487-496: LGTM!The method correctly strips the
/logsuffix from custom API URLs, ensuring feature flag endpoints work with self-hosted instances.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.