Skip to content

Commit 404f36f

Browse files
committed
Add: MMS
1 parent f6ee6f1 commit 404f36f

File tree

7 files changed

+174
-13
lines changed

7 files changed

+174
-13
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
test.ts
2+
assets
3+
@types
24
package-lock.json
35
yarn.lock
46

.npmignore

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,110 @@
1-
./test
1+
test.ts
2+
assets
3+
@types
4+
package-lock.json
5+
yarn.lock
6+
7+
# Logs
8+
logs
9+
*.log
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
lerna-debug.log*
14+
15+
# Diagnostic reports (https://nodejs.org/api/report.html)
16+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
17+
18+
# Runtime data
19+
pids
20+
*.pid
21+
*.seed
22+
*.pid.lock
23+
24+
# Directory for instrumented libs generated by jscoverage/JSCover
25+
lib-cov
26+
27+
# Coverage directory used by tools like istanbul
28+
coverage
29+
*.lcov
30+
31+
# nyc test coverage
32+
.nyc_output
33+
34+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
35+
.grunt
36+
37+
# Bower dependency directory (https://bower.io/)
38+
bower_components
39+
40+
# node-waf configuration
41+
.lock-wscript
42+
43+
# Compiled binary addons (https://nodejs.org/api/addons.html)
44+
build/Release
45+
46+
# Dependency directories
47+
node_modules/
48+
jspm_packages/
49+
50+
# TypeScript v1 declaration files
51+
typings/
52+
53+
# TypeScript cache
54+
*.tsbuildinfo
55+
56+
# Optional npm cache directory
57+
.npm
58+
59+
# Optional eslint cache
60+
.eslintcache
61+
62+
# Microbundle cache
63+
.rpt2_cache/
64+
.rts2_cache_cjs/
65+
.rts2_cache_es/
66+
.rts2_cache_umd/
67+
68+
# Optional REPL history
69+
.node_repl_history
70+
71+
# Output of 'npm pack'
72+
*.tgz
73+
74+
# Yarn Integrity file
75+
.yarn-integrity
76+
77+
# dotenv environment variables file
78+
.env
79+
.env.test
80+
81+
# parcel-bundler cache (https://parceljs.org/)
82+
.cache
83+
84+
# Next.js build output
85+
.next
86+
87+
# Nuxt.js build / generate output
88+
.nuxt
89+
dist
90+
91+
# Gatsby files
92+
.cache/
93+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
94+
# https://nextjs.org/blog/next-9-1#public-directory-support
95+
# public
96+
97+
# vuepress build output
98+
.vuepress/dist
99+
100+
# Serverless directories
101+
.serverless/
102+
103+
# FuseBox cache
104+
.fusebox/
105+
106+
# DynamoDB Local files
107+
.dynamodb/
108+
109+
# TernJS port file
110+
.tern-port

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
2-
"name": "gabia-sms",
3-
"version": "1.0.0",
2+
"name": "node-gabia-sms",
3+
"version": "0.0.1",
44
"description": "using gabiad sms service",
55
"main": "index.js",
66
"scripts": {
7+
"prepare": "rm -rf dist && tsc",
78
"build": "tsc"
89
},
910
"repository": {
@@ -28,4 +29,4 @@
2829
"axios": "^0.21.1",
2930
"js-base64": "^3.6.1"
3031
}
31-
}
32+
}

src/index.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import axios, { AxiosInstance } from "axios";
22
import { Base64 } from "js-base64";
33
import { CustomAxiosError } from "./error";
4-
import { initSMSToken, sendLMS, sendShortSMS } from "./modules/gabia-service";
4+
import { initSMSToken, sendLMS, sendShortSMS, sendMMS } from "./modules/gabia-service";
55
import { IGabiaAPIConfiguration } from "./types/IGabiaAPIConfiguration";
66
import { IDefaultRes, IDefaultResData, IGetAccessToken } from "./types/IGabiaResponse";
77

