@@ -10,6 +10,15 @@ export type ClientConfiguration = {
1010 origin ?: string ;
1111} ;
1212
13+ type ProfilingOptions = {
14+ onRequest : ( query : string , variables ?: VariablesType ) => void ;
15+ onRequestResolved : ( processingTimeMs : number , query : string , variables ?: VariablesType ) => void ;
16+ } ;
17+
18+ export type CreateClientOptions = {
19+ profiling ?: ProfilingOptions ;
20+ } ;
21+
1322export type VariablesType = Record < string , any > ;
1423export type ApiCaller < T > = ( query : string , variables ?: VariablesType ) => Promise < T > ;
1524
@@ -45,6 +54,7 @@ async function post<T>(
4554 query : string ,
4655 variables ?: VariablesType ,
4756 init ?: RequestInit | any | undefined ,
57+ profiling ?: ProfilingOptions ,
4858) : Promise < T > {
4959 try {
5060 const commonHeaders = {
@@ -56,6 +66,11 @@ async function post<T>(
5666 ...authenticationHeaders ( config ) ,
5767 } ;
5868 const body = JSON . stringify ( { query, variables } ) ;
69+ let start : number = 0 ;
70+ if ( profiling ) {
71+ start = Date . now ( ) ;
72+ profiling . onRequest ( query , variables ) ;
73+ }
5974
6075 const response = await fetch ( path , {
6176 ...init ,
@@ -64,6 +79,10 @@ async function post<T>(
6479 body,
6580 } ) ;
6681
82+ if ( profiling ) {
83+ const ms = Date . now ( ) - start ;
84+ profiling . onRequestResolved ( ms , query , variables ) ;
85+ }
6786 if ( response . ok && 204 === response . status ) {
6887 return < T > { } ;
6988 }
@@ -93,34 +112,26 @@ async function post<T>(
93112 }
94113}
95114
96- function createApiCaller ( uri : string , configuration : ClientConfiguration ) : ApiCaller < any > {
97- /**
98- * Call a crystallize. Will automatically handle access tokens
99- * @param query The GraphQL query
100- * @param variables Variables to inject into query.
101- */
115+ function createApiCaller (
116+ uri : string ,
117+ configuration : ClientConfiguration ,
118+ options ?: CreateClientOptions ,
119+ ) : ApiCaller < any > {
102120 return function callApi < T > ( query : string , variables ?: VariablesType ) : Promise < T > {
103- return post < T > ( uri , configuration , query , variables ) ;
121+ return post < T > ( uri , configuration , query , variables , undefined , options ?. profiling ) ;
104122 } ;
105123}
106124
107- /**
108- * Create one api client for each api endpoint Crystallize offers (catalogue, search, order, subscription, pim).
109- *
110- * @param configuration
111- * @returns ClientInterface
112- */
113- export function createClient ( configuration : ClientConfiguration ) : ClientInterface {
125+ export function createClient ( configuration : ClientConfiguration , options ?: CreateClientOptions ) : ClientInterface {
114126 const identifier = configuration . tenantIdentifier ;
115127 const origin = configuration . origin || '.crystallize.com' ;
116128 const apiHost = ( path : string [ ] , prefix : 'api' | 'pim' = 'api' ) => `https://${ prefix } ${ origin } /${ path . join ( '/' ) } ` ;
117-
118129 return {
119- catalogueApi : createApiCaller ( apiHost ( [ identifier , 'catalogue' ] ) , configuration ) ,
120- searchApi : createApiCaller ( apiHost ( [ identifier , 'search' ] ) , configuration ) ,
121- orderApi : createApiCaller ( apiHost ( [ identifier , 'orders' ] ) , configuration ) ,
122- subscriptionApi : createApiCaller ( apiHost ( [ identifier , 'subscriptions' ] ) , configuration ) ,
123- pimApi : createApiCaller ( apiHost ( [ 'graphql' ] , 'pim' ) , configuration ) ,
130+ catalogueApi : createApiCaller ( apiHost ( [ identifier , 'catalogue' ] ) , configuration , options ) ,
131+ searchApi : createApiCaller ( apiHost ( [ identifier , 'search' ] ) , configuration , options ) ,
132+ orderApi : createApiCaller ( apiHost ( [ identifier , 'orders' ] ) , configuration , options ) ,
133+ subscriptionApi : createApiCaller ( apiHost ( [ identifier , 'subscriptions' ] ) , configuration , options ) ,
134+ pimApi : createApiCaller ( apiHost ( [ 'graphql' ] , 'pim' ) , configuration , options ) ,
124135 config : {
125136 tenantId : configuration . tenantId ,
126137 tenantIdentifier : configuration . tenantIdentifier ,
0 commit comments