diff --git a/package.json b/package.json index ed5ad7e..5254ae2 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "3.0.4", + "version": "3.0.5", "license": "Apache-2.0", "main": "dist/index.js", "typings": "dist/index.d.ts", diff --git a/src/messaging/classes/Auth.ts b/src/messaging/classes/Auth.ts index e5c3968..3af8630 100644 --- a/src/messaging/classes/Auth.ts +++ b/src/messaging/classes/Auth.ts @@ -1,6 +1,6 @@ import { Constants } from '../constants'; import { getAuthConfig, setAuthConfig } from '../utils'; -import { AuthError } from './Errors'; +import { AuthError } from '../common/Errors'; import { AuthConfigProps, AuthCredentials, TAuthConfig } from '../types'; const fs = require('fs'); diff --git a/src/messaging/classes/HttpClient.ts b/src/messaging/classes/HttpClient.ts index d06d395..9bbb1ee 100644 --- a/src/messaging/classes/HttpClient.ts +++ b/src/messaging/classes/HttpClient.ts @@ -8,8 +8,8 @@ import { AuthConfigProps, AuthCredentials } from '../types'; import { Constants } from '../constants'; import { Auth } from './Auth'; import { URLSearchParams } from 'url'; -import { RequestError, AuthError } from './Errors'; -import { getAuthToken, setAuthToken, checkTokenValidity } from '../utils'; +import { RequestError, AuthError } from '../common/Errors'; +import { setAuthToken, checkTokenValidity } from '../utils'; import { addMinutes } from 'date-fns'; declare module 'axios' { @@ -68,16 +68,10 @@ export abstract class HttpClient { if (config.url !== '/v2/oauth/token') { // check token validity - const isTokenValid = await checkTokenValidity(); + const access_token = await checkTokenValidity(); - if (isTokenValid) { - // retrieve token from storage - const authToken = await getAuthToken(); - - if (authToken) { - // set authorization headers from storage - config.headers['Authorization'] = `Bearer ${authToken}`; - } + if (access_token) { + config.headers['Authorization'] = `Bearer ${access_token}`; } else { // retrieve auth credentials const authCredentials = await this.auth.getCredentials(); @@ -102,6 +96,7 @@ export abstract class HttpClient { } } + return config; }; diff --git a/src/messaging/classes/Messages.ts b/src/messaging/classes/Messages.ts index b47cf5b..cd862b9 100644 --- a/src/messaging/classes/Messages.ts +++ b/src/messaging/classes/Messages.ts @@ -12,7 +12,7 @@ import { } from '../types'; import { Validator } from './Validator'; import { Schemas } from '../schemas'; -import { AssertionError } from './Errors'; +import { AssertionError } from '../common/Errors'; import * as uuid from 'uuid'; import { ToQueryString } from '../utils'; diff --git a/src/messaging/classes/Reports.ts b/src/messaging/classes/Reports.ts index 6f9d5c9..be069c8 100644 --- a/src/messaging/classes/Reports.ts +++ b/src/messaging/classes/Reports.ts @@ -2,7 +2,7 @@ import { HttpClient } from './HttpClient'; import { TReport, AuthConfigProps, TCreateReport } from '../types'; import { Validator } from './Validator'; import { Schemas } from '../schemas'; -import { AssertionError } from './Errors'; +import { AssertionError } from '../common/Errors'; import * as uuid from 'uuid'; export class Reports extends HttpClient { diff --git a/src/messaging/classes/Validator.ts b/src/messaging/classes/Validator.ts index 466a99f..8e55833 100644 --- a/src/messaging/classes/Validator.ts +++ b/src/messaging/classes/Validator.ts @@ -1,4 +1,4 @@ -import { AssertionError } from './Errors'; +import { AssertionError } from '../common/Errors'; var Ajv = require('ajv'); var ajv = new Ajv({ allErrors: true, format: false }); diff --git a/src/messaging/classes/index.ts b/src/messaging/classes/index.ts index 4a2120c..399d5c5 100644 --- a/src/messaging/classes/index.ts +++ b/src/messaging/classes/index.ts @@ -3,4 +3,3 @@ export * from './FreeTrialNumbers'; export * from './VirtualNumbers'; export * from './Reports'; export * from './HealthCheck'; -export * from './Errors'; diff --git a/src/messaging/classes/Errors.ts b/src/messaging/common/Errors.ts similarity index 61% rename from src/messaging/classes/Errors.ts rename to src/messaging/common/Errors.ts index 216f022..74943c2 100644 --- a/src/messaging/classes/Errors.ts +++ b/src/messaging/common/Errors.ts @@ -18,4 +18,13 @@ export class RequestError extends TError {} export class AuthError extends TError {} -export class AssertionError extends TError {} +export class AssertionError extends TError { + errorCode: string; + errorMessage: string; + + constructor({ errorCode, errorMessage }: { errorCode: string; errorMessage: string }) { + super({ errorCode, errorMessage }); + this.errorCode = errorCode; + this.errorMessage = errorMessage; + } +} diff --git a/src/messaging/common/index.ts b/src/messaging/common/index.ts new file mode 100644 index 0000000..ea81adf --- /dev/null +++ b/src/messaging/common/index.ts @@ -0,0 +1 @@ +export * from './Errors'; \ No newline at end of file diff --git a/src/messaging/utils/config.ts b/src/messaging/utils/config.ts index 1201e49..e4760ca 100644 --- a/src/messaging/utils/config.ts +++ b/src/messaging/utils/config.ts @@ -1,6 +1,6 @@ import { storage } from './storage'; import { TAuthConfig } from '../types'; -import { StorageError } from '../classes'; +import { StorageError } from '../common/Errors'; import { Constants } from '../constants'; import { getTime } from 'date-fns'; @@ -106,33 +106,27 @@ export const getAuthToken = async (): Promise< } }; -export const checkTokenValidity = async (): Promise => { +export const checkTokenValidity = async (): Promise => { try { const authData = await getAuthToken(); - const accessToken = (authData as { - accessToken: string; - timeExp: string; - })?.accessToken; + const { accessToken, timeExp } = authData as { accessToken: string; timeExp: string }; - const timeExp = (authData as { accessToken: string; timeExp: string }) - ?.timeExp; const timeExpTimestamp = Number(timeExp); - const currentTimeStamp = getTime(new Date()); if (accessToken && timeExp) { if (currentTimeStamp < timeExpTimestamp) { // Token is still valid - return true; + return accessToken; } else { - // Token has expired, renew token - return false; + // Token has expired + return null; } } else { - // Token, expire_in or timestamp not found, renew token - return false; + // Token or timeExp not found + return null; } } catch (error) { - return false; + return null; } }; diff --git a/test/src/messaging/classes/FreeTrialNumbers.spec.js b/test/src/messaging/classes/FreeTrialNumbers.spec.js index 8064627..6e8b87f 100644 --- a/test/src/messaging/classes/FreeTrialNumbers.spec.js +++ b/test/src/messaging/classes/FreeTrialNumbers.spec.js @@ -1,6 +1,7 @@ /* eslint-disable */ const { server, rest } = require('../testServer'); -const { FreeTrialNumbers, AssertionError } = require('../../../../src/messaging/classes'); +const { FreeTrialNumbers} = require('../../../../src/messaging/classes'); +const { AssertionError } = require('../../../../src/messaging/common'); const AUTH_CONFIG = require('../credentials.json'); const { Constants } = require('../Constants'); diff --git a/test/src/messaging/classes/Messages.spec.js b/test/src/messaging/classes/Messages.spec.js index 521567b..62acd44 100644 --- a/test/src/messaging/classes/Messages.spec.js +++ b/test/src/messaging/classes/Messages.spec.js @@ -1,9 +1,11 @@ /* eslint-disable */ const { server, rest } = require('../testServer'); -const { Messages, AssertionError } = require('../../../../src/messaging/classes'); +const { Messages } = require('../../../../src/messaging/classes'); +const { AssertionError } = require('../../../../src/messaging/common'); const AUTH_CONFIG = require('../credentials.json'); const { Constants } = require('../Constants'); + const messages = new Messages(AUTH_CONFIG); describe('Message', () => { diff --git a/test/src/messaging/classes/Reports.spec.js b/test/src/messaging/classes/Reports.spec.js index 7905b69..0666f7f 100644 --- a/test/src/messaging/classes/Reports.spec.js +++ b/test/src/messaging/classes/Reports.spec.js @@ -1,6 +1,6 @@ /* eslint-disable */ const { server, rest } = require('../testServer'); -const { Reports, AssertionError } = require('../../../../src/messaging/classes'); +const { Reports} = require('../../../../src/messaging/classes'); const AUTH_CONFIG = require('../credentials.json'); const { Constants } = require('../Constants'); const { format, subDays } = require('date-fns'); diff --git a/test/src/messaging/classes/VirtualNumbers.spec.js b/test/src/messaging/classes/VirtualNumbers.spec.js index 5f51857..375686e 100644 --- a/test/src/messaging/classes/VirtualNumbers.spec.js +++ b/test/src/messaging/classes/VirtualNumbers.spec.js @@ -1,6 +1,7 @@ /* eslint-disable */ const { server, rest } = require('../testServer'); -const { VirtualNumbers, AssertionError } = require('../../../../src/messaging/classes'); +const { VirtualNumbers } = require('../../../../src/messaging/classes'); +const { AssertionError } = require('../../../../src/messaging/common'); const AUTH_CONFIG = require('../credentials.json'); const { Constants } = require('../Constants'); diff --git a/test/src/messaging/utils/config.spec.js b/test/src/messaging/utils/config.spec.js index a64ac70..bc3fd0f 100644 --- a/test/src/messaging/utils/config.spec.js +++ b/test/src/messaging/utils/config.spec.js @@ -19,27 +19,31 @@ describe('config', () => { }); }); + it('should return null if the token is empty string', async () => { + const access_token = await checkTokenValidity(); + expect(access_token).toBeNull(); + }); - it('should return true if the token is valid', async () => { + it('should return token if the token is valid', async () => { const token = 'valid_token'; const timeExp = new Date().getTime() + 40 * 60 * 1000; // 40 minutes later expect(await setAuthToken(token, String(timeExp))).toBeTruthy(); - const isValid = await checkTokenValidity(); - expect(isValid).toBeTruthy(); + const access_token = await checkTokenValidity(); + expect(access_token).toEqual(token); }); - it('should return false if the token is expired', async () => { + it('should return null if the token is expired', async () => { const token = 'expired_token'; const timeExp = new Date().getTime() - 60 * 60 * 1000; // 60 minutes ago setAuthToken(token, String(timeExp)); - const isValid = await checkTokenValidity(); - expect(isValid).toBeFalsy(); + const access_token = await checkTokenValidity(); + expect(access_token).toBeNull(); }); - it('should return false if the token is null', async () => { + it('should return null if the token is null', async () => { expect(await setAuthToken(null, null)).toBeFalsy(); - const isValid = await checkTokenValidity(); - expect(isValid).toBeFalsy(); + const access_token = await checkTokenValidity(); + expect(access_token).toBeNull(); }); });