diff --git a/components/js-api-client/package.json b/components/js-api-client/package.json index 0575146c..925cd27f 100644 --- a/components/js-api-client/package.json +++ b/components/js-api-client/package.json @@ -1,7 +1,7 @@ { "name": "@crystallize/js-api-client", "license": "MIT", - "version": "2.3.2", + "version": "2.4.0", "author": "Crystallize (https://crystallize.com)", "contributors": [ "Sébastien Morel ", diff --git a/components/js-api-client/src/core/client.ts b/components/js-api-client/src/core/client.ts index 3f638402..bda05b1e 100644 --- a/components/js-api-client/src/core/client.ts +++ b/components/js-api-client/src/core/client.ts @@ -158,11 +158,19 @@ function createApiCaller( }; } +const getExpirationAtFromToken = (token: string) => { + const payload = token.split('.')[1]; + const decodedPayload = Buffer.from(payload, 'base64').toString('utf-8'); + const parsedPayload = JSON.parse(decodedPayload); + return parsedPayload.exp * 1000; +}; function shopApiCaller(configuration: ClientConfiguration, options?: CreateClientOptions) { const identifier = configuration.tenantIdentifier; let shopApiToken = configuration.shopApiToken; return async function callApi(query: string, variables?: VariablesType): Promise { - if (!shopApiToken && options?.shopApiToken?.doNotFetch !== true) { + const tokenExpiresAt: number | null = shopApiToken ? getExpirationAtFromToken(shopApiToken) : null; + const isTokenAboutToExpireOrIsExpired = tokenExpiresAt ? tokenExpiresAt - Date.now() < 1000 * 60 * 5 : true; + if ((!shopApiToken || isTokenAboutToExpireOrIsExpired) && options?.shopApiToken?.doNotFetch !== true) { //static auth token must be removed to fetch the shop api token const { staticAuthToken, ...withoutStaticAuthToken } = configuration; const headers = {