Skip to content

Commit cb3554d

Browse files
committedAug 23, 2023
test: Add test setup and test to send message
1 parent 9d21fcd commit cb3554d

File tree

11 files changed

+1158
-85
lines changed

11 files changed

+1158
-85
lines changed
 

‎__tests__/services/message.spec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Message } from '../../src/services'
2+
import { ISendMessage, ISendMessageResponse } from '../../src/types'
3+
4+
const message = new Message(
5+
'TLaURAdBvnUNS9sEbugcE2gyRdKd1rNPSVxAG3fQp9sfbtpVh6575KoTon2Fv9'
6+
)
7+
const mockMessage: ISendMessage = {
8+
to: '+2348012345678',
9+
sms: 'Hello World',
10+
from: 'Acme',
11+
type: 'plain',
12+
channel: 'generic',
13+
}
14+
15+
const responseData: ISendMessageResponse = {
16+
code: '200',
17+
message_id: '123456789',
18+
message_id_str: '123456789',
19+
message: 'Message sent successfully',
20+
balance: 50.0, // Assuming this is a balance in some currency
21+
user: 'JohnDoe', // The user who sent the message
22+
}
23+
24+
describe('Message', () => {
25+
it('should send a message', async () => {
26+
try {
27+
const response = await message.sendMessage(mockMessage)
28+
29+
expect(response).toHaveBeenCalledWith(mockMessage)
30+
expect(response).toEqual(responseData)
31+
} catch (error) {
32+
expect(error).toBeInstanceOf(Error)
33+
}
34+
})
35+
})

‎jest.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
moduleFileExtensions: ['js', 'json', 'ts'],
3+
rootDir: './',
4+
testRegex: '.*\\.spec\\.ts$',
5+
transform: {
6+
'.(ts)': 'ts-jest',
7+
},
8+
collectCoverageFrom: ['**/*.(t|j)s'],
9+
coverageDirectory: './coverage',
10+
testEnvironment: 'node',
11+
testTimeout: 120000,
12+
}

‎package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525
"scripts": {
2626
"start": "tsdx watch",
2727
"build": "tsdx build",
28-
"lint": "tsdx lint"
28+
"lint": "tsdx lint",
29+
"test": "jest --config jest.config.js --no-cache"
2930
},
3031
"keywords": [],
3132
"license": "ISC",
3233
"devDependencies": {
34+
"@types/jest": "^29.5.3",
3335
"@types/node": "^20.4.8",
3436
"class-validator": "^0.14.0",
37+
"jest": "^29.6.2",
38+
"ts-jest": "^29.1.1",
3539
"ts-node": "^10.9.1",
3640
"tsdx": "^0.14.1",
3741
"tslib": "^2.6.1",

‎pnpm-lock.yaml

+986
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/api.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios, { AxiosInstance } from 'axios'
2-
import { IAxiosStruct, BaseError, BASE_URL, handleAxiosError } from './utils'
2+
import { IAxiosStruct, BaseError, BASE_URL, handleErrors } from './utils'
33

44
export class TermiiCore {
55
public request: AxiosInstance
@@ -40,7 +40,9 @@ export class TermiiCore {
4040
throw new BaseError({ message: 'Invalid HTTP method' })
4141
}
4242
} catch (error) {
43-
throw new BaseError({ message: handleAxiosError(error) })
43+
// console.log(error)
44+
console.log('--0=======00000========3030303=-===')
45+
throw new BaseError({ message: handleErrors(error) })
4446
}
4547
}
4648
}

‎src/services/message/message.dto.ts

-46
This file was deleted.

‎src/services/message/message.ts

+33-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
import { TermiiCore } from '../../api'
2-
import { BaseError, IAxiosStruct, handleAxiosError } from '../../utils'
3-
import { SendMessageDto } from './message.dto'
2+
import { IAxiosStruct } from '../../utils'
3+
// import { AxiosError } from 'axios'
4+
// import { SendMessageDto } from './message.dto'
5+
import { ISendMessageResponse, ISendMessage } from '../../types/message'
6+
// import { ISendMessageResponse } from '../../types/message'
47

