UNPKG

@azure-rest/ai-translation-text

Version:

An isomorphic client library for the Azure Cognitive Translator Service

94 lines 4.46 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. import { getClient } from "@azure-rest/core-client"; import { logger } from "./logger.js"; import * as coreRestPipeline from "@azure/core-rest-pipeline"; import { DEFAULT_SCOPE, TranslatorAuthenticationPolicy, TranslatorAzureKeyAuthenticationPolicy, TranslatorTokenCredentialAuthenticationPolicy, } from "./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 export default 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.info, } }); const client = getClient(baseUrl, options); if (isTranslatorKeyCredential(credential)) { const mtAuthneticationPolicy = new TranslatorAuthenticationPolicy(credential); client.pipeline.addPolicy(mtAuthneticationPolicy); } else if (isKeyCredential(credential)) { const mtKeyAuthenticationPolicy = new 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 : 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 : DEFAULT_SCOPE, })); client.pipeline.addPolicy(new TranslatorTokenCredentialAuthenticationPolicy(credential)); } return client; } //# sourceMappingURL=customClient.js.map