Skip to content

Commit cdbbd02

Browse files
hive-botclaude
andcommitted
fix: URL-encode document IDs in request paths
The document status and download requests put the document ID into the path unencoded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9fefb08 commit cdbbd02

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
190190
- An HTTP 503 response to `translateDocumentGetStatus()` and `getDocumentStatus()`
191191
now throws `DeepLError`; before it threw `DocumentNotReadyError`, which is meant
192192
for document downloads only.
193+
- The document ID is now URL-encoded in the request paths of the document
194+
status and download methods.
193195
- `extraBodyParameters` (and `extraRequestParameters`) of `translateText()` and
194196
`uploadDocument()`/`translateDocument()` now replace every parameter of the same
195197
name set by the library. Before, parameters appended after the languages and

‎src/translator.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ export class Translator {
473473
const data = new URLSearchParams({ document_key: handle.documentKey });
474474
const { statusCode, content } = await this.httpClient.sendRequestWithBackoff<string>(
475475
'POST',
476-
`/v2/document/${handle.documentId}`,
476+
`/v2/document/${encodeURIComponent(handle.documentId)}`,
477477
{ data },
478478
);
479479
await checkStatusCode(statusCode, content);
@@ -751,7 +751,7 @@ export class Translator {
751751
const { statusCode, content } =
752752
await this.httpClient.sendRequestWithBackoff<IncomingMessage>(
753753
'POST',
754-
`/v2/document/${handle.documentId}/result`,
754+
`/v2/document/${encodeURIComponent(handle.documentId)}/result`,
755755
{ data },
756756
true,
757757
);

‎tests/offline/translateDocument.test.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@ describe('Translator.translateDocumentGetStatus', () => {
219219
expect([status.status, status.ok(), status.done()]).toEqual([expected, true, false]);
220220
});
221221

222+
it('URL-encodes the document ID in the path', async () => {
223+
nock(serverUrl).post('/v2/document/a%2Fb%3F', keyBody).reply(200, { status: 'done' });
224+
225+
const status = await makeDeeplClient().translateDocumentGetStatus({
226+
documentId: 'a/b?',
227+
documentKey: handle.documentKey,
228+
});
229+
230+
expect(status.done()).toBe(true);
231+
});
232+
222233
it('throws DeepLError, not DocumentNotReadyError, when the server answers 503', async () => {
223234
nock(serverUrl).post('/v2/document/doc-id').reply(503);
224235

@@ -370,11 +381,14 @@ describe('Translator.downloadDocument', () => {
370381
});
371382

372383
describe('Translator.translateDocumentDownload', () => {
373-
it('sends the document key and streams the result to a file', async () => {
374-
nock(serverUrl).post('/v2/document/doc-id/result', keyBody).reply(200, 'result');
384+
it('sends the document key to the URL-encoded result path and writes the file', async () => {
385+
nock(serverUrl).post('/v2/document/a%2Fb/result', keyBody).reply(200, 'result');
375386
const [, outputPath] = createTextDocument();
376387

377-
await makeDeeplClient().translateDocumentDownload(handle, outputPath);
388+
await makeDeeplClient().translateDocumentDownload(
389+
{ documentId: 'a/b', documentKey: handle.documentKey },
390+
outputPath,
391+
);
378392

379393
expect(fs.readFileSync(outputPath, 'utf8')).toBe('result');
380394
});

0 commit comments

Comments
 (0)