omni-sdk-ts
v2.0.8
Published
An ts SDK for OMNI services
Downloads
770
Readme
OMNI-SDK-TS
omni sdk typescript 버전입니다. node.js 와 next 환경에서 구동할 수 있으며 npm 에 올라가 있는 상태입니다
설치 (Installation)
npm install omni-sdk-ts
yarn add omni-sdk-ts
사용법 (Usage)
토큰 발급
Node.js
// node 일 경우
const { OMNI, OMNIOptionsBuilder } = require('omni-sdk-ts');
// main 으로 감싸기
async function main() {
try {
const option = new OMNIOptionsBuilder()
.setBaseURL("https://stg-omni.ibapi.kr")
.setId("mira")
.setPassword("12345678")
.build();
const omni = new OMNI(option);
// 비동기 함수인 getToken을 await로 호출
const token = await omni.auth.getToken();
console.log('Token:', token);
console.log(omni);
} catch (error) {
console.error('Error:', error);
}
}
// main 함수 실행
main();
Next.js
import { OMNI, OMNIOptionsBuilder } from 'omni-sdk-ts';
export async function POST(req) {
try {
const { baseURL, userId, userPassword } = await req.json();
const authParam = new OMNIOptionsBuilder().setBaseURL(baseURL).setId(userId).setPassword(userPassword).build();
const authService = new OMNI(authParam);
const tokenResponse = await authService.auth.getToken();
return new Response(JSON.stringify({ message: 'Login successful', success: tokenResponse }), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
});
} catch (error) {
console.error('Login failed:', error);
return new Response(JSON.stringify({ message: 'Login failed', error: error.message }), {
status: 500,
headers: {
'Content-Type': 'application/json',
},
});
}
}
전송
Node.js
async function send() {
try {
const option = new OMNIOptionsBuilder()
.setBaseURL("https://stg-omni.ibapi.kr")
.setToken("{{발급한 토큰}}")
.build();
const omni = new OMNI(option);
const req = new SMSRequestBodyBuilder()
.setTo("01000000000")
.setFrom("0316281500")
.setText("테스트 발송입니다.")
.build();
const res = await omni.send?.sendSMS(req);
console.log('전송결과:', res);
console.log(omni);
} catch (error) {
console.error('Error:', error);
}
}
send();
Next.js
// src/app/api/test/route.js
import { NextResponse } from 'next/server';
import { OMNI, OMNIOptionsBuilder, SMSRequestBodyBuilder} from 'omni-sdk-ts';
export async function POST() {
const option = new OMNIOptionsBuilder().setBaseURL("http://172.16.0.73:7000").setToken("{{발급한 토큰}}").build();
const omni = new OMNI(option);
const req = new SMSRequestBodyBuilder().setFrom("0310000000").setTo("01012364566").setText("test 발송입니다.").build();
const req1 = {
form : "0310000000",
to : "010123455678",
text : "test 발송입니다."
}
const result = await omni.send?.sendSMS(JSON.stringify(req1));
console.log(result);
return NextResponse.json({ result: result });
}
환경 변수 설정 (Environment Variables)
.env 파일에다가 해당 값을 설정하거나 localstorage, cookie 등에서 담아놓고 사용해도 무방합니다.