@povio/openapi-codegen-cli 2.0.6 → 2.0.8-rc.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@povio/openapi-codegen-cli",
3
- "version": "2.0.6",
3
+ "version": "2.0.8-rc.1",
4
4
  "keywords": [
5
5
  "codegen",
6
6
  "openapi",
@@ -70,7 +70,7 @@
70
70
  "@apidevtools/swagger-parser": "^10.1.0",
71
71
  "@casl/ability": "^6.7.3",
72
72
  "@casl/react": "^5.0.0",
73
- "@tanstack/react-query": "~5.85.9",
73
+ "@tanstack/react-query": "~5.90.21",
74
74
  "@types/node": "^20.12.12",
75
75
  "@types/prompt-sync": "^4.2.3",
76
76
  "@types/react": "^19.1.0",
@@ -100,7 +100,7 @@
100
100
  "peerDependencies": {
101
101
  "@casl/ability": "^6.7.3",
102
102
  "@casl/react": "^5.0.0",
103
- "@tanstack/react-query": "^5.85.9",
103
+ "@tanstack/react-query": "^5.90.21",
104
104
  "axios": "^1.13.1",
105
105
  "react": "^19.1.0",
106
106
  "zod": "^4.1.12"
@@ -0,0 +1,40 @@
1
+ import type { QueryClient, QueryKey } from "@tanstack/react-query";
2
+
3
+ const CROSS_TAB_INVALIDATE_KEY = "__rq_invalidate__";
4
+
5
+ /**
6
+ * Broadcasts a query invalidation event to all other open tabs via localStorage.
7
+ *
8
+ * @param queryKeys - An array of query keys to invalidate (array of arrays).
9
+ *
10
+ * NOTE: The `storage` event only fires in *other* tabs — the calling tab
11
+ * must invalidate its own queryClient separately if needed.
12
+ */
13
+ export const broadcastQueryInvalidation = (queryKeys: QueryKey[]) => {
14
+ localStorage.setItem(CROSS_TAB_INVALIDATE_KEY, JSON.stringify({ keys: queryKeys, timestamp: Date.now() }));
15
+ };
16
+
17
+ /**
18
+ * Registers a one-time global `storage` event listener that reacts to
19
+ * cross-tab invalidation broadcasts. Safe to call from multiple hooks —
20
+ * only the first call sets up the listener.
21
+ */
22
+ let isListenerSetUp = false;
23
+
24
+ export const setupCrossTabListener = (queryClient: QueryClient) => {
25
+ if (isListenerSetUp) return;
26
+ isListenerSetUp = true;
27
+
28
+ window.addEventListener("storage", (e: StorageEvent) => {
29
+ if (e.key !== CROSS_TAB_INVALIDATE_KEY || !e.newValue) return;
30
+
31
+ try {
32
+ const { keys } = JSON.parse(e.newValue) as { keys: QueryKey[] };
33
+ for (const queryKey of keys) {
34
+ queryClient.invalidateQueries({ queryKey });
35
+ }
36
+ } catch {
37
+ // Ignore malformed payloads
38
+ }
39
+ });
40
+ };
@@ -1,12 +1,15 @@
1
- import { useCallback } from "react";
1
+ import { useCallback, useEffect } from "react";
2
2
 
3
3
  import { OpenApiQueryConfig } from "@povio/openapi-codegen-cli";
4
4
  import { QueryKey, useQueryClient } from "@tanstack/react-query";
5
5
 
6
6
  import { QueryModule } from "./queryModules";
7
+ import { broadcastQueryInvalidation, setupCrossTabListener } from "./useCrossTabQueryInvalidation";
7
8
 
8
9
  export interface MutationEffectsOptions {
9
10
  invalidateCurrentModule?: boolean;
11
+ crossTabInvalidation?: boolean;
12
+ invalidationMap?: Record<string, (context: Record<string, string>) => QueryKey[]>;
10
13
  invalidateModules?: QueryModule[];
11
14
  invalidateKeys?: QueryKey[];
12
15
  preferUpdate?: boolean;
@@ -20,14 +23,23 @@ export function useMutationEffects({ currentModule }: UseMutationEffectsProps) {
20
23
  const queryClient = useQueryClient();
21
24
  const config = OpenApiQueryConfig.useConfig();
22
25
 
26
+ useEffect(() => {
27
+ if (!config.crossTabInvalidation) return;
28
+ setupCrossTabListener(queryClient);
29
+ }, [queryClient, config.crossTabInvalidation]);
30
+
23
31
  const runMutationEffects = useCallback(
24
32
  async <TData>(data: TData, options: MutationEffectsOptions = {}, updateKeys?: QueryKey[]) => {
25
33
  const { invalidateCurrentModule = true, invalidateModules, invalidateKeys, preferUpdate } = options;
26
34
  const shouldUpdate = preferUpdate || (preferUpdate === undefined && config.preferUpdate);
35
+ const shouldInvalidateCurrentModule =
36
+ invalidateCurrentModule || (invalidateCurrentModule === undefined && config.invalidateCurrentModule);
27
37
 
28
38
  const isQueryKeyEqual = (keyA: QueryKey, keyB: QueryKey) =>
29
39
  keyA.length === keyB.length && keyA.every((item, index) => item === keyB[index]);
30
40
 
41
+ const invalidatedQueryKeys: QueryKey[] = [];
42
+
31
43
  queryClient.invalidateQueries({
32
44
  predicate: ({ queryKey }) => {
33
45
  const isUpdateKey = updateKeys?.some((key) => isQueryKeyEqual(queryKey, key));
@@ -35,18 +47,32 @@ export function useMutationEffects({ currentModule }: UseMutationEffectsProps) {
35
47
  return false;
36
48
  }
37
49
 
38
- const isCurrentModule = invalidateCurrentModule && queryKey[0] === currentModule;
50
+ const isCurrentModule = shouldInvalidateCurrentModule && queryKey[0] === currentModule;
39
51
  const isInvalidateModule = !!invalidateModules && invalidateModules.some((module) => queryKey[0] === module);
40
52
  const isInvalidateKey = !!invalidateKeys && invalidateKeys.some((key) => isQueryKeyEqual(queryKey, key));
41
- return isCurrentModule || isInvalidateModule || isInvalidateKey;
53
+
54
+ const map = config.invalidationMap?.[currentModule]?.(data);
55
+ const isMappedKey = !!map && map.some((key) => isQueryKeyEqual(queryKey, key));
56
+
57
+ const shouldInvalidate = isCurrentModule || isInvalidateModule || isInvalidateKey || isMappedKey;
58
+
59
+ if (shouldInvalidate && config.crossTabInvalidation) {
60
+ invalidatedQueryKeys.push([...queryKey]);
61
+ }
62
+
63
+ return shouldInvalidate;
42
64
  },
43
65
  });
44
66
 
67
+ if (config.crossTabInvalidation && invalidatedQueryKeys.length > 0) {
68
+ broadcastQueryInvalidation(invalidatedQueryKeys);
69
+ }
70
+
45
71
  if (shouldUpdate && updateKeys) {
46
72
  updateKeys.map((queryKey) => queryClient.setQueryData(queryKey, data));
47
73
  }
48
74
  },
49
- [queryClient, currentModule, config.preferUpdate],
75
+ [queryClient, currentModule, config.preferUpdate, config.invalidationMap, config.crossTabInvalidation],
50
76
  );
51
77
 
52
78
  return { runMutationEffects };