Skip to content

Commit 6198f47

Browse files
Merge pull request #31 from telstra/maintain/MessagingAPI-V3-SDK
release 3.0.5
2 parents 78153e7 + 413e88a commit 6198f47

File tree

15 files changed

+52
-46
lines changed

15 files changed

+52
-46
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "3.0.4",
2+
"version": "3.0.5",
33
"license": "Apache-2.0",
44
"main": "dist/index.js",
55
"typings": "dist/index.d.ts",

src/messaging/classes/Auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Constants } from '../constants';
22
import { getAuthConfig, setAuthConfig } from '../utils';
3-
import { AuthError } from './Errors';
3+
import { AuthError } from '../common/Errors';
44
import { AuthConfigProps, AuthCredentials, TAuthConfig } from '../types';
55

66
const fs = require('fs');

src/messaging/classes/HttpClient.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { AuthConfigProps, AuthCredentials } from '../types';
88
import { Constants } from '../constants';
99
import { Auth } from './Auth';
1010
import { URLSearchParams } from 'url';
11-
import { RequestError, AuthError } from './Errors';
12-
import { getAuthToken, setAuthToken, checkTokenValidity } from '../utils';
11+
import { RequestError, AuthError } from '../common/Errors';
12+
import { setAuthToken, checkTokenValidity } from '../utils';
1313
import { addMinutes } from 'date-fns';
1414

