Skip to content

Commit 72c676f

Browse files
author
Kewin Polok
committed
Check if response is error
1 parent 8e5c9db commit 72c676f

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

src/modules/baseMessageModule/index.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import snakeCase from 'lodash/snakeCase';
77

88
import { BaseModule } from '../baseModule';
99
import { SmsDetails } from '../sms/types/SmsDetails';
10+
import { ErrorResponse, isErrorResponse } from '../../types/ErrorResponse';
1011
import { MessageResponse } from '../../types/MessageResponse';
1112

1213
import {
@@ -39,7 +40,7 @@ export class BaseMessageModule extends BaseModule {
3940
content: MessageContent,
4041
recipient: Recipient,
4142
details?: SmsDetails
42-
): Promise<MessageResponse> {
43+
): Promise<MessageResponse | ErrorResponse> {
4344
const body: Record<string, unknown> = {
4445
details: true,
4546
encoding: 'utf-8',
@@ -88,15 +89,15 @@ export class BaseMessageModule extends BaseModule {
8889
}
8990
);
9091

91-
return this.formatSmsResponse(data);
92+
return this.formatResponse(data);
9293
}
9394

94-
const data = await this.httpClient.post<MessageResponse, MessageResponse>(
95-
this.endpoint,
96-
body
97-
);
95+
const data = await this.httpClient.post<
96+
MessageResponse | ErrorResponse,
97+
MessageResponse | ErrorResponse
98+
>(this.endpoint, body);
9899

99-
return this.formatSmsResponse(data);
100+
return this.formatResponse(data);
100101
}
101102

102103
private isNumberRecipient(
@@ -192,7 +193,13 @@ export class BaseMessageModule extends BaseModule {
192193
});
193194
}
194195

195-
protected formatSmsResponse(response: MessageResponse): MessageResponse {
196+
protected formatResponse(
197+
response: MessageResponse | ErrorResponse
198+
): MessageResponse | ErrorResponse {
199+
if (isErrorResponse(response)) {
200+
return response;
201+
}
202+
196203
return {
197204
...response,
198205
list: response.list.map((sms) => ({

src/modules/vms/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BaseMessageModule } from '../baseMessageModule';
22
import { MessageResponse } from '../../types/MessageResponse';
3+
import { ErrorResponse } from '../../types/ErrorResponse';
34

45
import { VmsTtsLector } from './types/VmsTtsLector';
56
import { VmsDetails } from './types/VmsDetails';
@@ -12,7 +13,7 @@ export class Vms extends BaseMessageModule {
1213
tts: string,
1314
ttsLector?: VmsTtsLector,
1415
details?: VmsDetails
15-
): Promise<MessageResponse> {
16+
): Promise<MessageResponse | ErrorResponse> {
1617
return await this.send(
1718
{
1819
tts,
@@ -29,7 +30,7 @@ export class Vms extends BaseMessageModule {
2930
numbers: string | string[],
3031
pathToLocaleFile: string,
3132
details?: VmsDetails
32-
): Promise<MessageResponse> {
33+
): Promise<MessageResponse | ErrorResponse> {
3334
return await this.send(
3435
{
3536
localPath: pathToLocaleFile,
@@ -45,7 +46,7 @@ export class Vms extends BaseMessageModule {
4546
numbers: string | string[],
4647
pathToRemoteFile: string,
4748
details?: VmsDetails
48-
): Promise<MessageResponse> {
49+
): Promise<MessageResponse | ErrorResponse> {
4950
return await this.send(
5051
{
5152
remotePath: pathToRemoteFile,
@@ -62,7 +63,7 @@ export class Vms extends BaseMessageModule {
6263
tts: string,
6364
ttsLector?: VmsTtsLector,
6465
details?: VmsDetails
65-
): Promise<MessageResponse> {
66+
): Promise<MessageResponse | ErrorResponse> {
6667
return await this.send(
6768
{
6869
tts,
@@ -79,7 +80,7 @@ export class Vms extends BaseMessageModule {
7980
groups: string | string[],
8081
pathToLocaleFile: string,
8182
details?: VmsDetails
82-
): Promise<MessageResponse> {
83+
): Promise<MessageResponse | ErrorResponse> {
8384
return await this.send(
8485
{
8586
localPath: pathToLocaleFile,
@@ -95,7 +96,7 @@ export class Vms extends BaseMessageModule {
9596
groups: string | string[],
9697
pathToRemoteFile: string,
9798
details?: VmsDetails
98-
): Promise<MessageResponse> {
99+
): Promise<MessageResponse | ErrorResponse> {
99100
return await this.send(
100101
{
101102
remotePath: pathToRemoteFile,

src/types/ErrorResponse.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function isErrorResponse(response: any): response is ErrorResponse {
2+
return response.error !== undefined && response.message !== undefined;
3+
}
4+
5+
export interface ErrorResponse {
6+
error: number;
7+
message: string;
8+
}

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ import { SubuserPoints } from '../modules/subusers/types/SubuserPoints';
1616
import { Template } from '../modules/templates/types/Template';
1717
import { UpdateSubuser } from '../modules/subusers/types/UpdateSubuser';
1818

19+
import { ErrorResponse } from './ErrorResponse';
1920
import { MessageStatus } from './MessageStatus';
2021
import { MessageResponse } from './MessageResponse';
2122
import { ApiCollection } from './ApiCollection';
2223

2324
export {
2425
ApiCollection,
26+
ErrorResponse,
2527
HlrCheck,
2628
HlrCheckError,
2729
HlrCheckResponse,

0 commit comments

Comments
 (0)