From 3b7004fdd72adba7266003598ee58bd17312a37e Mon Sep 17 00:00:00 2001 From: NJ Date: Tue, 15 Oct 2024 17:17:00 +0700 Subject: [PATCH] feat: add support for the new Pre-Translation API endpoint --- src/translations/index.ts | 29 +++++++++++++++++ tests/translations/api.test.ts | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/src/translations/index.ts b/src/translations/index.ts index aa2fe45cc..9f89890eb 100644 --- a/src/translations/index.ts +++ b/src/translations/index.ts @@ -3,6 +3,7 @@ import { DownloadLink, isOptionalNumber, PaginationOptions, + PatchRequest, ResponseList, ResponseObject, Status, @@ -15,6 +16,19 @@ import { * Pre-translate and build are asynchronous operations and shall be completed with sequence of API methods. */ export class Translations extends CrowdinApi { + /** + * @param projectId project identifier + * @param options optional parameters for the request + * @see https://developer.crowdin.com/api/v2/#operation/api.projects.pre-translations.getMany + */ + listPreTranslations( + projectId: number, + options?: PaginationOptions, + ): Promise>> { + const url = `${this.url}/projects/${projectId}/pre-translations`; + return this.getList(url, options?.limit, options?.offset); + } + /** * @param projectId project identifier * @param preTranslationId pre translation identifier @@ -41,6 +55,21 @@ export class Translations extends CrowdinApi { return this.post(url, request, this.defaultConfig()); } + /** + * @param projectId project identifier + * @param preTranslationId pre translation identifier + * @param request request body + * @see https://developer.crowdin.com/api/v2/#operation/api.projects.pre-translations.patch + */ + editPreTranslation( + projectId: number, + preTranslationId: string, + request: PatchRequest[], + ): Promise>> { + const url = `${this.url}/projects/${projectId}/pre-translations/${preTranslationId}`; + return this.patch(url, request, this.defaultConfig()); + } + /** * @param projectId project identifier * @param directoryId directory identifier diff --git a/tests/translations/api.test.ts b/tests/translations/api.test.ts index 7b1199a19..e0ad37628 100644 --- a/tests/translations/api.test.ts +++ b/tests/translations/api.test.ts @@ -21,11 +21,30 @@ describe('Translations API', () => { const languageId = 'uk'; const sampleLabelIds = [101, 102]; const sampleExcludeLabelIds = [103, 104]; + const sampleStatus = 'canceled'; const limit = 25; beforeAll(() => { scope = nock(api.url) + .get(`/projects/${projectId}/pre-translations`, undefined, { + reqheaders: { + Authorization: `Bearer ${api.token}`, + }, + }) + .reply(200, { + data: [ + { + data: { + identifier: preTranslationId, + }, + }, + ], + pagination: { + offset: 0, + limit: limit, + }, + }) .post( `/projects/${projectId}/pre-translations`, { @@ -53,6 +72,26 @@ describe('Translations API', () => { identifier: preTranslationId, }, }) + .patch( + `/projects/${projectId}/pre-translations/${preTranslationId}`, + [ + { + value: sampleStatus, + op: 'replace', + path: '/status', + }, + ], + { + reqheaders: { + Authorization: `Bearer ${api.token}`, + }, + }, + ) + .reply(200, { + data: { + identifier: preTranslationId, + }, + }) .post( `/projects/${projectId}/translations/builds/directories/${directoryId}`, { @@ -225,6 +264,13 @@ describe('Translations API', () => { scope.done(); }); + it('List Pre-Translations', async () => { + const preTranslations = await api.listPreTranslations(projectId); + expect(preTranslations.data.length).toBe(1); + expect(preTranslations.data[0].data.identifier).toBe(preTranslationId); + expect(preTranslations.pagination.limit).toBe(limit); + }); + it('Apply Pre-Translation', async () => { const preTranslation = await api.applyPreTranslation(projectId, { fileIds: [], @@ -248,6 +294,17 @@ describe('Translations API', () => { expect(preTranslation.data.identifier).toBe(preTranslationId); }); + it('Edit Pre-translation', async () => { + const preTranslation = await api.editPreTranslation(projectId, preTranslationId, [ + { + op: 'replace', + path: '/status', + value: sampleStatus, + }, + ]); + expect(preTranslation.data.identifier).toBe(preTranslationId); + }); + it('Build Project Direcotry Translation', async () => { const result = await api.buildProjectDirectoryTranslation(projectId, directoryId, { targetLanguageIds: [languageId],