@terreno/rtk 0.14.0 → 0.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/authSlice.d.ts +4 -4
  2. package/dist/authSlice.d.ts.map +1 -1
  3. package/dist/authSlice.js +7 -7
  4. package/dist/authSlice.js.map +1 -1
  5. package/dist/betterAuthSlice.d.ts.map +1 -1
  6. package/dist/betterAuthSlice.js +3 -2
  7. package/dist/betterAuthSlice.js.map +1 -1
  8. package/dist/constants.test.js +41 -0
  9. package/dist/constants.test.js.map +1 -1
  10. package/dist/emptyApi.d.ts +1 -1
  11. package/dist/emptyApi.d.ts.map +1 -1
  12. package/dist/emptyApi.js +2 -2
  13. package/dist/emptyApi.js.map +1 -1
  14. package/dist/mongooseSlice.d.ts +3 -2
  15. package/dist/mongooseSlice.d.ts.map +1 -1
  16. package/dist/mongooseSlice.js +3 -4
  17. package/dist/mongooseSlice.js.map +1 -1
  18. package/dist/offlineMiddleware.d.ts.map +1 -1
  19. package/dist/offlineMiddleware.js +1 -3
  20. package/dist/offlineMiddleware.js.map +1 -1
  21. package/dist/offlineSlice.d.ts +24 -2
  22. package/dist/offlineSlice.d.ts.map +1 -1
  23. package/dist/offlineSlice.js +2 -2
  24. package/dist/offlineSlice.js.map +1 -1
  25. package/dist/tagGenerator.d.ts.map +1 -1
  26. package/dist/tagGenerator.js +3 -10
  27. package/dist/tagGenerator.js.map +1 -1
  28. package/dist/useFeatureFlags.d.ts +5 -2
  29. package/dist/useFeatureFlags.d.ts.map +1 -1
  30. package/dist/useFeatureFlags.js +35 -16
  31. package/dist/useFeatureFlags.js.map +1 -1
  32. package/dist/useFeatureFlags.test.js +22 -0
  33. package/dist/useFeatureFlags.test.js.map +1 -1
  34. package/dist/useServerStatus.js +1 -1
  35. package/dist/useServerStatus.js.map +1 -1
  36. package/package.json +2 -2
  37. package/src/authSlice.ts +9 -9
  38. package/src/betterAuthSlice.ts +3 -2
  39. package/src/constants.test.ts +45 -0
  40. package/src/emptyApi.ts +3 -3
  41. package/src/mongooseSlice.ts +4 -5
  42. package/src/offlineMiddleware.ts +1 -3
  43. package/src/offlineSlice.ts +5 -3
  44. package/src/tagGenerator.ts +2 -8
  45. package/src/useFeatureFlags.test.ts +25 -0
  46. package/src/useFeatureFlags.ts +47 -22
  47. package/src/useServerStatus.ts +1 -1
@@ -1,7 +1,49 @@
1
1
  import type {Api} from "@reduxjs/toolkit/query/react";
2
- import {useCallback, useEffect, useMemo, useRef} from "react";
2
+ import {useCallback, useEffect, useRef} from "react";
3
3
 
4
- type FlagValues = Record<string, boolean | string | null>;
4
+ interface FlagValues {
5
+ [key: string]: boolean | string | null;
6
+ }
7
+
8
+ // biome-ignore lint/suspicious/noExplicitAny: RTK Query API generic typing is intentionally flexible here.
9
+ type FlagsApi = Api<any, any, any, any>;
10
+
11
+ // biome-ignore lint/suspicious/noExplicitAny: Endpoint hook is injected dynamically by RTK Query.
12
+ type EnhancedFlagsApi = FlagsApi & {useEvaluateFeatureFlagsQuery: any};
13
+
14
+ /**
15
+ * Cache the enhanced api per (api, basePath). `injectEndpoints` logs a console
16
+ * error in development whenever an endpoint with the same name is re-injected
17
+ * with `overrideExisting: false`, so calling it on every render of every
18
+ * consumer would flood the console. WeakMap-by-api lets the GC reclaim entries
19
+ * when the api object is unreachable.
20
+ */
21
+ const enhancedApiCache = new WeakMap<FlagsApi, Map<string, EnhancedFlagsApi>>();
22
+
23
+ const getEnhancedApi = (api: FlagsApi, basePath: string): EnhancedFlagsApi => {
24
+ let byBase = enhancedApiCache.get(api);
25
+ if (!byBase) {
26
+ byBase = new Map();
27
+ enhancedApiCache.set(api, byBase);
28
+ }
29
+ const cached = byBase.get(basePath);
30
+ if (cached) {
31
+ return cached;
32
+ }
33
+ const enhanced = api.injectEndpoints({
34
+ endpoints: (builder) => ({
35
+ evaluateFeatureFlags: builder.query<FlagValues, void>({
36
+ query: () => ({
37
+ method: "GET",
38
+ url: `${basePath}/evaluate`,
39
+ }),
40
+ }),
41
+ }),
42
+ overrideExisting: false,
43
+ }) as EnhancedFlagsApi;
44
+ byBase.set(basePath, enhanced);
45
+ return enhanced;
46
+ };
5
47
 
6
48
  interface UseFeatureFlagsResult {
7
49
  flags: FlagValues;
@@ -65,30 +107,13 @@ export const resolveFeatureFlagsOptions = (
65
107
  // Overloaded signature preserves backwards compatibility with callers that
66
108
  // pass a string basePath as the second argument.
67
109
  export function useFeatureFlags(
68
- // biome-ignore lint/suspicious/noExplicitAny: RTK Query API generic typing is intentionally flexible here.
69
- api: Api<any, any, any, any>,
110
+ api: FlagsApi,
70
111
  basePathOrOptions?: string | UseFeatureFlagsOptions
71
112
  ): UseFeatureFlagsResult {
72
113
  const {basePath, skip} = resolveFeatureFlagsOptions(basePathOrOptions);
73
114
 
74
- const enhancedApi = useMemo(
75
- () =>
76
- api.injectEndpoints({
77
- endpoints: (builder) => ({
78
- evaluateFeatureFlags: builder.query<FlagValues, void>({
79
- query: () => ({
80
- method: "GET",
81
- url: `${basePath}/evaluate`,
82
- }),
83
- }),
84
- }),
85
- overrideExisting: false,
86
- }),
87
- [api, basePath]
88
- );
89
-
90
- // biome-ignore lint/suspicious/noExplicitAny: Endpoint hook is injected dynamically by RTK Query.
91
- const useEvaluateQuery = (enhancedApi as any).useEvaluateFeatureFlagsQuery;
115
+ const enhancedApi = getEnhancedApi(api, basePath);
116
+ const useEvaluateQuery = enhancedApi.useEvaluateFeatureFlagsQuery;
92
117
  const {data, isLoading, error, refetch} = useEvaluateQuery(undefined, {skip});
93
118
  const evaluateStartedAtRef = useRef<number | null>(null);
94
119
 
@@ -96,7 +96,7 @@ export const useServerStatus = (options: ServerStatusOptions = {}): ServerStatus
96
96
 
97
97
  // Also respond to browser online/offline events for instant detection
98
98
  useEffect(() => {
99
- if (skip || typeof window === "undefined") {
99
+ if (skip || typeof window === "undefined" || typeof window.addEventListener !== "function") {
100
100
  return;
101
101
  }
102
102