diff --git a/src/services/callflowService.ts b/src/services/callflowService.ts index 40a21b7..17e45b6 100644 --- a/src/services/callflowService.ts +++ b/src/services/callflowService.ts @@ -44,6 +44,15 @@ export class CallflowService { */ async patchCallflowById(id: string, data: any) { let route = `/callflows/${id}`; - return await this.connector.axios.patch(route, {data}); + return await this.connector.axios.patch(route, { data }); + } + + /** + * PUT {baseURL}/v2/accounts/{accountId}/callflows + * @param {any} data callflow data + */ + async createCallflow(data: any) { + let route = `/callflows`; + return await this.connector.axios.put(route, { data }); } } diff --git a/src/services/connector.ts b/src/services/connector.ts index c77a8cd..47750e1 100644 --- a/src/services/connector.ts +++ b/src/services/connector.ts @@ -1,26 +1,27 @@ import axiosStatic, { AxiosInstance, AxiosRequestConfig } from 'axios'; -import { AccountService } from "./accountService"; -import { ApiAuthService } from "./apiAuthService"; -import { CallflowService } from "./callflowService"; -import { CallInspectorService } from "./callInspectorService"; -import { CdrService } from "./cdrService"; -import { DeviceService } from "./deviceService"; -import { FaxService } from "./faxService"; -import { RecordingService } from "./recordingService"; -import { UserAuthService } from "./userAuthService"; -import { UserService } from "./userService"; -import { VoicemailService } from "./voicemailService"; +import { AccountService } from './accountService'; +import { ApiAuthService } from './apiAuthService'; +import { CallflowService } from './callflowService'; +import { CallInspectorService } from './callInspectorService'; +import { CdrService } from './cdrService'; +import { DeviceService } from './deviceService'; +import { FaxService } from './faxService'; +import { RecordingService } from './recordingService'; +import { UserAuthService } from './userAuthService'; +import { UserService } from './userService'; +import { VoicemailService } from './voicemailService'; +import { PhoneNumberService } from './phoneNumberService'; export interface CrossbarConfig { baseURL: string; accountId: string; pvtApiKey?: string; authToken?: string; -}; +} const defaultCrossbarConfig: CrossbarConfig = { - baseURL: "", - accountId: "", + baseURL: '', + accountId: '', }; export class Crossbar { @@ -39,24 +40,31 @@ export class Crossbar { readonly userAuthService: UserAuthService; readonly userService: UserService; readonly voicemailService: VoicemailService; + readonly phoneNumberService: PhoneNumberService; constructor(config?: Partial) { this.config = { ...defaultCrossbarConfig, ...config }; this.axios = axiosStatic.create({ baseURL: `${this.config.baseURL}/accounts/${this.config.accountId}`, - withCredentials: true + withCredentials: true, }); - this.axiosNonAccountConfig = {...this.axios.defaults, baseURL: this.config.baseURL}; + this.axiosNonAccountConfig = { + ...this.axios.defaults, + baseURL: this.config.baseURL, + }; if (this.config.pvtApiKey && !this.config.authToken) { const authPutData = { data: { api_key: this.config.pvtApiKey } }; - this.axios.put('/api_auth', authPutData, this.axiosNonAccountConfig).then(authResponse => { - this.config.authToken = authResponse.data.auth_token; - this.setAxiosInterceptors(); - }).catch(authErr => { - return Promise.reject(authErr); - }); + this.axios + .put('/api_auth', authPutData, this.axiosNonAccountConfig) + .then((authResponse) => { + this.config.authToken = authResponse.data.auth_token; + this.setAxiosInterceptors(); + }) + .catch((authErr) => { + return Promise.reject(authErr); + }); } else { this.setAxiosInterceptors(); } @@ -72,31 +80,40 @@ export class Crossbar { this.userAuthService = new UserAuthService(this); this.userService = new UserService(this); this.voicemailService = new VoicemailService(this); + this.phoneNumberService = new PhoneNumberService(this); } setAxiosInterceptors() { - this.axios.interceptors.request.use(async config => { + this.axios.interceptors.request.use(async (config) => { if (this.config.authToken != null && this.config.authToken != '') { - config.headers['X-Auth-Token'] = this.config.authToken + config.headers['X-Auth-Token'] = this.config.authToken; } return config; }); - this.axios.interceptors.response.use(response => response, async error => { - let errResponse = error.response; - if (errResponse.status === 401 && this.config.pvtApiKey) { - const authPutData = { data: { api_key: this.config.pvtApiKey } }; - return this.axios.put('/api_auth', authPutData, this.axiosNonAccountConfig).then(authResponse => { - this.config.authToken = authResponse.data.auth_token; - this.setAxiosInterceptors(); - errResponse.config.headers['X-Auth-Token'] = this.config.authToken; - return this.axios(errResponse.config); - }).catch(authErr => { - return Promise.reject(authErr); - }); - } else { - return error; + this.axios.interceptors.response.use( + (response) => response, + async (error) => { + let errResponse = error.response; + if (errResponse.status === 401 && this.config.pvtApiKey) { + const authPutData = { + data: { api_key: this.config.pvtApiKey }, + }; + return this.axios + .put('/api_auth', authPutData, this.axiosNonAccountConfig) + .then((authResponse) => { + this.config.authToken = authResponse.data.auth_token; + this.setAxiosInterceptors(); + errResponse.config.headers['X-Auth-Token'] = this.config.authToken; + return this.axios(errResponse.config); + }) + .catch((authErr) => { + return Promise.reject(authErr); + }); + } else { + return error; + } } - }); + ); } -} \ No newline at end of file +} diff --git a/src/services/phoneNumberService.ts b/src/services/phoneNumberService.ts new file mode 100644 index 0000000..f9dce4d --- /dev/null +++ b/src/services/phoneNumberService.ts @@ -0,0 +1,44 @@ +import { Crossbar } from './connector'; +import { QueryParams } from '../models/queryParams'; +import { applyQueryParams } from '../helpers/requestHelper'; + +export class PhoneNumberService { + constructor(public connector: Crossbar) {} + + /** + * PUT {baseURL}/v2/accounts/{accountId}/phone_numbers/collection + * @param {any} data phoneNumber data + */ + async addPhoneNumbers(data: any) { + let route = `/phone_numbers/collection`; + return await this.connector.axios.put(route, { data }); + } + /** + * PUT {baseURL}/v2/accounts/{accountId}/phone_numbers/{number} + * @param {any} data phoneNumber data + */ + async addSinglePhoneNumber(number: string, data: any) { + let route = `/phone_numbers/${number}`; + return await this.connector.axios.put(route, { data }); + } + + /** + * GET {baseURL}/v2/accounts/{accountId}/phone_numbers + * @param {QueryParams} queryParams global API query paramters + */ + async getPhoneNumbers(queryParams?: QueryParams) { + let route = `/phone_numbers`; + if (queryParams) route = applyQueryParams(route, queryParams); + return await this.connector.axios.get(route); + } + /** + * GET {baseURL}/v2/accounts/{accountId}/phone_numbers/{number} + * @param {string} phone number to query + * @param {QueryParams} queryParams global API query paramters + */ + async getPhoneNumberByNumber(number: string, queryParams?: QueryParams) { + let route = `/phone_numbers/${number}`; + if (queryParams) route = applyQueryParams(route, queryParams); + return await this.connector.axios.get(route); + } +}