58
export class Message extends TermiiCore {
69
constructor(apiKey: string) {
710
super(apiKey)
811
}
9-
public async sendMessage(data: SendMessageDto) {
10-
try {
11-
const requestObj: IAxiosStruct = {
12-
method: 'POST',
13-
url: `/sms/send`,
14-
data,
15-
}
12+
public async sendMessage(data: ISendMessage): Promise<ISendMessageResponse> {
13+
// public async sendMessage(data: SendMessageDto) {
14+
// try {
15+
const requestObj: IAxiosStruct = {
16+
method: 'POST',
17+
url: `/sms/send`,
18+
data,
19+
}
1620

17-
const response = await this.useRequest(requestObj)
18-
return response.data
19-
} catch (error) {
20-
throw new BaseError({ message: handleAxiosError(error) })
21+
const message: ISendMessage = {
22+
to: '+2348138666495',
23+
from: 'Acme',
24+
// sms: 'Your SMS message here',
25+
type: 'plain',
26+
channel: 'whatsapp', // This should match one of the allowed values: 'dnd', 'generic', or 'whatsapp'
27+
media: {
28+
caption: 'image',
29+
url: 'https://i.imgur.com/1kK2i3E.jpg',
30+
},
2131
}
32+
33+
// console.log(requestObj)
34+
console.log(message)
35+
36+
const response = await this.useRequest(requestObj)
37+
return response?.data as ISendMessageResponse
38+
// } catch (error) {
39+
// const { response } = error as AxiosError
40+
// throw response
41+
// }
2242
}
2343
}

‎src/termii.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { Message } from './services'
22

33
/**
4-
* The Fincra class is the main class that is used to access the other classes
5-
* @class Fincra
6-
* @param {string} publicKey - The public key of the merchant
7-
* @param {string} privateKey - The private key of the merchant
8-
* @param {IEnvironment} environment - The environment to use
9-
* @returns The Fincra class
4+
* The Termii class is the main class that is used to access the other classes
5+
* @class Termii
6+
* @param {string} apiKey - The api key to authorize usage
7+
* @returns The Termii class
108
* @example
11-
* const fincra = new Fincra('pk_NjI3ZmVmYmU1YTY1ZWM5OWJhOWFmMGJlOjoxMjE2NzA=', 'hzjMvDeY0dmBrDPSxZH5exnmdNc0aUXy', {sandbox: true});
9+
* const termii = new Termii('pk_NjI3ZmVmYmU1YTY1ZWM5OWJhOWFmMGJlOjoxMjE2NzA);
1210
**/
1311
export class Termii {
1412
constructor(public apiKey: string) {

‎src/types/message/index.ts

+67-9
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,78 @@ export interface Media {
55
caption: string
66
}
77

8-
export interface ISendMessage {
9-
api_key: string
10-
to: string | string[]
11-
from: string
12-
sms: string
13-
type: string
14-
channel: ChannelType
15-
media?: Media
8+
// export interface ISendMessageWithSMS {
9+
// to: string | string[]
10+
// from: string
11+
// type: string
12+
// channel: 'dnd' | 'generic' | 'whatsapp'
13+
// sms: string
14+
// media?: never // Ensure media is not present when sms is used
15+
// }
16+
17+
// export interface ISendMessageWithMedia {
18+
// to: string | string[]
19+
// from: string
20+
// type: string
21+
// channel: 'whatsapp'
22+
// sms?: never // Ensure sms is not present when media is used
23+
// media: Media
24+
// }
25+
26+
// export type ISendMessage = ISendMessageWithSMS | ISendMessageWithMedia
27+
28+
export interface Media {
29+
url: string
30+
caption: string
1631
}
32+
33+
// Define a base message type with common properties.
34+
// export interface BaseMessage {
35+
// to: string | string[]
36+
// from: string
37+
// type: string
38+
// channel: 'dnd' | 'generic' | 'whatsapp'
39+
// }
40+
41+
// Define a type for messages with only SMS.
42+
// export type SMSMessage = BaseMessage & {
43+
// sms: string
44+
// media?: never
45+
// }
46+
47+
// Define a type for messages with media allowed only when channel is 'whatsapp'.
48+
// export type MediaMessage = BaseMessage & {
49+
// sms?: never
50+
// channel: 'whatsapp'
51+
// media: Media
52+
// }
53+
54+
// Create a union type for messages that can be either SMS or Media.
55+
// export type ISendMessage = SMSMessage | MediaMessage
56+
57+
export type ISendMessage =
58+
| {
59+
to: string | string[]
60+
from: string
61+
type: string
62+
channel: 'dnd' | 'generic' | 'whatsapp'
63+
sms: string
64+
media?: never
65+
}
66+
| {
67+
to: string | string[]
68+
from: string
69+
type: string
70+
channel: 'whatsapp'
71+
sms?: never
72+
media: Media
73+
}
74+
1775
export interface ISendMessageResponse {
1876
code: string
1977
message_id: string
2078
message_id_str: string
2179
message: string
22-
balance: string
80+
balance: number
2381
user: string
2482
}

‎src/utils/error.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ export class BaseError extends Error {
1010
* @constructor BaseError
1111
*/
1212
constructor(options: Record<any, any> = {}) {
13-
const errorMessage = options.message || '' // Default to an empty string if options.message is not provided
14-
super(errorMessage)
15-
// Error.captureStackTrace(this, this.constructor)
13+
// const errorMessage = options.message || '' // Default to an empty string if options.message is not provided
14+
// super(errorMessage)
15+
// // Error.captureStackTrace(this, this.constructor)
16+
// this.name = this.constructor.name
17+
// this.message = options.message
18+
// this.stack = new Error().stack
19+
20+
super()
21+
Error.captureStackTrace(this, this.constructor)
1622
this.name = this.constructor.name
1723
this.message = options.message
18-
this.stack = new Error().stack
1924
}
2025
}

‎src/utils/interfaces/axios.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// import { AxiosRequestConfig } from 'axios'
1+
import { ISendMessage } from '../../types'
22

3-
// export interface IAxiosStruct extends AxiosRequestConfig {
43
export interface IAxiosStruct {
54
method: 'GET' | 'POST' | 'PUT' | 'DELETE'
65
url: string
7-
data?: any
6+
data?: ISendMessage
87
}

0 commit comments

Comments
 (0)
Please sign in to comment.