Skip to content

Commit

Permalink
API updates (#187)
Browse files Browse the repository at this point in the history
* added report settings template API methods

* bundles api updates
  • Loading branch information
yevheniyJ authored Sep 4, 2022
1 parent 2488100 commit e1aa636
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 8 deletions.
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-api-client",
"version": "1.19.0",
"version": "1.19.1",
"description": "JavaScript library for Crowdin API v2.",
"main": "out/index.js",
"types": "out/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/bundles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export namespace BundlesModel {
}

export interface CreateBundleRequest {
name: string;
format: string;
sourcePatterns: string[];
ignorePatterns?: string[];
Expand Down
104 changes: 100 additions & 4 deletions src/reports/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { CrowdinApi, DownloadLink, ResponseObject, Status } from '../core';
import {
CrowdinApi,
DownloadLink,
PaginationOptions,
PatchRequest,
ResponseList,
ResponseObject,
Status,
} from '../core';

/**
* Reports help to estimate costs, calculate translation costs, and identify the top members.
Expand Down Expand Up @@ -110,6 +118,70 @@ export class Reports extends CrowdinApi {
const url = `${this.url}/projects/${projectId}/reports/${reportId}/download`;
return this.get(url, this.defaultConfig());
}

/**
* @param projectId project identifier
* @param options optional parameters for the request
* @see https://developer.crowdin.com/api/v2/#operation/api.projects.reports.settings-templates.getMany
*/
listReportSettingsTemplates(
projectId: number,
options?: PaginationOptions,
): Promise<ResponseList<ReportsModel.ReportSettings>> {
const url = `${this.url}/projects/${projectId}/reports/settings-templates`;
return this.getList(url, options?.limit, options?.offset);
}

/**
* @param projectId project identifier
* @param request request body
* @see https://developer.crowdin.com/api/v2/#operation/api.projects.reports.settings-templates.post
*/
addReportSettingsTemplate(
projectId: number,
request: Omit<ReportsModel.ReportSettings, 'id' | 'createdAt' | 'updatedAt'>,
): Promise<ResponseObject<ReportsModel.ReportSettings>> {
const url = `${this.url}/projects/${projectId}/reports/settings-templates`;
return this.post(url, request, this.defaultConfig());
}

/**
* @param projectId project identifier
* @param reportSettingsTemplateId report settings template identifier
* @see https://developer.crowdin.com/api/v2/#operation/api.projects.reports.settings-templates.get
*/
getReportSettingsTemplate(
projectId: number,
reportSettingsTemplateId: number,
): Promise<ResponseObject<ReportsModel.ReportSettings>> {
const url = `${this.url}/projects/${projectId}/reports/settings-templates/${reportSettingsTemplateId}`;
return this.get(url, this.defaultConfig());
}

/**
* @param projectId project identifier
* @param reportSettingsTemplateId report settings template identifier
* @param request request body
* @see https://developer.crowdin.com/api/v2/#operation/api.projects.reports.settings-templates.patch
*/
editReportSettingsTemplate(
projectId: number,
reportSettingsTemplateId: number,
request: PatchRequest[],
): Promise<ResponseObject<ReportsModel.ReportSettings>> {
const url = `${this.url}/projects/${projectId}/reports/settings-templates/${reportSettingsTemplateId}`;
return this.patch(url, request, this.defaultConfig());
}

/**
* @param projectId project identifier
* @param reportSettingsTemplateId report settings template identifier
* @see https://developer.crowdin.com/api/v2/#operation/api.projects.settings-templates.delete
*/
deleteReportSettingsTemplate(projectId: number, reportSettingsTemplateId: number): Promise<void> {
const url = `${this.url}/projects/${projectId}/reports/settings-templates/${reportSettingsTemplateId}`;
return this.delete(url, this.defaultConfig());
}
}

