diff --git a/common.ts b/common.ts index f70fbcd..0be317d 100644 --- a/common.ts +++ b/common.ts @@ -108,8 +108,8 @@ export const toPathString = function (url: URL) { } export const createRequestFunction = function (axiosArgs: RequestArgs, globalAxios: AxiosInstance, BASE_PATH: string, configuration?: Configuration) { - return >(axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH) => { + return function >(axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH): Promise { const axiosRequestArgs = {...axiosArgs.options, url: (axios.defaults.baseURL ? '' : configuration?.basePath ?? basePath) + axiosArgs.url}; - return axios.request(axiosRequestArgs); + return axios.request(axiosRequestArgs) as Promise; }; } diff --git a/models/bxml/verbs/Refer.ts b/models/bxml/verbs/Refer.ts new file mode 100644 index 0000000..90060c8 --- /dev/null +++ b/models/bxml/verbs/Refer.ts @@ -0,0 +1,38 @@ +import { NestableVerb } from '../NestableVerb'; +import { SipUri } from './SipUri'; + +export interface ReferAttributes { + referCompleteUrl?: string; + referCompleteMethod?: string; + tag?: string; +} + +/** + * @export + * @class Refer + * @extends {NestableVerb} + * Represents a Refer BXML verb. + * NOTE: On success the call is terminated - the remote SIP endpoint redirects away from Bandwidth. + * Recovery BXML in referCompleteUrl only makes sense for failure handling. + */ +export class Refer extends NestableVerb { + attributes: ReferAttributes; + + /** + * Creates an instance of Refer + * @param {SipUri} sipUri The SipUri child element (required - spec mandates exactly one) + * @param {ReferAttributes} attributes The attributes to add to the element + */ + constructor(sipUri: SipUri, attributes?: ReferAttributes) { + super('Refer', undefined, attributes, [sipUri]); + } + + /** + * Set the SipUri for this Refer verb + * @param {SipUri} sipUri The SipUri to refer to + */ + setSipUri(sipUri: SipUri): void { + // Replaces the single required SipUri child - allows exactly one. + this.nestedVerbs = [sipUri]; + } +} diff --git a/models/bxml/verbs/index.ts b/models/bxml/verbs/index.ts index 4dd67a4..ceda920 100644 --- a/models/bxml/verbs/index.ts +++ b/models/bxml/verbs/index.ts @@ -12,6 +12,7 @@ export * from './PhoneNumber'; export * from './PlayAudio'; export * from './Record'; export * from './Redirect'; +export * from './Refer'; export * from './ResumeRecording'; export * from './Ring'; export * from './SendDtmf'; diff --git a/tests/unit/models/bxml/verbs/Refer.test.ts b/tests/unit/models/bxml/verbs/Refer.test.ts new file mode 100644 index 0000000..6c2eca8 --- /dev/null +++ b/tests/unit/models/bxml/verbs/Refer.test.ts @@ -0,0 +1,42 @@ +import { Refer, ReferAttributes } from '../../../../../models/bxml/verbs/Refer'; +import { SipUri } from '../../../../../models/bxml/verbs/SipUri'; + +describe('Refer', () => { + test('should generate Refer XML with SipUri and all attributes', () => { + const attributes: ReferAttributes = { + referCompleteUrl: 'https://example.com/handleRefer', + referCompleteMethod: 'POST', + tag: 'my-tag', + }; + const sipUri = new SipUri('sip:alice@atlanta.example.com'); + const refer = new Refer(sipUri, attributes); + + const xml = refer.toBxml(); + expect(xml).toContain('sip:alice@atlanta.example.com'); + expect(xml).toContain(''); + }); + + test('should generate Refer XML with no attributes', () => { + const sipUri = new SipUri('sip:bob@biloxi.example.com'); + const refer = new Refer(sipUri); + + const xml = refer.toBxml(); + expect(xml).toContain(''); + expect(xml).toContain('sip:bob@biloxi.example.com'); + }); + + test('setSipUri should replace the nested SipUri', () => { + const sipUri1 = new SipUri('sip:alice@atlanta.example.com'); + const sipUri2 = new SipUri('sip:bob@biloxi.example.com'); + const refer = new Refer(sipUri1); + + refer.setSipUri(sipUri2); + const xml = refer.toBxml(); + expect(xml).not.toContain('alice'); + expect(xml).toContain('sip:bob@biloxi.example.com'); + }); +});