From 7ab722dfff3fb64c75e985ee358a573bcf1984aa Mon Sep 17 00:00:00 2001 From: ZiWei Chen <98569699+kazrael2119@users.noreply.github.com> Date: Fri, 20 Dec 2024 08:31:52 +0800 Subject: [PATCH] [data-plane] refresh ai-translation-text-rest (#31637) https://github.com/Azure/azure-sdk-for-js/issues/30680 --- .../ai-translation-text-rest/CHANGELOG.md | 9 +- .../{src => }/generated/clientDefinitions.ts | 1 + .../generated/index.ts | 15 + .../{src => }/generated/isUnexpected.ts | 1 + .../generated/logger.ts | 6 + .../{src => }/generated/models.ts | 3 +- .../generated/outputModels.ts | 324 ++++++++++++++++++ .../{src => }/generated/parameters.ts | 21 +- .../{src => }/generated/responses.ts | 1 + .../{src => }/generated/serializeHelper.ts | 1 + .../generated/textTranslationClient.ts | 27 +- .../review/ai-translation-text.api.md | 14 +- ...ication.ts => authenticationCustomized.ts} | 0 .../src/clientDefinitions.ts | 89 +++++ .../src/customClient.ts | 11 +- .../ai-translation-text-rest/src/index.ts | 16 +- .../src/isUnexpected.ts | 141 ++++++++ .../src/{generated => }/logger.ts | 0 .../ai-translation-text-rest/src/models.ts | 25 ++ .../src/{generated => }/outputModels.ts | 0 .../src/parameters.ts | 300 ++++++++++++++++ .../ai-translation-text-rest/src/responses.ts | 172 ++++++++++ .../src/serializeHelper.ts | 13 + .../src/textTranslationClient.ts | 60 ++++ 24 files changed, 1211 insertions(+), 39 deletions(-) rename sdk/translation/ai-translation-text-rest/{src => }/generated/clientDefinitions.ts (99%) create mode 100644 sdk/translation/ai-translation-text-rest/generated/index.ts rename sdk/translation/ai-translation-text-rest/{src => }/generated/isUnexpected.ts (99%) create mode 100644 sdk/translation/ai-translation-text-rest/generated/logger.ts rename sdk/translation/ai-translation-text-rest/{src => }/generated/models.ts (94%) create mode 100644 sdk/translation/ai-translation-text-rest/generated/outputModels.ts rename sdk/translation/ai-translation-text-rest/{src => }/generated/parameters.ts (97%) rename sdk/translation/ai-translation-text-rest/{src => }/generated/responses.ts (99%) rename sdk/translation/ai-translation-text-rest/{src => }/generated/serializeHelper.ts (99%) rename sdk/translation/ai-translation-text-rest/{src => }/generated/textTranslationClient.ts (59%) rename sdk/translation/ai-translation-text-rest/src/{authentication.ts => authenticationCustomized.ts} (100%) create mode 100644 sdk/translation/ai-translation-text-rest/src/clientDefinitions.ts create mode 100644 sdk/translation/ai-translation-text-rest/src/isUnexpected.ts rename sdk/translation/ai-translation-text-rest/src/{generated => }/logger.ts (100%) create mode 100644 sdk/translation/ai-translation-text-rest/src/models.ts rename sdk/translation/ai-translation-text-rest/src/{generated => }/outputModels.ts (100%) create mode 100644 sdk/translation/ai-translation-text-rest/src/parameters.ts create mode 100644 sdk/translation/ai-translation-text-rest/src/responses.ts create mode 100644 sdk/translation/ai-translation-text-rest/src/serializeHelper.ts create mode 100644 sdk/translation/ai-translation-text-rest/src/textTranslationClient.ts diff --git a/sdk/translation/ai-translation-text-rest/CHANGELOG.md b/sdk/translation/ai-translation-text-rest/CHANGELOG.md index c9b5bd652fdc..24e0a7277b11 100644 --- a/sdk/translation/ai-translation-text-rest/CHANGELOG.md +++ b/sdk/translation/ai-translation-text-rest/CHANGELOG.md @@ -1,14 +1,9 @@ # Release History -## 1.0.1 (Unreleased) +## 1.0.1 (2024-12-16) ### Features Added - -### Breaking Changes - -### Bugs Fixed - -### Other Changes +- refresh @azure-rest/ai-translation-text sdk ## 1.0.0 (2024-05-21) diff --git a/sdk/translation/ai-translation-text-rest/src/generated/clientDefinitions.ts b/sdk/translation/ai-translation-text-rest/generated/clientDefinitions.ts similarity index 99% rename from sdk/translation/ai-translation-text-rest/src/generated/clientDefinitions.ts rename to sdk/translation/ai-translation-text-rest/generated/clientDefinitions.ts index 07996cd15d50..e4141c22c810 100644 --- a/sdk/translation/ai-translation-text-rest/src/generated/clientDefinitions.ts +++ b/sdk/translation/ai-translation-text-rest/generated/clientDefinitions.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. + import { GetSupportedLanguagesParameters, TranslateParameters, diff --git a/sdk/translation/ai-translation-text-rest/generated/index.ts b/sdk/translation/ai-translation-text-rest/generated/index.ts new file mode 100644 index 000000000000..6de7383875e4 --- /dev/null +++ b/sdk/translation/ai-translation-text-rest/generated/index.ts @@ -0,0 +1,15 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import TextTranslationClient from "./textTranslationClient.js"; + +export * from "./textTranslationClient.js"; +export * from "./parameters.js"; +export * from "./responses.js"; +export * from "./clientDefinitions.js"; +export * from "./isUnexpected.js"; +export * from "./models.js"; +export * from "./outputModels.js"; +export * from "./serializeHelper.js"; + +export default TextTranslationClient; diff --git a/sdk/translation/ai-translation-text-rest/src/generated/isUnexpected.ts b/sdk/translation/ai-translation-text-rest/generated/isUnexpected.ts similarity index 99% rename from sdk/translation/ai-translation-text-rest/src/generated/isUnexpected.ts rename to sdk/translation/ai-translation-text-rest/generated/isUnexpected.ts index 221013830501..e1b06275aa24 100644 --- a/sdk/translation/ai-translation-text-rest/src/generated/isUnexpected.ts +++ b/sdk/translation/ai-translation-text-rest/generated/isUnexpected.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. + import { GetSupportedLanguages200Response, GetSupportedLanguagesDefaultResponse, diff --git a/sdk/translation/ai-translation-text-rest/generated/logger.ts b/sdk/translation/ai-translation-text-rest/generated/logger.ts new file mode 100644 index 000000000000..9bdd56aa86e0 --- /dev/null +++ b/sdk/translation/ai-translation-text-rest/generated/logger.ts @@ -0,0 +1,6 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + + +import { createClientLogger } from "@azure/logger"; +export const logger = createClientLogger("ai-translation-text"); diff --git a/sdk/translation/ai-translation-text-rest/src/generated/models.ts b/sdk/translation/ai-translation-text-rest/generated/models.ts similarity index 94% rename from sdk/translation/ai-translation-text-rest/src/generated/models.ts rename to sdk/translation/ai-translation-text-rest/generated/models.ts index 725e8ddd1306..8fad16da1ae7 100644 --- a/sdk/translation/ai-translation-text-rest/src/generated/models.ts +++ b/sdk/translation/ai-translation-text-rest/generated/models.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. + /** Element containing the text for translation. */ export interface InputTextItem { /** Text to translate. */ @@ -18,7 +19,7 @@ export interface DictionaryExampleTextItem extends InputTextItem { } /** Alias for TextType */ -export type TextType = string | "Plain" | "Html"; +export type TextType = string; /** Translator profanity actions */ export type ProfanityAction = "NoAction" | "Marked" | "Deleted"; /** Translator profanity markers */ diff --git a/sdk/translation/ai-translation-text-rest/generated/outputModels.ts b/sdk/translation/ai-translation-text-rest/generated/outputModels.ts new file mode 100644 index 000000000000..076b41a221ec --- /dev/null +++ b/sdk/translation/ai-translation-text-rest/generated/outputModels.ts @@ -0,0 +1,324 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + + +/** Response for the languages API. */ +export interface GetSupportedLanguagesResultOutput { + /** Languages that support translate API. */ + translation?: Record; + /** Languages that support transliteration API. */ + transliteration?: Record; + /** Languages that support dictionary API. */ + dictionary?: Record; +} + +/** + * The value of the translation property is a dictionary of (key, value) pairs. Each key is a BCP 47 language tag. + * A key identifies a language for which text can be translated to or translated from. + */ +export interface TranslationLanguageOutput { + /** Display name of the language in the locale requested via Accept-Language header. */ + name: string; + /** Display name of the language in the locale native for this language. */ + nativeName: string; + /** Directionality, which is rtl for right-to-left languages or ltr for left-to-right languages. */ + dir: LanguageDirectionalityOutput; +} + +/** + * The value of the transliteration property is a dictionary of (key, value) pairs. + * Each key is a BCP 47 language tag. A key identifies a language for which text can be converted from one script + * to another script. + */ +export interface TransliterationLanguageOutput { + /** Display name of the language in the locale requested via Accept-Language header. */ + name: string; + /** Display name of the language in the locale native for this language. */ + nativeName: string; + /** List of scripts to convert from. */ + scripts: Array; +} + +/** Script definition with list of script into which given script can be translitered. */ +export interface TransliterableScriptOutput extends LanguageScriptOutput { + /** List of scripts available to convert text to. */ + toScripts: Array; +} + +/** Common properties of language script */ +export interface LanguageScriptOutput { + /** Code identifying the script. */ + code: string; + /** Display name of the script in the locale requested via Accept-Language header. */ + name: string; + /** Display name of the language in the locale native for the language. */ + nativeName: string; + /** Directionality, which is rtl for right-to-left languages or ltr for left-to-right languages. */ + dir: LanguageDirectionalityOutput; +} + +/** Properties ot the source dictionary language */ +export interface SourceDictionaryLanguageOutput { + /** Display name of the language in the locale requested via Accept-Language header. */ + name: string; + /** Display name of the language in the locale native for this language. */ + nativeName: string; + /** Directionality, which is rtl for right-to-left languages or ltr for left-to-right languages. */ + dir: LanguageDirectionalityOutput; + /** List of languages with alterative translations and examples for the query expressed in the source language. */ + translations: Array; +} + +/** Properties of the target dictionary language */ +export interface TargetDictionaryLanguageOutput { + /** Display name of the language in the locale requested via Accept-Language header. */ + name: string; + /** Display name of the language in the locale native for this language. */ + nativeName: string; + /** Directionality, which is rtl for right-to-left languages or ltr for left-to-right languages. */ + dir: LanguageDirectionalityOutput; + /** Language code identifying the target language. */ + code: string; +} + +/** Representation of the Error Response from Translator Service. */ +export interface ErrorResponseOutput { + /** Error details. */ + error: ErrorDetailsOutput; +} + +/** Error details as returned by Translator Service. */ +export interface ErrorDetailsOutput { + /** Number identifier of the error. */ + code: number; + /** Human readable error description. */ + message: string; +} + +/** Element containing the translated text */ +export interface TranslatedTextItemOutput { + /** The detectedLanguage property is only present in the result object when language auto-detection is requested. */ + detectedLanguage?: DetectedLanguageOutput; + /** + * An array of translation results. The size of the array matches the number of target + * languages specified through the to query parameter. + */ + translations: Array; + /** + * Input text in the default script of the source language. sourceText property is present only when + * the input is expressed in a script that's not the usual script for the language. For example, + * if the input were Arabic written in Latin script, then sourceText.text would be the same Arabic text + * converted into Arab script. + */ + sourceText?: SourceTextOutput; +} + +/** An object describing the detected language. */ +export interface DetectedLanguageOutput { + /** A string representing the code of the detected language. */ + language: string; + /** + * A float value indicating the confidence in the result. + * The score is between zero and one and a low score indicates a low confidence. + */ + score: number; +} + +/** Translation result */ +export interface TranslationTextOutput { + /** A string representing the language code of the target language. */ + to: string; + /** A string giving the translated text. */ + text: string; + /** An object giving the translated text in the script specified by the toScript parameter. */ + transliteration?: TransliteratedTextOutput; + /** Alignment information. */ + alignment?: TranslatedTextAlignmentOutput; + /** Sentence boundaries in the input and output texts. */ + sentLen?: SentenceBoundariesOutput; +} + +/** Transliterated text element. */ +export interface TransliteratedTextOutput { + /** A string which is the result of converting the input string to the output script. */ + text: string; + /** A string specifying the script used in the output. */ + script: string; +} + +/** Alignment information object. */ +export interface TranslatedTextAlignmentOutput { + /** + * Maps input text to translated text. The alignment information is only provided when the request + * parameter includeAlignment is true. Alignment is returned as a string value of the following + * format: [[SourceTextStartIndex]:[SourceTextEndIndex]–[TgtTextStartIndex]:[TgtTextEndIndex]]. + * The colon separates start and end index, the dash separates the languages, and space separates the words. + * One word may align with zero, one, or multiple words in the other language, and the aligned words may + * be non-contiguous. When no alignment information is available, the alignment element will be empty. + */ + proj: string; +} + +/** An object returning sentence boundaries in the input and output texts. */ +export interface SentenceBoundariesOutput { + /** + * An integer array representing the lengths of the sentences in the input text. + * The length of the array is the number of sentences, and the values are the length of each sentence. + */ + srcSentLen: number[]; + /** + * An integer array representing the lengths of the sentences in the translated text. + * The length of the array is the number of sentences, and the values are the length of each sentence. + */ + transSentLen: number[]; +} + +/** Input text in the default script of the source language. */ +export interface SourceTextOutput { + /** Input text in the default script of the source language. */ + text: string; +} + +/** Item containing break sentence result. */ +export interface BreakSentenceItemOutput { + /** The detectedLanguage property is only present in the result object when language auto-detection is requested. */ + detectedLanguage?: DetectedLanguageOutput; + /** + * An integer array representing the lengths of the sentences in the input text. + * The length of the array is the number of sentences, and the values are the length of each sentence. + */ + sentLen: number[]; +} + +/** Dictionary Lookup Element */ +export interface DictionaryLookupItemOutput { + /** + * A string giving the normalized form of the source term. + * For example, if the request is "JOHN", the normalized form will be "john". + * The content of this field becomes the input to lookup examples. + */ + normalizedSource: string; + /** + * A string giving the source term in a form best suited for end-user display. + * For example, if the input is "JOHN", the display form will reflect the usual + * spelling of the name: "John". + */ + displaySource: string; + /** A list of translations for the source term. */ + translations: Array; +} + +/** Translation source term. */ +export interface DictionaryTranslationOutput { + /** + * A string giving the normalized form of this term in the target language. + * This value should be used as input to lookup examples. + */ + normalizedTarget: string; + /** + * A string giving the term in the target language and in a form best suited + * for end-user display. Generally, this will only differ from the normalizedTarget + * in terms of capitalization. For example, a proper noun like "Juan" will have + * normalizedTarget = "juan" and displayTarget = "Juan". + */ + displayTarget: string; + /** A string associating this term with a part-of-speech tag. */ + posTag: string; + /** + * A value between 0.0 and 1.0 which represents the "confidence" + * (or perhaps more accurately, "probability in the training data") of that translation pair. + * The sum of confidence scores for one source word may or may not sum to 1.0. + */ + confidence: number; + /** + * A string giving the word to display as a prefix of the translation. Currently, + * this is the gendered determiner of nouns, in languages that have gendered determiners. + * For example, the prefix of the Spanish word "mosca" is "la", since "mosca" is a feminine noun in Spanish. + * This is only dependent on the translation, and not on the source. + * If there is no prefix, it will be the empty string. + */ + prefixWord: string; + /** + * A list of "back translations" of the target. For example, source words that the target can translate to. + * The list is guaranteed to contain the source word that was requested (e.g., if the source word being + * looked up is "fly", then it is guaranteed that "fly" will be in the backTranslations list). + * However, it is not guaranteed to be in the first position, and often will not be. + */ + backTranslations: Array; +} + +/** Back Translation */ +export interface BackTranslationOutput { + /** + * A string giving the normalized form of the source term that is a back-translation of the target. + * This value should be used as input to lookup examples. + */ + normalizedText: string; + /** + * A string giving the source term that is a back-translation of the target in a form best + * suited for end-user display. + */ + displayText: string; + /** + * An integer representing the number of examples that are available for this translation pair. + * Actual examples must be retrieved with a separate call to lookup examples. The number is mostly + * intended to facilitate display in a UX. For example, a user interface may add a hyperlink + * to the back-translation if the number of examples is greater than zero and show the back-translation + * as plain text if there are no examples. Note that the actual number of examples returned + * by a call to lookup examples may be less than numExamples, because additional filtering may be + * applied on the fly to remove "bad" examples. + */ + numExamples: number; + /** + * An integer representing the frequency of this translation pair in the data. The main purpose of this + * field is to provide a user interface with a means to sort back-translations so the most frequent terms are first. + */ + frequencyCount: number; +} + +/** Dictionary Example element */ +export interface DictionaryExampleItemOutput { + /** + * A string giving the normalized form of the source term. Generally, this should be identical + * to the value of the Text field at the matching list index in the body of the request. + */ + normalizedSource: string; + /** + * A string giving the normalized form of the target term. Generally, this should be identical + * to the value of the Translation field at the matching list index in the body of the request. + */ + normalizedTarget: string; + /** A list of examples for the (source term, target term) pair. */ + examples: Array; +} + +/** Dictionary Example */ +export interface DictionaryExampleOutput { + /** + * The string to concatenate before the value of sourceTerm to form a complete example. + * Do not add a space character, since it is already there when it should be. + * This value may be an empty string. + */ + sourcePrefix: string; + /** + * A string equal to the actual term looked up. The string is added with sourcePrefix + * and sourceSuffix to form the complete example. Its value is separated so it can be + * marked in a user interface, e.g., by bolding it. + */ + sourceTerm: string; + /** + * The string to concatenate after the value of sourceTerm to form a complete example. + * Do not add a space character, since it is already there when it should be. + * This value may be an empty string. + */ + sourceSuffix: string; + /** A string similar to sourcePrefix but for the target. */ + targetPrefix: string; + /** A string similar to sourceTerm but for the target. */ + targetTerm: string; + /** A string similar to sourceSuffix but for the target. */ + targetSuffix: string; +} + +/** Language Directionality */ +export type LanguageDirectionalityOutput = "ltr" | "rtl"; diff --git a/sdk/translation/ai-translation-text-rest/src/generated/parameters.ts b/sdk/translation/ai-translation-text-rest/generated/parameters.ts similarity index 97% rename from sdk/translation/ai-translation-text-rest/src/generated/parameters.ts rename to sdk/translation/ai-translation-text-rest/generated/parameters.ts index e0adc1680033..963f63fc1276 100644 --- a/sdk/translation/ai-translation-text-rest/src/generated/parameters.ts +++ b/sdk/translation/ai-translation-text-rest/generated/parameters.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. + import { RawHttpHeadersInput } from "@azure/core-rest-pipeline"; import { RequestParameters } from "@azure-rest/core-client"; import { @@ -83,6 +84,8 @@ export interface TranslateQueryParamProperties { /** * Defines whether the text being translated is plain text or HTML text. Any HTML needs to be a well-formed, * complete element. Possible values are: plain (default) or html. + * + * Possible values: "Plain", "Html" */ textType?: TextType; /** @@ -222,9 +225,9 @@ export interface FindSentenceBoundariesHeaderParam { export type FindSentenceBoundariesParameters = FindSentenceBoundariesQueryParam & - FindSentenceBoundariesHeaderParam & - FindSentenceBoundariesBodyParam & - RequestParameters; + FindSentenceBoundariesHeaderParam & + FindSentenceBoundariesBodyParam & + RequestParameters; export interface LookupDictionaryEntriesHeaders { /** A client-generated GUID to uniquely identify the request. */ @@ -259,9 +262,9 @@ export interface LookupDictionaryEntriesHeaderParam { export type LookupDictionaryEntriesParameters = LookupDictionaryEntriesQueryParam & - LookupDictionaryEntriesHeaderParam & - LookupDictionaryEntriesBodyParam & - RequestParameters; + LookupDictionaryEntriesHeaderParam & + LookupDictionaryEntriesBodyParam & + RequestParameters; export interface LookupDictionaryExamplesHeaders { /** A client-generated GUID to uniquely identify the request. */ @@ -296,6 +299,6 @@ export interface LookupDictionaryExamplesHeaderParam { export type LookupDictionaryExamplesParameters = LookupDictionaryExamplesQueryParam & - LookupDictionaryExamplesHeaderParam & - LookupDictionaryExamplesBodyParam & - RequestParameters; + LookupDictionaryExamplesHeaderParam & + LookupDictionaryExamplesBodyParam & + RequestParameters; diff --git a/sdk/translation/ai-translation-text-rest/src/generated/responses.ts b/sdk/translation/ai-translation-text-rest/generated/responses.ts similarity index 99% rename from sdk/translation/ai-translation-text-rest/src/generated/responses.ts rename to sdk/translation/ai-translation-text-rest/generated/responses.ts index 0a04a06cbd8f..3b8074c67097 100644 --- a/sdk/translation/ai-translation-text-rest/src/generated/responses.ts +++ b/sdk/translation/ai-translation-text-rest/generated/responses.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. + import { RawHttpHeaders } from "@azure/core-rest-pipeline"; import { HttpResponse } from "@azure-rest/core-client"; import { diff --git a/sdk/translation/ai-translation-text-rest/src/generated/serializeHelper.ts b/sdk/translation/ai-translation-text-rest/generated/serializeHelper.ts similarity index 99% rename from sdk/translation/ai-translation-text-rest/src/generated/serializeHelper.ts rename to sdk/translation/ai-translation-text-rest/generated/serializeHelper.ts index a19f11ca222a..07805c2217f9 100644 --- a/sdk/translation/ai-translation-text-rest/src/generated/serializeHelper.ts +++ b/sdk/translation/ai-translation-text-rest/generated/serializeHelper.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. + export function buildMultiCollection( items: string[], parameterName: string, diff --git a/sdk/translation/ai-translation-text-rest/src/generated/textTranslationClient.ts b/sdk/translation/ai-translation-text-rest/generated/textTranslationClient.ts similarity index 59% rename from sdk/translation/ai-translation-text-rest/src/generated/textTranslationClient.ts rename to sdk/translation/ai-translation-text-rest/generated/textTranslationClient.ts index 6ab4169b8d8b..dfa777d7f17b 100644 --- a/sdk/translation/ai-translation-text-rest/src/generated/textTranslationClient.ts +++ b/sdk/translation/ai-translation-text-rest/generated/textTranslationClient.ts @@ -5,6 +5,13 @@ import { getClient, ClientOptions } from "@azure-rest/core-client"; import { logger } from "./logger.js"; import { TextTranslationClient } from "./clientDefinitions.js"; + +/** The optional parameters for the client */ +export interface TextTranslationClientOptions extends ClientOptions { + /** The api version option of the client */ + apiVersion?: string; +} + /** * Initialize a new instance of `TextTranslationClient` * @param endpointParam - Supported Text Translation endpoints (protocol and hostname, for example: @@ -13,10 +20,9 @@ import { TextTranslationClient } from "./clientDefinitions.js"; */ export default function createClient( endpointParam: string, - options: ClientOptions = {}, + { apiVersion = "3.0", ...options }: TextTranslationClientOptions = {}, ): TextTranslationClient { const endpointUrl = options.endpoint ?? options.baseUrl ?? `${endpointParam}`; - options.apiVersion = options.apiVersion ?? "3.0"; const userAgentInfo = `azsdk-js-ai-translation-text-rest/1.0.0-beta.1`; const userAgentPrefix = options.userAgentOptions && options.userAgentOptions.userAgentPrefix @@ -31,8 +37,23 @@ export default function createClient( logger: options.loggingOptions?.logger ?? logger.info, }, }; - const client = getClient(endpointUrl, options) as TextTranslationClient; + client.pipeline.removePolicy({ name: "ApiVersionPolicy" }); + client.pipeline.addPolicy({ + name: "ClientApiVersionPolicy", + sendRequest: (req, next) => { + // Use the apiVersion defined in request url directly + // Append one if there is no apiVersion and we have one at client options + const url = new URL(req.url); + if (!url.searchParams.get("api-version") && apiVersion) { + req.url = `${req.url}${Array.from(url.searchParams.keys()).length > 0 ? "&" : "?" + }api-version=${apiVersion}`; + } + + return next(req); + }, + }); + return client; } diff --git a/sdk/translation/ai-translation-text-rest/review/ai-translation-text.api.md b/sdk/translation/ai-translation-text-rest/review/ai-translation-text.api.md index 0ca842f5da27..f8d95bbb2a55 100644 --- a/sdk/translation/ai-translation-text-rest/review/ai-translation-text.api.md +++ b/sdk/translation/ai-translation-text-rest/review/ai-translation-text.api.md @@ -4,14 +4,14 @@ ```ts -import { Client } from '@azure-rest/core-client'; +import type { Client } from '@azure-rest/core-client'; import type { ClientOptions } from '@azure-rest/core-client'; -import { HttpResponse } from '@azure-rest/core-client'; +import type { HttpResponse } from '@azure-rest/core-client'; import type { KeyCredential } from '@azure/core-auth'; -import { RawHttpHeaders } from '@azure/core-rest-pipeline'; -import { RawHttpHeadersInput } from '@azure/core-rest-pipeline'; -import { RequestParameters } from '@azure-rest/core-client'; -import { StreamableMethod } from '@azure-rest/core-client'; +import type { RawHttpHeaders } from '@azure/core-rest-pipeline'; +import type { RawHttpHeadersInput } from '@azure/core-rest-pipeline'; +import type { RequestParameters } from '@azure-rest/core-client'; +import type { StreamableMethod } from '@azure-rest/core-client'; import type { TokenCredential } from '@azure/core-auth'; // @public @@ -448,7 +448,7 @@ export type TextTranslationClient = Client & { }; // @public -export type TextType = string | "Plain" | "Html"; +export type TextType = string; // @public (undocumented) export interface Translate { diff --git a/sdk/translation/ai-translation-text-rest/src/authentication.ts b/sdk/translation/ai-translation-text-rest/src/authenticationCustomized.ts similarity index 100% rename from sdk/translation/ai-translation-text-rest/src/authentication.ts rename to sdk/translation/ai-translation-text-rest/src/authenticationCustomized.ts diff --git a/sdk/translation/ai-translation-text-rest/src/clientDefinitions.ts b/sdk/translation/ai-translation-text-rest/src/clientDefinitions.ts new file mode 100644 index 000000000000..46589011ce2d --- /dev/null +++ b/sdk/translation/ai-translation-text-rest/src/clientDefinitions.ts @@ -0,0 +1,89 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import type { + GetSupportedLanguagesParameters, + TranslateParameters, + TransliterateParameters, + FindSentenceBoundariesParameters, + LookupDictionaryEntriesParameters, + LookupDictionaryExamplesParameters, +} from "./parameters.js"; +import type { + GetSupportedLanguages200Response, + GetSupportedLanguagesDefaultResponse, + Translate200Response, + TranslateDefaultResponse, + Transliterate200Response, + TransliterateDefaultResponse, + FindSentenceBoundaries200Response, + FindSentenceBoundariesDefaultResponse, + LookupDictionaryEntries200Response, + LookupDictionaryEntriesDefaultResponse, + LookupDictionaryExamples200Response, + LookupDictionaryExamplesDefaultResponse, +} from "./responses.js"; +import type { Client, StreamableMethod } from "@azure-rest/core-client"; + +export interface GetSupportedLanguages { + /** Gets the set of languages currently supported by other operations of the Translator. */ + get( + options?: GetSupportedLanguagesParameters, + ): StreamableMethod; +} + +export interface Translate { + /** Translate Text */ + post( + options: TranslateParameters, + ): StreamableMethod; +} + +export interface Transliterate { + /** Transliterate Text */ + post( + options: TransliterateParameters, + ): StreamableMethod; +} + +export interface FindSentenceBoundaries { + /** Find Sentence Boundaries */ + post( + options: FindSentenceBoundariesParameters, + ): StreamableMethod; +} + +export interface LookupDictionaryEntries { + /** Lookup Dictionary Entries */ + post( + options: LookupDictionaryEntriesParameters, + ): StreamableMethod; +} + +export interface LookupDictionaryExamples { + /** Lookup Dictionary Examples */ + post( + options: LookupDictionaryExamplesParameters, + ): StreamableMethod< + LookupDictionaryExamples200Response | LookupDictionaryExamplesDefaultResponse + >; +} + +export interface Routes { + /** Resource for '/languages' has methods for the following verbs: get */ + (path: "/languages"): GetSupportedLanguages; + /** Resource for '/translate' has methods for the following verbs: post */ + (path: "/translate"): Translate; + /** Resource for '/transliterate' has methods for the following verbs: post */ + (path: "/transliterate"): Transliterate; + /** Resource for '/breaksentence' has methods for the following verbs: post */ + (path: "/breaksentence"): FindSentenceBoundaries; + /** Resource for '/dictionary/lookup' has methods for the following verbs: post */ + (path: "/dictionary/lookup"): LookupDictionaryEntries; + /** Resource for '/dictionary/examples' has methods for the following verbs: post */ + (path: "/dictionary/examples"): LookupDictionaryExamples; +} + +export type TextTranslationClient = Client & { + path: Routes; +}; diff --git a/sdk/translation/ai-translation-text-rest/src/customClient.ts b/sdk/translation/ai-translation-text-rest/src/customClient.ts index fa1db50f5561..761de5b7d427 100644 --- a/sdk/translation/ai-translation-text-rest/src/customClient.ts +++ b/sdk/translation/ai-translation-text-rest/src/customClient.ts @@ -3,16 +3,19 @@ import type { ClientOptions } from "@azure-rest/core-client"; import { getClient } from "@azure-rest/core-client"; -import { logger } from "./generated/logger.js"; +import { logger } from "./logger.js"; import * as coreRestPipeline from "@azure/core-rest-pipeline"; -import type { TextTranslationClient } from "./generated/clientDefinitions.js"; -import type { TranslatorCredential, TranslatorTokenCredential } from "./authentication.js"; +import type { TextTranslationClient } from "./clientDefinitions.js"; +import type { + TranslatorCredential, + TranslatorTokenCredential, +} from "./authenticationCustomized.js"; import { DEFAULT_SCOPE, TranslatorAuthenticationPolicy, TranslatorAzureKeyAuthenticationPolicy, TranslatorTokenCredentialAuthenticationPolicy, -} from "./authentication.js"; +} from "./authenticationCustomized.js"; import type { AzureKeyCredential, KeyCredential, TokenCredential } from "@azure/core-auth"; const DEFAULT_ENPOINT = "https://api.cognitive.microsofttranslator.com"; diff --git a/sdk/translation/ai-translation-text-rest/src/index.ts b/sdk/translation/ai-translation-text-rest/src/index.ts index 3a414add33d4..e5d83e965eae 100644 --- a/sdk/translation/ai-translation-text-rest/src/index.ts +++ b/sdk/translation/ai-translation-text-rest/src/index.ts @@ -4,14 +4,14 @@ import TextTranslationClient from "./customClient.js"; export * from "./customClient.js"; -export * from "./generated/parameters.js"; -export * from "./generated/responses.js"; -export * from "./generated/clientDefinitions.js"; -export * from "./generated/isUnexpected.js"; -export * from "./generated/models.js"; -export * from "./generated/outputModels.js"; -export * from "./generated/serializeHelper.js"; -export { TranslatorCredential, TranslatorTokenCredential } from "./authentication.js"; +export * from "./parameters.js"; +export * from "./responses.js"; +export * from "./clientDefinitions.js"; +export * from "./isUnexpected.js"; +export * from "./models.js"; +export * from "./outputModels.js"; +export * from "./serializeHelper.js"; +export { TranslatorCredential, TranslatorTokenCredential } from "./authenticationCustomized.js"; // eslint-disable-next-line @azure/azure-sdk/ts-modules-only-named export default TextTranslationClient; diff --git a/sdk/translation/ai-translation-text-rest/src/isUnexpected.ts b/sdk/translation/ai-translation-text-rest/src/isUnexpected.ts new file mode 100644 index 000000000000..230aba3bf188 --- /dev/null +++ b/sdk/translation/ai-translation-text-rest/src/isUnexpected.ts @@ -0,0 +1,141 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import type { + GetSupportedLanguages200Response, + GetSupportedLanguagesDefaultResponse, + Translate200Response, + TranslateDefaultResponse, + Transliterate200Response, + TransliterateDefaultResponse, + FindSentenceBoundaries200Response, + FindSentenceBoundariesDefaultResponse, + LookupDictionaryEntries200Response, + LookupDictionaryEntriesDefaultResponse, + LookupDictionaryExamples200Response, + LookupDictionaryExamplesDefaultResponse, +} from "./responses.js"; + +const responseMap: Record = { + "GET /languages": ["200"], + "POST /translate": ["200"], + "POST /transliterate": ["200"], + "POST /breaksentence": ["200"], + "POST /dictionary/lookup": ["200"], + "POST /dictionary/examples": ["200"], +}; + +export function isUnexpected( + response: GetSupportedLanguages200Response | GetSupportedLanguagesDefaultResponse, +): response is GetSupportedLanguagesDefaultResponse; +export function isUnexpected( + response: Translate200Response | TranslateDefaultResponse, +): response is TranslateDefaultResponse; +export function isUnexpected( + response: Transliterate200Response | TransliterateDefaultResponse, +): response is TransliterateDefaultResponse; +export function isUnexpected( + response: FindSentenceBoundaries200Response | FindSentenceBoundariesDefaultResponse, +): response is FindSentenceBoundariesDefaultResponse; +export function isUnexpected( + response: LookupDictionaryEntries200Response | LookupDictionaryEntriesDefaultResponse, +): response is LookupDictionaryEntriesDefaultResponse; +export function isUnexpected( + response: LookupDictionaryExamples200Response | LookupDictionaryExamplesDefaultResponse, +): response is LookupDictionaryExamplesDefaultResponse; +export function isUnexpected( + response: + | GetSupportedLanguages200Response + | GetSupportedLanguagesDefaultResponse + | Translate200Response + | TranslateDefaultResponse + | Transliterate200Response + | TransliterateDefaultResponse + | FindSentenceBoundaries200Response + | FindSentenceBoundariesDefaultResponse + | LookupDictionaryEntries200Response + | LookupDictionaryEntriesDefaultResponse + | LookupDictionaryExamples200Response + | LookupDictionaryExamplesDefaultResponse, +): response is + | GetSupportedLanguagesDefaultResponse + | TranslateDefaultResponse + | TransliterateDefaultResponse + | FindSentenceBoundariesDefaultResponse + | LookupDictionaryEntriesDefaultResponse + | LookupDictionaryExamplesDefaultResponse { + const lroOriginal = response.headers["x-ms-original-url"]; + const url = new URL(lroOriginal ?? response.request.url); + const method = response.request.method; + let pathDetails = responseMap[`${method} ${url.pathname}`]; + if (!pathDetails) { + pathDetails = getParametrizedPathSuccess(method, url.pathname); + } + return !pathDetails.includes(response.status); +} + +function getParametrizedPathSuccess(method: string, path: string): string[] { + const pathParts = path.split("/"); + + // Traverse list to match the longest candidate + // matchedLen: the length of candidate path + // matchedValue: the matched status code array + let matchedLen = -1, + matchedValue: string[] = []; + + // Iterate the responseMap to find a match + for (const [key, value] of Object.entries(responseMap)) { + // Extracting the path from the map key which is in format + // GET /path/foo + if (!key.startsWith(method)) { + continue; + } + const candidatePath = getPathFromMapKey(key); + // Get each part of the url path + const candidateParts = candidatePath.split("/"); + + // track if we have found a match to return the values found. + let found = true; + for (let i = candidateParts.length - 1, j = pathParts.length - 1; i >= 1 && j >= 1; i--, j--) { + if (candidateParts[i]?.startsWith("{") && candidateParts[i]?.indexOf("}") !== -1) { + const start = candidateParts[i]!.indexOf("}") + 1, + end = candidateParts[i]?.length; + // If the current part of the candidate is a "template" part + // Try to use the suffix of pattern to match the path + // {guid} ==> $ + // {guid}:export ==> :export$ + const isMatched = new RegExp(`${candidateParts[i]?.slice(start, end)}`).test( + pathParts[j] || "", + ); + + if (!isMatched) { + found = false; + break; + } + continue; + } + + // If the candidate part is not a template and + // the parts don't match mark the candidate as not found + // to move on with the next candidate path. + if (candidateParts[i] !== pathParts[j]) { + found = false; + break; + } + } + + // We finished evaluating the current candidate parts + // Update the matched value if and only if we found the longer pattern + if (found && candidatePath.length > matchedLen) { + matchedLen = candidatePath.length; + matchedValue = value; + } + } + + return matchedValue; +} + +function getPathFromMapKey(mapKey: string): string { + const pathStart = mapKey.indexOf("/"); + return mapKey.slice(pathStart); +} diff --git a/sdk/translation/ai-translation-text-rest/src/generated/logger.ts b/sdk/translation/ai-translation-text-rest/src/logger.ts similarity index 100% rename from sdk/translation/ai-translation-text-rest/src/generated/logger.ts rename to sdk/translation/ai-translation-text-rest/src/logger.ts diff --git a/sdk/translation/ai-translation-text-rest/src/models.ts b/sdk/translation/ai-translation-text-rest/src/models.ts new file mode 100644 index 000000000000..1596d852b98b --- /dev/null +++ b/sdk/translation/ai-translation-text-rest/src/models.ts @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** Element containing the text for translation. */ +export interface InputTextItem { + /** Text to translate. */ + text: string; +} + +/** Element containing the text with translation. */ +export interface DictionaryExampleTextItem extends InputTextItem { + /** + * A string specifying the translated text previously returned by the Dictionary lookup operation. + * This should be the value from the normalizedTarget field in the translations list of the Dictionary + * lookup response. The service will return examples for the specific source-target word-pair. + */ + translation: string; +} + +/** Alias for TextType */ +export type TextType = string; +/** Translator profanity actions */ +export type ProfanityAction = "NoAction" | "Marked" | "Deleted"; +/** Translator profanity markers */ +export type ProfanityMarker = "Asterisk" | "Tag"; diff --git a/sdk/translation/ai-translation-text-rest/src/generated/outputModels.ts b/sdk/translation/ai-translation-text-rest/src/outputModels.ts similarity index 100% rename from sdk/translation/ai-translation-text-rest/src/generated/outputModels.ts rename to sdk/translation/ai-translation-text-rest/src/outputModels.ts diff --git a/sdk/translation/ai-translation-text-rest/src/parameters.ts b/sdk/translation/ai-translation-text-rest/src/parameters.ts new file mode 100644 index 000000000000..5b34749977dc --- /dev/null +++ b/sdk/translation/ai-translation-text-rest/src/parameters.ts @@ -0,0 +1,300 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import type { RawHttpHeadersInput } from "@azure/core-rest-pipeline"; +import type { RequestParameters } from "@azure-rest/core-client"; +import type { + TextType, + ProfanityAction, + ProfanityMarker, + InputTextItem, + DictionaryExampleTextItem, +} from "./models.js"; + +export interface GetSupportedLanguagesHeaders { + /** A client-generated GUID to uniquely identify the request. */ + "X-ClientTraceId"?: string; + /** + * The language to use for user interface strings. Some of the fields in the response are names of languages or + * names of regions. Use this parameter to define the language in which these names are returned. + * The language is specified by providing a well-formed BCP 47 language tag. For instance, use the value `fr` + * to request names in French or use the value `zh-Hant` to request names in Chinese Traditional. + * Names are provided in the English language when a target language is not specified or when localization + * is not available. + */ + "Accept-Language"?: string; + /** + * Passing the value of the ETag response header in an If-None-Match field will allow the service to optimize the response. + * If the resource has not been modified, the service will return status code 304 and an empty response body. + */ + "If-None-Match"?: string; +} + +export interface GetSupportedLanguagesQueryParamProperties { + /** + * A comma-separated list of names defining the group of languages to return. + * Allowed group names are: `translation`, `transliteration` and `dictionary`. + * If no scope is given, then all groups are returned, which is equivalent to passing + * `scope=translation,transliteration,dictionary`. To decide which set of supported languages + * is appropriate for your scenario, see the description of the [response object](#response-body). + */ + scope?: string; +} + +export interface GetSupportedLanguagesQueryParam { + queryParameters?: GetSupportedLanguagesQueryParamProperties; +} + +export interface GetSupportedLanguagesHeaderParam { + headers?: RawHttpHeadersInput & GetSupportedLanguagesHeaders; +} + +export type GetSupportedLanguagesParameters = GetSupportedLanguagesQueryParam & + GetSupportedLanguagesHeaderParam & + RequestParameters; + +export interface TranslateHeaders { + /** A client-generated GUID to uniquely identify the request. */ + "X-ClientTraceId"?: string; +} + +export interface TranslateBodyParam { + /** Defines the content of the request */ + body: Array; +} + +export interface TranslateQueryParamProperties { + /** + * Specifies the language of the output text. The target language must be one of the supported languages included + * in the translation scope. For example, use to=de to translate to German. + * It's possible to translate to multiple languages simultaneously by repeating the parameter in the query string. + * For example, use to=de&to=it to translate to German and Italian. This parameter needs to be formatted as multi collection, we provide buildMultiCollection from serializeHelper.ts to help, you will probably need to set skipUrlEncoding as true when sending the request + */ + to: string; + /** + * Specifies the language of the input text. Find which languages are available to translate from by + * looking up supported languages using the translation scope. If the from parameter isn't specified, + * automatic language detection is applied to determine the source language. + * + * You must use the from parameter rather than autodetection when using the dynamic dictionary feature. + * Note: the dynamic dictionary feature is case-sensitive. + */ + from?: string; + /** + * Defines whether the text being translated is plain text or HTML text. Any HTML needs to be a well-formed, + * complete element. Possible values are: plain (default) or html. + * + * Possible values: "Plain", "Html" + */ + textType?: TextType; + /** + * A string specifying the category (domain) of the translation. This parameter is used to get translations + * from a customized system built with Custom Translator. Add the Category ID from your Custom Translator + * project details to this parameter to use your deployed customized system. Default value is: general. + */ + category?: string; + /** + * Specifies how profanities should be treated in translations. + * Possible values are: NoAction (default), Marked or Deleted. + */ + profanityAction?: ProfanityAction; + /** + * Specifies how profanities should be marked in translations. + * Possible values are: Asterisk (default) or Tag. + */ + profanityMarker?: ProfanityMarker; + /** + * Specifies whether to include alignment projection from source text to translated text. + * Possible values are: true or false (default). + */ + includeAlignment?: boolean; + /** + * Specifies whether to include sentence boundaries for the input text and the translated text. + * Possible values are: true or false (default). + */ + includeSentenceLength?: boolean; + /** + * Specifies a fallback language if the language of the input text can't be identified. + * Language autodetection is applied when the from parameter is omitted. If detection fails, + * the suggestedFrom language will be assumed. + */ + suggestedFrom?: string; + /** Specifies the script of the input text. */ + fromScript?: string; + /** Specifies the script of the translated text. */ + toScript?: string; + /** + * Specifies that the service is allowed to fall back to a general system when a custom system doesn't exist. + * Possible values are: true (default) or false. + * + * allowFallback=false specifies that the translation should only use systems trained for the category specified + * by the request. If a translation for language X to language Y requires chaining through a pivot language E, + * then all the systems in the chain (X → E and E → Y) will need to be custom and have the same category. + * If no system is found with the specific category, the request will return a 400 status code. allowFallback=true + * specifies that the service is allowed to fall back to a general system when a custom system doesn't exist. + */ + allowFallback?: boolean; +} + +export interface TranslateQueryParam { + queryParameters: TranslateQueryParamProperties; +} + +export interface TranslateHeaderParam { + headers?: RawHttpHeadersInput & TranslateHeaders; +} + +export type TranslateParameters = TranslateQueryParam & + TranslateHeaderParam & + TranslateBodyParam & + RequestParameters; + +export interface TransliterateHeaders { + /** A client-generated GUID to uniquely identify the request. */ + "X-ClientTraceId"?: string; +} + +export interface TransliterateBodyParam { + /** Defines the content of the request */ + body: Array; +} + +export interface TransliterateQueryParamProperties { + /** + * Specifies the language of the text to convert from one script to another. + * Possible languages are listed in the transliteration scope obtained by querying the service + * for its supported languages. + */ + language: string; + /** + * Specifies the script used by the input text. Look up supported languages using the transliteration scope, + * to find input scripts available for the selected language. + */ + fromScript: string; + /** + * Specifies the output script. Look up supported languages using the transliteration scope, to find output + * scripts available for the selected combination of input language and input script. + */ + toScript: string; +} + +export interface TransliterateQueryParam { + queryParameters: TransliterateQueryParamProperties; +} + +export interface TransliterateHeaderParam { + headers?: RawHttpHeadersInput & TransliterateHeaders; +} + +export type TransliterateParameters = TransliterateQueryParam & + TransliterateHeaderParam & + TransliterateBodyParam & + RequestParameters; + +export interface FindSentenceBoundariesHeaders { + /** A client-generated GUID to uniquely identify the request. */ + "X-ClientTraceId"?: string; +} + +export interface FindSentenceBoundariesBodyParam { + /** Defines the content of the request */ + body: Array; +} + +export interface FindSentenceBoundariesQueryParamProperties { + /** + * Language tag identifying the language of the input text. + * If a code isn't specified, automatic language detection will be applied. + */ + language?: string; + /** + * Script tag identifying the script used by the input text. + * If a script isn't specified, the default script of the language will be assumed. + */ + script?: string; +} + +export interface FindSentenceBoundariesQueryParam { + queryParameters?: FindSentenceBoundariesQueryParamProperties; +} + +export interface FindSentenceBoundariesHeaderParam { + headers?: RawHttpHeadersInput & FindSentenceBoundariesHeaders; +} + +export type FindSentenceBoundariesParameters = FindSentenceBoundariesQueryParam & + FindSentenceBoundariesHeaderParam & + FindSentenceBoundariesBodyParam & + RequestParameters; + +export interface LookupDictionaryEntriesHeaders { + /** A client-generated GUID to uniquely identify the request. */ + "X-ClientTraceId"?: string; +} + +export interface LookupDictionaryEntriesBodyParam { + /** Defines the content of the request */ + body: Array; +} + +export interface LookupDictionaryEntriesQueryParamProperties { + /** + * Specifies the language of the input text. + * The source language must be one of the supported languages included in the dictionary scope. + */ + from: string; + /** + * Specifies the language of the output text. + * The target language must be one of the supported languages included in the dictionary scope. + */ + to: string; +} + +export interface LookupDictionaryEntriesQueryParam { + queryParameters: LookupDictionaryEntriesQueryParamProperties; +} + +export interface LookupDictionaryEntriesHeaderParam { + headers?: RawHttpHeadersInput & LookupDictionaryEntriesHeaders; +} + +export type LookupDictionaryEntriesParameters = LookupDictionaryEntriesQueryParam & + LookupDictionaryEntriesHeaderParam & + LookupDictionaryEntriesBodyParam & + RequestParameters; + +export interface LookupDictionaryExamplesHeaders { + /** A client-generated GUID to uniquely identify the request. */ + "X-ClientTraceId"?: string; +} + +export interface LookupDictionaryExamplesBodyParam { + /** Defines the content of the request */ + body: Array; +} + +export interface LookupDictionaryExamplesQueryParamProperties { + /** + * Specifies the language of the input text. + * The source language must be one of the supported languages included in the dictionary scope. + */ + from: string; + /** + * Specifies the language of the output text. + * The target language must be one of the supported languages included in the dictionary scope. + */ + to: string; +} + +export interface LookupDictionaryExamplesQueryParam { + queryParameters: LookupDictionaryExamplesQueryParamProperties; +} + +export interface LookupDictionaryExamplesHeaderParam { + headers?: RawHttpHeadersInput & LookupDictionaryExamplesHeaders; +} + +export type LookupDictionaryExamplesParameters = LookupDictionaryExamplesQueryParam & + LookupDictionaryExamplesHeaderParam & + LookupDictionaryExamplesBodyParam & + RequestParameters; diff --git a/sdk/translation/ai-translation-text-rest/src/responses.ts b/sdk/translation/ai-translation-text-rest/src/responses.ts new file mode 100644 index 000000000000..63317a3b0ca4 --- /dev/null +++ b/sdk/translation/ai-translation-text-rest/src/responses.ts @@ -0,0 +1,172 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import type { RawHttpHeaders } from "@azure/core-rest-pipeline"; +import type { HttpResponse } from "@azure-rest/core-client"; +import type { + GetSupportedLanguagesResultOutput, + ErrorResponseOutput, + TranslatedTextItemOutput, + TransliteratedTextOutput, + BreakSentenceItemOutput, + DictionaryLookupItemOutput, + DictionaryExampleItemOutput, +} from "./outputModels.js"; + +export interface GetSupportedLanguages200Headers { + /** Value generated by the service to identify the request. It is used for troubleshooting purposes. */ + "x-requestid": string; + /** + * Current value of the entity tag for the requested groups of supported languages. + * To make subsequent requests more efficient, the client may send the `ETag` value in an + * `If-None-Match` header field. + */ + etag: string; +} + +/** The request has succeeded. */ +export interface GetSupportedLanguages200Response extends HttpResponse { + status: "200"; + body: GetSupportedLanguagesResultOutput; + headers: RawHttpHeaders & GetSupportedLanguages200Headers; +} + +export interface GetSupportedLanguagesDefaultHeaders { + /** Value generated by the service to identify the request. It is used for troubleshooting purposes. */ + "x-requestid": string; +} + +export interface GetSupportedLanguagesDefaultResponse extends HttpResponse { + status: string; + body: ErrorResponseOutput; + headers: RawHttpHeaders & GetSupportedLanguagesDefaultHeaders; +} + +export interface Translate200Headers { + /** Value generated by the service to identify the request. It is used for troubleshooting purposes. */ + "x-requestid": string; + /** + * Specifies the system type that was used for translation for each 'to' language requested for translation. + * The value is a comma-separated list of strings. Each string indicates a type: + * + * * Custom - Request includes a custom system and at least one custom system was used during translation. + * * Team - All other requests + */ + "x-mt-system": string; + /** + * Specifies consumption (the number of characters for which the user will be charged) for the translation + * job request. For example, if the word "Hello" is translated from English (en) to French (fr), + * this field will return the value '5'. + */ + "x-metered-usage": number; +} + +/** Response for the translation API. */ +export interface Translate200Response extends HttpResponse { + status: "200"; + body: Array; + headers: RawHttpHeaders & Translate200Headers; +} + +export interface TranslateDefaultHeaders { + /** Value generated by the service to identify the request. It is used for troubleshooting purposes. */ + "x-requestid": string; +} + +export interface TranslateDefaultResponse extends HttpResponse { + status: string; + body: ErrorResponseOutput; + headers: RawHttpHeaders & TranslateDefaultHeaders; +} + +export interface Transliterate200Headers { + /** Value generated by the service to identify the request. It is used for troubleshooting purposes. */ + "x-requestid": string; +} + +/** Response for the transliteration API. */ +export interface Transliterate200Response extends HttpResponse { + status: "200"; + body: Array; + headers: RawHttpHeaders & Transliterate200Headers; +} + +export interface TransliterateDefaultHeaders { + /** Value generated by the service to identify the request. It is used for troubleshooting purposes. */ + "x-requestid": string; +} + +export interface TransliterateDefaultResponse extends HttpResponse { + status: string; + body: ErrorResponseOutput; + headers: RawHttpHeaders & TransliterateDefaultHeaders; +} + +export interface FindSentenceBoundaries200Headers { + /** Value generated by the service to identify the request. It is used for troubleshooting purposes. */ + "x-requestid": string; +} + +/** Response for the Break SEntence API. */ +export interface FindSentenceBoundaries200Response extends HttpResponse { + status: "200"; + body: Array; + headers: RawHttpHeaders & FindSentenceBoundaries200Headers; +} + +export interface FindSentenceBoundariesDefaultHeaders { + /** Value generated by the service to identify the request. It is used for troubleshooting purposes. */ + "x-requestid": string; +} + +export interface FindSentenceBoundariesDefaultResponse extends HttpResponse { + status: string; + body: ErrorResponseOutput; + headers: RawHttpHeaders & FindSentenceBoundariesDefaultHeaders; +} + +export interface LookupDictionaryEntries200Headers { + /** Value generated by the service to identify the request. It is used for troubleshooting purposes. */ + "x-requestid": string; +} + +/** Response for the dictionary lookup API. */ +export interface LookupDictionaryEntries200Response extends HttpResponse { + status: "200"; + body: Array; + headers: RawHttpHeaders & LookupDictionaryEntries200Headers; +} + +export interface LookupDictionaryEntriesDefaultHeaders { + /** Value generated by the service to identify the request. It is used for troubleshooting purposes. */ + "x-requestid": string; +} + +export interface LookupDictionaryEntriesDefaultResponse extends HttpResponse { + status: string; + body: ErrorResponseOutput; + headers: RawHttpHeaders & LookupDictionaryEntriesDefaultHeaders; +} + +export interface LookupDictionaryExamples200Headers { + /** Value generated by the service to identify the request. It is used for troubleshooting purposes. */ + "x-requestid": string; +} + +/** Response for the dictionary examples API. */ +export interface LookupDictionaryExamples200Response extends HttpResponse { + status: "200"; + body: Array; + headers: RawHttpHeaders & LookupDictionaryExamples200Headers; +} + +export interface LookupDictionaryExamplesDefaultHeaders { + /** Value generated by the service to identify the request. It is used for troubleshooting purposes. */ + "x-requestid": string; +} + +export interface LookupDictionaryExamplesDefaultResponse extends HttpResponse { + status: string; + body: ErrorResponseOutput; + headers: RawHttpHeaders & LookupDictionaryExamplesDefaultHeaders; +} diff --git a/sdk/translation/ai-translation-text-rest/src/serializeHelper.ts b/sdk/translation/ai-translation-text-rest/src/serializeHelper.ts new file mode 100644 index 000000000000..593ee9fcc7ba --- /dev/null +++ b/sdk/translation/ai-translation-text-rest/src/serializeHelper.ts @@ -0,0 +1,13 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +export function buildMultiCollection(items: string[], parameterName: string): string { + return items + .map((item, index) => { + if (index === 0) { + return item; + } + return `${parameterName}=${item}`; + }) + .join("&"); +} diff --git a/sdk/translation/ai-translation-text-rest/src/textTranslationClient.ts b/sdk/translation/ai-translation-text-rest/src/textTranslationClient.ts new file mode 100644 index 000000000000..a5375b7a48d8 --- /dev/null +++ b/sdk/translation/ai-translation-text-rest/src/textTranslationClient.ts @@ -0,0 +1,60 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import type { ClientOptions } from "@azure-rest/core-client"; +import { getClient } from "@azure-rest/core-client"; +import { logger } from "./logger.js"; +import type { TextTranslationClient } from "./clientDefinitions.js"; + +/** The optional parameters for the client */ +export interface TextTranslationClientOptions extends ClientOptions { + /** The api version option of the client */ + apiVersion?: string; +} + +/** + * Initialize a new instance of `TextTranslationClient` + * @param endpointParam - Supported Text Translation endpoints (protocol and hostname, for example: + * https://api.cognitive.microsofttranslator.com). + * @param options - the parameter for all optional parameters + */ +export default function createClient( + endpointParam: string, + { apiVersion = "3.0", ...options }: TextTranslationClientOptions = {}, +): TextTranslationClient { + const endpointUrl = options.endpoint ?? options.baseUrl ?? `${endpointParam}`; + const userAgentInfo = `azsdk-js-ai-translation-text-rest/1.0.0-beta.1`; + const userAgentPrefix = + options.userAgentOptions && options.userAgentOptions.userAgentPrefix + ? `${options.userAgentOptions.userAgentPrefix} ${userAgentInfo}` + : `${userAgentInfo}`; + options = { + ...options, + userAgentOptions: { + userAgentPrefix, + }, + loggingOptions: { + logger: options.loggingOptions?.logger ?? logger.info, + }, + }; + const client = getClient(endpointUrl, options) as TextTranslationClient; + + client.pipeline.removePolicy({ name: "ApiVersionPolicy" }); + client.pipeline.addPolicy({ + name: "ClientApiVersionPolicy", + sendRequest: (req, next) => { + // Use the apiVersion defined in request url directly + // Append one if there is no apiVersion and we have one at client options + const url = new URL(req.url); + if (!url.searchParams.get("api-version") && apiVersion) { + req.url = `${req.url}${ + Array.from(url.searchParams.keys()).length > 0 ? "&" : "?" + }api-version=${apiVersion}`; + } + + return next(req); + }, + }); + + return client; +}