export namespace ReportsModel {
Expand All @@ -135,8 +207,12 @@ export namespace ReportsModel {
export interface ReportStatusAttributes {
format: Format;
reportName: string;
//TODO improve this type with unions and generics according to the documentation
schema: unknown;
schema:
| CostEstimateSchema
| CostEstimateFuzzyModeSchema
| TranslationCostSchema
| TopMembersSchema
| ContributionRawDataSchema;
}

export interface GroupTranslationCostSchema {
Expand Down Expand Up @@ -209,6 +285,22 @@ export namespace ReportsModel {
dateTo?: string;
}

export interface ReportSettings {
id: number;
name: string;
currency: Currency;
unit: Unit;
mode: 'fuzzy' | 'simple';
config: ReportSettinsConfig;
createdAt: string;
updatedAt: string;
}

export interface ReportSettinsConfig {
regularRates: RegularRate[];
individualRates: UsersIndividualRate[];
}

export type Unit = 'strings' | 'words' | 'chars' | 'chars_with_spaces';

export type Currency =
Expand Down Expand Up @@ -256,12 +348,16 @@ export namespace ReportsModel {
value: number;
}

export interface UsersIndividualRate extends IndividualRate {
userIds: number[];
}

export interface IndividualRate {
languageIds: string[];
rates: RegularRate[];
}

export type Mode = 'no_match' | 'tm_match' | 'approval';
export type Mode = 'no_match' | 'tm_match' | 'approval' | '99-95' | '94-90' | '89-80' | 'perfect' | '100';

export type ContributionMode = 'translations' | 'approvals' | 'votes';

Expand Down
3 changes: 3 additions & 0 deletions tests/bundles/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('Bundles API', () => {
const projectId = 2;
const bundleId = 3;
const fileId = 4;
const name = 'test';
const format = 'crowdin-resx';
const exportPattern = 'strings-%two_letter_code%.resx';

Expand Down Expand Up @@ -40,6 +41,7 @@ describe('Bundles API', () => {
`/projects/${projectId}/bundles`,
{
format,
name,
sourcePatterns: [],
exportPattern,
},
Expand Down Expand Up @@ -125,6 +127,7 @@ describe('Bundles API', () => {
const bundle = await api.addBundle(projectId, {
exportPattern,
format,
name,
sourcePatterns: [],
});
expect(bundle.data.id).toBe(bundleId);
Expand Down
121 changes: 120 additions & 1 deletion tests/reports/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ describe('Reports API', () => {
groupBy: 'language',
projectIds: [projectId],
};
const reportSettingsTemplateId = 234;
const currency: ReportsModel.Currency = 'USD';
const unit: ReportsModel.Unit = 'words';
const config: ReportsModel.ReportSettinsConfig = {
individualRates: [],
regularRates: [],
};

beforeAll(() => {
scope = nock(api.url)
Expand Down Expand Up @@ -136,7 +143,81 @@ describe('Reports API', () => {
data: {
url: downloadLink,
},
});
})
.get(`/projects/${projectId}/reports/settings-templates`, undefined, {
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
})
.reply(200, {
data: [
{
data: {
id: reportSettingsTemplateId,
},
},
],
pagination: {
offset: 0,
limit: 1,
},
})
.post(
`/projects/${projectId}/reports/settings-templates`,
{
name: reportName,
currency,
unit,
mode: 'simple',
config,
},
{
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
},
)
.reply(200, {
data: {
id: reportSettingsTemplateId,
},
})
.get(`/projects/${projectId}/reports/settings-templates/${reportSettingsTemplateId}`, undefined, {
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
})
.reply(200, {
data: {
id: reportSettingsTemplateId,
},
})
.patch(
`/projects/${projectId}/reports/settings-templates/${reportSettingsTemplateId}`,
[
{
value: reportName,
op: 'replace',
path: '/name',
},
],
{
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
},
)
.reply(200, {
data: {
id: reportSettingsTemplateId,
},
})
.delete(`/projects/${projectId}/reports/settings-templates/${reportSettingsTemplateId}`, undefined, {
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
})
.reply(200);
});

afterAll(() => {
Expand Down Expand Up @@ -196,4 +277,42 @@ describe('Reports API', () => {
const downloadUrl = await api.downloadReport(projectId, reportId);
expect(downloadUrl.data.url).toBe(downloadLink);
});

it('List Report Settings Templates', async () => {
const templates = await api.listReportSettingsTemplates(projectId);
expect(templates.data.length).toBe(1);
expect(templates.data[0].data.id).toBe(reportSettingsTemplateId);
expect(templates.pagination.limit).toBe(1);
});

it('Add Report Settings Template', async () => {
const template = await api.addReportSettingsTemplate(projectId, {
config,
currency,
mode: 'simple',
name: reportName,
unit,
});
expect(template.data.id).toBe(reportSettingsTemplateId);
});

it('Get Report Settings Template', async () => {
const template = await api.getReportSettingsTemplate(projectId, reportSettingsTemplateId);
expect(template.data.id).toBe(reportSettingsTemplateId);
});

it('Edit Report Settings Template', async () => {
const template = await api.editReportSettingsTemplate(projectId, reportSettingsTemplateId, [
{
op: 'replace',
path: '/name',
value: reportName,
},
]);
expect(template.data.id).toBe(reportSettingsTemplateId);
});

it('Delete Report Settings Template', async () => {
await api.deleteReportSettingsTemplate(projectId, reportSettingsTemplateId);
});
});

0 comments on commit e1aa636

Please sign in to comment.