Skip to content

Commit c09f1bd

Browse files
Merge pull request #32 from telstra/maintain/MessagingAPI-V3-SDK
release 3.0.6
2 parents 6198f47 + 671d95c commit c09f1bd

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
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.5",
2+
"version": "3.0.6",
33
"license": "Apache-2.0",
44
"main": "dist/index.js",
55
"typings": "dist/index.d.ts",

src/messaging/classes/HttpClient.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,39 +64,38 @@ export abstract class HttpClient {
6464
config.headers['Accept-Charset'] = `utf-8`;
6565
config.headers['Accept'] = `application/json`;
6666
config.headers['Content-Type'] = `application/json`;
67+
//config.headers['content-type'] = `application/json`;
6768
}
6869

6970
if (config.url !== '/v2/oauth/token') {
7071
// check token validity
71-
const access_token = await checkTokenValidity();
72-
72+
let access_token = await checkTokenValidity();
73+
7374
if (access_token) {
74-
config.headers['Authorization'] = `Bearer ${access_token}`;
75+
config.headers['Authorization'] = `Bearer ${access_token}`;
7576
} else {
7677
// retrieve auth credentials
7778
const authCredentials = await this.auth.getCredentials();
7879

79-
// request new token
80-
const { access_token } = await this.renewToken(authCredentials);
81-
80+
// request new token
81+
let renewed_token = await this.renewToken(authCredentials);
8282
// set token if valid
83-
if (access_token) {
83+
if (renewed_token) {
8484
// set authorization headers
85-
config.headers['Authorization'] = `Bearer ${access_token}`;
85+
config.headers['Authorization'] = `Bearer ${renewed_token}`;
8686
// set authorization token in storage
8787
const futureTimeStamp = addMinutes(
8888
new Date(),
8989
Constants.TOKEN_EXPIRE_IN_50_MINS
9090
);
9191
await setAuthToken(
92-
access_token,
92+
renewed_token,
9393
futureTimeStamp.toISOString()
9494
);
9595
}
9696
}
9797
}
9898

99-
10099
return config;
101100
};
102101

@@ -132,16 +131,16 @@ export abstract class HttpClient {
132131
const authCredentials = await this.auth.getCredentials();
133132

134133
// request new token
135-
const { access_token } = await this.renewToken(authCredentials);
134+
let renewed_token = await this.renewToken(authCredentials);
136135

137136
// set token if valid
138-
if (access_token) {
137+
if (renewed_token) {
139138
// set authorization token in storage
140139
const futureTimeStamp = addMinutes(
141140
new Date(),
142141
Constants.TOKEN_EXPIRE_IN_50_MINS
143142
);
144-
await setAuthToken(access_token, futureTimeStamp.toISOString());
143+
await setAuthToken(renewed_token, futureTimeStamp.toISOString());
145144

146145
return this.instance(
147146
originalRequest as InternalAxiosRequestConfig
@@ -169,9 +168,7 @@ export abstract class HttpClient {
169168
return Promise.reject(error);
170169
};
171170

172-
private async renewToken(
173-
authCredentials: AuthCredentials
174-
): Promise<{ access_token: string }> {
171+
private async renewToken(authCredentials: AuthCredentials): Promise<string | null> {
175172
const params = new URLSearchParams();
176173
params.append('client_id', `${authCredentials.client_id}`);
177174
params.append('client_secret', `${authCredentials.client_secret}`);
@@ -180,10 +177,17 @@ export abstract class HttpClient {
180177
'scope',
181178
'free-trial-numbers:read free-trial-numbers:write messages:read messages:write virtual-numbers:read virtual-numbers:write reports:read reports:write'
182179
);
183-
184-
const auth = await this.instance.post(`/v2/oauth/token`, params);
185-
if (!auth) return auth;
186-
const { access_token } = auth;
187-
return access_token;
180+
181+
try {
182+
const auth = await this.instance.post(`/v2/oauth/token`, params);
183+
184+
if (auth && auth.access_token) {
185+
return auth.access_token;
186+
} else {
187+
return null;
188+
}
189+
} catch (error) {
190+
return null;
191+
}
188192
}
189193
}

src/messaging/utils/config.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,20 @@ export const getAuthToken = async (): Promise<
108108

109109
export const checkTokenValidity = async (): Promise<string | null> => {
110110
try {
111-
const authData = await getAuthToken();
112-
const { accessToken, timeExp } = authData as { accessToken: string; timeExp: string };
111+
const authData = await getAuthToken();
112+
113+
if (authData) {
114+
let { accessToken, timeExp } = authData as { accessToken: string; timeExp: string };
115+
let timeExpTimestamp: number = 0;
116+
if ((timeExp !== '') && (accessToken !== '')) {
117+
timeExpTimestamp = Number(timeExp);
118+
}
119+
else {
120+
return null;
121+
}
113122

114-
const timeExpTimestamp = Number(timeExp);
115-
const currentTimeStamp = getTime(new Date());
123+
const currentTimeStamp = getTime(new Date());
116124

117-
if (accessToken && timeExp) {
118125
if (currentTimeStamp < timeExpTimestamp) {
119126
// Token is still valid
120127
return accessToken;

0 commit comments

Comments
 (0)