File tree Expand file tree Collapse file tree 2 files changed +10
-2
lines changed Expand file tree Collapse file tree 2 files changed +10
-2
lines changed Original file line number Diff line number Diff line change 11{
22 "name" : " @crystallize/js-api-client" ,
33 "license" : " MIT" ,
4- "version" : " 2.3.2 " ,
4+ "version" : " 2.4.0 " ,
55 "author" :
" Crystallize <[email protected] > (https://crystallize.com)" ,
66 "contributors" : [
77 " Sébastien Morel <[email protected] >" ,
Original file line number Diff line number Diff line change @@ -158,11 +158,19 @@ function createApiCaller(
158158 } ;
159159}
160160
161+ const getExpirationAtFromToken = ( token : string ) => {
162+ const payload = token . split ( '.' ) [ 1 ] ;
163+ const decodedPayload = Buffer . from ( payload , 'base64' ) . toString ( 'utf-8' ) ;
164+ const parsedPayload = JSON . parse ( decodedPayload ) ;
165+ return parsedPayload . exp * 1000 ;
166+ } ;
161167function shopApiCaller ( configuration : ClientConfiguration , options ?: CreateClientOptions ) {
162168 const identifier = configuration . tenantIdentifier ;
163169 let shopApiToken = configuration . shopApiToken ;
164170 return async function callApi < T > ( query : string , variables ?: VariablesType ) : Promise < T > {
165- if ( ! shopApiToken && options ?. shopApiToken ?. doNotFetch !== true ) {
171+ const tokenExpiresAt : number | null = shopApiToken ? getExpirationAtFromToken ( shopApiToken ) : null ;
172+ const isTokenAboutToExpireOrIsExpired = tokenExpiresAt ? tokenExpiresAt - Date . now ( ) < 1000 * 60 * 5 : true ;
173+ if ( ( ! shopApiToken || isTokenAboutToExpireOrIsExpired ) && options ?. shopApiToken ?. doNotFetch !== true ) {
166174 //static auth token must be removed to fetch the shop api token
167175 const { staticAuthToken, ...withoutStaticAuthToken } = configuration ;
168176 const headers = {
You can’t perform that action at this time.
0 commit comments