Skip to content

Commit 43f3fbf

Browse files
authored
Merge pull request #103 from Smartling/ZD-825-locales-api
ZD-825: Added locales api
2 parents fafab95 + be6abc7 commit 43f3fbf

11 files changed

+149
-6
lines changed

api/dto/base-locale-dto.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface BaseLocaleDto {
2+
localeId: string;
3+
description: string;
4+
}
5+
6+
export { BaseLocaleDto };

api/locales/dto/country-dto.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface CountryDto {
2+
countryId: string;
3+
description: string;
4+
}
5+
6+
export { CountryDto };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
interface SmartlingLanguageDto {
2+
description: string;
3+
direction: string;
4+
languageId: string;
5+
wordDelimiter: string;
6+
}
7+
8+
export { SmartlingLanguageDto };
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { BaseLocaleDto } from "../../dto/base-locale-dto";
2+
import { CountryDto } from "./country-dto";
3+
import { SmartlingLanguageDto } from "./smartling-language-dto";
4+
5+
interface SmartlingLocaleDto extends BaseLocaleDto {
6+
mtSupported: boolean;
7+
country: CountryDto;
8+
language: SmartlingLanguageDto;
9+
}
10+
11+
export { SmartlingLocaleDto };

api/locales/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { SmartlingBaseApi } from "../base/index";
2+
import { SmartlingAuthApi } from "../auth/index";
3+
import { Logger } from "../logger";
4+
import { SmartlingLocaleDto } from "./dto/smartling-locale-dto";
5+
import { SmartlingListResponse } from "../http/smartling-list-response";
6+
import { GetLocalesParameters } from "./params/get-locales-parameters";
7+
8+
export class SmartlingLocalesApi extends SmartlingBaseApi {
9+
constructor(smartlingApiBaseUrl: string, authApi: SmartlingAuthApi, logger: Logger) {
10+
super(logger);
11+
this.authApi = authApi;
12+
this.entrypoint = `${smartlingApiBaseUrl}/locales-api/v2/dictionary`;
13+
}
14+
15+
async getLocales(
16+
params?: GetLocalesParameters
17+
): Promise<SmartlingListResponse<SmartlingLocaleDto>> {
18+
return await this.makeRequest(
19+
"get",
20+
`${this.entrypoint}/locales`,
21+
params ? params.export() : null
22+
);
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { BaseParameters } from "../../parameters";
2+
3+
export class GetLocalesParameters extends BaseParameters {
4+
public setLocaleIds(localeIds: Array<string>): GetLocalesParameters {
5+
this.set("localeIds", localeIds);
6+
7+
return this;
8+
}
9+
10+
public setSupportedOnly(supportedOnly: boolean): GetLocalesParameters {
11+
this.set("supportedOnly", supportedOnly);
12+
13+
return this;
14+
}
15+
}

api/projects/dto/locale-dto.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
interface LocaleDto {
2-
localeId: string;
3-
description: string;
1+
import { BaseLocaleDto } from "../../dto/base-locale-dto";
2+
3+
interface LocaleDto extends BaseLocaleDto {
44
enabled: boolean;
55
}
66

index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,8 @@ export * from "./api/logger";
9696
export * from "./api/jobs/params/add-file-parameters";
9797
export * from "./api/files/params/download-file-all-translations-parameters";
9898
export * from "./api/jobs/params/authorize-job-parameters";
99+
export * from "./api/locales/dto/country-dto";
100+
export * from "./api/locales/dto/smartling-language-dto";
101+
export * from "./api/locales/dto/smartling-locale-dto";
102+
export * from "./api/locales/params/get-locales-parameters";
103+
export * from "./api/locales/index";

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "smartling-api-sdk-nodejs",
3-
"version": "2.8.1",
3+
"version": "2.9.0",
44
"description": "Package for Smartling API",
55
"main": "built/index.js",
66
"engines": {

test/locales.spec.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import sinon from "sinon";
2+
import { SmartlingLocalesApi } from "../api/locales";
3+
import { GetLocalesParameters } from "../api/locales/params/get-locales-parameters";
4+
import { SmartlingAuthApi } from "../api/auth/index";
5+
import { loggerMock, authMock, responseMock } from "./mock";
6+
7+
describe("SmartlingLocalesApi class tests.", () => {
8+
let localesApi;
9+
let localesApiFetchStub;
10+
let localesApiUaStub;
11+
let responseMockTextStub;
12+
13+
beforeEach(() => {
14+
localesApi = new SmartlingLocalesApi("https://test.com", authMock as unknown as SmartlingAuthApi, loggerMock);
15+
16+
localesApiFetchStub = sinon.stub(localesApi, "fetch");
17+
localesApiUaStub = sinon.stub(localesApi, "ua");
18+
responseMockTextStub = sinon.stub(responseMock, "text");
19+
20+
localesApiUaStub.returns("test_user_agent");
21+
localesApiFetchStub.returns(responseMock);
22+
responseMockTextStub.returns("{\"response\": {}}");
23+
});
24+
25+
afterEach(() => {
26+
localesApiFetchStub.restore();
27+
responseMockTextStub.restore();
28+
localesApiUaStub.restore();
29+
});
30+
31+
it("Get all locales", async () => {
32+
await localesApi.getLocales();
33+
34+
sinon.assert.calledOnce(localesApiFetchStub);
35+
sinon.assert.calledWithExactly(
36+
localesApiFetchStub,
37+
"https://test.com/locales-api/v2/dictionary/locales", {
38+
headers: {
39+
Authorization: "test_token_type test_access_token",
40+
"Content-Type": "application/json",
41+
"User-Agent": "test_user_agent"
42+
},
43+
method: "get"
44+
}
45+
);
46+
});
47+
48+
it("Get locales with filters", async () => {
49+
const params = new GetLocalesParameters()
50+
.setLocaleIds(["fr-FR", "de-DE"])
51+
.setSupportedOnly(true);
52+
53+
await localesApi.getLocales(params);
54+
55+
sinon.assert.calledOnce(localesApiFetchStub);
56+
sinon.assert.calledWithExactly(
57+
localesApiFetchStub,
58+
"https://test.com/locales-api/v2/dictionary/locales?localeIds=fr-FR&localeIds=de-DE&supportedOnly=true", {
59+
headers: {
60+
Authorization: "test_token_type test_access_token",
61+
"Content-Type": "application/json",
62+
"User-Agent": "test_user_agent"
63+
},
64+
method: "get"
65+
}
66+
);
67+
});
68+
});

0 commit comments

Comments
 (0)