From b2c42c62b47a9e621e29fb0b0886599248fa504e Mon Sep 17 00:00:00 2001 From: kkanwar <33885037+kkanwar@users.noreply.github.com> Date: Thu, 30 Nov 2023 23:16:53 -0500 Subject: [PATCH] SHOTS-4317: handle auth token expiration better (#20) * SHOTS-4317: handle auth token expiration better --- package.json | 2 +- src/index.ts | 67 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 9b18fb9..9b1f74e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spresso-sdk/price_optimization", - "version": "2.2.0", + "version": "2.2.1", "description": "Spresso Price Optimization SDK for Node", "main": "./dist/cjs/index.js", "module": "./dist/esm/index.js", diff --git a/src/index.ts b/src/index.ts index 54d49fc..63c155f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,9 @@ const DEFAULT_CONNECTION_TIMEOUT_MS = 1000; const DEFAULT_KEEPALIVE_TIMEOUT_MS = 30000; const DEFAULT_SOCKET_COUNT = 128; +// 30 minute expiration padding for auth tokens +const AUTH_EXPIRATION_PAD_MS = 30 * 60 * 1000; + export interface ILogger { error(message?: any, ...optionalParams: any[]): void; info?(message?: any, ...optionalParams: any[]): void; @@ -15,7 +18,7 @@ export type PricingRequest = { defaultPrice?: number; deviceId: string; overrideToDefaultPrice?: boolean; - sku: string; + itemId: string; userId?: string; }; @@ -23,7 +26,7 @@ export type PricingResponse = { deviceId: string; isPriceOptimized: boolean; price: number | null; - sku: string; + itemId: string; userId: string | null; }; @@ -90,44 +93,52 @@ class SpressoSDK { } async getPrice(request: PricingRequest, userAgent: string | undefined): Promise { - const response = await this.makeRequest( - 'get', - request, - undefined, - userAgent - ).catch((err) => { - this.handleAxiosError(err); - return this.emptyResponse(request); - }); + try { + const response = await this.makeRequest( + 'get', + request, + undefined, + userAgent + ).catch((err) => { + this.handleAxiosError(err); + return this.emptyResponse(request); + }); - if (response == null) { + if (response == null) { + return this.emptyResponse(request); + } + + return response as PricingResponse; + } catch (err) { return this.emptyResponse(request); } - - return response as PricingResponse; } async getPrices(requests: PricingRequest[], userAgent: string | undefined): Promise { - const response = await this.makeRequest( - 'post', - undefined, - { requests }, - userAgent - ).catch((err) => { - this.handleAxiosError(err); - return this.emptyResponses(requests); - }); + try { + const response = await this.makeRequest( + 'post', + undefined, + { requests }, + userAgent + ).catch((err) => { + this.handleAxiosError(err); + return this.emptyResponses(requests); + }); - if (response == null) { + if (response == null) { + return this.emptyResponses(requests); + } + + return response as PricingResponse[]; + } catch (err) { return this.emptyResponses(requests); } - - return response as PricingResponse[]; } private async authenticate(): Promise { const now = new Date().getTime(); - if (this.authToken != null && this.tokenExpiration != null && now < this.tokenExpiration) { + if (this.authToken != null && this.tokenExpiration != null && now < (this.tokenExpiration - AUTH_EXPIRATION_PAD_MS)) { return Promise.resolve(); // Current token is valid and non-expired, no need to refetch } @@ -216,7 +227,7 @@ class SpressoSDK { deviceId: request.deviceId, isPriceOptimized: false, price: request.defaultPrice ?? null, - sku: request.sku, + itemId: request.itemId, userId: request.userId ?? null, }; }