Skip to content

Commit e6bc67b

Browse files
committed
feat: Add extra parameters to translation requests
1 parent 475a495 commit e6bc67b

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77

88
## [Unreleased]
9+
### Added
10+
* Add possibility to add extra parameters to a translation request (both text and document).
11+
DeepL engineers use this to test features in the API before they are released. Library users
12+
who cannot update their DeepL library dependency could use these extra parameters to access
13+
features in the API that are released in the future.
914
### Security
1015
* Increase `follow-redirects` locked-version due to security
1116
[vulnerability in follow-redirects <1.15.5](https://github.com/advisories/GHSA-cxjh-pqwp-8mfp)

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ The following options are only used if `tagHandling` is `'xml'`:
192192
into sentences. Format and default are the same as for `splittingTags`.
193193
- `ignoreTags`: list of XML tags that containing content that should not be
194194
translated. Format and default are the same as for `splittingTags`.
195+
- `extraRequestParameters`: Extra body parameters to be passed along with the
196+
HTTP request. Only string values are permitted.
197+
For example: `{'param': 'value', 'param2': 'value2'}`
198+
195199

196200
### Translating documents
197201

@@ -246,6 +250,7 @@ directly:
246250
- `glossary`: same as in [Text translation options](#text-translation-options).
247251
- `filename`: if the input file is not provided as file path, this option is
248252
needed to specify the file extension.
253+
- `extraRequestParameters`: same as in [Text translation options](#text-translation-options).
249254

250255
### Glossaries
251256

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
GlossaryInfo,
3232
LanguageCode,
3333
NonRegionalLanguageCode,
34+
RequestParameters,
3435
SentenceSplittingMode,
3536
SourceGlossaryLanguageCode,
3637
SourceLanguageCode,
@@ -255,6 +256,7 @@ function buildURLSearchParams(
255256
targetLang: LanguageCode,
256257
formality: Formality | undefined,
257258
glossary: GlossaryId | GlossaryInfo | undefined,
259+
extraRequestParameters: RequestParameters | undefined,
258260
): URLSearchParams {
259261
targetLang = standardizeLanguageCode(targetLang);
260262
if (sourceLang !== null) {
@@ -305,6 +307,11 @@ function buildURLSearchParams(
305307
}
306308
searchParams.append('glossary_id', glossary);
307309
}
310+
if (extraRequestParameters !== undefined) {
311+
for (const paramName in extraRequestParameters) {
312+
searchParams.append(paramName, extraRequestParameters[paramName]);
313+
}
314+
}
308315
return searchParams;
309316
}
310317

@@ -567,6 +574,7 @@ export class Translator {
567574
targetLang,
568575
options?.formality,
569576
options?.glossary,
577+
options?.extraRequestParameters,
570578
);
571579
const singular = appendTextsAndReturnIsSingular(data, texts);
572580
validateAndAppendTextOptions(data, options);
@@ -913,6 +921,7 @@ export class Translator {
913921
targetLang,
914922
options?.formality,
915923
options?.glossary,
924+
options?.extraRequestParameters,
916925
);
917926
const { statusCode, content } = await this.httpClient.sendRequestWithBackoff<string>(
918927
'POST',

src/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ export interface TranslateTextOptions {
144144

145145
/** List of XML tags containing content that should not be translated. */
146146
ignoreTags?: TagList;
147+
148+
/** Extra parameters to be added to a text translation request. */
149+
extraRequestParameters?: RequestParameters;
147150
}
148151

149152
/**
@@ -158,6 +161,9 @@ export interface DocumentTranslateOptions {
158161

159162
/** Filename including extension, only required when translating documents as streams. */
160163
filename?: string;
164+
165+
/** Extra parameters to be added to a text translation request. */
166+
extraRequestParameters?: RequestParameters;
161167
}
162168

163169
/**
@@ -246,3 +252,9 @@ export type SourceGlossaryLanguageCode =
246252
* API accept case-insensitive language codes.
247253
*/
248254
export type TargetGlossaryLanguageCode = SourceGlossaryLanguageCode;
255+
256+
/**
257+
* Extra request parameters to be passed with translation requests.
258+
* They are stored as an object where each field represents a request parameter.
259+
*/
260+
export type RequestParameters = Record<string, string>;

0 commit comments

Comments
 (0)