diff --git a/.eslintrc b/.eslintrc index 1bab34c3..9a724d80 100644 --- a/.eslintrc +++ b/.eslintrc @@ -32,6 +32,7 @@ "@typescript-eslint/no-var-requires": "off", "semi": "warn", "comma-dangle": "warn", + "quotes": ["warn", "single"], "jest-extended/prefer-to-be-true": "warn", "jest-extended/prefer-to-be-false": "error", "jest-formatting/padding-around-describe-blocks": 2, diff --git a/packages/conversation/src/rest/v1/app/app-api.ts b/packages/conversation/src/rest/v1/app/app-api.ts index 0ffad1b9..78d481d2 100644 --- a/packages/conversation/src/rest/v1/app/app-api.ts +++ b/packages/conversation/src/rest/v1/app/app-api.ts @@ -58,10 +58,10 @@ export class AppApi extends ConversationDomainApi { }; const body: RequestBody = data['appCreateRequestBody'] ? JSON.stringify(data['appCreateRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/apps`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/apps`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -85,11 +85,11 @@ export class AppApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -113,10 +113,10 @@ export class AppApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -140,10 +140,10 @@ export class AppApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/apps`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/apps`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -167,11 +167,11 @@ export class AppApi extends ConversationDomainApi { }; const body: RequestBody = data['appUpdateRequestBody'] ? JSON.stringify(data['appUpdateRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PATCH', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/conversation/src/rest/v1/capability/capability-api.ts b/packages/conversation/src/rest/v1/capability/capability-api.ts index 2822783f..e62cd2fa 100644 --- a/packages/conversation/src/rest/v1/capability/capability-api.ts +++ b/packages/conversation/src/rest/v1/capability/capability-api.ts @@ -42,10 +42,10 @@ export class CapabilityApi extends ConversationDomainApi { const body: RequestBody = data['lookupCapabilityRequestBody'] ? JSON.stringify(data['lookupCapabilityRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/capability:query`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/capability:query`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/conversation/src/rest/v1/contact/contact-api.ts b/packages/conversation/src/rest/v1/contact/contact-api.ts index e106068f..11b0f181 100644 --- a/packages/conversation/src/rest/v1/contact/contact-api.ts +++ b/packages/conversation/src/rest/v1/contact/contact-api.ts @@ -87,10 +87,10 @@ export class ContactApi extends ConversationDomainApi { const body: RequestBody = data['contactCreateRequestBody'] ? JSON.stringify(data['contactCreateRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -114,11 +114,11 @@ export class ContactApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['contact_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['contact_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -145,10 +145,10 @@ export class ContactApi extends ConversationDomainApi { const body: RequestBody = data['getChannelProfileRequestBody'] ? JSON.stringify(data['getChannelProfileRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts:getChannelProfile`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts:getChannelProfile`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -174,10 +174,10 @@ export class ContactApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['contact_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['contact_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -204,7 +204,7 @@ export class ContactApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); @@ -245,10 +245,10 @@ export class ContactApi extends ConversationDomainApi { }; const body: RequestBody = data['mergeContactRequestBody'] ? JSON.stringify(data['mergeContactRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['destination_id']}:merge`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['destination_id']}:merge`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -274,11 +274,11 @@ export class ContactApi extends ConversationDomainApi { const body: RequestBody = data['updateContactRequestBody'] ? JSON.stringify(data['updateContactRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['contact_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['contact_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PATCH', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/conversation/src/rest/v1/conversation-domain-api.ts b/packages/conversation/src/rest/v1/conversation-domain-api.ts index ff577b5a..cd5d3fd7 100644 --- a/packages/conversation/src/rest/v1/conversation-domain-api.ts +++ b/packages/conversation/src/rest/v1/conversation-domain-api.ts @@ -21,15 +21,15 @@ export class ConversationDomainApi implements Api { } /** - * Update the default basePath for the API - * @param {string} basePath - The new base path to use for the APIs. + * Update the default hostname for the API + * @param {string} hostname - The new hostname to use for the APIs. */ - public setBasePath(basePath: string) { + public setHostname(hostname: string) { try { this.client = this.getSinchClient(); - this.client.apiClientOptions.basePath = basePath; + this.client.apiClientOptions.hostname = hostname; } catch (error) { - console.error('Impossible to set a new base path, the credentials need to be provided first.'); + console.error('Impossible to set a new hostname, the credentials need to be provided first.'); throw error; } } @@ -41,7 +41,7 @@ export class ConversationDomainApi implements Api { public setRegion(region: Region) { this.sinchClientParameters.region = region; if (this.client) { - this.client.apiClientOptions.basePath = this.buildBasePath(region); + this.client.apiClientOptions.hostname = this.buildHostname(region); } } @@ -83,7 +83,7 @@ export class ConversationDomainApi implements Api { } const apiClientOptions = this.buildApiClientOptions(this.sinchClientParameters); this.client = new ApiFetchClient(apiClientOptions); - this.client.apiClientOptions.basePath = this.buildBasePath(region); + this.client.apiClientOptions.hostname = this.buildHostname(region); } return this.client; } @@ -100,13 +100,13 @@ export class ConversationDomainApi implements Api { }; } - private buildBasePath(region: Region) { + private buildHostname(region: Region) { switch (this.apiName) { case 'TemplatesV1Api': case 'TemplatesV2Api': - return `https://${region}.template.api.sinch.com`; + return this.sinchClientParameters.conversationTemplatesHostname ?? `https://${region}.template.api.sinch.com`; default: - return `https://${region}.conversation.api.sinch.com`; + return this.sinchClientParameters.conversationHostname ?? `https://${region}.conversation.api.sinch.com`; } } diff --git a/packages/conversation/src/rest/v1/conversation-service.ts b/packages/conversation/src/rest/v1/conversation-service.ts index 391721d1..34c22349 100644 --- a/packages/conversation/src/rest/v1/conversation-service.ts +++ b/packages/conversation/src/rest/v1/conversation-service.ts @@ -37,20 +37,26 @@ export class ConversationService { } /** - * Update the default basePath for each API - * - * @param {string} basePath - The new base path to use for all the APIs. + * Update the default hostname for each API (except Templates) + * @param {string} hostname - The new hostname to use for all the APIs. */ - public setBasePath(basePath: string) { - this.contact.setBasePath(basePath); - this.app.setBasePath(basePath); - this.events.setBasePath(basePath); - this.messages.setBasePath(basePath); - this.transcoding.setBasePath(basePath); - this.capability.setBasePath(basePath); - this.conversation.setBasePath(basePath); - this.webhooks.setBasePath(basePath); - this.templatesV1.setBasePath(basePath); - this.templatesV2.setBasePath(basePath); + public setHostname(hostname: string) { + this.contact.setHostname(hostname); + this.app.setHostname(hostname); + this.events.setHostname(hostname); + this.messages.setHostname(hostname); + this.transcoding.setHostname(hostname); + this.capability.setHostname(hostname); + this.conversation.setHostname(hostname); + this.webhooks.setHostname(hostname); + } + + /** + * Update the default hostname for the Templates API + * @param {string} hostname - The new hostname to use for all the APIs. + */ + public setTemplatesHostname(hostname: string) { + this.templatesV1.setHostname(hostname); + this.templatesV2.setHostname(hostname); } } diff --git a/packages/conversation/src/rest/v1/conversation/conversation-api.ts b/packages/conversation/src/rest/v1/conversation/conversation-api.ts index 7a596ee0..fb90f6f1 100644 --- a/packages/conversation/src/rest/v1/conversation/conversation-api.ts +++ b/packages/conversation/src/rest/v1/conversation/conversation-api.ts @@ -111,10 +111,10 @@ export class ConversationApi extends ConversationDomainApi { const body: RequestBody = data['createConversationRequestBody'] ? JSON.stringify(data['createConversationRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/conversations`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/conversations`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -138,11 +138,11 @@ export class ConversationApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/conversations/${data['conversation_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/conversations/${data['conversation_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -166,10 +166,10 @@ export class ConversationApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/conversations/${data['conversation_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/conversations/${data['conversation_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -195,10 +195,10 @@ export class ConversationApi extends ConversationDomainApi { const body: RequestBody = data['injectConversationEventRequestBody'] ? JSON.stringify(data['injectConversationEventRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/conversations/${data['conversation_id']}:inject-event`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/conversations/${data['conversation_id']}:inject-event`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -223,10 +223,10 @@ export class ConversationApi extends ConversationDomainApi { const body: RequestBody = data['injectMessageRequestBody'] ? JSON.stringify(data['injectMessageRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/conversations/${data['conversation_id']}:inject-message`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/conversations/${data['conversation_id']}:inject-message`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -258,7 +258,7 @@ export class ConversationApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/conversations`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/conversations`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); @@ -306,7 +306,7 @@ export class ConversationApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/conversations:recent`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/conversations:recent`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); @@ -347,10 +347,10 @@ export class ConversationApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/conversations/${data['conversation_id']}:stop`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/conversations/${data['conversation_id']}:stop`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -382,11 +382,11 @@ export class ConversationApi extends ConversationDomainApi { const body: RequestBody = data['updateConversationRequestBody'] ? JSON.stringify(data['updateConversationRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/conversations/${data['conversation_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/conversations/${data['conversation_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PATCH', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/conversation/src/rest/v1/events/events-api.ts b/packages/conversation/src/rest/v1/events/events-api.ts index 969d55e0..496e878c 100644 --- a/packages/conversation/src/rest/v1/events/events-api.ts +++ b/packages/conversation/src/rest/v1/events/events-api.ts @@ -95,11 +95,11 @@ export class EventsApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/events/${data['event_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/events/${data['event_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -123,10 +123,10 @@ export class EventsApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/events/${data['event_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/events/${data['event_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -156,7 +156,7 @@ export class EventsApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/events`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/events`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); @@ -261,10 +261,10 @@ export class EventsApi extends ConversationDomainApi { const body: RequestBody = data['sendEventRequestBody'] ? JSON.stringify(data['sendEventRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/events:send`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/events:send`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/conversation/src/rest/v1/messages/messages-api.ts b/packages/conversation/src/rest/v1/messages/messages-api.ts index 5cca2ac1..9f13262b 100644 --- a/packages/conversation/src/rest/v1/messages/messages-api.ts +++ b/packages/conversation/src/rest/v1/messages/messages-api.ts @@ -141,11 +141,11 @@ export class MessagesApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/messages/${data['message_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/messages/${data['message_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -170,10 +170,10 @@ export class MessagesApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/messages/${data['message_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/messages/${data['message_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -212,7 +212,7 @@ export class MessagesApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/messages`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/messages`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); @@ -364,10 +364,10 @@ export class MessagesApi extends ConversationDomainApi { const body: RequestBody = data['sendMessageRequestBody'] ? JSON.stringify(data['sendMessageRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/messages:send`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/messages:send`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -394,11 +394,11 @@ export class MessagesApi extends ConversationDomainApi { const body: RequestBody = data['updateMessageRequestBody'] ? JSON.stringify(data['updateMessageRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/messages/${data['message_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/messages/${data['message_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PATCH', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/conversation/src/rest/v1/templates-v1/templates-v1-api.ts b/packages/conversation/src/rest/v1/templates-v1/templates-v1-api.ts index cd970e13..795e0068 100644 --- a/packages/conversation/src/rest/v1/templates-v1/templates-v1-api.ts +++ b/packages/conversation/src/rest/v1/templates-v1/templates-v1-api.ts @@ -57,10 +57,10 @@ export class TemplatesV1Api extends ConversationDomainApi { const body: RequestBody = data['createTemplateRequestBody'] ? JSON.stringify(data['createTemplateRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/templates`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/templates`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -84,11 +84,11 @@ export class TemplatesV1Api extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -112,10 +112,10 @@ export class TemplatesV1Api extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -139,10 +139,10 @@ export class TemplatesV1Api extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/templates`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/templates`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -167,11 +167,11 @@ export class TemplatesV1Api extends ConversationDomainApi { const body: RequestBody = data['updateTemplateRequestBody'] ? JSON.stringify(data['updateTemplateRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PATCH', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/conversation/src/rest/v1/templates-v2/templates-v2-api.ts b/packages/conversation/src/rest/v1/templates-v2/templates-v2-api.ts index abca8e56..5ec58177 100644 --- a/packages/conversation/src/rest/v1/templates-v2/templates-v2-api.ts +++ b/packages/conversation/src/rest/v1/templates-v2/templates-v2-api.ts @@ -65,10 +65,10 @@ export class TemplatesV2Api extends ConversationDomainApi { const body: RequestBody = data['createTemplateRequestBody'] ? JSON.stringify(data['createTemplateRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v2/projects/${this.client.apiClientOptions.projectId}/templates`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v2/projects/${this.client.apiClientOptions.projectId}/templates`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -92,11 +92,11 @@ export class TemplatesV2Api extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v2/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v2/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -120,10 +120,10 @@ export class TemplatesV2Api extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v2/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v2/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -147,10 +147,10 @@ export class TemplatesV2Api extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v2/projects/${this.client.apiClientOptions.projectId}/templates`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v2/projects/${this.client.apiClientOptions.projectId}/templates`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -176,10 +176,10 @@ export class TemplatesV2Api extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v2/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}/translations`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v2/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}/translations`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -204,10 +204,10 @@ export class TemplatesV2Api extends ConversationDomainApi { const body: RequestBody = data['updateTemplateRequestBody'] ? JSON.stringify(data['updateTemplateRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v2/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v2/projects/${this.client.apiClientOptions.projectId}/templates/${data['template_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PUT', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/conversation/src/rest/v1/transcoding/transcoding-api.ts b/packages/conversation/src/rest/v1/transcoding/transcoding-api.ts index 48dd1951..446513be 100644 --- a/packages/conversation/src/rest/v1/transcoding/transcoding-api.ts +++ b/packages/conversation/src/rest/v1/transcoding/transcoding-api.ts @@ -39,10 +39,10 @@ export class TranscodingApi extends ConversationDomainApi { const body: RequestBody = data['transcodeMessageRequestBody'] ? JSON.stringify(data['transcodeMessageRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/messages:transcode`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/messages:transcode`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/conversation/src/rest/v1/webhooks/webhooks-api.ts b/packages/conversation/src/rest/v1/webhooks/webhooks-api.ts index f5726608..758295bb 100644 --- a/packages/conversation/src/rest/v1/webhooks/webhooks-api.ts +++ b/packages/conversation/src/rest/v1/webhooks/webhooks-api.ts @@ -60,10 +60,10 @@ export class WebhooksApi extends ConversationDomainApi { const body: RequestBody = data['webhookCreateRequestBody'] ? JSON.stringify(data['webhookCreateRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/webhooks`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/webhooks`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -87,11 +87,11 @@ export class WebhooksApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/webhooks/${data['webhook_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/webhooks/${data['webhook_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -115,10 +115,10 @@ export class WebhooksApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/webhooks/${data['webhook_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/webhooks/${data['webhook_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -142,10 +142,10 @@ export class WebhooksApi extends ConversationDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}/webhooks`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}/webhooks`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -171,11 +171,11 @@ export class WebhooksApi extends ConversationDomainApi { const body: RequestBody = data['webhookUpdateRequestBody'] ? JSON.stringify(data['webhookUpdateRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/webhooks/${data['webhook_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/webhooks/${data['webhook_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PATCH', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/conversation/tests/rest/v1/conversation-domain-api.test.ts b/packages/conversation/tests/rest/v1/conversation-domain-api.test.ts index 4ee62a91..22f9cbde 100644 --- a/packages/conversation/tests/rest/v1/conversation-domain-api.test.ts +++ b/packages/conversation/tests/rest/v1/conversation-domain-api.test.ts @@ -1,9 +1,13 @@ import { ConversationDomainApi } from '../../../src/rest/v1/conversation-domain-api'; -import { Region, UnifiedCredentials } from '@sinch/sdk-client'; +import { TemplatesV2Api } from '../../../src'; +import { ApiHostname, Region, UnifiedCredentials } from '@sinch/sdk-client'; describe('Conversation API', () => { let conversationApi: ConversationDomainApi; - let params: UnifiedCredentials; + let templateApi: TemplatesV2Api; + let params: UnifiedCredentials & Pick; + const CUSTOM_HOSTNAME = 'https://new.host.name'; + const CUSTOM_HOSTNAME_TEMPLATES = 'https://templates.new.host.name'; beforeEach(() => { params = { @@ -17,20 +21,20 @@ describe('Conversation API', () => { conversationApi = new ConversationDomainApi(params, 'dummy'); conversationApi.getSinchClient(); expect(conversationApi.client).toBeDefined(); - expect(conversationApi.client?.apiClientOptions.basePath).toBe('https://us.conversation.api.sinch.com'); + expect(conversationApi.client?.apiClientOptions.hostname).toBe('https://us.conversation.api.sinch.com'); }); it('should change the URL when specifying a different region', () => { params.region = Region.EUROPE; conversationApi = new ConversationDomainApi(params, 'dummy'); conversationApi.getSinchClient(); - expect(conversationApi.client?.apiClientOptions.basePath).toBe('https://eu.conversation.api.sinch.com'); + expect(conversationApi.client?.apiClientOptions.hostname).toBe('https://eu.conversation.api.sinch.com'); }); it('should log a warning when using an unsupported region', async () => { params.region = Region.CANADA; conversationApi = new ConversationDomainApi(params, 'dummy'); - const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); + const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); conversationApi.getSinchClient(); // Add a small delay to allow jest to capture the warning setTimeout(() => { @@ -39,11 +43,42 @@ describe('Conversation API', () => { }, 20); }); + it('should use the hostname parameter but not for templates', () => { + params.conversationHostname = CUSTOM_HOSTNAME; + conversationApi = new ConversationDomainApi(params, 'dummy'); + conversationApi.getSinchClient(); + templateApi = new TemplatesV2Api(params); + templateApi.getSinchClient(); + expect(conversationApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(templateApi.client?.apiClientOptions.hostname).toBe('https://us.template.api.sinch.com'); + }); + + it('should use the hostname parameter for templates only', () => { + params.conversationTemplatesHostname = CUSTOM_HOSTNAME_TEMPLATES; + conversationApi = new ConversationDomainApi(params, 'dummy'); + conversationApi.getSinchClient(); + templateApi = new TemplatesV2Api(params); + templateApi.getSinchClient(); + expect(conversationApi.client?.apiClientOptions.hostname).toBe('https://us.conversation.api.sinch.com'); + expect(templateApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME_TEMPLATES); + }); + + it('should use the hostname parameter for the 2 different domains', () => { + params.conversationHostname = CUSTOM_HOSTNAME; + params.conversationTemplatesHostname = CUSTOM_HOSTNAME_TEMPLATES; + conversationApi = new ConversationDomainApi(params, 'dummy'); + conversationApi.getSinchClient(); + templateApi = new TemplatesV2Api(params); + templateApi.getSinchClient(); + expect(conversationApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(templateApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME_TEMPLATES); + }); + it('should set a custom URL', () => { conversationApi = new ConversationDomainApi(params, 'dummy'); - conversationApi.setBasePath('https:/foo.com'); + conversationApi.setHostname(CUSTOM_HOSTNAME); expect(conversationApi.client).toBeDefined(); - expect(conversationApi.client?.apiClientOptions.basePath).toBe('https:/foo.com'); + expect(conversationApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); }); it ('should update the region', () => { @@ -51,25 +86,25 @@ describe('Conversation API', () => { conversationApi.setRegion(Region.EUROPE); conversationApi.getSinchClient(); expect(conversationApi.client).toBeDefined(); - expect(conversationApi.client?.apiClientOptions.basePath).toBe('https://eu.conversation.api.sinch.com'); + expect(conversationApi.client?.apiClientOptions.hostname).toBe('https://eu.conversation.api.sinch.com'); }); it ('should update the template v1 region', () => { conversationApi = new ConversationDomainApi(params, 'TemplatesV1Api'); conversationApi.getSinchClient(); expect(conversationApi.client).toBeDefined(); - expect(conversationApi.client?.apiClientOptions.basePath).toBe('https://us.template.api.sinch.com'); + expect(conversationApi.client?.apiClientOptions.hostname).toBe('https://us.template.api.sinch.com'); conversationApi.setRegion(Region.EUROPE); - expect(conversationApi.client?.apiClientOptions.basePath).toBe('https://eu.template.api.sinch.com'); + expect(conversationApi.client?.apiClientOptions.hostname).toBe('https://eu.template.api.sinch.com'); }); it ('should update the template v2 region', () => { conversationApi = new ConversationDomainApi(params, 'TemplatesV2Api'); conversationApi.getSinchClient(); expect(conversationApi.client).toBeDefined(); - expect(conversationApi.client?.apiClientOptions.basePath).toBe('https://us.template.api.sinch.com'); + expect(conversationApi.client?.apiClientOptions.hostname).toBe('https://us.template.api.sinch.com'); conversationApi.setRegion(Region.EUROPE); - expect(conversationApi.client?.apiClientOptions.basePath).toBe('https://eu.template.api.sinch.com'); + expect(conversationApi.client?.apiClientOptions.hostname).toBe('https://eu.template.api.sinch.com'); }); }); diff --git a/packages/conversation/tests/rest/v1/conversation-service.test.ts b/packages/conversation/tests/rest/v1/conversation-service.test.ts new file mode 100644 index 00000000..6f678036 --- /dev/null +++ b/packages/conversation/tests/rest/v1/conversation-service.test.ts @@ -0,0 +1,95 @@ +import { SinchClientParameters } from '@sinch/sdk-client'; +import { + AppApi, + CapabilityApi, + ContactApi, + ConversationApi, + ConversationService, + EventsApi, + MessagesApi, + TemplatesV1Api, + TemplatesV2Api, + TranscodingApi, + WebhooksApi, +} from '../../../src'; + +describe('Conversation Service', () => { + const DEFAULT_HOSTNAME = 'https://us.conversation.api.sinch.com'; + const CUSTOM_HOSTNAME = 'https://new.host.name'; + const DEFAULT_HOSTNAME_TEMPLATES = 'https://us.template.api.sinch.com'; + const CUSTOM_HOSTNAME_TEMPLATES = 'https://templates.new.host.name'; + + it('should initialize all the APIs', () => { + // Given + const params: SinchClientParameters = { + projectId: 'PROJECT_ID', + keyId: 'KEY_ID', + keySecret: 'KEY_SECRET', + }; + + // When + const conversationService = new ConversationService(params); + + // Then + expect(conversationService.contact).toBeInstanceOf(ContactApi); + expect(conversationService.app).toBeInstanceOf(AppApi); + expect(conversationService.events).toBeInstanceOf(EventsApi); + expect(conversationService.messages).toBeInstanceOf(MessagesApi); + expect(conversationService.transcoding).toBeInstanceOf(TranscodingApi); + expect(conversationService.capability).toBeInstanceOf(CapabilityApi); + expect(conversationService.conversation).toBeInstanceOf(ConversationApi); + expect(conversationService.webhooks).toBeInstanceOf(WebhooksApi); + expect(conversationService.templatesV1).toBeInstanceOf(TemplatesV1Api); + expect(conversationService.templatesV2).toBeInstanceOf(TemplatesV2Api); + }); + + it('should set a custom hostname for all APIs but templates', () => { + // Given + const params: SinchClientParameters = { + projectId: 'PROJECT_ID', + keyId: 'KEY_ID', + keySecret: 'KEY_SECRET', + }; + + // When + const conversationService = new ConversationService(params); + conversationService.setHostname(CUSTOM_HOSTNAME); + + // Then + expect(conversationService.contact.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(conversationService.app.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(conversationService.events.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(conversationService.messages.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(conversationService.transcoding.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(conversationService.capability.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(conversationService.conversation.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(conversationService.webhooks.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(conversationService.templatesV1.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME_TEMPLATES); + expect(conversationService.templatesV2.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME_TEMPLATES); + }); + + it('should set a custom hostnames for the templates APIs only', () => { + // Given + const params: SinchClientParameters = { + projectId: 'PROJECT_ID', + keyId: 'KEY_ID', + keySecret: 'KEY_SECRET', + }; + + // When + const conversationService = new ConversationService(params); + conversationService.setTemplatesHostname(CUSTOM_HOSTNAME_TEMPLATES); + + // Then + expect(conversationService.contact.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(conversationService.app.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(conversationService.events.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(conversationService.messages.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(conversationService.transcoding.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(conversationService.capability.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(conversationService.conversation.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(conversationService.webhooks.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(conversationService.templatesV1.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME_TEMPLATES); + expect(conversationService.templatesV2.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME_TEMPLATES); + }); +}); diff --git a/packages/fax/src/rest/v3/emails/emails-api.ts b/packages/fax/src/rest/v3/emails/emails-api.ts index 1135753a..9249f8b9 100644 --- a/packages/fax/src/rest/v3/emails/emails-api.ts +++ b/packages/fax/src/rest/v3/emails/emails-api.ts @@ -64,10 +64,10 @@ export class EmailsApi extends FaxDomainApi { }; const body: RequestBody = data['emailRequestBody'] ? JSON.stringify(data['emailRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/emails`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/emails`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -91,11 +91,11 @@ export class EmailsApi extends FaxDomainApi { }; const body: RequestBody = data['email'] ? JSON.stringify(data['email']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/emails/${data['email']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/emails/${data['email']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -120,7 +120,7 @@ export class EmailsApi extends FaxDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/emails`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/emails`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); @@ -165,7 +165,7 @@ export class EmailsApi extends FaxDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/emails/${data['email']}/numbers`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/emails/${data['email']}/numbers`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); @@ -208,10 +208,10 @@ export class EmailsApi extends FaxDomainApi { const body: RequestBody = data['updateEmailRequestBody'] ? JSON.stringify(data['updateEmailRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/emails/${data['email']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/emails/${data['email']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PUT', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/fax/src/rest/v3/fax-domain-api.ts b/packages/fax/src/rest/v3/fax-domain-api.ts index 9071386b..2e1cd81e 100644 --- a/packages/fax/src/rest/v3/fax-domain-api.ts +++ b/packages/fax/src/rest/v3/fax-domain-api.ts @@ -19,12 +19,12 @@ export class FaxDomainApi implements Api { } /** - * Update the default basePath for the API - * @param {string} basePath - The new base path to use for the APIs. + * Update the default hostname for the API + * @param {string} hostname - The new hostname to use for the APIs. */ - public setBasePath(basePath: string) { + public setHostname(hostname: string) { this.client = this.getSinchClient(); - this.client.apiClientOptions.basePath = basePath; + this.client.apiClientOptions.hostname = hostname; } /** @@ -61,7 +61,7 @@ export class FaxDomainApi implements Api { if (!this.client) { const apiClientOptions = this.buildApiClientOptions(this.sinchClientParameters); this.client = new ApiFetchClient(apiClientOptions); - this.client.apiClientOptions.basePath = 'https://fax.api.sinch.com'; + this.client.apiClientOptions.hostname = this.sinchClientParameters.faxHostname ?? 'https://fax.api.sinch.com'; } return this.client; } diff --git a/packages/fax/src/rest/v3/fax-service.ts b/packages/fax/src/rest/v3/fax-service.ts index c8c0436c..873debc5 100644 --- a/packages/fax/src/rest/v3/fax-service.ts +++ b/packages/fax/src/rest/v3/fax-service.ts @@ -15,13 +15,13 @@ export class FaxService { } /** - * Update the default basePath for each API + * Update the default hostname for each API * - * @param {string} basePath - The new base path to use for all the APIs. + * @param {string} hostname - The new hostname to use for all the APIs. */ - public setBasePath(basePath: string) { - this.emails.setBasePath(basePath); - this.faxes.setBasePath(basePath); - this.services.setBasePath(basePath); + public setHostname(hostname: string) { + this.emails.setHostname(hostname); + this.faxes.setHostname(hostname); + this.services.setHostname(hostname); } } diff --git a/packages/fax/src/rest/v3/faxes/faxes-api.ts b/packages/fax/src/rest/v3/faxes/faxes-api.ts index ad74fa2a..43ed5598 100644 --- a/packages/fax/src/rest/v3/faxes/faxes-api.ts +++ b/packages/fax/src/rest/v3/faxes/faxes-api.ts @@ -77,11 +77,11 @@ export class FaxesApi extends FaxDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/faxes/${data['id']}/file`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/faxes/${data['id']}/file`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -106,10 +106,10 @@ export class FaxesApi extends FaxDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/faxes/${data['id']}/file.${data['fileFormat']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/faxes/${data['id']}/file.${data['fileFormat']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processFileCall({ url, @@ -133,10 +133,10 @@ export class FaxesApi extends FaxDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/faxes/${data['id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/faxes/${data['id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -168,7 +168,7 @@ export class FaxesApi extends FaxDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/faxes`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/faxes`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); @@ -257,10 +257,10 @@ export class FaxesApi extends FaxDomainApi { body = this.client.processFormData(formParams, 'multipart/form-data'); } - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/faxes`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/faxes`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/fax/src/rest/v3/services/services-api.ts b/packages/fax/src/rest/v3/services/services-api.ts index 642aaf26..74c55a07 100644 --- a/packages/fax/src/rest/v3/services/services-api.ts +++ b/packages/fax/src/rest/v3/services/services-api.ts @@ -84,10 +84,10 @@ export class ServicesApi extends FaxDomainApi { const body: RequestBody = data['createServiceRequestBody'] ? JSON.stringify(data['createServiceRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/services`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/services`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -111,10 +111,10 @@ export class ServicesApi extends FaxDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/services/${data['serviceId']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/services/${data['serviceId']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -139,7 +139,7 @@ export class ServicesApi extends FaxDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/services/${data['serviceId']}/numbers/${data['phoneNumber']}/emails`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/services/${data['serviceId']}/numbers/${data['phoneNumber']}/emails`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); @@ -182,7 +182,7 @@ export class ServicesApi extends FaxDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/services/${data['serviceId']}/numbers`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/services/${data['serviceId']}/numbers`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); @@ -225,7 +225,7 @@ export class ServicesApi extends FaxDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/services`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/services`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); @@ -266,11 +266,11 @@ export class ServicesApi extends FaxDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/services/${data['serviceId']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/services/${data['serviceId']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -296,11 +296,11 @@ export class ServicesApi extends FaxDomainApi { const body: RequestBody = data['updateServiceRequestBody'] ? JSON.stringify(data['updateServiceRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v3/projects/${this.client.apiClientOptions.projectId}/services/${data['serviceId']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v3/projects/${this.client.apiClientOptions.projectId}/services/${data['serviceId']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PATCH', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/fax/tests/rest/v3/fax-domain-api.test.ts b/packages/fax/tests/rest/v3/fax-domain-api.test.ts new file mode 100644 index 00000000..1ed06b37 --- /dev/null +++ b/packages/fax/tests/rest/v3/fax-domain-api.test.ts @@ -0,0 +1,38 @@ +import { FaxDomainApi } from '../../../src/rest/v3/fax-domain-api'; +import { ApiHostname, UnifiedCredentials } from '@sinch/sdk-client'; + +describe('Fax API', () => { + let faxApi: FaxDomainApi; + let params: UnifiedCredentials & Pick; + const CUSTOM_HOSTNAME = 'https://new.host.name'; + + beforeEach(() => { + params = { + projectId: 'PROJECT_ID', + keyId: 'KEY_ID', + keySecret: 'KEY_SECRET', + }; + }); + + it('should initialize the client with the default hostname', () => { + faxApi = new FaxDomainApi(params, 'dummy'); + faxApi.getSinchClient(); + expect(faxApi.client).toBeDefined(); + expect(faxApi.client?.apiClientOptions.hostname).toBe('https://fax.api.sinch.com'); + }); + + it('should use the hostname parameter', () => { + params.faxHostname = CUSTOM_HOSTNAME; + faxApi = new FaxDomainApi(params, 'dummy'); + faxApi.getSinchClient(); + expect(faxApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + }); + + it('should set a custom URL', () => { + faxApi = new FaxDomainApi(params, 'dummy'); + faxApi.setHostname(CUSTOM_HOSTNAME); + expect(faxApi.client).toBeDefined(); + expect(faxApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + }); + +}); diff --git a/packages/fax/tests/rest/v3/fax-service.test.ts b/packages/fax/tests/rest/v3/fax-service.test.ts new file mode 100644 index 00000000..c12860ef --- /dev/null +++ b/packages/fax/tests/rest/v3/fax-service.test.ts @@ -0,0 +1,45 @@ +import { SinchClientParameters } from '@sinch/sdk-client'; +import { EmailsApi, FaxesApi, FaxService, ServicesApi } from '../../../src'; + +describe('Fax Service', () => { + const DEFAULT_HOSTNAME = 'https://fax.api.sinch.com'; + const CUSTOM_HOSTNAME = 'https://new.host.name'; + + it('should initialize all the APIs', () => { + // Given + const params: SinchClientParameters = { + projectId: 'PROJECT_ID', + keyId: 'KEY_ID', + keySecret: 'KEY_SECRET', + }; + + // When + const faxService = new FaxService(params); + + // Then + expect(faxService.faxes).toBeInstanceOf(FaxesApi); + expect(faxService.emails).toBeInstanceOf(EmailsApi); + expect(faxService.services).toBeInstanceOf(ServicesApi); + expect(faxService.faxes.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(faxService.emails.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(faxService.services.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + }); + + it('should set a custom hostname for all APIs', () => { + // Given + const params: SinchClientParameters = { + projectId: 'PROJECT_ID', + keyId: 'KEY_ID', + keySecret: 'KEY_SECRET', + }; + + // When + const faxService = new FaxService(params); + faxService.setHostname(CUSTOM_HOSTNAME); + + // Then + expect(faxService.faxes.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(faxService.emails.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(faxService.services.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + }); +}); diff --git a/packages/numbers/src/rest/v1/active-number/active-number-api.ts b/packages/numbers/src/rest/v1/active-number/active-number-api.ts index 98d003f0..52250042 100644 --- a/packages/numbers/src/rest/v1/active-number/active-number-api.ts +++ b/packages/numbers/src/rest/v1/active-number/active-number-api.ts @@ -79,7 +79,7 @@ export class ActiveNumberApi extends NumbersDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/activeNumbers/${data['phoneNumber']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/activeNumbers/${data['phoneNumber']}`; const requestOptions = await this.client.prepareOptions( basePathUrl, @@ -88,7 +88,7 @@ export class ActiveNumberApi extends NumbersDomainApi { headers, body || undefined, ); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -123,7 +123,7 @@ export class ActiveNumberApi extends NumbersDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/activeNumbers`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/activeNumbers`; const requestOptionsPromise = this.client.prepareOptions( basePathUrl, 'GET', getParams, headers, body || undefined); @@ -169,7 +169,7 @@ export class ActiveNumberApi extends NumbersDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/activeNumbers/${data['phoneNumber']}:release`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/activeNumbers/${data['phoneNumber']}:release`; const requestOptions = await this.client.prepareOptions( basePathUrl, @@ -178,7 +178,7 @@ export class ActiveNumberApi extends NumbersDomainApi { headers, body || undefined, ); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -209,7 +209,7 @@ export class ActiveNumberApi extends NumbersDomainApi { const body: RequestBody = data['activeNumberRequestBody'] ? JSON.stringify(data['activeNumberRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/activeNumbers/${data['phoneNumber']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/activeNumbers/${data['phoneNumber']}`; const requestOptions = await this.client.prepareOptions( basePathUrl, @@ -218,7 +218,7 @@ export class ActiveNumberApi extends NumbersDomainApi { headers, body || undefined, ); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/numbers/src/rest/v1/available-number/available-number-api.ts b/packages/numbers/src/rest/v1/available-number/available-number-api.ts index 7c76fe72..81955d45 100644 --- a/packages/numbers/src/rest/v1/available-number/available-number-api.ts +++ b/packages/numbers/src/rest/v1/available-number/available-number-api.ts @@ -72,7 +72,7 @@ export class AvailableNumberApi extends NumbersDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/availableNumbers/${data['phoneNumber']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/availableNumbers/${data['phoneNumber']}`; const requestOptions = await this.client.prepareOptions( basePathUrl, @@ -81,7 +81,7 @@ export class AvailableNumberApi extends NumbersDomainApi { headers, body || undefined, ); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -113,7 +113,7 @@ export class AvailableNumberApi extends NumbersDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/availableNumbers`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/availableNumbers`; const requestOptions = await this.client.prepareOptions( basePathUrl, @@ -122,7 +122,7 @@ export class AvailableNumberApi extends NumbersDomainApi { headers, body || undefined, ); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams, true); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams, true); return this.client.processCall({ url, @@ -151,7 +151,7 @@ export class AvailableNumberApi extends NumbersDomainApi { const body: RequestBody = data['rentAnyNumberRequestBody'] ? JSON.stringify(data['rentAnyNumberRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/availableNumbers:rentAny`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/availableNumbers:rentAny`; const requestOptions = await this.client.prepareOptions( basePathUrl, @@ -160,7 +160,7 @@ export class AvailableNumberApi extends NumbersDomainApi { headers, body || undefined, ); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -189,7 +189,7 @@ export class AvailableNumberApi extends NumbersDomainApi { const body: RequestBody = data['rentNumberRequestBody'] ? JSON.stringify(data['rentNumberRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/availableNumbers/${data['phoneNumber']}:rent`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/availableNumbers/${data['phoneNumber']}:rent`; const requestOptions = await this.client.prepareOptions( basePathUrl, @@ -198,7 +198,7 @@ export class AvailableNumberApi extends NumbersDomainApi { headers, body || undefined, ); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/numbers/src/rest/v1/available-regions/available-regions-api.ts b/packages/numbers/src/rest/v1/available-regions/available-regions-api.ts index a9c44f82..0e6fa676 100644 --- a/packages/numbers/src/rest/v1/available-regions/available-regions-api.ts +++ b/packages/numbers/src/rest/v1/available-regions/available-regions-api.ts @@ -45,7 +45,7 @@ export class AvailableRegionsApi extends NumbersDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/availableRegions`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/availableRegions`; const requestOptions = await this.client.prepareOptions( basePathUrl, @@ -54,7 +54,7 @@ export class AvailableRegionsApi extends NumbersDomainApi { headers, body || undefined, ); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams, true); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams, true); return this.client.processCall({ url, diff --git a/packages/numbers/src/rest/v1/callbacks/callbacks-api.ts b/packages/numbers/src/rest/v1/callbacks/callbacks-api.ts index 4dd8b110..0159122e 100644 --- a/packages/numbers/src/rest/v1/callbacks/callbacks-api.ts +++ b/packages/numbers/src/rest/v1/callbacks/callbacks-api.ts @@ -40,7 +40,7 @@ export class CallbacksApi extends NumbersDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/callbackConfiguration`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/callbackConfiguration`; const requestOptions = await this.client.prepareOptions( basePathUrl, @@ -49,7 +49,7 @@ export class CallbacksApi extends NumbersDomainApi { headers, body || undefined, ); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -79,7 +79,7 @@ export class CallbacksApi extends NumbersDomainApi { const body: RequestBody = data['callbackConfigurationUpdateRequestBody'] ? JSON.stringify(data['callbackConfigurationUpdateRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/callbackConfiguration`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/callbackConfiguration`; const requestOptions = await this.client.prepareOptions( basePathUrl, @@ -88,7 +88,7 @@ export class CallbacksApi extends NumbersDomainApi { headers, body || undefined, ); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/numbers/src/rest/v1/numbers-domain-api.ts b/packages/numbers/src/rest/v1/numbers-domain-api.ts index 59993864..edbea46f 100644 --- a/packages/numbers/src/rest/v1/numbers-domain-api.ts +++ b/packages/numbers/src/rest/v1/numbers-domain-api.ts @@ -17,12 +17,12 @@ export class NumbersDomainApi implements Api { } /** - * Update the default basePath for the API - * @param {string} basePath - The new base path to use for the APIs. + * Update the default hostname for the API + * @param {string} hostname - The new hostname to use for the APIs. */ - public setBasePath(basePath: string) { + public setHostname(hostname: string) { this.client = this.getSinchClient(); - this.client.apiClientOptions.basePath = basePath; + this.client.apiClientOptions.hostname = hostname; } /** @@ -59,7 +59,7 @@ export class NumbersDomainApi implements Api { if (!this.client) { const apiClientOptions = this.buildApiClientOptions(this.sinchClientParameters); this.client = new ApiFetchClient(apiClientOptions); - this.client.apiClientOptions.basePath = 'https://numbers.api.sinch.com'; + this.client.apiClientOptions.hostname = this.sinchClientParameters.numbersHostname ?? 'https://numbers.api.sinch.com'; } return this.client; } diff --git a/packages/numbers/src/rest/v1/numbers-service.ts b/packages/numbers/src/rest/v1/numbers-service.ts index 2e8570b1..be1a7654 100644 --- a/packages/numbers/src/rest/v1/numbers-service.ts +++ b/packages/numbers/src/rest/v1/numbers-service.ts @@ -18,14 +18,14 @@ export class NumbersService { } /** - * Update the default basePath for each API + * Update the default hostname for each API * - * @param {string} basePath - The new base path to use for all the APIs. + * @param {string} hostname - The new hostname to use for all the APIs. */ - public setBasePath(basePath: string) { - this.activeNumber.setBasePath(basePath); - this.availableNumber.setBasePath(basePath); - this.availableRegions.setBasePath(basePath); - this.callbacks.setBasePath(basePath); + public setHostname(hostname: string) { + this.activeNumber.setHostname(hostname); + this.availableNumber.setHostname(hostname); + this.availableRegions.setHostname(hostname); + this.callbacks.setHostname(hostname); } } diff --git a/packages/numbers/tests/rest/v1/available-number/available-number-api.test.ts b/packages/numbers/tests/rest/v1/available-number/available-number-api.test.ts index 6f0a1649..4c560d1d 100644 --- a/packages/numbers/tests/rest/v1/available-number/available-number-api.test.ts +++ b/packages/numbers/tests/rest/v1/available-number/available-number-api.test.ts @@ -42,7 +42,7 @@ describe('AvailableNumberApi', () => { ], setupPrice: { currencyCode: 'EUR', - amount: "2.00", + amount: '2.00', }, monthlyPrice: { currencyCode: 'EUR', @@ -85,7 +85,7 @@ describe('AvailableNumberApi', () => { ], setupPrice: { currencyCode: 'EUR', - amount: "2.00", + amount: '2.00', }, monthlyPrice: { currencyCode: 'EUR', diff --git a/packages/numbers/tests/rest/v1/callbacks/callbacks-webhook.test.ts b/packages/numbers/tests/rest/v1/callbacks/callbacks-webhook.test.ts index 9c7e14bd..693c81a9 100644 --- a/packages/numbers/tests/rest/v1/callbacks/callbacks-webhook.test.ts +++ b/packages/numbers/tests/rest/v1/callbacks/callbacks-webhook.test.ts @@ -4,7 +4,8 @@ describe('Callback Webhook', () => { let callbackWebhooks: NumbersCallbackWebhooks; let callbackSecret: string; - const BODY = `{"eventId":"01hpa0mww4m79q8j2dwn3ggbgz","timestamp":"2024-02-10T17:22:09.412722588","projectId":"37b62a7b-0177-abcd-efgh-e10f848de123","resourceId":"+17818510001","resourceType":"ACTIVE_NUMBER","eventType":"DEPROVISIONING_FROM_VOICE_PLATFORM","status":"SUCCEEDED","failureCode":null}`; + // eslint-disable-next-line max-len + const BODY = '{"eventId":"01hpa0mww4m79q8j2dwn3ggbgz","timestamp":"2024-02-10T17:22:09.412722588","projectId":"37b62a7b-0177-abcd-efgh-e10f848de123","resourceId":"+17818510001","resourceType":"ACTIVE_NUMBER","eventType":"DEPROVISIONING_FROM_VOICE_PLATFORM","status":"SUCCEEDED","failureCode":null}'; beforeEach(() => { callbackSecret = 'callback-secret'; diff --git a/packages/numbers/tests/rest/v1/numbers-api.test.ts b/packages/numbers/tests/rest/v1/numbers-api.test.ts index efde96ca..34a6c2eb 100644 --- a/packages/numbers/tests/rest/v1/numbers-api.test.ts +++ b/packages/numbers/tests/rest/v1/numbers-api.test.ts @@ -1,9 +1,10 @@ import { NumbersDomainApi } from '../../../src/rest/v1/numbers-domain-api'; -import { SinchClientParameters } from '@sinch/sdk-client'; +import { ApiHostname, UnifiedCredentials } from '@sinch/sdk-client'; describe('Numbers API', () => { let numbersApi: NumbersDomainApi; - let params: SinchClientParameters; + let params: UnifiedCredentials & Pick; + const CUSTOM_HOSTNAME = 'https://new.host.name'; beforeEach(() => { params = { @@ -18,14 +19,20 @@ describe('Numbers API', () => { numbersApi.getSinchClient(); expect(numbersApi.client).toBeDefined(); expect(numbersApi.client?.apiClientOptions.projectId).toBe('PROJECT_ID'); - expect(numbersApi.client?.apiClientOptions.basePath).toBe('https://numbers.api.sinch.com'); + expect(numbersApi.client?.apiClientOptions.hostname).toBe('https://numbers.api.sinch.com'); + }); + + it('should use the hostname parameter', () => { + params.numbersHostname = CUSTOM_HOSTNAME; + numbersApi = new NumbersDomainApi(params, 'dummy'); + numbersApi.getSinchClient(); + expect(numbersApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); }); it('should update the basePath', () => { - const newPath = 'https://new.base.path'; numbersApi = new NumbersDomainApi(params, 'dummy'); - numbersApi.setBasePath(newPath); - expect(numbersApi.client?.apiClientOptions.basePath).toBe(newPath); + numbersApi.setHostname(CUSTOM_HOSTNAME); + expect(numbersApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); }); it('should update the credentials', () => { diff --git a/packages/numbers/tests/rest/v1/numbers-service.test.ts b/packages/numbers/tests/rest/v1/numbers-service.test.ts new file mode 100644 index 00000000..b9c627ed --- /dev/null +++ b/packages/numbers/tests/rest/v1/numbers-service.test.ts @@ -0,0 +1,48 @@ +import { SinchClientParameters } from '@sinch/sdk-client'; +import { ActiveNumberApi, AvailableNumberApi, AvailableRegionsApi, CallbacksApi, NumbersService } from '../../../src'; + +describe('Numbers Service', () => { + const DEFAULT_HOSTNAME = 'https://numbers.api.sinch.com'; + const CUSTOM_HOSTNAME = 'https://new.host.name'; + + it('should initialize all the APIs', () => { + // Given + const params: SinchClientParameters = { + projectId: 'PROJECT_ID', + keyId: 'KEY_ID', + keySecret: 'KEY_SECRET', + }; + + // When + const numbersService = new NumbersService(params); + + // Then + expect(numbersService.availableRegions).toBeInstanceOf(AvailableRegionsApi); + expect(numbersService.availableNumber).toBeInstanceOf(AvailableNumberApi); + expect(numbersService.activeNumber).toBeInstanceOf(ActiveNumberApi); + expect(numbersService.callbacks).toBeInstanceOf(CallbacksApi); + expect(numbersService.availableRegions.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(numbersService.availableNumber.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(numbersService.activeNumber.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(numbersService.callbacks.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + }); + + it('should set a custom hostname for all APIs', () => { + // Given + const params: SinchClientParameters = { + projectId: 'PROJECT_ID', + keyId: 'KEY_ID', + keySecret: 'KEY_SECRET', + }; + + // When + const numbersService = new NumbersService(params); + numbersService.setHostname(CUSTOM_HOSTNAME); + + // Then + expect(numbersService.availableRegions.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(numbersService.availableNumber.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(numbersService.activeNumber.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(numbersService.callbacks.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + }); +}); diff --git a/packages/sdk-client/src/api/api-client-options.ts b/packages/sdk-client/src/api/api-client-options.ts index fc36a47d..643cc68b 100644 --- a/packages/sdk-client/src/api/api-client-options.ts +++ b/packages/sdk-client/src/api/api-client-options.ts @@ -8,8 +8,8 @@ interface BaseApiClientOptions { */ projectId: string; - /** Base path of the API server */ - basePath: string; + /** Hostname of the API server */ + hostname: string; /** * List of plugins to apply to the request before calling the API diff --git a/packages/sdk-client/src/api/api-client.ts b/packages/sdk-client/src/api/api-client.ts index 5a34a176..2c2cbc92 100644 --- a/packages/sdk-client/src/api/api-client.ts +++ b/packages/sdk-client/src/api/api-client.ts @@ -111,7 +111,7 @@ export class ApiClient { headers: new Headers(filterUndefinedValues(headers)), body, queryParams: filterUndefinedValues(queryParams), - basePath: url, + hostname: url, path, }; diff --git a/packages/sdk-client/src/client/api-client-pagination-helper.ts b/packages/sdk-client/src/client/api-client-pagination-helper.ts index 6bec6955..564d8f62 100644 --- a/packages/sdk-client/src/client/api-client-pagination-helper.ts +++ b/packages/sdk-client/src/client/api-client-pagination-helper.ts @@ -94,7 +94,7 @@ const updateQueryParamsAndSendRequest = ( queryParams: newQueryParams, }; const newUrl = apiClient.prepareUrl( - requestOptions.basePath, + requestOptions.hostname, newQueryParams, ); return apiClient.processCallWithPagination({ @@ -221,7 +221,7 @@ export const buildPageResultPromise = async ( // Await the promise in this async method and store the result in client so that they can be reused const requestOptions = await requestOptionsPromise; const url = client.prepareUrl( - requestOptions.basePath, requestOptions.queryParams, repeatParamArray); + requestOptions.hostname, requestOptions.queryParams, repeatParamArray); return client.processCallWithPagination({ url, diff --git a/packages/sdk-client/src/domain/domain-interface.ts b/packages/sdk-client/src/domain/domain-interface.ts index 8802b000..d630c7ac 100644 --- a/packages/sdk-client/src/domain/domain-interface.ts +++ b/packages/sdk-client/src/domain/domain-interface.ts @@ -1,7 +1,8 @@ export interface SinchClientParameters extends Partial, Partial, - Partial {} + Partial, + ApiHostname {} export interface UnifiedCredentials { /** The project ID associated with the API Client. You can find this on your [Dashboard](https://dashboard.sinch.com/account/access-keys). */ @@ -34,6 +35,18 @@ export interface ApplicationCredentials { voiceRegion?: VoiceRegion; } +export interface ApiHostname { + authHostname?: string; + conversationHostname?: string; + conversationTemplatesHostname?: string; + faxHostname?: string; + numbersHostname?: string; + smsHostname?: string; + verificationHostname?: string; + voiceHostname?: string; + voiceApplicationManagementHostname?: string; +} + export const isUnifiedCredentials = (credentials: any): credentials is UnifiedCredentials => { const candidate = (credentials) as UnifiedCredentials; return candidate.projectId !== undefined diff --git a/packages/sdk-client/src/plugins/core/request-plugin.ts b/packages/sdk-client/src/plugins/core/request-plugin.ts index f829a7cc..79ca4696 100644 --- a/packages/sdk-client/src/plugins/core/request-plugin.ts +++ b/packages/sdk-client/src/plugins/core/request-plugin.ts @@ -22,7 +22,7 @@ export interface RequestOptions extends RequestInit { /** Force headers to Headers type */ headers: Headers; /** URL targeted without the query parameters */ - basePath: string; + hostname: string; /** Path of the API*/ path?: string; } diff --git a/packages/sdk-client/src/plugins/oauth2/oauth2-api.ts b/packages/sdk-client/src/plugins/oauth2/oauth2-api.ts index f7df9d9c..92fbeec6 100644 --- a/packages/sdk-client/src/plugins/oauth2/oauth2-api.ts +++ b/packages/sdk-client/src/plugins/oauth2/oauth2-api.ts @@ -25,7 +25,7 @@ export class OAuth2Api implements Api { const body = `${encodeURIComponent('grant_type')}=${encodeURIComponent( 'client_credentials', )}`; - const url = `${this.client.apiClientOptions.basePath}/oauth2/token`; + const url = `${this.client.apiClientOptions.hostname}/oauth2/token`; const requestOptions = await this.client.prepareOptions( url, 'POST', diff --git a/packages/sdk-client/src/plugins/oauth2/oauth2-token.request.ts b/packages/sdk-client/src/plugins/oauth2/oauth2-token.request.ts index d97ca6f1..5d8fe179 100644 --- a/packages/sdk-client/src/plugins/oauth2/oauth2-token.request.ts +++ b/packages/sdk-client/src/plugins/oauth2/oauth2-token.request.ts @@ -28,7 +28,7 @@ export class Oauth2TokenRequest implements RequestPlugin { authenticationUrl = 'https://auth.sinch.com'; } this.apiClient = new ApiFetchClient({ - basePath: authenticationUrl, + hostname: authenticationUrl, requestPlugins: [basicAuthenticationPlugin], }); } diff --git a/packages/sdk-client/tests/plugins/signing/signing.request.test.ts b/packages/sdk-client/tests/plugins/signing/signing.request.test.ts index faa1e3b7..65c09f57 100644 --- a/packages/sdk-client/tests/plugins/signing/signing.request.test.ts +++ b/packages/sdk-client/tests/plugins/signing/signing.request.test.ts @@ -19,7 +19,7 @@ describe('Signed request plugin', () => { method: 'post', headers: headers, body: JSON.stringify(requestData), - basePath: 'https://test.com/path/pathParamValue', + hostname: 'https://test.com/path/pathParamValue', path: '/path/pathParamValue', }; const plugin = new SigningRequest('my-key-id', btoa('my-key-secret')); @@ -39,7 +39,7 @@ describe('Signed request plugin', () => { method: 'get', headers: headers, body: '', - basePath: 'https://test.com/path/pathParamValue', + hostname: 'https://test.com/path/pathParamValue', path: '/path/pathParamValue', }; const plugin = new SigningRequest('my-key-id', btoa('my-key-secret')); diff --git a/packages/sdk-client/tests/plugins/timezone/timezone.response.test.ts b/packages/sdk-client/tests/plugins/timezone/timezone.response.test.ts index 3f6f9327..661ed0e9 100644 --- a/packages/sdk-client/tests/plugins/timezone/timezone.response.test.ts +++ b/packages/sdk-client/tests/plugins/timezone/timezone.response.test.ts @@ -17,7 +17,7 @@ describe('Timezone response plugin', () => { url: '', requestOptions: { headers: new Headers(), - basePath: '', + hostname: '', }, }; }); diff --git a/packages/sdk-client/tests/utils/authorization.helper.test.ts b/packages/sdk-client/tests/utils/authorization.helper.test.ts index 9c1538cb..5734e268 100644 --- a/packages/sdk-client/tests/utils/authorization.helper.test.ts +++ b/packages/sdk-client/tests/utils/authorization.helper.test.ts @@ -14,7 +14,7 @@ describe('Authorization validation', () => { const APPLICATION_KEY = 'app-key'; const APPLICATION_SECRET = 'app-secret'; const PATH = '/webhook'; - const BODY = `{"id":"018d2104-aaa-bbbb-1234","price":{"amount":0.0308},"rate":{"amount":0.0}}`; + const BODY = '{"id":"018d2104-aaa-bbbb-1234","price":{"amount":0.0308},"rate":{"amount":0.0}}'; const METHOD = 'POST'; const CONTENT_TYPE = 'application/json; charset=utf-8'; const X_TIMESTAMP = 'x-timestamp:2024-01-19T09:19:28.9372196Z'; @@ -31,7 +31,7 @@ describe('Authorization validation', () => { }); it('should calculate the signature', () => { - const secret = btoa("my-secret"); + const secret = btoa('my-secret'); const stringToSign = 'pKXhl9sOsUjClws1oANArA=='; expect(calculateSignature(secret, stringToSign)).toBe('1vZeB9AYiJthOvaZeZFhOxZWLSqHHFWzFw7AGjrTtmk='); }); @@ -182,7 +182,8 @@ describe('Authorization validation', () => { 'X-Sinch-Signature': 'b93021e0182c2772dbcdf4e8983ae8f409e78c3b', }; const secret = 'my-callback-secret'; - const stringToSign = `{"eventId":"01hpa0mww4m79q8j2dwn3ggbgz","timestamp":"2024-02-10T17:22:09.412722588","projectId":"37b62a7b-0177-abcd-efgh-e10f848de123","resourceId":"+17818510001","resourceType":"ACTIVE_NUMBER","eventType":"DEPROVISIONING_FROM_VOICE_PLATFORM","status":"SUCCEEDED","failureCode":null}`; + // eslint-disable-next-line max-len + const stringToSign = '{"eventId":"01hpa0mww4m79q8j2dwn3ggbgz","timestamp":"2024-02-10T17:22:09.412722588","projectId":"37b62a7b-0177-abcd-efgh-e10f848de123","resourceId":"+17818510001","resourceType":"ACTIVE_NUMBER","eventType":"DEPROVISIONING_FROM_VOICE_PLATFORM","status":"SUCCEEDED","failureCode":null}'; const validated = validateSignatureHeader( secret, headers, @@ -194,7 +195,8 @@ describe('Authorization validation', () => { it('should reject the signature header when missing', () => { const headers = {}; const secret = 'my-callback-secret'; - const stringToSign = `{"eventId":"01hpa0mww4m79q8j2dwn3ggbgz","timestamp":"2024-02-10T17:22:09.412722588","projectId":"37b62a7b-0177-abcd-efgh-e10f848de123","resourceId":"+17818510001","resourceType":"ACTIVE_NUMBER","eventType":"DEPROVISIONING_FROM_VOICE_PLATFORM","status":"SUCCEEDED","failureCode":null}`; + // eslint-disable-next-line max-len + const stringToSign = '{"eventId":"01hpa0mww4m79q8j2dwn3ggbgz","timestamp":"2024-02-10T17:22:09.412722588","projectId":"37b62a7b-0177-abcd-efgh-e10f848de123","resourceId":"+17818510001","resourceType":"ACTIVE_NUMBER","eventType":"DEPROVISIONING_FROM_VOICE_PLATFORM","status":"SUCCEEDED","failureCode":null}'; const validated = validateSignatureHeader( secret, headers, @@ -208,7 +210,8 @@ describe('Authorization validation', () => { 'X-Sinch-Signature': 'invalid-signature', }; const secret = 'my-callback-secret'; - const stringToSign = `{"eventId":"01hpa0mww4m79q8j2dwn3ggbgz","timestamp":"2024-02-10T17:22:09.412722588","projectId":"37b62a7b-0177-abcd-efgh-e10f848de123","resourceId":"+17818510001","resourceType":"ACTIVE_NUMBER","eventType":"DEPROVISIONING_FROM_VOICE_PLATFORM","status":"SUCCEEDED","failureCode":null}`; + // eslint-disable-next-line max-len + const stringToSign = '{"eventId":"01hpa0mww4m79q8j2dwn3ggbgz","timestamp":"2024-02-10T17:22:09.412722588","projectId":"37b62a7b-0177-abcd-efgh-e10f848de123","resourceId":"+17818510001","resourceType":"ACTIVE_NUMBER","eventType":"DEPROVISIONING_FROM_VOICE_PLATFORM","status":"SUCCEEDED","failureCode":null}'; const validated = validateSignatureHeader( secret, headers, @@ -221,7 +224,8 @@ describe('Authorization validation', () => { describe('Webhook signature (Conversation API)', () => { - const CONVERSATION_BODY = `{"app_id":"","accepted_time":"2021-10-18T17:49:13.813615Z","project_id":"e2df3a34-a71b-4448-9db5-a8d2baad28e4","contact_create_notification":{"contact":{"id":"01FJA8B466Y0R2GNXD78MD9SM1","channel_identities":[{"channel":"SMS","identity":"48123456789","app_id":""}],"display_name":"New Test Contact","email":"new.contact@email.com","external_id":"","metadata":"","language":"EN_US"}},"message_metadata":""}`; + // eslint-disable-next-line max-len + const CONVERSATION_BODY = '{"app_id":"","accepted_time":"2021-10-18T17:49:13.813615Z","project_id":"e2df3a34-a71b-4448-9db5-a8d2baad28e4","contact_create_notification":{"contact":{"id":"01FJA8B466Y0R2GNXD78MD9SM1","channel_identities":[{"channel":"SMS","identity":"48123456789","app_id":""}],"display_name":"New Test Contact","email":"new.contact@email.com","external_id":"","metadata":"","language":"EN_US"}},"message_metadata":""}'; const NONCE = '01FJA8B4A7BM43YGWSG9GBV067'; const TIMESTAMP = '1634579353'; const APP_SECRET = 'foo_secret1234'; diff --git a/packages/sms/src/rest/v1/batches/batches-api.ts b/packages/sms/src/rest/v1/batches/batches-api.ts index a7a9e2cf..566aad76 100644 --- a/packages/sms/src/rest/v1/batches/batches-api.ts +++ b/packages/sms/src/rest/v1/batches/batches-api.ts @@ -114,11 +114,11 @@ export class BatchesApi extends SmsDomainApi { const body: RequestBody = ''; const basePathUrl - = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}`; + = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -145,11 +145,11 @@ export class BatchesApi extends SmsDomainApi { const body: RequestBody = data['deliveryFeedbackRequestBody'] ? JSON.stringify(data['deliveryFeedbackRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}/delivery_feedback`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}/delivery_feedback`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -176,11 +176,11 @@ export class BatchesApi extends SmsDomainApi { }; const body: RequestBody = data['dryRunRequestBody'] ? JSON.stringify(data['dryRunRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/batches/dry_run`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/batches/dry_run`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -204,11 +204,11 @@ export class BatchesApi extends SmsDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -237,7 +237,7 @@ export class BatchesApi extends SmsDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/batches`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/batches`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); @@ -280,11 +280,11 @@ export class BatchesApi extends SmsDomainApi { const body: RequestBody = data['replaceBatchMessageRequestBody'] ? JSON.stringify(data['replaceBatchMessageRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PUT', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -312,11 +312,11 @@ export class BatchesApi extends SmsDomainApi { }; const body: RequestBody = data['sendSMSRequestBody'] ? JSON.stringify(data['sendSMSRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/batches`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/batches`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -383,11 +383,11 @@ export class BatchesApi extends SmsDomainApi { const body: RequestBody = data['updateBatchMessageRequestBody'] ? JSON.stringify(data['updateBatchMessageRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/sms/src/rest/v1/delivery-reports/delivery-reports-api.ts b/packages/sms/src/rest/v1/delivery-reports/delivery-reports-api.ts index 614f76f6..ae1a5ff2 100644 --- a/packages/sms/src/rest/v1/delivery-reports/delivery-reports-api.ts +++ b/packages/sms/src/rest/v1/delivery-reports/delivery-reports-api.ts @@ -76,11 +76,11 @@ export class DeliveryReportsApi extends SmsDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}/delivery_report`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}/delivery_report`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -104,11 +104,11 @@ export class DeliveryReportsApi extends SmsDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}/delivery_report/${data['recipient_msisdn']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/batches/${data['batch_id']}/delivery_report/${data['recipient_msisdn']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -138,7 +138,7 @@ export class DeliveryReportsApi extends SmsDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/delivery_reports`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/delivery_reports`; const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); diff --git a/packages/sms/src/rest/v1/groups/groups-api.ts b/packages/sms/src/rest/v1/groups/groups-api.ts index 94bbf7f8..ffe7c154 100644 --- a/packages/sms/src/rest/v1/groups/groups-api.ts +++ b/packages/sms/src/rest/v1/groups/groups-api.ts @@ -78,11 +78,11 @@ export class GroupsApi extends SmsDomainApi { }; const body: RequestBody = data['createGroupRequestBody'] ? JSON.stringify(data['createGroupRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/groups`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/groups`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -106,11 +106,11 @@ export class GroupsApi extends SmsDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/groups/${data['group_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/groups/${data['group_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -134,11 +134,11 @@ export class GroupsApi extends SmsDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/groups/${data['group_id']}/members`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/groups/${data['group_id']}/members`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -165,7 +165,7 @@ export class GroupsApi extends SmsDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/groups`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/groups`; const requestOptionsPromise = this.client.prepareOptions( basePathUrl, 'GET', getParams, headers, body || undefined); @@ -207,11 +207,11 @@ export class GroupsApi extends SmsDomainApi { }; const body: RequestBody = data['replaceGroupRequestBody'] ? JSON.stringify(data['replaceGroupRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/groups/${data['group_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/groups/${data['group_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PUT', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -235,11 +235,11 @@ export class GroupsApi extends SmsDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/groups/${data['group_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/groups/${data['group_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -263,11 +263,11 @@ export class GroupsApi extends SmsDomainApi { }; const body: RequestBody = data['updateGroupRequestBody'] ? JSON.stringify(data['updateGroupRequestBody']) : '{}'; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/groups/${data['group_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/groups/${data['group_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/sms/src/rest/v1/inbounds/inbounds-api.ts b/packages/sms/src/rest/v1/inbounds/inbounds-api.ts index 57403cc5..71748170 100644 --- a/packages/sms/src/rest/v1/inbounds/inbounds-api.ts +++ b/packages/sms/src/rest/v1/inbounds/inbounds-api.ts @@ -60,7 +60,7 @@ export class InboundsApi extends SmsDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/inbounds`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/inbounds`; const requestOptionsPromise = this.client.prepareOptions( basePathUrl, 'GET', getParams, headers, body || undefined); @@ -102,11 +102,11 @@ export class InboundsApi extends SmsDomainApi { }; const body: RequestBody = ''; - const basePathUrl = `${this.client.apiClientOptions.basePath}/xms/v1/${this.client.apiClientOptions.projectId}/inbounds/${data['inbound_id']}`; + const basePathUrl = `${this.client.apiClientOptions.hostname}/xms/v1/${this.client.apiClientOptions.projectId}/inbounds/${data['inbound_id']}`; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/sms/src/rest/v1/sms-domain-api.ts b/packages/sms/src/rest/v1/sms-domain-api.ts index ccff6a3c..c4c7527f 100644 --- a/packages/sms/src/rest/v1/sms-domain-api.ts +++ b/packages/sms/src/rest/v1/sms-domain-api.ts @@ -20,15 +20,15 @@ export class SmsDomainApi implements Api { } /** - * Update the default basePath for the API - * @param {string} basePath - The new base path to use for the APIs. + * Update the default hostname for the API + * @param {string} hostname - The new hostname to use for the APIs. */ - public setBasePath(basePath: string) { + public setHostname(hostname: string) { try { this.client = this.getSinchClient(); - this.client.apiClientOptions.basePath = basePath; + this.client.apiClientOptions.hostname = hostname; } catch (error) { - console.error('Impossible to set a new base path, the Application credentials need to be provided first.'); + console.error('Impossible to set a new hostname, the Application credentials need to be provided first.'); throw error; } } @@ -67,9 +67,8 @@ export class SmsDomainApi implements Api { if (!this.client) { const region = this.sinchClientParameters.region || Region.UNITED_STATES; const apiClientOptions = this.buildApiClientOptions(this.sinchClientParameters, region); - const basePath = `https://${apiClientOptions.useServicePlanId?'':'zt.'}${region}.sms.api.sinch.com`; this.client = new ApiFetchClient(apiClientOptions); - this.client.apiClientOptions.basePath = basePath; + this.client.apiClientOptions.hostname = this.sinchClientParameters.smsHostname ?? `https://${apiClientOptions.useServicePlanId?'':'zt.'}${region}.sms.api.sinch.com`; } return this.client; } diff --git a/packages/sms/src/rest/v1/sms-service.ts b/packages/sms/src/rest/v1/sms-service.ts index ce146d9c..1bffc1f8 100644 --- a/packages/sms/src/rest/v1/sms-service.ts +++ b/packages/sms/src/rest/v1/sms-service.ts @@ -20,14 +20,14 @@ export class SmsService { } /** - * Update the default basePath for each API + * Update the default hostname for each API * - * @param {string} basePath - The new base path to use for all the APIs. + * @param {string} hostname - The new hostname to use for all the APIs. */ - public setBasePath(basePath: string) { - this.groups.setBasePath(basePath); - this.deliveryReports.setBasePath(basePath); - this.batches.setBasePath(basePath); - this.inbounds.setBasePath(basePath); + public setHostname(hostname: string) { + this.groups.setHostname(hostname); + this.deliveryReports.setHostname(hostname); + this.batches.setHostname(hostname); + this.inbounds.setHostname(hostname); } } diff --git a/packages/sms/tests/rest/v1/sms-api.test.ts b/packages/sms/tests/rest/v1/sms-api.test.ts index b924f65a..2d796922 100644 --- a/packages/sms/tests/rest/v1/sms-api.test.ts +++ b/packages/sms/tests/rest/v1/sms-api.test.ts @@ -1,10 +1,12 @@ import { Region, ServicePlanIdCredentials, UnifiedCredentials } from '../../../src'; import { SmsDomainApi } from '../../../src/rest/v1/sms-domain-api'; +import { ApiHostname } from '@sinch/sdk-client'; describe('SMS API', () => { let smsApi: SmsDomainApi; - let paramsWithServicePlanId: ServicePlanIdCredentials; - let paramsWithProjectId: UnifiedCredentials; + let paramsWithServicePlanId: ServicePlanIdCredentials & Pick; + let paramsWithProjectId: UnifiedCredentials & Pick; + const CUSTOM_HOSTNAME = 'https://new.host.name'; beforeEach(() => { paramsWithServicePlanId = { @@ -23,7 +25,7 @@ describe('SMS API', () => { smsApi.getSinchClient(); expect(smsApi.client).toBeDefined(); expect(smsApi.client?.apiClientOptions.projectId).toBe('PROJECT_ID'); - expect(smsApi.client?.apiClientOptions.basePath).toBe('https://zt.us.sms.api.sinch.com'); + expect(smsApi.client?.apiClientOptions.hostname).toBe('https://zt.us.sms.api.sinch.com'); }); it('should initialize the client with servicePlanId credentials and use standard URL', () => { @@ -31,56 +33,69 @@ describe('SMS API', () => { smsApi.getSinchClient(); expect(smsApi.client).toBeDefined(); expect(smsApi.client?.apiClientOptions.projectId).toBe('SERVICE_PLAN_ID'); - expect(smsApi.client?.apiClientOptions.basePath).toBe('https://us.sms.api.sinch.com'); + expect(smsApi.client?.apiClientOptions.hostname).toBe('https://us.sms.api.sinch.com'); }); it('should change the URL when specifying a different region', () => { paramsWithServicePlanId.region = Region.CANADA; smsApi = new SmsDomainApi(paramsWithServicePlanId, 'dummy'); smsApi.getSinchClient(); - expect(smsApi.client?.apiClientOptions.basePath).toBe('https://ca.sms.api.sinch.com'); + expect(smsApi.client?.apiClientOptions.hostname).toBe('https://ca.sms.api.sinch.com'); }); - it('should update the base path', () => { - const newPath = 'https://new.base.path'; + it('should use the hostname parameter when using projectId credentials', () => { + paramsWithProjectId.smsHostname = CUSTOM_HOSTNAME; + smsApi = new SmsDomainApi(paramsWithProjectId, 'dummy'); + smsApi.getSinchClient(); + expect(smsApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + }); + + it('should use the hostname parameter when using servicePlanId credentials', () => { + paramsWithServicePlanId.smsHostname = CUSTOM_HOSTNAME; + smsApi = new SmsDomainApi(paramsWithServicePlanId, 'dummy'); + smsApi.getSinchClient(); + expect(smsApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + }); + + it('should update the hostname', () => { smsApi = new SmsDomainApi(paramsWithServicePlanId, 'dummy'); - smsApi.setBasePath(newPath); - expect(smsApi.client?.apiClientOptions.basePath).toBe(newPath); + smsApi.setHostname(CUSTOM_HOSTNAME); + expect(smsApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); }); it('should not update the credentials when adding servicePlanId credentials on default region', () => { smsApi = new SmsDomainApi(paramsWithProjectId, 'dummy'); smsApi.getSinchClient(); expect(smsApi.client?.apiClientOptions.projectId).toBe('PROJECT_ID'); - expect(smsApi.client?.apiClientOptions.basePath).toBe('https://zt.us.sms.api.sinch.com'); + expect(smsApi.client?.apiClientOptions.hostname).toBe('https://zt.us.sms.api.sinch.com'); smsApi.setCredentials(paramsWithServicePlanId); expect(smsApi.client?.apiClientOptions.projectId).toBe('PROJECT_ID'); - expect(smsApi.client?.apiClientOptions.basePath).toBe('https://zt.us.sms.api.sinch.com'); + expect(smsApi.client?.apiClientOptions.hostname).toBe('https://zt.us.sms.api.sinch.com'); }); it('should update the credentials and URL when forcing servicePlanId credentials', () => { smsApi = new SmsDomainApi(paramsWithProjectId, 'dummy'); smsApi.getSinchClient(); expect(smsApi.client?.apiClientOptions.projectId).toBe('PROJECT_ID'); - expect(smsApi.client?.apiClientOptions.basePath).toBe('https://zt.us.sms.api.sinch.com'); + expect(smsApi.client?.apiClientOptions.hostname).toBe('https://zt.us.sms.api.sinch.com'); smsApi.setCredentials({ ...paramsWithServicePlanId, forceServicePlanIdUsageForSmsApi: true, }); expect(smsApi.client?.apiClientOptions.projectId).toBe('SERVICE_PLAN_ID'); - expect(smsApi.client?.apiClientOptions.basePath).toBe('https://us.sms.api.sinch.com'); + expect(smsApi.client?.apiClientOptions.hostname).toBe('https://us.sms.api.sinch.com'); }); it('should update the credentials and URL when adding servicePlanId credentials on BR region', () => { smsApi = new SmsDomainApi(paramsWithProjectId, 'dummy'); smsApi.getSinchClient(); expect(smsApi.client?.apiClientOptions.projectId).toBe('PROJECT_ID'); - expect(smsApi.client?.apiClientOptions.basePath).toBe('https://zt.us.sms.api.sinch.com'); + expect(smsApi.client?.apiClientOptions.hostname).toBe('https://zt.us.sms.api.sinch.com'); smsApi.setCredentials({ ...paramsWithServicePlanId, region: Region.BRAZIL, }); expect(smsApi.client?.apiClientOptions.projectId).toBe('SERVICE_PLAN_ID'); - expect(smsApi.client?.apiClientOptions.basePath).toBe('https://br.sms.api.sinch.com'); + expect(smsApi.client?.apiClientOptions.hostname).toBe('https://br.sms.api.sinch.com'); }); }); diff --git a/packages/sms/tests/rest/v1/sms-service.test.ts b/packages/sms/tests/rest/v1/sms-service.test.ts new file mode 100644 index 00000000..20940932 --- /dev/null +++ b/packages/sms/tests/rest/v1/sms-service.test.ts @@ -0,0 +1,48 @@ +import { SinchClientParameters } from '@sinch/sdk-client'; +import { BatchesApi, DeliveryReportsApi, GroupsApi, InboundsApi, SmsService } from '../../../src'; + +describe('SMS Service', () => { + const DEFAULT_HOSTNAME = 'https://zt.us.sms.api.sinch.com'; + const CUSTOM_HOSTNAME = 'https://new.host.name'; + + it('should initialize all the APIs', () => { + // Given + const params: SinchClientParameters = { + projectId: 'PROJECT_ID', + keyId: 'KEY_ID', + keySecret: 'KEY_SECRET', + }; + + // When + const smsService = new SmsService(params); + + // Then + expect(smsService.batches).toBeInstanceOf(BatchesApi); + expect(smsService.deliveryReports).toBeInstanceOf(DeliveryReportsApi); + expect(smsService.inbounds).toBeInstanceOf(InboundsApi); + expect(smsService.groups).toBeInstanceOf(GroupsApi); + expect(smsService.batches.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(smsService.deliveryReports.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(smsService.inbounds.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(smsService.groups.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + }); + + it('should set a custom hostname for all APIs', () => { + // Given + const params: SinchClientParameters = { + projectId: 'PROJECT_ID', + keyId: 'KEY_ID', + keySecret: 'KEY_SECRET', + }; + + // When + const smsService = new SmsService(params); + smsService.setHostname(CUSTOM_HOSTNAME); + + // Then + expect(smsService.batches.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(smsService.deliveryReports.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(smsService.inbounds.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(smsService.groups.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + }); +}); diff --git a/packages/verification/src/rest/v1/verification-domain-api.ts b/packages/verification/src/rest/v1/verification-domain-api.ts index 210f7db5..c5c84a99 100644 --- a/packages/verification/src/rest/v1/verification-domain-api.ts +++ b/packages/verification/src/rest/v1/verification-domain-api.ts @@ -17,12 +17,17 @@ export class VerificationDomainApi implements Api { } /** - * Update the default basePath for the API - * @param {string} basePath - The new base path to use for the APIs. + * Update the default hostname for the API + * @param {string} hostname - The new hostname to use for the APIs. */ - public setBasePath(basePath: string) { - this.client = this.getSinchClient(); - this.client.apiClientOptions.basePath = basePath; + public setHostname(hostname: string) { + try { + this.client = this.getSinchClient(); + this.client.apiClientOptions.hostname = hostname; + } catch (error) { + console.error('Impossible to set a new hostname, the Application credentials need to be provided first.'); + throw error; + } } /** @@ -68,7 +73,7 @@ export class VerificationDomainApi implements Api { ], }; this.client = new ApiFetchClient(apiClientOptions); - this.client.apiClientOptions.basePath = 'https://verification.api.sinch.com'; + this.client.apiClientOptions.hostname = this.sinchClientParameters.verificationHostname ?? 'https://verification.api.sinch.com'; } return this.client; } diff --git a/packages/verification/src/rest/v1/verification-service.ts b/packages/verification/src/rest/v1/verification-service.ts index 8d367f51..221bb5d7 100644 --- a/packages/verification/src/rest/v1/verification-service.ts +++ b/packages/verification/src/rest/v1/verification-service.ts @@ -12,12 +12,12 @@ export class VerificationService { } /** - * Update the default basePath for each API + * Update the default hostname for each API * - * @param {string} basePath - The new base path to use for all the APIs. + * @param {string} hostname - The new hostname to use for all the APIs. */ - public setBasePath(basePath: string) { - this.verificationStatus.setBasePath(basePath); - this.verifications.setBasePath(basePath); + public setHostname(hostname: string) { + this.verificationStatus.setHostname(hostname); + this.verifications.setHostname(hostname); } } diff --git a/packages/verification/src/rest/v1/verification-status/verification-status-api.ts b/packages/verification/src/rest/v1/verification-status/verification-status-api.ts index 2957e64a..49c02d81 100644 --- a/packages/verification/src/rest/v1/verification-status/verification-status-api.ts +++ b/packages/verification/src/rest/v1/verification-status/verification-status-api.ts @@ -46,11 +46,11 @@ export class VerificationStatusApi extends VerificationDomainApi { const body: RequestBody = ''; const path = `/verification/v1/verifications/id/${data['id']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -75,11 +75,11 @@ export class VerificationStatusApi extends VerificationDomainApi { const body: RequestBody = ''; const path = `/verification/v1/verifications/${data['method']}/number/${data['endpoint']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -104,11 +104,11 @@ export class VerificationStatusApi extends VerificationDomainApi { const body: RequestBody = ''; const path = `/verification/v1/verifications/reference/${data['reference']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/verification/src/rest/v1/verifications/verifications-api.ts b/packages/verification/src/rest/v1/verifications/verifications-api.ts index 7a421b90..be43e998 100644 --- a/packages/verification/src/rest/v1/verifications/verifications-api.ts +++ b/packages/verification/src/rest/v1/verifications/verifications-api.ts @@ -103,11 +103,11 @@ export class VerificationsApi extends VerificationDomainApi { ? JSON.stringify(data['reportSmsVerificationByIdRequestBody']) : '{}'; const path = `/verification/v1/verifications/id/${data['id']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PUT', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -139,11 +139,11 @@ export class VerificationsApi extends VerificationDomainApi { ? JSON.stringify(data['reportFlashCallVerificationByIdRequestBody']) : '{}'; const path = `/verification/v1/verifications/id/${data['id']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PUT', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -173,11 +173,11 @@ export class VerificationsApi extends VerificationDomainApi { ? JSON.stringify(data['reportCalloutVerificationByIdRequestBody']) : '{}'; const path = `/verification/v1/verifications/id/${data['id']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PUT', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -207,11 +207,11 @@ export class VerificationsApi extends VerificationDomainApi { ? JSON.stringify(data['reportSmsVerificationByIdentityRequestBody']) : '{}'; const path = `/verification/v1/verifications/number/${data['endpoint']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PUT', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -242,11 +242,11 @@ export class VerificationsApi extends VerificationDomainApi { ? JSON.stringify(data['reportFlashCallVerificationByIdentityRequestBody']) : '{}'; const path = `/verification/v1/verifications/number/${data['endpoint']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PUT', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -277,11 +277,11 @@ export class VerificationsApi extends VerificationDomainApi { ? JSON.stringify(data['reportCalloutVerificationByIdentityRequestBody']) : '{}'; const path = `/verification/v1/verifications/number/${data['endpoint']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PUT', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -309,11 +309,11 @@ export class VerificationsApi extends VerificationDomainApi { ? JSON.stringify(data['startVerificationWithSmsRequestBody']) : '{}'; const path = '/verification/v1/verifications'; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -343,11 +343,11 @@ export class VerificationsApi extends VerificationDomainApi { ? JSON.stringify(data['startVerificationWithFlashCallRequestBody']) : '{}'; const path = '/verification/v1/verifications'; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -375,11 +375,11 @@ export class VerificationsApi extends VerificationDomainApi { ? JSON.stringify(data['startVerificationWithCalloutRequestBody']) : '{}'; const path = '/verification/v1/verifications'; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -407,11 +407,11 @@ export class VerificationsApi extends VerificationDomainApi { ? JSON.stringify(data['startSeamlessVerificationRequestBody']) : '{}'; const path = '/verification/v1/verifications'; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/verification/tests/models/v1/helper.test.ts b/packages/verification/tests/models/v1/helper.test.ts index 73d773c9..cd051ebe 100644 --- a/packages/verification/tests/models/v1/helper.test.ts +++ b/packages/verification/tests/models/v1/helper.test.ts @@ -16,7 +16,7 @@ import { const PHONE_NUMBER = '+46700000000'; const REFERENCE = 'test-reference'; const DIAL_TIMEOUT = 30; -const VERIFICATION_ID = "a_verification_id"; +const VERIFICATION_ID = 'a_verification_id'; const VERIFICATION_CODE = '0000'; const VERIFICATION_CLI = '+46000000000'; diff --git a/packages/verification/tests/rest/v1/callbacks/callbacks-webhook.test.ts b/packages/verification/tests/rest/v1/callbacks/callbacks-webhook.test.ts index b5224075..eb9ab84e 100644 --- a/packages/verification/tests/rest/v1/callbacks/callbacks-webhook.test.ts +++ b/packages/verification/tests/rest/v1/callbacks/callbacks-webhook.test.ts @@ -8,7 +8,7 @@ describe('Verification Callback Webhook', () => { const CONTENT_TYPE = 'application/json; charset=utf-8'; const X_TIMESTAMP = 'x-timestamp:2024-01-19T09:19:28.9372196Z'; const PATH = '/webhook'; - const BODY = `{"id":"018d2104-aaa-bbbb-1234","price":{"amount":0.0308},"rate":{"amount":0.0}}`; + const BODY = '{"id":"018d2104-aaa-bbbb-1234","price":{"amount":0.0308},"rate":{"amount":0.0}}'; const METHOD = 'POST'; beforeEach(() => { diff --git a/packages/verification/tests/rest/v1/verification-api.test.ts b/packages/verification/tests/rest/v1/verification-api.test.ts index 5b0aad94..f3d387d5 100644 --- a/packages/verification/tests/rest/v1/verification-api.test.ts +++ b/packages/verification/tests/rest/v1/verification-api.test.ts @@ -1,9 +1,10 @@ import { VerificationDomainApi } from '../../../src/rest/v1/verification-domain-api'; -import { ApplicationCredentials, SigningRequest } from '@sinch/sdk-client'; +import { ApiHostname, ApplicationCredentials, SigningRequest } from '@sinch/sdk-client'; describe('Verification API', () => { let verificationApi: VerificationDomainApi; - let params: ApplicationCredentials; + let params: ApplicationCredentials & Pick; + const CUSTOM_HOSTNAME = 'https://new.host.name'; beforeEach(() => { params = { @@ -17,15 +18,21 @@ describe('Verification API', () => { verificationApi.getSinchClient(); expect(verificationApi.client).toBeDefined(); expect(verificationApi.client?.apiClientOptions.projectId).toBeUndefined(); - expect(verificationApi.client?.apiClientOptions.basePath).toBe('https://verification.api.sinch.com'); + expect(verificationApi.client?.apiClientOptions.hostname).toBe('https://verification.api.sinch.com'); expect(verificationApi.client?.apiClientOptions.requestPlugins?.length).toBe(3); }); - it('should update the base path', () => { - const newPath = 'https://new.base.path'; + it('should use the hostname parameter', () => { + params.verificationHostname = CUSTOM_HOSTNAME; verificationApi = new VerificationDomainApi(params, 'dummy'); - verificationApi.setBasePath(newPath); - expect(verificationApi.client?.apiClientOptions.basePath).toBe('https://new.base.path'); + verificationApi.getSinchClient(); + expect(verificationApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + }); + + it('should update the hostname', () => { + verificationApi = new VerificationDomainApi(params, 'dummy'); + verificationApi.setHostname(CUSTOM_HOSTNAME); + expect(verificationApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); }); it('should update the credentials', () => { diff --git a/packages/verification/tests/rest/v1/verification-service.test.ts b/packages/verification/tests/rest/v1/verification-service.test.ts new file mode 100644 index 00000000..6f6fb07c --- /dev/null +++ b/packages/verification/tests/rest/v1/verification-service.test.ts @@ -0,0 +1,40 @@ +import { SinchClientParameters } from '@sinch/sdk-client'; +import { VerificationsApi, VerificationService, VerificationStatusApi } from '../../../src'; + +describe('Verification Service', () => { + const DEFAULT_HOSTNAME = 'https://verification.api.sinch.com'; + const CUSTOM_HOSTNAME = 'https://new.host.name'; + + it('should initialize all the APIs', () => { + // Given + const params: SinchClientParameters = { + applicationKey: 'APPLICATION_KEY', + applicationSecret: 'APPLICATION_SECRET', + }; + + // When + const verificationService = new VerificationService(params); + + // Then + expect(verificationService.verifications).toBeInstanceOf(VerificationsApi); + expect(verificationService.verificationStatus).toBeInstanceOf(VerificationStatusApi); + expect(verificationService.verifications.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(verificationService.verificationStatus.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + }); + + it('should set a custom hostname for all APIs', () => { + // Given + const params: SinchClientParameters = { + applicationKey: 'APPLICATION_KEY', + applicationSecret: 'APPLICATION_SECRET', + }; + + // When + const verificationService = new VerificationService(params); + verificationService.setHostname(CUSTOM_HOSTNAME); + + // Then + expect(verificationService.verifications.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(verificationService.verificationStatus.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + }); +}); diff --git a/packages/verification/tests/rest/v1/verifications/verifications-api.test.ts b/packages/verification/tests/rest/v1/verifications/verifications-api.test.ts index 9137ebc4..cde10d52 100644 --- a/packages/verification/tests/rest/v1/verifications/verifications-api.test.ts +++ b/packages/verification/tests/rest/v1/verifications/verifications-api.test.ts @@ -15,14 +15,14 @@ import { const _links: LinksObject[] = [ { - rel: "status", - href: "https://dc-euc1-std.verification.api.sinch.com/verification/v1/verifications/id/some_verification_id", - method: "GET", + rel: 'status', + href: 'https://dc-euc1-std.verification.api.sinch.com/verification/v1/verifications/id/some_verification_id', + method: 'GET', }, { - rel: "report", - href: "https://dc-euc1-std.verification.api.sinch.com/verification/v1/verifications/id/some_verification_id", - method: "PUT", + rel: 'report', + href: 'https://dc-euc1-std.verification.api.sinch.com/verification/v1/verifications/id/some_verification_id', + method: 'PUT', }, ]; diff --git a/packages/voice/src/rest/v1/applications/applications-api.ts b/packages/voice/src/rest/v1/applications/applications-api.ts index f03d84a0..1eb7cea6 100644 --- a/packages/voice/src/rest/v1/applications/applications-api.ts +++ b/packages/voice/src/rest/v1/applications/applications-api.ts @@ -63,12 +63,12 @@ export class ApplicationsApi extends VoiceDomainApi { const body: RequestBody = ''; // Remove the spaces in the phone number in order to not have inconsistencies in the way the URL is encoded in the SDK and server side when calculating the request signature - const path = `/v1/calling/query/number/${data['number'].split(" ").join("")}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const path = `/v1/calling/query/number/${data['number'].split(' ').join('')}`; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -93,11 +93,11 @@ export class ApplicationsApi extends VoiceDomainApi { const body: RequestBody = ''; const path = `/v1/configuration/callbacks/applications/${data['applicationkey']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -121,12 +121,12 @@ export class ApplicationsApi extends VoiceDomainApi { }; const body: RequestBody = ''; - const path = `/v1/configuration/numbers`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const path = '/v1/configuration/numbers'; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -151,12 +151,12 @@ export class ApplicationsApi extends VoiceDomainApi { const body: RequestBody = data['unassignNumbersRequestBody'] ? JSON.stringify(data['unassignNumbersRequestBody']) : '{}'; - const path = `/v1/configuration/numbers`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const path = '/v1/configuration/numbers'; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -182,11 +182,11 @@ export class ApplicationsApi extends VoiceDomainApi { const body: RequestBody = data['updateCallbacksRequestBody'] ? JSON.stringify(data['updateCallbacksRequestBody']) : '{}'; const path = `/v1/configuration/callbacks/applications/${data['applicationkey']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -211,12 +211,12 @@ export class ApplicationsApi extends VoiceDomainApi { const body: RequestBody = data['assignNumbersRequestBody'] ? JSON.stringify(data['assignNumbersRequestBody']) : '{}'; - const path = `/v1/configuration/numbers`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const path = '/v1/configuration/numbers'; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/voice/src/rest/v1/callouts/callouts-api.ts b/packages/voice/src/rest/v1/callouts/callouts-api.ts index c043b369..747e1ad8 100644 --- a/packages/voice/src/rest/v1/callouts/callouts-api.ts +++ b/packages/voice/src/rest/v1/callouts/callouts-api.ts @@ -62,17 +62,17 @@ export class CalloutsApi extends VoiceDomainApi { }; const body: RequestBody = data['ttsCalloutRequestBody'] ? JSON.stringify(data['ttsCalloutRequestBody']) : '{}'; - const path = `/calling/v1/callouts`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const path = '/calling/v1/callouts'; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, requestOptions, apiName: this.apiName, - operationId: 'Callouts', + operationId: 'TtsCallouts', }); } @@ -91,17 +91,17 @@ export class CalloutsApi extends VoiceDomainApi { const body: RequestBody = data['conferenceCalloutRequestBody'] ? JSON.stringify(data['conferenceCalloutRequestBody']) : '{}'; - const path = `/calling/v1/callouts`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const path = '/calling/v1/callouts'; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, requestOptions, apiName: this.apiName, - operationId: 'Callouts', + operationId: 'ConferenceCallouts', }); } @@ -120,17 +120,17 @@ export class CalloutsApi extends VoiceDomainApi { const body: RequestBody = data['customCalloutRequestBody'] ? JSON.stringify(data['customCalloutRequestBody']) : '{}'; - const path = `/calling/v1/callouts`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const path = '/calling/v1/callouts'; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, requestOptions, apiName: this.apiName, - operationId: 'Callouts', + operationId: 'CustomCallouts', }); } diff --git a/packages/voice/src/rest/v1/calls/calls-api.ts b/packages/voice/src/rest/v1/calls/calls-api.ts index 62481697..c00e5e1a 100644 --- a/packages/voice/src/rest/v1/calls/calls-api.ts +++ b/packages/voice/src/rest/v1/calls/calls-api.ts @@ -53,11 +53,11 @@ export class CallsApi extends VoiceDomainApi { const body: RequestBody = ''; const path = `/calling/v1/calls/id/${data['callId']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -84,10 +84,10 @@ export class CallsApi extends VoiceDomainApi { ? JSON.stringify(data['manageWithCallLegRequestBody']) : '{}'; const path = `/calling/v1/calls/id/${data['callId']}/leg/${data['callLeg']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PATCH', getParams, headers, body, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -112,10 +112,10 @@ export class CallsApi extends VoiceDomainApi { const body: RequestBody = data['updateCallRequestBody'] ? JSON.stringify(data['updateCallRequestBody']) : '{}'; const path = `/calling/v1/calls/id/${data['callId']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PATCH', getParams, headers, body, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/voice/src/rest/v1/conferences/conferences-api.ts b/packages/voice/src/rest/v1/conferences/conferences-api.ts index a98b7013..09290220 100644 --- a/packages/voice/src/rest/v1/conferences/conferences-api.ts +++ b/packages/voice/src/rest/v1/conferences/conferences-api.ts @@ -71,11 +71,11 @@ export class ConferencesApi extends VoiceDomainApi { const body: RequestBody = ''; const path = `/calling/v1/conferences/id/${data['conferenceId']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -100,10 +100,10 @@ export class ConferencesApi extends VoiceDomainApi { const body: RequestBody = ''; const path = `/calling/v1/conferences/id/${data['conferenceId']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -128,10 +128,10 @@ export class ConferencesApi extends VoiceDomainApi { const body: RequestBody = ''; const path = `/calling/v1/conferences/id/${data['conferenceId']}/${data['callId']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, @@ -157,10 +157,10 @@ export class ConferencesApi extends VoiceDomainApi { const body: RequestBody = data['manageParticipantRequestBody'] ? JSON.stringify(data['manageParticipantRequestBody']) : '{}'; const path = `/calling/v1/conferences/id/${data['conferenceId']}/${data['callId']}`; - const basePathUrl = this.client.apiClientOptions.basePath + path; + const basePathUrl = this.client.apiClientOptions.hostname + path; const requestOptions = await this.client.prepareOptions(basePathUrl, 'PATCH', getParams, headers, body, path); - const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); return this.client.processCall({ url, diff --git a/packages/voice/src/rest/v1/voice-domain-api.ts b/packages/voice/src/rest/v1/voice-domain-api.ts index 62e4e7dd..d01f33c7 100644 --- a/packages/voice/src/rest/v1/voice-domain-api.ts +++ b/packages/voice/src/rest/v1/voice-domain-api.ts @@ -19,27 +19,27 @@ export class VoiceDomainApi implements Api { } /** - * Update the default basePath for the API - * @param {string} basePath - The new base path to use for the APIs. + * Update the default hostname for the API + * @param {string} hostname - The new hostname to use for the APIs. */ - public setBasePath(basePath: string) { + public setHostname(hostname: string) { try { this.client = this.getSinchClient(); - this.client.apiClientOptions.basePath = basePath; + this.client.apiClientOptions.hostname = hostname; } catch (error) { - console.error('Impossible to set a new base path, the Application credentials need to be provided first.'); + console.error('Impossible to set a new hostname, the Application credentials need to be provided first.'); throw error; } } /** - * Update the region in the basePath + * Update the region in the hostname * @param {VoiceRegion} region - The new region to send the requests to */ public setRegion(region: VoiceRegion) { this.sinchClientParameters.voiceRegion = region; if (this.client) { - this.client.apiClientOptions.basePath = this.buildBasePath(region); + this.client.apiClientOptions.hostname = this.buildHostname(region); } } @@ -87,17 +87,17 @@ export class VoiceDomainApi implements Api { }; this.client = new ApiFetchClient(apiClientOptions); const region: VoiceRegion = this.sinchClientParameters.voiceRegion || VoiceRegion.DEFAULT; - this.client.apiClientOptions.basePath = this.buildBasePath(region); + this.client.apiClientOptions.hostname = this.buildHostname(region); } return this.client; } - private buildBasePath(region: VoiceRegion) { + private buildHostname(region: VoiceRegion) { switch (this.apiName) { case 'ApplicationsApi': - return `https://callingapi.sinch.com`; + return this.sinchClientParameters.voiceApplicationManagementHostname ?? 'https://callingapi.sinch.com'; default: - return `https://calling${region}.api.sinch.com`; + return this.sinchClientParameters.voiceHostname ?? `https://calling${region}.api.sinch.com`; } } diff --git a/packages/voice/src/rest/v1/voice-service.ts b/packages/voice/src/rest/v1/voice-service.ts index e5c16234..46a07f89 100644 --- a/packages/voice/src/rest/v1/voice-service.ts +++ b/packages/voice/src/rest/v1/voice-service.ts @@ -18,15 +18,18 @@ export class VoiceService { } /** - * Update the default basePath for each API + * Update the default hostname for each API * - * @param {string} basePath - The new base path to use for all the APIs. + * @param {string} hostname - The new hostname to use for all the APIs. */ - public setBasePath(basePath: string) { - this.applications.setBasePath(basePath); - this.conferences.setBasePath(basePath); - this.calls.setBasePath(basePath); - this.callouts.setBasePath(basePath); + public setHostname(hostname: string) { + this.conferences.setHostname(hostname); + this.calls.setHostname(hostname); + this.callouts.setHostname(hostname); + } + + public setApplicationManagementHostname(hostname: string) { + this.applications.setHostname(hostname); } public setRegion(region: VoiceRegion) { diff --git a/packages/voice/tests/models/v1/helper.test.ts b/packages/voice/tests/models/v1/helper.test.ts index a2868efd..12cb09c1 100644 --- a/packages/voice/tests/models/v1/helper.test.ts +++ b/packages/voice/tests/models/v1/helper.test.ts @@ -322,8 +322,8 @@ describe('Voice models helpers', () => { .addInstruction(aceInstructionHelper.startRecording(RECORDING_OPTIONS)) .addInstruction(aceInstructionHelper.say('Hello from Sinch.', 'en-US')) .addInstruction(aceInstructionHelper.playFiles([ - "#tts[Hello from Sinch]", - "#ssml[Have a great day!]", + '#tts[Hello from Sinch]', + '#ssml[Have a great day!]', ], 'en-US')) .build(); const expectedResult: AceResponse = { @@ -349,7 +349,7 @@ describe('Voice models helpers', () => { }, { name: 'playFiles', - ids: ["#tts[Hello from Sinch]", "#ssml[Have a great day!]"], + ids: ['#tts[Hello from Sinch]', '#ssml[Have a great day!]'], locale: 'en-US', }, ], diff --git a/packages/voice/tests/rest/v1/callbacks/callbacks-webhook.test.ts b/packages/voice/tests/rest/v1/callbacks/callbacks-webhook.test.ts index ec0a5e48..d6811a8c 100644 --- a/packages/voice/tests/rest/v1/callbacks/callbacks-webhook.test.ts +++ b/packages/voice/tests/rest/v1/callbacks/callbacks-webhook.test.ts @@ -8,7 +8,7 @@ describe('Voice Callback Webhook', () => { const CONTENT_TYPE = 'application/json; charset=utf-8'; const X_TIMESTAMP = 'x-timestamp:2024-01-19T09:19:28.9372196Z'; const PATH = '/webhook'; - const BODY = `{"id":"018d2104-aaa-bbbb-1234","price":{"amount":0.0308},"rate":{"amount":0.0}}`; + const BODY = '{"id":"018d2104-aaa-bbbb-1234","price":{"amount":0.0308},"rate":{"amount":0.0}}'; const METHOD = 'POST'; const DATE_AS_STRING = '2023-12-29T15:07:22Z'; diff --git a/packages/voice/tests/rest/v1/voice-domain-api.test.ts b/packages/voice/tests/rest/v1/voice-domain-api.test.ts new file mode 100644 index 00000000..decad7bd --- /dev/null +++ b/packages/voice/tests/rest/v1/voice-domain-api.test.ts @@ -0,0 +1,93 @@ +import { VoiceDomainApi } from '../../../src/rest/v1/voice-domain-api'; +import { ApplicationsApi } from '../../../src'; +import { ApiHostname, ApplicationCredentials, SigningRequest, VoiceRegion } from '@sinch/sdk-client'; + +describe('Voice API', () => { + let voiceApi: VoiceDomainApi; + let voiceApplicationApi: ApplicationsApi; + let params: ApplicationCredentials & Pick; + const CUSTOM_HOSTNAME = 'https://new.host.name'; + const CUSTOM_HOSTNAME_APPLICATIONS = 'https://apps.new.host.name'; + + beforeEach(() => { + params = { + applicationKey: 'APPLICATION_KEY', + applicationSecret: 'APPLICATION_SECRET', + }; + }); + + it('should initialize the client', () => { + voiceApi = new VoiceDomainApi(params, 'dummy'); + voiceApi.getSinchClient(); + expect(voiceApi.client).toBeDefined(); + expect(voiceApi.client?.apiClientOptions.projectId).toBeUndefined(); + expect(voiceApi.client?.apiClientOptions.hostname).toBe('https://calling.api.sinch.com'); + expect(voiceApi.client?.apiClientOptions.requestPlugins?.length).toBe(3); + }); + + it('should change the URL when specifying a different region', () => { + params.voiceRegion = VoiceRegion.UNITED_STATES; + voiceApi = new VoiceDomainApi(params, 'dummy'); + voiceApi.getSinchClient(); + expect(voiceApi.client?.apiClientOptions.hostname).toBe('https://calling-use1.api.sinch.com'); + }); + + it('should use the hostname parameter but not for voice application management', () => { + params.voiceHostname = CUSTOM_HOSTNAME; + voiceApi = new VoiceDomainApi(params, 'dummy'); + voiceApi.getSinchClient(); + voiceApplicationApi = new ApplicationsApi(params); + voiceApplicationApi.getSinchClient(); + expect(voiceApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(voiceApplicationApi.client?.apiClientOptions.hostname).toBe('https://callingapi.sinch.com'); + }); + + it('should use the hostname parameter for voice application management only', () => { + params.voiceApplicationManagementHostname = CUSTOM_HOSTNAME_APPLICATIONS; + voiceApi = new VoiceDomainApi(params, 'dummy'); + voiceApi.getSinchClient(); + voiceApplicationApi = new ApplicationsApi(params); + voiceApplicationApi.getSinchClient(); + expect(voiceApi.client?.apiClientOptions.hostname).toBe('https://calling.api.sinch.com'); + expect(voiceApplicationApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME_APPLICATIONS); + }); + + it('should use the hostname parameter for the 2 different domains', () => { + params.voiceHostname = CUSTOM_HOSTNAME; + params.voiceApplicationManagementHostname = CUSTOM_HOSTNAME_APPLICATIONS; + voiceApi = new VoiceDomainApi(params, 'dummy'); + voiceApi.getSinchClient(); + voiceApplicationApi = new ApplicationsApi(params); + voiceApplicationApi.getSinchClient(); + expect(voiceApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(voiceApplicationApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME_APPLICATIONS); + }); + + it('should update the hostname', () => { + voiceApi = new VoiceDomainApi(params, 'dummy'); + voiceApi.setHostname(CUSTOM_HOSTNAME); + expect(voiceApi.client?.apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + }); + + it('should update the credentials', () => { + voiceApi = new VoiceDomainApi(params, 'dummy'); + voiceApi.setApplication({ + applicationKey: 'NEW_APPLICATION_KEY', + applicationSecret: 'NEW_APPLICATION_SECRET', + }); + const signingPlugin = voiceApi.client?.apiClientOptions.requestPlugins?.find( + (plugin) => plugin instanceof SigningRequest, + ); + expect(signingPlugin).toBeDefined(); + expect((signingPlugin as any).applicationId).toBe('NEW_APPLICATION_KEY'); + expect((signingPlugin as any).applicationSecret).toBe('NEW_APPLICATION_SECRET'); + }); + + it('should update the region', () => { + voiceApi = new VoiceDomainApi(params, 'dummy'); + voiceApi.setRegion(VoiceRegion.EUROPE); + voiceApi.getSinchClient(); + expect(voiceApi.client).toBeDefined(); + expect(voiceApi.client?.apiClientOptions.hostname).toBe('https://calling-euc1.api.sinch.com'); + }); +}); diff --git a/packages/voice/tests/rest/v1/voice-service.test.ts b/packages/voice/tests/rest/v1/voice-service.test.ts new file mode 100644 index 00000000..2322f419 --- /dev/null +++ b/packages/voice/tests/rest/v1/voice-service.test.ts @@ -0,0 +1,62 @@ +import { SinchClientParameters } from '@sinch/sdk-client'; +import { ApplicationsApi, CalloutsApi, CallsApi, ConferencesApi, VoiceService } from '../../../src'; + +describe('Voice Service', () => { + const DEFAULT_HOSTNAME = 'https://calling.api.sinch.com'; + const CUSTOM_HOSTNAME = 'https://new.host.name'; + const DEFAULT_HOSTNAME_APPLICATIONS = 'https://callingapi.sinch.com'; + const CUSTOM_HOSTNAME_APPLICATIONS = 'https://apps.new.host.name'; + + it('should initialize all the APIs', () => { + // Given + const params: SinchClientParameters = { + applicationKey: 'APPLICATION_KEY', + applicationSecret: 'APPLICATION_SECRET', + }; + + // When + const voiceService = new VoiceService(params); + + // Then + expect(voiceService.callouts).toBeInstanceOf(CalloutsApi); + expect(voiceService.calls).toBeInstanceOf(CallsApi); + expect(voiceService.conferences).toBeInstanceOf(ConferencesApi); + expect(voiceService.applications).toBeInstanceOf(ApplicationsApi); + }); + + it('should set a custom hostname for all APIs but applications', () => { + // Given + const params: SinchClientParameters = { + applicationKey: 'APPLICATION_KEY', + applicationSecret: 'APPLICATION_SECRET', + }; + + // When + const voiceService = new VoiceService(params); + voiceService.setHostname(CUSTOM_HOSTNAME); + + // Then + expect(voiceService.callouts.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(voiceService.calls.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(voiceService.conferences.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME); + expect(voiceService.applications.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME_APPLICATIONS); + }); + + it('should set a custom hostname for the applications API only', () => { + // Given + const params: SinchClientParameters = { + applicationKey: 'APPLICATION_KEY', + applicationSecret: 'APPLICATION_SECRET', + }; + + // When + const voiceService = new VoiceService(params); + voiceService.setApplicationManagementHostname(CUSTOM_HOSTNAME_APPLICATIONS); + + // Then + expect(voiceService.callouts.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(voiceService.calls.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(voiceService.conferences.getSinchClient().apiClientOptions.hostname).toBe(DEFAULT_HOSTNAME); + expect(voiceService.applications.getSinchClient().apiClientOptions.hostname).toBe(CUSTOM_HOSTNAME_APPLICATIONS); + }); +});