Skip to content

Commit f0a854c

Browse files
committed
fix: Resolve axios config and message dto
1 parent 54bf037 commit f0a854c

File tree

9 files changed

+77
-63
lines changed

9 files changed

+77
-63
lines changed

global.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare namespace NodeJS {
2+
interface ProcessEnv {
3+
TERMII_API_KEY: string
4+
}
5+
}

package.json

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
{
22
"name": "termii",
33
"version": "1.0.0",
4-
"description": "",
5-
"main": "index.js",
4+
"description": "Nodejs SDK for Termii",
5+
"author": "",
6+
"module": "dist/termii.esm.js",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/peoray/termii-node-sdk.git"
10+
},
11+
"homepage": "https://github.com/peoray/termii-node-sdk#readme",
12+
"bugs": {
13+
"url": "https://github.com/peoray/termii-node-sdk/issues"
14+
},
15+
"readme": "README.md",
16+
"main": "dist/index.js",
17+
"types": "dist/index.d.ts",
18+
"typings": "dist/index.d.ts",
19+
"files": [
20+
"dist/**/*"
21+
],
622
"engines": {
723
"node": ">=16"
824
},
@@ -12,16 +28,16 @@
1228
"lint": "tsdx lint"
1329
},
1430
"keywords": [],
15-
"author": "",
1631
"license": "ISC",
1732
"devDependencies": {
33+
"@types/node": "^20.4.8",
1834
"class-validator": "^0.14.0",
1935
"ts-node": "^10.9.1",
2036
"tsdx": "^0.14.1",
2137
"tslib": "^2.6.1",
2238
"typescript": "^5.1.6"
2339
},
2440
"dependencies": {
25-
"axios": "^1.4.0"
41+
"axios": "^1.3.6"
2642
}
2743
}

pnpm-lock.yaml

Lines changed: 20 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import axios, { AxiosInstance } from 'axios'
2-
import {
3-
IAxiosStruct,
4-
BaseError,
5-
BASE_URL,
6-
excludeFields,
7-
handleAxiosError,
8-
} from './utils'
2+
import { IAxiosStruct, BaseError, BASE_URL, handleAxiosError } from './utils'
93

104
export class TermiiCore {
115
public request: AxiosInstance
@@ -23,19 +17,28 @@ export class TermiiCore {
2317

2418
public async useRequest(req: IAxiosStruct) {
2519
try {
26-
const customHeaders = excludeFields(
27-
['common', 'delete', 'get', 'head', 'put', 'patch', 'post'],
28-
this.request.defaults.headers
29-
)
20+
const { method, url, data } = req
3021

31-
const getUrl = this.request.defaults.baseURL
32-
const requestInstance = await axios.request({
33-
url: `${getUrl}${req.url}`,
34-
method: req.method,
35-
headers: customHeaders,
36-
data: req.data,
37-
})
38-
return requestInstance
22+
if (method === 'GET' || method === 'DELETE') {
23+
return this.request({
24+
method,
25+
url,
26+
params: {
27+
api_key: this.apiKey,
28+
},
29+
})
30+
} else if (method === 'POST' || method === 'PUT') {
31+
return this.request({
32+
method,
33+
url,
34+
data: {
35+
...data,
36+
api_key: this.apiKey,
37+
},
38+
})
39+
} else {
40+
throw new BaseError({ message: 'Invalid HTTP method' })
41+
}
3942
} catch (error) {
4043
throw new BaseError({ message: handleAxiosError(error) })
4144
}

src/services/message/message.dto.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
IsOptional,
55
IsObject,
66
ValidateNested,
7-
IsEnum,
87
} from 'class-validator'
98

109
class MediaDto {
@@ -17,17 +16,7 @@ class MediaDto {
1716
caption: string
1817
}
1918

20-
enum ChannelType {
21-
DND = 'dnd',
22-
GENERIC = 'generic',
23-
WHATSAPP = 'whatsapp',
24-
}
25-
2619
export class SendMessageDto {
27-
@IsNotEmpty()
28-
@IsString()
29-
apiKey: string
30-
3120
@IsNotEmpty()
3221
@IsString()
3322
to: string
@@ -46,8 +35,7 @@ export class SendMessageDto {
4635

4736
@IsNotEmpty()
4837
@IsString()
49-
@IsEnum(ChannelType)
50-
channel: ChannelType
38+
channel: string
5139

5240
@IsOptional()
5341
@IsObject()

src/services/message/message.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { TermiiCore } from '../../api'
2-
import { BaseError, handleAxiosError } from '../../utils'
2+
import { BaseError, IAxiosStruct, handleAxiosError } from '../../utils'
33
import { SendMessageDto } from './message.dto'
44

55
export class Message extends TermiiCore {
66
constructor(apiKey: string) {
77
super(apiKey)
88
}
9-
public async sendSMS(data: SendMessageDto) {
9+
public async sendMessage(data: SendMessageDto) {
1010
try {
11-
const requestObj = {
11+
const requestObj: IAxiosStruct = {
1212
method: 'POST',
1313
url: `/sms/send`,
1414
data,
1515
}
1616

1717
const response = await this.useRequest(requestObj)
18-
return response
19-
// return data
18+
return response.data
2019
} catch (error) {
2120
throw new BaseError({ message: handleAxiosError(error) })
2221
}

src/types/message/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface Media {
66
}
77

88
export interface ISendMessage {
9-
apiKey: string
9+
api_key: string
1010
to: string
1111
from: string
1212
sms: string
@@ -15,7 +15,9 @@ export interface ISendMessage {
1515
media?: Media
1616
}
1717
export interface ISendMessageResponse {
18+
code: string
1819
message_id: string
20+
message_id_str: string
1921
message: string
2022
balance: string
2123
user: string

src/utils/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class BaseError extends Error {
1212
constructor(options: Record<any, any> = {}) {
1313
const errorMessage = options.message || '' // Default to an empty string if options.message is not provided
1414
super(errorMessage)
15-
Error.captureStackTrace(this, this.constructor)
15+
// Error.captureStackTrace(this, this.constructor)
1616
this.name = this.constructor.name
1717
this.message = options.message
1818
this.stack = new Error().stack

src/utils/interfaces/axios.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
// export interface IAxiosStruct extends AxiosRequestConfig {
44
export interface IAxiosStruct {
5-
// method: 'GET' | 'POST' | 'PUT' | 'DELETE'
6-
method: string
5+
method: 'GET' | 'POST' | 'PUT' | 'DELETE'
76
url: string
87
data?: any
9-
params?: any
108
}

0 commit comments

Comments
 (0)