Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ committed Oct 29, 2023
1 parent 6ea2bfd commit 5da3093
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
15 changes: 5 additions & 10 deletions src/crowdin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-ignore */
import Crowdin, {
DownloadLink,
ReportsModel,
SourceFilesModel,
TranslationsModel,
WebhooksModel,
} from '@crowdin/crowdin-api-client';
import Crowdin, { ReportsModel, SourceFilesModel, TranslationsModel, WebhooksModel } from '@crowdin/crowdin-api-client';
import axios from 'axios';

interface UpdateOrCreateFileArgs {
Expand Down Expand Up @@ -588,16 +582,17 @@ export async function generateReport({
client: Crowdin;
projectId: number;
request: ReportsModel.GenerateReportRequest;
}): Promise<DownloadLink | undefined> {
}): Promise<any | undefined> {
const report = await client.reportsApi.generateReport(projectId, request);

while (true) {
const status = await client.reportsApi.checkReportStatus(projectId, report.data.identifier);

if (status.data.status === 'finished') {
const url = await client.reportsApi.downloadReport(projectId, report.data.identifier);
const downloadRes = await client.reportsApi.downloadReport(projectId, report.data.identifier);

return url.data;
const reportBlob = await axios.get(downloadRes.data.url);
return reportBlob.data;
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions tests/crowdin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Crowdin, {
UploadStorageModel,
WebhooksModel,
} from '@crowdin/crowdin-api-client';
import axios from 'axios';
import { createMock } from 'ts-auto-mock';
import {
FileEntity,
Expand Down Expand Up @@ -498,23 +499,26 @@ describe('generateReport function', () => {
const projectId = 1;
const reportId = '123';
const reportUrl = 'report-url';
const reportBlob = 'some-data';
const generateReportMock = jest.fn().mockImplementation(() => ({ data: { identifier: reportId } }));
const checkReportStatustMock = jest.fn().mockImplementation(() => ({ data: { status: 'finished' } }));
const downloadReportMock = jest.fn().mockImplementation(() => ({ data: { url: reportUrl } }));
const axiosGet = jest.fn().mockImplementation(() => ({ data: reportBlob }));

beforeEach(() => {
jest.mock('axios');
client = createMock<Crowdin>({
reportsApi: {
generateReport: generateReportMock,
checkReportStatus: checkReportStatustMock,
downloadReport: downloadReportMock,
},
});
axios.get = axiosGet;
});

it('should generate report', async () => {
const res = await generateReport({ client, projectId, request: { name: 'test', schema: {} } });
expect(res).toBeDefined();
expect(res?.url).toBe(reportUrl);
expect(res).toBe(reportBlob);
});
});

0 comments on commit 5da3093

Please sign in to comment.