Skip to content

Commit

Permalink
Merge pull request #47 from crowdin/generate-report-fnc
Browse files Browse the repository at this point in the history
feat: generate report fnc
  • Loading branch information
yevheniyJ committed Oct 29, 2023
2 parents 0aec4fd + 5da3093 commit aeb8fd6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Please refer to JSDoc for more details.
| `getSubscription` | returns an information about app subscription |
| `convertString` | converts source or translation according to specified config |
| `getBundleConfigurationForm` | returns UI template config for custom formatters |
| `generateReport` | generates Crowdin report, covers check if finished |

Also please have a look to working example of the [Crowdin App](https://github.com/crowdin/create-crowdin-app). It can be used as a basis for your app.

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crowdin/crowdin-apps-functions",
"version": "0.5.1",
"version": "0.6.0",
"description": "Utility library to easily and quickly develop Crowdin App",
"main": "out/index.js",
"types": "out/index.d.ts",
Expand Down
25 changes: 24 additions & 1 deletion src/crowdin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-ignore */
import Crowdin, { 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 @@ -574,6 +574,29 @@ export async function getSubscription({
}
}

export async function generateReport({
client,
projectId,
request,
}: {
client: Crowdin;
projectId: number;
request: ReportsModel.GenerateReportRequest;
}): 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 downloadRes = await client.reportsApi.downloadReport(projectId, report.data.identifier);

const reportBlob = await axios.get(downloadRes.data.url);
return reportBlob.data;
}
}
}

export interface FileEntity {
name: string;
title: string;
Expand Down
31 changes: 31 additions & 0 deletions tests/crowdin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import Crowdin, {
UploadStorageModel,
WebhooksModel,
} from '@crowdin/crowdin-api-client';
import axios from 'axios';
import { createMock } from 'ts-auto-mock';
import {
FileEntity,
createOrUpdateWebhook,
generateReport,
getFolder,
getOrCreateFolder,
updateOrCreateFile,
Expand Down Expand Up @@ -491,3 +493,32 @@ describe('CreateOrUpdateWebhook function', () => {
});
});
});

describe('generateReport function', () => {
let client: Crowdin;
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).toBe(reportBlob);
});
});

0 comments on commit aeb8fd6

Please sign in to comment.