77import type { RobotClient } from '@viamrobotics/sdk' ;
88import { toStore , fromStore } from 'svelte/store' ;
99import { usePolling } from './use-polling.svelte' ;
10+ import { useQueryLogger } from '$lib/query-logger' ;
1011
1112// eslint-disable-next-line @typescript-eslint/no-explicit-any
1213export type ArgumentsType < T > = T extends ( ...args : infer U ) => any ? U : never ;
@@ -35,6 +36,7 @@ export const createRobotQuery = <T extends RobotClient, K extends keyof T>(
3536 ]
3637 | [ options ?: ( ( ) => QueryOptions ) | QueryOptions ]
3738) : { current : QueryObserverResult < ResolvedReturnType < T [ K ] > > } => {
39+ const debug = useQueryLogger ( ) ;
3840 let [ args , options ] = additional ;
3941
4042 if ( options === undefined && args !== undefined ) {
@@ -46,6 +48,7 @@ export const createRobotQuery = <T extends RobotClient, K extends keyof T>(
4648 typeof options === 'function' ? options ( ) : options
4749 ) ;
4850 const _args = $derived ( typeof args === 'function' ? args ( ) : args ) ;
51+ const methodName = $derived ( String ( method ) ) ;
4952
5053 const queryOptions = $derived (
5154 createQueryOptions ( {
@@ -54,7 +57,7 @@ export const createRobotQuery = <T extends RobotClient, K extends keyof T>(
5457 'partID' ,
5558 ( client . current as T & { partID : string } ) ?. partID ,
5659 'robotClient' ,
57- String ( method ) ,
60+ methodName ,
5861 ...( _args ? [ _args ] : [ ] ) ,
5962 ] ,
6063 enabled : client . current !== undefined && _options ?. enabled !== false ,
@@ -68,10 +71,21 @@ export const createRobotQuery = <T extends RobotClient, K extends keyof T>(
6871 ) ;
6972 }
7073
71- // Call entity.resource.func(args).
72- return clientFunc ?. apply ( client . current , _args ) as Promise <
73- ResolvedReturnType < T [ K ] >
74- > ;
74+ const logger = debug . createLogger ( ) ;
75+ logger ( 'REQ' , 'robot' , methodName , _args ) ;
76+
77+ try {
78+ const response = ( await clientFunc ?. apply (
79+ client . current ,
80+ _args
81+ ) ) as Promise < ResolvedReturnType < T [ K ] > > ;
82+
83+ logger ( 'RES' , 'robot' , methodName , response ) ;
84+ return response ;
85+ } catch ( error ) {
86+ logger ( 'ERR' , 'robot' , methodName , error ) ;
87+ throw error ;
88+ }
7589 } ,
7690 ..._options ,
7791 refetchInterval : false ,
0 commit comments