1515
declare module 'axios' {
@@ -68,16 +68,10 @@ export abstract class HttpClient {
6868

6969
if (config.url !== '/v2/oauth/token') {
7070
// check token validity
71-
const isTokenValid = await checkTokenValidity();
71+
const access_token = await checkTokenValidity();
7272

73-
if (isTokenValid) {
74-
// retrieve token from storage
75-
const authToken = await getAuthToken();
76-
77-
if (authToken) {
78-
// set authorization headers from storage
79-
config.headers['Authorization'] = `Bearer ${authToken}`;
80-
}
73+
if (access_token) {
74+
config.headers['Authorization'] = `Bearer ${access_token}`;
8175
} else {
8276
// retrieve auth credentials
8377
const authCredentials = await this.auth.getCredentials();
@@ -102,6 +96,7 @@ export abstract class HttpClient {
10296
}
10397
}
10498

99+
105100
return config;
106101
};
107102

src/messaging/classes/Messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '../types';
1313
import { Validator } from './Validator';
1414
import { Schemas } from '../schemas';
15-
import { AssertionError } from './Errors';
15+
import { AssertionError } from '../common/Errors';
1616
import * as uuid from 'uuid';
1717
import { ToQueryString } from '../utils';
1818

src/messaging/classes/Reports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { HttpClient } from './HttpClient';
22
import { TReport, AuthConfigProps, TCreateReport } from '../types';
33
import { Validator } from './Validator';
44
import { Schemas } from '../schemas';
5-
import { AssertionError } from './Errors';
5+
import { AssertionError } from '../common/Errors';
66
import * as uuid from 'uuid';
77

88
export class Reports extends HttpClient {

src/messaging/classes/Validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AssertionError } from './Errors';
1+
import { AssertionError } from '../common/Errors';
22
var Ajv = require('ajv');
33
var ajv = new Ajv({ allErrors: true, format: false });
44

src/messaging/classes/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ export * from './FreeTrialNumbers';
33
export * from './VirtualNumbers';
44
export * from './Reports';
55
export * from './HealthCheck';
6-
export * from './Errors';

src/messaging/classes/Errors.ts renamed to src/messaging/common/Errors.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ export class RequestError extends TError {}
1818

1919
export class AuthError extends TError {}
2020

21-
export class AssertionError extends TError {}
21+
export class AssertionError extends TError {
22+
errorCode: string;
23+
errorMessage: string;
24+
25+
constructor({ errorCode, errorMessage }: { errorCode: string; errorMessage: string }) {
26+
super({ errorCode, errorMessage });
27+
this.errorCode = errorCode;
28+
this.errorMessage = errorMessage;
29+
}
30+
}

src/messaging/common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Errors';

src/messaging/utils/config.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { storage } from './storage';
22
import { TAuthConfig } from '../types';
3-
import { StorageError } from '../classes';
3+
import { StorageError } from '../common/Errors';
44
import { Constants } from '../constants';
55
import { getTime } from 'date-fns';
66

@@ -106,33 +106,27 @@ export const getAuthToken = async (): Promise<
106106
}
107107
};
108108

109-
export const checkTokenValidity = async (): Promise<boolean> => {
109+
export const checkTokenValidity = async (): Promise<string | null> => {
110110
try {
111111
const authData = await getAuthToken();
112-
const accessToken = (authData as {
113-
accessToken: string;
114-
timeExp: string;
115-
})?.accessToken;
112+
const { accessToken, timeExp } = authData as { accessToken: string; timeExp: string };
116113

117-
const timeExp = (authData as { accessToken: string; timeExp: string })
118-
?.timeExp;
119114
const timeExpTimestamp = Number(timeExp);
120-
121115
const currentTimeStamp = getTime(new Date());
122116

123117
if (accessToken && timeExp) {
124118
if (currentTimeStamp < timeExpTimestamp) {
125119
// Token is still valid
126-
return true;
120+
return accessToken;
127121
} else {
128-
// Token has expired, renew token
129-
return false;
122+
// Token has expired
123+
return null;
130124
}
131125
} else {
132-
// Token, expire_in or timestamp not found, renew token
133-
return false;
126+
// Token or timeExp not found
127+
return null;
134128
}
135129
} catch (error) {
136-
return false;
130+
return null;
137131
}
138132
};

test/src/messaging/classes/FreeTrialNumbers.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable */
22
const { server, rest } = require('../testServer');
3-
const { FreeTrialNumbers, AssertionError } = require('../../../../src/messaging/classes');
3+
const { FreeTrialNumbers} = require('../../../../src/messaging/classes');
4+
const { AssertionError } = require('../../../../src/messaging/common');
45
const AUTH_CONFIG = require('../credentials.json');
56
const { Constants } = require('../Constants');
67

test/src/messaging/classes/Messages.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/* eslint-disable */
22
const { server, rest } = require('../testServer');
3-
const { Messages, AssertionError } = require('../../../../src/messaging/classes');
3+
const { Messages } = require('../../../../src/messaging/classes');
4+
const { AssertionError } = require('../../../../src/messaging/common');
45
const AUTH_CONFIG = require('../credentials.json');
56
const { Constants } = require('../Constants');
67

8+
79
const messages = new Messages(AUTH_CONFIG);
810

911
describe('Message', () => {

test/src/messaging/classes/Reports.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable */
22
const { server, rest } = require('../testServer');
3-
const { Reports, AssertionError } = require('../../../../src/messaging/classes');
3+
const { Reports} = require('../../../../src/messaging/classes');
44
const AUTH_CONFIG = require('../credentials.json');
55
const { Constants } = require('../Constants');
66
const { format, subDays } = require('date-fns');

test/src/messaging/classes/VirtualNumbers.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable */
22
const { server, rest } = require('../testServer');
3-
const { VirtualNumbers, AssertionError } = require('../../../../src/messaging/classes');
3+
const { VirtualNumbers } = require('../../../../src/messaging/classes');
4+
const { AssertionError } = require('../../../../src/messaging/common');
45
const AUTH_CONFIG = require('../credentials.json');
56
const { Constants } = require('../Constants');
67

test/src/messaging/utils/config.spec.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,31 @@ describe('config', () => {
1919
});
2020
});
2121

22+
it('should return null if the token is empty string', async () => {
23+
const access_token = await checkTokenValidity();
24+
expect(access_token).toBeNull();
25+
});
2226

23-
it('should return true if the token is valid', async () => {
27+
it('should return token if the token is valid', async () => {
2428
const token = 'valid_token';
2529
const timeExp = new Date().getTime() + 40 * 60 * 1000; // 40 minutes later
2630
expect(await setAuthToken(token, String(timeExp))).toBeTruthy();
27-
const isValid = await checkTokenValidity();
28-
expect(isValid).toBeTruthy();
31+
const access_token = await checkTokenValidity();
32+
expect(access_token).toEqual(token);
2933
});
3034

31-
it('should return false if the token is expired', async () => {
35+
it('should return null if the token is expired', async () => {
3236
const token = 'expired_token';
3337
const timeExp = new Date().getTime() - 60 * 60 * 1000; // 60 minutes ago
3438
setAuthToken(token, String(timeExp));
35-
const isValid = await checkTokenValidity();
36-
expect(isValid).toBeFalsy();
39+
const access_token = await checkTokenValidity();
40+
expect(access_token).toBeNull();
3741
});
3842

39-
it('should return false if the token is null', async () => {
43+
it('should return null if the token is null', async () => {
4044
expect(await setAuthToken(null, null)).toBeFalsy();
41-
const isValid = await checkTokenValidity();
42-
expect(isValid).toBeFalsy();
45+
const access_token = await checkTokenValidity();
46+
expect(access_token).toBeNull();
4347
});
4448

4549
});

0 commit comments

Comments
 (0)