Skip to content

Commit

Permalink
release 3.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanganderson committed Aug 22, 2024
1 parent 694c881 commit 413e88a
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/messaging/classes/Auth.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
17 changes: 6 additions & 11 deletions src/messaging/classes/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down Expand Up @@ -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();
Expand All @@ -102,6 +96,7 @@ export abstract class HttpClient {
}
}


return config;
};

Expand Down
2 changes: 1 addition & 1 deletion src/messaging/classes/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion src/messaging/classes/Reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/messaging/classes/Validator.ts
Original file line number Diff line number Diff line change
@@ -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 });

Expand Down
1 change: 0 additions & 1 deletion src/messaging/classes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export * from './FreeTrialNumbers';
export * from './VirtualNumbers';
export * from './Reports';
export * from './HealthCheck';
export * from './Errors';
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions src/messaging/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Errors';
24 changes: 9 additions & 15 deletions src/messaging/utils/config.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -106,33 +106,27 @@ export const getAuthToken = async (): Promise<
}
};

export const checkTokenValidity = async (): Promise<boolean> => {
export const checkTokenValidity = async (): Promise<string | null> => {
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;
}
};
3 changes: 2 additions & 1 deletion test/src/messaging/classes/FreeTrialNumbers.spec.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
4 changes: 3 additions & 1 deletion test/src/messaging/classes/Messages.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/src/messaging/classes/Reports.spec.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
3 changes: 2 additions & 1 deletion test/src/messaging/classes/VirtualNumbers.spec.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
22 changes: 13 additions & 9 deletions test/src/messaging/utils/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

});
Expand Down

0 comments on commit 413e88a

Please sign in to comment.