Skip to content

Commit f81f423

Browse files
author
Lucas Mathis
committed
feat: add support for extra request params
1 parent d2d4360 commit f81f423

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

CHANGELOG.md

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

77
## [Unreleased]
88

9+
### Added
10+
* Added `extraRequestParameters` option to text and document translation methods to pass arbitrary parameters in the request body. This can be used to access beta features or override built-in parameters (such as `target_lang`, `source_lang`, etc.).
11+
912
## [1.20.0] - 2025-10-08
1013
### Added
1114
* Official support for Node.js versions 20, 22 and 24

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,12 @@ The following options are only used if `tagHandling` is `'xml'`:
202202
into sentences. Format and default are the same as for `splittingTags`.
203203
- `ignoreTags`: list of XML tags that containing content that should not be
204204
translated. Format and default are the same as for `splittingTags`.
205-
- `extraRequestParameters`: Extra body parameters to be passed along with the
205+
- `extraRequestParameters`: Extra body parameters to be passed along with the
206206
HTTP request. Only string values are permitted.
207207
For example: `{'param': 'value', 'param2': 'value2'}`
208+
Note: Extra request parameters can override parameters explicitly set by the library
209+
(such as `target_lang`, `source_lang`, etc.). This is primarily useful for testing or
210+
accessing beta features.
208211

209212

210213
### Translating documents

src/types.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ export type ModelType = 'quality_optimized' | 'latency_optimized' | 'prefer_qual
8888
export type GlossaryId = string;
8989
export type TagList = string | string[];
9090

91+
/**
92+
* Extra request parameters to be passed with translation requests.
93+
*/
94+
export type RequestParameters = Record<string, string>;
95+
96+
/**
97+
* Base options that apply to all translation endpoints. Planned to be extended to other endpoints in future.
98+
*/
99+
export interface BaseRequestOptions {
100+
/**
101+
* Extra parameters to be added to the request body.
102+
* Keys in this object will be added to the request body, and can override built-in parameters.
103+
* This is mostly used by DeepL employees to test functionality, or for beta programs.
104+
*/
105+
extraRequestParameters?: RequestParameters;
106+
}
107+
91108
/**
92109
* Information about a glossary, excluding the entry list. {@link GlossaryInfo} is compatible with the
93110
* /v2 glossary endpoints and can only support mono-lingual glossaries (e.g. a glossary with only one source and
@@ -113,7 +130,7 @@ export interface GlossaryInfo {
113130
/**
114131
* Options that can be specified when translating text.
115132
*/
116-
export interface TranslateTextOptions {
133+
export interface TranslateTextOptions extends BaseRequestOptions {
117134
/**
118135
* Specifies how input translation text should be split into sentences.
119136
* - 'on': Input translation text will be split into sentences using both newlines and
@@ -161,17 +178,14 @@ export interface TranslateTextOptions {
161178
/** List of XML tags containing content that should not be translated. */
162179
ignoreTags?: TagList;
163180

164-
/** Extra parameters to be added to a text translation request. */
165-
extraRequestParameters?: RequestParameters;
166-
167181
/** (internal only) Override path to send translate request to. */
168182
__path?: string;
169183
}
170184

171185
/**
172186
* Options that can be specified when translating documents.
173187
*/
174-
export interface DocumentTranslateOptions {
188+
export interface DocumentTranslateOptions extends BaseRequestOptions {
175189
/** Controls whether translations should lean toward formal or informal language. */
176190
formality?: Formality;
177191

@@ -183,9 +197,6 @@ export interface DocumentTranslateOptions {
183197
/** Filename including extension, only required when translating documents as streams. */
184198
filename?: string;
185199

186-
/** Extra parameters to be added to a text translation request. */
187-
extraRequestParameters?: RequestParameters;
188-
189200
/** Controls whether to use Document Minification for translation, if available. */
190201
enableDocumentMinification?: boolean;
191202
}
@@ -308,12 +319,6 @@ export type SourceGlossaryLanguageCode =
308319
*/
309320
export type TargetGlossaryLanguageCode = SourceGlossaryLanguageCode;
310321

311-
/**
312-
* Extra request parameters to be passed with translation requests.
313-
* They are stored as an object where each field represents a request parameter.
314-
*/
315-
export type RequestParameters = Record<string, string>;
316-
317322
/**
318323
* Information about the API usage: how much has been translated in this billing period, and the
319324
* maximum allowable amount.

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export function buildURLSearchParams(
185185
}
186186
if (extraRequestParameters !== undefined) {
187187
for (const paramName in extraRequestParameters) {
188-
searchParams.append(paramName, extraRequestParameters[paramName]);
188+
searchParams.set(paramName, extraRequestParameters[paramName]);
189189
}
190190
}
191191
return searchParams;

tests/translateText.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,17 @@ describe('translate text', () => {
270270
expect(result.text).toContain('<p translate="no">My first paragraph.</p>');
271271
});
272272

273+
it('allows extra parameters to override standard parameters', async () => {
274+
const translator = makeTranslator();
275+
const options: TranslateTextOptions = {
276+
extraRequestParameters: { target_lang: 'fr', example_extra_param: 'true' },
277+
};
278+
const result = await translator.translateText(exampleText.en, null, 'de', options);
279+
expect(result.text).toBe(exampleText.fr);
280+
expect(result.detectedSourceLang).toBe('en');
281+
expect(result.billedCharacters).toBe(exampleText.en.length);
282+
});
283+
273284
describe('request parameter tests', () => {
274285
beforeAll(() => {
275286
nock.disableNetConnect();

0 commit comments

Comments
 (0)