66import type { Resource } from '@viamrobotics/sdk' ;
77import { toStore , fromStore } from 'svelte/store' ;
88import { usePolling } from './use-polling.svelte' ;
9+ import { useQueryLogger } from '../query-logger' ;
910
1011// eslint-disable-next-line @typescript-eslint/no-explicit-any
1112export type ArgumentsType < T > = T extends ( ...args : infer U ) => any ? U : never ;
@@ -34,6 +35,8 @@ export const createResourceQuery = <T extends Resource, K extends keyof T>(
3435 ]
3536 | [ options ?: ( ( ) => QueryOptions ) | QueryOptions ]
3637) : { current : QueryObserverResult < ResolvedReturnType < T [ K ] > > } => {
38+ const debug = useQueryLogger ( ) ;
39+
3740 let [ args , options ] = additional ;
3841
3942 if ( options === undefined && args !== undefined ) {
@@ -45,6 +48,8 @@ export const createResourceQuery = <T extends Resource, K extends keyof T>(
4548 typeof options === 'function' ? options ( ) : options
4649 ) ;
4750 const _args = $derived ( typeof args === 'function' ? args ( ) : args ) ;
51+ const name = $derived ( client . current ?. name ) ;
52+ const methodName = $derived ( String ( method ) ) ;
4853
4954 const queryOptions = $derived (
5055 createQueryOptions ( {
@@ -53,8 +58,8 @@ export const createResourceQuery = <T extends Resource, K extends keyof T>(
5358 'partID' ,
5459 ( client . current as T & { partID : string } ) ?. partID ,
5560 'resource' ,
56- client . current ?. name ,
57- String ( method ) ,
61+ name ,
62+ methodName ,
5863 ...( _args ? [ _args ] : [ ] ) ,
5964 ] ,
6065 enabled : client . current !== undefined && _options ?. enabled !== false ,
@@ -68,9 +73,21 @@ export const createResourceQuery = <T extends Resource, K extends keyof T>(
6873 ) ;
6974 }
7075
71- return clientFunc ?. apply ( client . current , _args ) as Promise <
72- ResolvedReturnType < T [ K ] >
73- > ;
76+ const logger = debug . createLogger ( ) ;
77+ logger ( 'REQ' , name , methodName , _args ) ;
78+
79+ try {
80+ const response = ( await clientFunc ?. apply (
81+ client . current ,
82+ _args
83+ ) ) as Promise < ResolvedReturnType < T [ K ] > > ;
84+
85+ logger ( 'RES' , name , methodName , response ) ;
86+ return response ;
87+ } catch ( error ) {
88+ logger ( 'ERR' , name , methodName , error ) ;
89+ throw error ;
90+ }
7491 } ,
7592 ..._options ,
7693 refetchInterval : false ,
0 commit comments