@sinch/mcp
Version:
Sinch MCP server
78 lines • 4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConversationRegion = exports.getConversationAppId = void 0;
exports.getConversationService = getConversationService;
exports.getConversationTemplateService = getConversationTemplateService;
const sdk_core_1 = require("@sinch/sdk-core");
const process_1 = __importDefault(require("process"));
const types_1 = require("../../../types");
const user_agent_1 = require("../../../user-agent");
function getConversationService() {
return getSinchService(sdk_core_1.CONVERSATION_HOSTNAME, (client, fetcher, hostname) => configureConversationApis(client, fetcher, hostname));
}
function getConversationTemplateService() {
return getSinchService(sdk_core_1.CONVERSATION_TEMPLATES_HOSTNAME, (client, fetcher, hostname) => configureTemplatesApis(client, fetcher, hostname));
}
/** Shared helper for both “conversation” and “templates” */
function getSinchService(hostnameTemplate, configure) {
const projectId = process_1.default.env.CONVERSATION_PROJECT_ID;
const keyId = process_1.default.env.CONVERSATION_KEY_ID;
const keySecret = process_1.default.env.CONVERSATION_KEY_SECRET;
if (!projectId || !keyId || !keySecret) {
return new types_1.PromptResponse('Missing env vars: CONVERSATION_PROJECT_ID, CONVERSATION_KEY_ID, CONVERSATION_KEY_SECRET.');
}
const client = new sdk_core_1.SinchClient({});
const fetcher = new sdk_core_1.ApiFetchClient({
projectId,
requestPlugins: [
new sdk_core_1.Oauth2TokenRequest(keyId, keySecret),
new sdk_core_1.AdditionalHeadersRequest({
headers: (0, sdk_core_1.buildHeader)('User-Agent', user_agent_1.USER_AGENT),
}),
],
});
// Remove the VersionRequest plugin, as we override the user-agent header
fetcher.apiClientOptions.requestPlugins?.shift();
// Replace the region placeholder with US by default
const hostname = hostnameTemplate.replace(sdk_core_1.REGION_PATTERN, `${sdk_core_1.ConversationRegion.UNITED_STATES}.`);
configure(client, fetcher, hostname);
return client;
}
const addPropertiesToApi = (api, client, hostname) => {
api.client = client;
api.setHostname(hostname);
};
const configureConversationApis = (sinchClient, apiFetchClient, hostnameTemplate) => {
const hostname = hostnameTemplate.replace(sdk_core_1.REGION_PATTERN, `${sdk_core_1.ConversationRegion.UNITED_STATES}.`);
const apis = [
sinchClient.conversation.app,
sinchClient.conversation.contact,
sinchClient.conversation.conversation,
sinchClient.conversation.messages,
sinchClient.conversation.events,
sinchClient.conversation.capability,
sinchClient.conversation.transcoding,
sinchClient.conversation.webhooks,
];
apis.forEach((api) => addPropertiesToApi(api, apiFetchClient, hostname));
};
const configureTemplatesApis = (sinchClient, apiFetchClient, hostnameTemplate) => {
const hostname = hostnameTemplate.replace(sdk_core_1.REGION_PATTERN, `${sdk_core_1.ConversationRegion.UNITED_STATES}.`);
addPropertiesToApi(sinchClient.conversation.templatesV2, apiFetchClient, hostname);
};
const getConversationAppId = (appId) => {
if (!appId) {
appId = process_1.default.env.CONVERSATION_APP_ID;
if (!appId) {
return new types_1.PromptResponse('The "CONVERSATION_APP_ID" is not set in the environment variables and the "appId" property is not provided.');
}
}
return appId;
};
exports.getConversationAppId = getConversationAppId;
const getConversationRegion = (region) => region ?? process_1.default.env.CONVERSATION_REGION ?? sdk_core_1.ConversationRegion.UNITED_STATES;
exports.getConversationRegion = getConversationRegion;
//# sourceMappingURL=conversation-service-helper.js.map