From a853c1492b01e3862283dc606813d500be7eec8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C6=B0=C6=A1ng=20=C4=90=E1=BB=97?= Date: Wed, 14 Nov 2018 11:18:27 +0700 Subject: [PATCH] feat(eSMS): Add https base url; Add option to set optional `BASE_URL` --- src/__mocks__/esms.mock.ts | 4 +-- src/constants/esms.ts | 7 +++-- src/{types => interfaces}/brand-name.ts | 0 src/services/esms/esms-interfaces.ts | 7 +++++ src/services/esms/esms.ts | 36 ++++++++++++++++++------- 5 files changed, 41 insertions(+), 13 deletions(-) rename src/{types => interfaces}/brand-name.ts (100%) diff --git a/src/__mocks__/esms.mock.ts b/src/__mocks__/esms.mock.ts index 20b1fa9..a12f676 100644 --- a/src/__mocks__/esms.mock.ts +++ b/src/__mocks__/esms.mock.ts @@ -58,7 +58,7 @@ export const eSMSMockAPI = async (url: string) => { // Calls sendMessage() with good data if ( url === - 'http://rest.esms.vn/MainService.svc/json/SendMultipleMessage_V4_get?Phone=0979477635&Content=Test%20message%20jest&SmsType=2&IsUnicode=0&Brandname=STORELAMMOC&RequestId=&Sandbox=1&ApiKey=__SUCCESS_API_KEY__&SecretKey=__SUCCESS_SECRET_KEY__' + `${BASE_URL}/SendMultipleMessage_V4_get?Phone=0979477635&Content=Test%20message%20jest&SmsType=2&IsUnicode=0&Brandname=STORELAMMOC&RequestId=&Sandbox=1&ApiKey=__SUCCESS_API_KEY__&SecretKey=__SUCCESS_SECRET_KEY__` ) { return { data: { @@ -72,7 +72,7 @@ export const eSMSMockAPI = async (url: string) => { // Calls sendMessage() with empty brand name if ( url === - 'http://rest.esms.vn/MainService.svc/json/SendMultipleMessage_V4_get?Phone=0979477635&Content=Test%20message%20jest&SmsType=2&IsUnicode=0&Brandname=&RequestId=&Sandbox=0&ApiKey=__SUCCESS_API_KEY__&SecretKey=__SUCCESS_SECRET_KEY__' + `${BASE_URL}/SendMultipleMessage_V4_get?Phone=0979477635&Content=Test%20message%20jest&SmsType=2&IsUnicode=0&Brandname=&RequestId=&Sandbox=0&ApiKey=__SUCCESS_API_KEY__&SecretKey=__SUCCESS_SECRET_KEY__` ) { return { data: { diff --git a/src/constants/esms.ts b/src/constants/esms.ts index 73ecdbd..851d350 100644 --- a/src/constants/esms.ts +++ b/src/constants/esms.ts @@ -1,4 +1,6 @@ -export const BASE_URL = 'http://rest.esms.vn/MainService.svc/json' +export const BASE_URL_HTTP = 'http://rest.esms.vn/MainService.svc/json' +export const BASE_URL_HTTPS = 'https://restapi.esms.vn/MainService.svc/json' +export const BASE_URL = BASE_URL_HTTPS export const ERROR_CODES = { // 99: 'Something went wrong. Please try again.', 100: 'Request succeeded.', @@ -14,6 +16,7 @@ export const BRAND_NAME_TYPES = { } export default { - BASE_URL, + BASE_URL: BASE_URL_HTTPS, + BASE_URL_HTTP, ERROR_CODES, } diff --git a/src/types/brand-name.ts b/src/interfaces/brand-name.ts similarity index 100% rename from src/types/brand-name.ts rename to src/interfaces/brand-name.ts diff --git a/src/services/esms/esms-interfaces.ts b/src/services/esms/esms-interfaces.ts index 7cbd9da..6913448 100644 --- a/src/services/esms/esms-interfaces.ts +++ b/src/services/esms/esms-interfaces.ts @@ -1,8 +1,15 @@ +import { ServiceOptions } from 'services' + export interface ESMSAuthConfig { API_KEY: string SECRET_KEY: string } +export interface ESMSServiceOptions extends ServiceOptions { + baseUrl?: string + useHttp?: boolean +} + export interface ESMSResponse { CodeResponse: string ErrorMessage?: string diff --git a/src/services/esms/esms.ts b/src/services/esms/esms.ts index a06f3c7..c5a0cc1 100644 --- a/src/services/esms/esms.ts +++ b/src/services/esms/esms.ts @@ -1,25 +1,41 @@ import axios from 'axios' import { stringify } from 'querystring' -import SMSService, { ServiceOptions } from '../index' +import SMSService from '../index' -import { BASE_URL, BRAND_NAME_TYPES, ERROR_CODES } from '../../constants/esms' -import { BrandName } from '../../types/brand-name' +import { + BASE_URL, + BASE_URL_HTTP, + BASE_URL_HTTPS, + BRAND_NAME_TYPES, + ERROR_CODES, +} from '../../constants/esms' +import { BrandName } from '../../interfaces/brand-name' import { ESMSAuthConfig, ESMSGetBalanceResponse, ESMSGetBrandNameListResponse, ESMSSendMessageArgs, ESMSSendMessageResponse, + ESMSServiceOptions, } from './esms-interfaces' /** * @see Documentation at https://drive.google.com/file/d/0ByZdl9Zt3D_fbWFkQ3pHc0I3Q0xSelAxMjFXWXRtTWdETGVr/view */ class ESMS extends SMSService { - constructor(authConfig: ESMSAuthConfig, serviceOptions?: ServiceOptions) { + BASE_URL = BASE_URL + constructor( + authConfig: ESMSAuthConfig, + serviceOptions: ESMSServiceOptions = { loglevel: 'silent' }, + ) { super(authConfig, serviceOptions) + if (serviceOptions.baseUrl) this.BASE_URL = serviceOptions.baseUrl + else { + this.BASE_URL = serviceOptions.useHttp ? BASE_URL_HTTP : BASE_URL_HTTPS + } + this.logger.info('eSMS service initialized with', authConfig) } @@ -29,7 +45,7 @@ class ESMS extends SMSService { */ const { API_KEY, SECRET_KEY } = this.authConfig - const getBalanceURL = `${BASE_URL}/GetBalance/${API_KEY}/${SECRET_KEY}` + const getBalanceURL = `${this.BASE_URL}/GetBalance/${API_KEY}/${SECRET_KEY}` this.logger.debug(`Getting eSMS balance by GET ${getBalanceURL}`) @@ -58,7 +74,9 @@ class ESMS extends SMSService { */ const { API_KEY, SECRET_KEY } = this.authConfig - const getBrandNameListURL = `${BASE_URL}/GetListBrandname/${API_KEY}/${SECRET_KEY}` + const getBrandNameListURL = `${ + this.BASE_URL + }/GetListBrandname/${API_KEY}/${SECRET_KEY}` this.logger.debug( `Getting eSMS brand name list by GET ${getBrandNameListURL}`, @@ -115,9 +133,9 @@ class ESMS extends SMSService { SecretKey: SECRET_KEY, } - const sendMessageURL = `${BASE_URL}/SendMultipleMessage_V4_get?${stringify( - messageData, - )}` + const sendMessageURL = `${ + this.BASE_URL + }/SendMultipleMessage_V4_get?${stringify(messageData)}` this.logger.debug(`Sending message by GET`, sendMessageURL)