Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate report fnc #47

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -9,7 +9,7 @@
title?: string;
type?: SourceFilesModel.FileType;
directoryId?: number;
data: any;

Check warning on line 12 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

Check warning on line 12 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
file?: SourceFilesModel.File;
}

Expand Down Expand Up @@ -37,7 +37,7 @@
title: string,
type: SourceFilesModel.FileType,
directoryId: number,
data: any,

Check warning on line 40 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

Check warning on line 40 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
file?: SourceFilesModel.File,
): Promise<number>;

Expand All @@ -50,7 +50,7 @@
title?: string,
type?: SourceFilesModel.FileType,
directoryId?: number,
data?: any,

Check warning on line 53 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

Check warning on line 53 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
file?: SourceFilesModel.File,
): Promise<number> {
let options: UpdateOrCreateFileArgs;
Expand Down Expand Up @@ -223,7 +223,7 @@
fileId: number;
language: string;
fileName: string;
fileContent: any;

Check warning on line 226 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

Check warning on line 226 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
request?: Omit<TranslationsModel.UploadTranslationRequest, 'fileId' | 'storageId'>;
}

Expand All @@ -249,7 +249,7 @@
fileId: number,
language: string,
fileName: string,
fileContent: any,

Check warning on line 252 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

Check warning on line 252 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
request?: Omit<TranslationsModel.UploadTranslationRequest, 'fileId' | 'storageId'>,
): Promise<TranslationsModel.UploadTranslationResponse>;

Expand All @@ -263,7 +263,7 @@
fileId?: number,
language?: string,
fileName?: string,
fileContent?: any,

Check warning on line 266 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

Check warning on line 266 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
request?: Omit<TranslationsModel.UploadTranslationRequest, 'fileId' | 'storageId'>,
): Promise<TranslationsModel.UploadTranslationResponse> {
let args: UploadTranslationsArgs;
Expand Down Expand Up @@ -361,7 +361,7 @@
directory: string;
request: TranslationsRequest;
parentDirectory?: SourceFilesModel.Directory;
handleFn: (translations: any, language: string, file: SourceFilesModel.File) => Promise<void>;

Check warning on line 364 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

Check warning on line 364 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
}

function isHandleTranslationsArgs(args: HandleTranslationsArgs | Crowdin): args is HandleTranslationsArgs {
Expand All @@ -383,7 +383,7 @@
projectId: number,
directory: string,
request: TranslationsRequest,
handleFn: (translations: any, language: string, file: SourceFilesModel.File) => Promise<void>,

Check warning on line 386 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

Check warning on line 386 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
parentDirectory?: SourceFilesModel.Directory,
): Promise<void>;

Expand All @@ -394,7 +394,7 @@
projectId?: number,
directory?: string,
request?: TranslationsRequest,
handleFn?: (translations: any, language: string, file: SourceFilesModel.File) => Promise<void>,

Check warning on line 397 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

Check warning on line 397 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
parentDirectory?: SourceFilesModel.Directory,
): Promise<void> {
let args: HandleTranslationsArgs;
Expand Down Expand Up @@ -446,7 +446,7 @@
projectId: number;
url: string;
events: WebhooksModel.Event[];
payload: any;

Check warning on line 449 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

Check warning on line 449 in src/crowdin.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
name: string;
requestType?: WebhooksModel.RequestType;
batchingEnabled?: boolean;
Expand Down Expand Up @@ -574,6 +574,29 @@
}
}

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);
});
});