@@ -52,7 +52,7 @@ class GabiaSMS {
5252
);
5353

5454
try {
55-
const data: any = await this.call<IGetAccessToken>(initSMSToken());
55+
const data: IGetAccessToken = await this.call<IGetAccessToken>(initSMSToken());
5656

5757
if (data === undefined) {
5858

@@ -92,10 +92,8 @@ class GabiaSMS {
9292
throw new Error('Please check the message.');
9393
}
9494

95+
await this.getAccesstoken();
9596
try {
96-
97-
await this.getAccesstoken();
98-
9997
return (await this.call<IDefaultRes>(sendShortSMS({
10098
phone,
10199
callback,
@@ -126,7 +124,6 @@ class GabiaSMS {
126124
}
127125

128126
await this.getAccesstoken();
129-
130127
try {
131128
return (await this.call<IDefaultRes>(sendLMS({
132129
phone,
@@ -140,6 +137,37 @@ class GabiaSMS {
140137
}
141138
}
142139

140+
async sendMMS(
141+
phone: string,
142+
callback: string,
143+
message: string,
144+
subject: string,
145+
images: File[],
146+
): Promise<IDefaultResData> {
147+
if (message === '') {
148+
throw new Error('Please check the message.');
149+
}
150+
151+
await this.getAccesstoken();
152+
153+
const formData = new FormData();
154+
formData.append('phone', phone);
155+
formData.append('callback', callback);
156+
formData.append('message', message);
157+
formData.append('subject', subject);
158+
formData.append('refkey', this.refKEY);
159+
formData.append('img_cnt', String(images.length));
160+
for (const [index, value] of images.entries()) {
161+
formData.append(`images${index}`, value);
162+
}
163+
164+
try {
165+
return (await this.call<IDefaultRes>(sendMMS(formData))).data;
166+
} catch (err) {
167+
throw new CustomAxiosError(err);
168+
}
169+
}
170+
143171
private _base64KeyEncode(key: string): string {
144172

145173
return 'Basic ' + Base64.encode(`${this.gabiaId}:${key}`);

src/modules/gabia-service.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import qs from "querystring";
22
import { AxiosRequestConfig } from "axios";
33
import { IShortMessage } from "../types/send/SMS.types";
44
import { ILongMessage } from "../types/send/LMS.types";
5+
import { IMultimediaMessage } from "../types/send/MMS.types";
56

67
export const initSMSToken = (): AxiosRequestConfig => ({
78
method: 'POST',
@@ -13,10 +14,16 @@ export const sendShortSMS = (data: IShortMessage): AxiosRequestConfig => ({
1314
method: 'POST',
1415
url: '/api/send/sms',
1516
data: qs.stringify({ ...data })
16-
})
17+
});
1718

1819
export const sendLMS = (data: ILongMessage): AxiosRequestConfig => ({
1920
method: 'POST',
2021
url: '/api/send/lms',
2122
data: qs.stringify({ ...data })
22-
})
23+
});
24+
25+
export const sendMMS = (formData: FormData): AxiosRequestConfig => ({
26+
method: 'POST',
27+
url: '/api/send/mms',
28+
data: formData,
29+
});

src/types/send/MMS.types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
export interface IMultimediaMessage {
3+
4+
phone: string;
5+
callback: string;
6+
message: string;
7+
refkey?: string;
8+
subject: string;
9+
img_cnt: number;
10+
images0?: File;
11+
images1?: File;
12+
images2?: File;
13+
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"declaration": true, /* Generates corresponding '.d.ts' file. */
66
"outDir": "./dist", /* Redirect output structure to the directory. */
77
"strict": true, /* Enable all strict type-checking options. */
8-
"esModuleInterop": true
8+
"esModuleInterop": true,
9+
"downlevelIteration": true,
910
}
1011
}

0 commit comments

Comments
 (0)