From abac86351fe431d9c8d4a3caf81730dc036dd44c Mon Sep 17 00:00:00 2001 From: edmund Date: Tue, 12 Jan 2021 12:04:09 -0800 Subject: [PATCH 1/2] added phone number services and callflow create --- src/services/callflowService.ts | 17 +++++-- src/services/connector.ts | 75 ++++++++++++++++++------------ src/services/phoneNumberService.ts | 44 ++++++++++++++++++ 3 files changed, 103 insertions(+), 33 deletions(-) create mode 100644 src/services/phoneNumberService.ts diff --git a/src/services/callflowService.ts b/src/services/callflowService.ts index 40a21b7..82a6033 100644 --- a/src/services/callflowService.ts +++ b/src/services/callflowService.ts @@ -1,6 +1,6 @@ -import { Crossbar } from './connector'; -import { QueryParams } from '../models/queryParams'; -import { applyQueryParams } from '../helpers/requestHelper'; +import { Crossbar } from "./connector"; +import { QueryParams } from "../models/queryParams"; +import { applyQueryParams } from "../helpers/requestHelper"; export class CallflowService { constructor(public connector: Crossbar) {} @@ -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..1525b7c 100644 --- a/src/services/connector.ts +++ b/src/services/connector.ts @@ -1,4 +1,4 @@ -import axiosStatic, { AxiosInstance, AxiosRequestConfig } from 'axios'; +import axiosStatic, { AxiosInstance, AxiosRequestConfig } from "axios"; import { AccountService } from "./accountService"; import { ApiAuthService } from "./apiAuthService"; import { CallflowService } from "./callflowService"; @@ -10,13 +10,14 @@ 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: "", @@ -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 => { - if (this.config.authToken != null && this.config.authToken != '') { - config.headers['X-Auth-Token'] = this.config.authToken + this.axios.interceptors.request.use(async (config) => { + if (this.config.authToken != null && 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..f607579 --- /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); + } +} From dbec8bdc05df3d9e7e9d1df6c462382a8981b802 Mon Sep 17 00:00:00 2001 From: edmund Date: Tue, 26 Jan 2021 13:06:24 -0800 Subject: [PATCH 2/2] changed to single quotes --- src/services/callflowService.ts | 6 ++--- src/services/connector.ts | 40 +++++++++++++++--------------- src/services/phoneNumberService.ts | 6 ++--- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/services/callflowService.ts b/src/services/callflowService.ts index 82a6033..17e45b6 100644 --- a/src/services/callflowService.ts +++ b/src/services/callflowService.ts @@ -1,6 +1,6 @@ -import { Crossbar } from "./connector"; -import { QueryParams } from "../models/queryParams"; -import { applyQueryParams } from "../helpers/requestHelper"; +import { Crossbar } from './connector'; +import { QueryParams } from '../models/queryParams'; +import { applyQueryParams } from '../helpers/requestHelper'; export class CallflowService { constructor(public connector: Crossbar) {} diff --git a/src/services/connector.ts b/src/services/connector.ts index 1525b7c..47750e1 100644 --- a/src/services/connector.ts +++ b/src/services/connector.ts @@ -1,16 +1,16 @@ -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 { PhoneNumberService } from "./phoneNumberService"; +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 { PhoneNumberService } from './phoneNumberService'; export interface CrossbarConfig { baseURL: string; @@ -20,8 +20,8 @@ export interface CrossbarConfig { } const defaultCrossbarConfig: CrossbarConfig = { - baseURL: "", - accountId: "", + baseURL: '', + accountId: '', }; export class Crossbar { @@ -57,7 +57,7 @@ export class Crossbar { if (this.config.pvtApiKey && !this.config.authToken) { const authPutData = { data: { api_key: this.config.pvtApiKey } }; this.axios - .put("/api_auth", authPutData, this.axiosNonAccountConfig) + .put('/api_auth', authPutData, this.axiosNonAccountConfig) .then((authResponse) => { this.config.authToken = authResponse.data.auth_token; this.setAxiosInterceptors(); @@ -85,8 +85,8 @@ export class Crossbar { setAxiosInterceptors() { this.axios.interceptors.request.use(async (config) => { - if (this.config.authToken != null && this.config.authToken != "") { - config.headers["X-Auth-Token"] = this.config.authToken; + if (this.config.authToken != null && this.config.authToken != '') { + config.headers['X-Auth-Token'] = this.config.authToken; } return config; }); @@ -100,11 +100,11 @@ export class Crossbar { data: { api_key: this.config.pvtApiKey }, }; return this.axios - .put("/api_auth", authPutData, this.axiosNonAccountConfig) + .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; + errResponse.config.headers['X-Auth-Token'] = this.config.authToken; return this.axios(errResponse.config); }) .catch((authErr) => { diff --git a/src/services/phoneNumberService.ts b/src/services/phoneNumberService.ts index f607579..f9dce4d 100644 --- a/src/services/phoneNumberService.ts +++ b/src/services/phoneNumberService.ts @@ -1,6 +1,6 @@ -import { Crossbar } from "./connector"; -import { QueryParams } from "../models/queryParams"; -import { applyQueryParams } from "../helpers/requestHelper"; +import { Crossbar } from './connector'; +import { QueryParams } from '../models/queryParams'; +import { applyQueryParams } from '../helpers/requestHelper'; export class PhoneNumberService { constructor(public connector: Crossbar) {}