Skip to content

Commit c1341bd

Browse files
hive-botclaude
andcommitted
fix: send options.filename for a file path document upload
translateDocumentUpload() (and so translateDocument()) sent the path's base name and ignored the filename option when the input was a file path. It now sends the filename option when set and falls back to the base name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 33ca37c commit c1341bd

5 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎CHANGELOG.md‎

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

267267
### Fixed
268+
- `translateDocument()` and `translateDocumentUpload()` send the `filename`
269+
option as the file name when the input is a file path, instead of always
270+
sending the path's base name. The base name remains the default.
268271
- An `Authorization` header in the `headers` option, in any casing, no longer
269272
replaces the `DeepL-Auth-Key` authorization header; it is ignored. A
270273
`User-Agent` header in the `headers` option replaces the default User-Agent in

‎README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,10 @@ statuses this library does not know.
344344
`DocumentTranslationError` whose message contains `Style profile not found.`.
345345
- `translationMemory`: same as in [Text translation options](#text-translation-options).
346346
- `translationMemoryThreshold`: same as in [Text translation options](#text-translation-options).
347-
- `filename`: if the input file is not provided as file path, this option is
348-
needed to specify the file extension.
347+
- `filename`: file name sent with the upload; its extension determines the
348+
document type. Required if the input file is not provided as file path. For a
349+
file path it defaults to the path's base name; set it when the path lacks the
350+
right extension, for example `filename: 'report.docx'` for a temporary file.
349351
- `outputFormat`: file extension of the desired format of the translated
350352
document, for example `'docx'`. If unspecified, the translated document has
351353
the same format as the input document.

‎src/translator.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ export class Translator {
303303
* translation to complete, then downloads translated document to specified output path.
304304
* @param inputFile String containing file path of document to be translated, or a Stream,
305305
* Buffer, or FileHandle containing file data. Note: unless file path is used, then
306-
* `options.filename` must be specified.
306+
* `options.filename` must be specified. For a file path, `options.filename` defaults to
307+
* the path's base name.
307308
* @param outputFile String containing file path to create translated document, or Stream or
308309
* FileHandle to write translated document content.
309310
* @param sourceLang Language code of input document, or null to use auto-detection.
@@ -403,6 +404,7 @@ export class Translator {
403404
* Uploads specified document to DeepL to translate into target language, and returns handle associated with the document.
404405
* @param inputFile String containing file path, stream containing file data, or FileHandle.
405406
* Note: if a Buffer, Stream, or FileHandle is used, then `options.filename` must be specified.
407+
* For a file path, `options.filename` defaults to the path's base name.
406408
* @param sourceLang Language code of input document, or null to use auto-detection.
407409
* @param targetLang Language code of language to translate into.
408410
* @param options Optional DocumentTranslateOptions object containing additional options controlling translation.
@@ -420,7 +422,7 @@ export class Translator {
420422
buffer,
421423
sourceLang,
422424
targetLang,
423-
path.basename(inputFile),
425+
options?.filename ?? path.basename(inputFile),
424426
options,
425427
);
426428
} else {

‎src/types.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,11 @@ export interface DocumentTranslateOptions extends BaseRequestOptions {
319319
/** Specifies the minimum similarity threshold (0 to 100) for translation memory matches. */
320320
translationMemoryThreshold?: number;
321321

322-
/** Filename including extension, only required when translating documents as streams. */
322+
/**
323+
* File name including extension, sent with the upload; the API uses its extension to
324+
* detect the document type. Required unless the input is a file path, where it defaults
325+
* to the path's base name.
326+
*/
323327
filename?: string;
324328

325329
/**

‎tests/offline/translateDocument.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ describe('Translator.translateDocumentUpload', () => {
204204
expect(upload.body()).toContain('name="target_lang"\r\n\r\nde\r\n');
205205
expect(upload.body()).toContain('filename="a.txt"');
206206
});
207+
208+
it('sends options.filename instead of the base name of a file path', async () => {
209+
const upload = expectUpload();
210+
const [exampleDocument] = createTextDocument();
211+
212+
await makeDeeplClient().translateDocumentUpload(exampleDocument, null, 'de', {
213+
filename: 'report.docx',
214+
});
215+
216+
expect(upload.body()).toContain('name="file"; filename="report.docx"');
217+
expect(upload.body()).not.toContain('example_document.txt');
218+
});
207219
});
208220

209221
describe('Translator.translateDocumentGetStatus', () => {

0 commit comments

Comments
 (0)