UNPKG

@azure-rest/ai-translation-text

Version:

An isomorphic client library for the Azure Cognitive Translator Service

98 lines 4.7 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); exports.default = createClient; const tslib_1 = require("tslib"); const core_client_1 = require("@azure-rest/core-client"); const logger_js_1 = require("./logger.js"); const coreRestPipeline = tslib_1.__importStar(require("@azure/core-rest-pipeline")); const authenticationCustomized_js_1 = require("./authenticationCustomized.js"); const DEFAULT_ENPOINT = "https://api.cognitive.microsofttranslator.com"; const PLATFORM_HOST = "cognitiveservices"; const PLATFORM_PATH = "/translator/text/v3.0"; function isKeyCredential(credential) { return (credential === null || credential === void 0 ? void 0 : credential.key) !== undefined; } function isTranslatorKeyCredential(credential) { return (credential === null || credential === void 0 ? void 0 : credential.key) !== undefined; } function isTokenCredential(credential) { return (credential === null || credential === void 0 ? void 0 : credential.getToken) !== undefined; } function isTranslatorTokenCredential(credential) { return ((credential === null || credential === void 0 ? void 0 : credential.tokenCredential) !== undefined && (credential === null || credential === void 0 ? void 0 : credential.azureResourceId) !== undefined); } function isCredentials(credential) { return (isKeyCredential(credential) || isTranslatorKeyCredential(credential) || isTokenCredential(credential) || isTranslatorTokenCredential(credential)); } // Implementation function createClient(arg1, arg2, arg3) { var _a, _b, _c, _d, _e, _f, _g, _h; let serviceEndpoint; let endpoint; let options; let credential; if (typeof arg1 === "string") { endpoint = arg1; } if (typeof arg1 !== "string" && isCredentials(arg1)) { credential = arg1; options = arg2; } else if (isCredentials(arg2)) { credential = arg2; options = arg3; } if (!options) { options = {}; } options.apiVersion = (_a = options.apiVersion) !== null && _a !== void 0 ? _a : "3.0"; if (!endpoint) { serviceEndpoint = DEFAULT_ENPOINT; } else if (endpoint.toLowerCase().indexOf(PLATFORM_HOST) !== -1) { serviceEndpoint = `${endpoint}${PLATFORM_PATH}`; } else { serviceEndpoint = endpoint; } const baseUrl = (_b = options.baseUrl) !== null && _b !== void 0 ? _b : `${serviceEndpoint}`; const userAgentInfo = `azsdk-js-ai-translation-text-rest/1.0.1`; const userAgentPrefix = options.userAgentOptions && options.userAgentOptions.userAgentPrefix ? `${options.userAgentOptions.userAgentPrefix} ${userAgentInfo}` : `${userAgentInfo}`; options = Object.assign(Object.assign({}, options), { userAgentOptions: { userAgentPrefix, }, loggingOptions: { logger: (_d = (_c = options.loggingOptions) === null || _c === void 0 ? void 0 : _c.logger) !== null && _d !== void 0 ? _d : logger_js_1.logger.info, } }); const client = (0, core_client_1.getClient)(baseUrl, options); if (isTranslatorKeyCredential(credential)) { const mtAuthneticationPolicy = new authenticationCustomized_js_1.TranslatorAuthenticationPolicy(credential); client.pipeline.addPolicy(mtAuthneticationPolicy); } else if (isKeyCredential(credential)) { const mtKeyAuthenticationPolicy = new authenticationCustomized_js_1.TranslatorAzureKeyAuthenticationPolicy(credential); client.pipeline.addPolicy(mtKeyAuthenticationPolicy); } else if (isTokenCredential(credential)) { client.pipeline.addPolicy(coreRestPipeline.bearerTokenAuthenticationPolicy({ credential: credential, scopes: (_f = (_e = options === null || options === void 0 ? void 0 : options.credentials) === null || _e === void 0 ? void 0 : _e.scopes) !== null && _f !== void 0 ? _f : authenticationCustomized_js_1.DEFAULT_SCOPE, })); } else if (isTranslatorTokenCredential(credential)) { client.pipeline.addPolicy(coreRestPipeline.bearerTokenAuthenticationPolicy({ credential: credential.tokenCredential, scopes: (_h = (_g = options === null || options === void 0 ? void 0 : options.credentials) === null || _g === void 0 ? void 0 : _g.scopes) !== null && _h !== void 0 ? _h : authenticationCustomized_js_1.DEFAULT_SCOPE, })); client.pipeline.addPolicy(new authenticationCustomized_js_1.TranslatorTokenCredentialAuthenticationPolicy(credential)); } return client; } //# sourceMappingURL=customClient.js.map