33
33
import type { MaybeRef } from '@vueuse/core' ;
34
34
import { toValue } from '@vueuse/core' ;
35
35
import type { ComputedRef } from 'vue' ;
36
- import { computed } from 'vue' ;
36
+ import { computed , getCurrentInstance } from 'vue' ;
37
37
38
38
import {
39
39
useQuery , type UseQueryOptions , type UseQueryReturnType ,
@@ -104,8 +104,17 @@ const _warnMissingRequiredScopes = (scopes: GrantScope[]) => {
104
104
}
105
105
} ;
106
106
107
- // Logs a warning once per queryKey when the current scope is invalid for this query
108
- const _warnedKeys = new Set < string > ( ) ;
107
+ /**
108
+ * Logs a warning when a query is not executed due to invalid scope,
109
+ * but only once per queryKey during development.
110
+ *
111
+ * Conditions to trigger warning:
112
+ * - `enabled` is true
113
+ * - app is ready (not loading)
114
+ * - currentScope is defined
115
+ * - currentScope NOT included in requiredScopes
116
+ */
117
+ const _warnedKeysPerInstance = new WeakMap < object , Set < string > > ( ) ;
109
118
const _warnInvalidScopeOnce = ( params : {
110
119
queryKey : QueryKeyArray ;
111
120
enabled : boolean ;
@@ -115,20 +124,39 @@ const _warnInvalidScopeOnce = (params: {
115
124
} ) => {
116
125
if ( ! import . meta. env . DEV ) return ;
117
126
127
+ // Get the current Vue component instance (used to scope the warning cache)
128
+ const instance = getCurrentInstance ( ) ;
129
+ if ( ! instance ) return ;
130
+
118
131
const {
119
132
queryKey, enabled, currentScope, requiredScopes, isAppReady,
120
133
} = params ;
121
- if ( ! enabled || ! isAppReady || ! currentScope ) return ;
122
134
123
- if ( requiredScopes . includes ( currentScope ) ) return ;
135
+ if ( ! isAppReady || ! currentScope ) return ;
124
136
125
- const key = Array . isArray ( queryKey )
126
- ? queryKey . join ( ':' )
127
- : String ( queryKey ) ;
137
+ const isValidScope = requiredScopes . includes ( currentScope ) ;
128
138
129
- if ( _warnedKeys . has ( key ) ) return ;
139
+ if ( ! enabled && isValidScope ) return ;
130
140
131
- _warnedKeys . add ( key ) ;
141
+ if ( isValidScope ) return ;
142
+
143
+ // Safely serialize the queryKey (even if it contains objects)
144
+ const key = ( ( ) => {
145
+ try {
146
+ return JSON . stringify ( queryKey ) ;
147
+ } catch {
148
+ return Array . isArray ( queryKey ) ? queryKey . join ( ':' ) : String ( queryKey ) ;
149
+ }
150
+ } ) ( ) ;
151
+
152
+ // Cache per component instance to prevent duplicate logs
153
+ let keySet = _warnedKeysPerInstance . get ( instance ) ;
154
+ if ( ! keySet ) {
155
+ keySet = new Set ( ) ;
156
+ _warnedKeysPerInstance . set ( instance , keySet ) ;
157
+ }
158
+ if ( keySet . has ( key ) ) return ;
159
+ keySet . add ( key ) ;
132
160
133
161
console . warn ( '[useScopedQuery] Query not executed due to invalid grant scope.' , {
134
162
queryKey,
0 commit comments