Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/services/callflowService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}
97 changes: 57 additions & 40 deletions src/services/connector.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -39,24 +40,31 @@ export class Crossbar {
readonly userAuthService: UserAuthService;
readonly userService: UserService;
readonly voicemailService: VoicemailService;
readonly phoneNumberService: PhoneNumberService;

constructor(config?: Partial<CrossbarConfig>) {
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();
}
Expand All @@ -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;
}
}
});
);
}
}
}
44 changes: 44 additions & 0 deletions src/services/phoneNumberService.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}