Skip to content

Commit 4e4145c

Browse files
committed
WIP
1 parent d36918a commit 4e4145c

File tree

8 files changed

+747
-2
lines changed

8 files changed

+747
-2
lines changed

app/scripts/translatte/commands/ifrc/api-types.ts

Lines changed: 584 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { fullAppimport, postTranslation } from "./utils";
2+
3+
async function testIfrc(apiUrl: string, apiKey: string) {
4+
const response = await fullAppimport(apiUrl, apiKey);
5+
const responseJson = await response.text();
6+
7+
console.info(responseJson);
8+
}
9+
10+
export default testIfrc;
1.08 MB
Binary file not shown.
882 KB
Binary file not shown.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { readFileSync } from "fs";
2+
import path from "path";
3+
4+
// FIXME: get this from params
5+
const applicationId = 18;
6+
7+
function resolveUrl(from: string, to: string) {
8+
const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
9+
if (resolvedUrl.protocol === 'resolve:') {
10+
const { pathname, search, hash } = resolvedUrl;
11+
return pathname + search + hash;
12+
}
13+
return resolvedUrl.toString();
14+
}
15+
16+
export async function fetchTranslations(ifrcApiUrl: string, ifrcApiKey: string) {
17+
const endpoint = resolveUrl(ifrcApiUrl, `Application/${applicationId}/Translation/`);
18+
19+
const headers: RequestInit['headers'] = {
20+
'Accept': 'application/json',
21+
'X-API-KEY': ifrcApiKey,
22+
}
23+
24+
const promise = fetch(
25+
endpoint,
26+
{
27+
method: 'GET',
28+
headers,
29+
}
30+
);
31+
32+
return promise;
33+
}
34+
35+
export async function postTranslation(ifrcApiUrl: string, ifrcApiKey: string) {
36+
const endpoint = resolveUrl(ifrcApiUrl, `Application/${applicationId}/Translation`);
37+
38+
const headers: RequestInit['headers'] = {
39+
// 'Accept': 'application/json',
40+
'X-API-KEY': ifrcApiKey,
41+
'Content-Type': 'application/json',
42+
}
43+
44+
const promise = fetch(
45+
endpoint,
46+
{
47+
method: 'POST',
48+
headers,
49+
body: JSON.stringify({
50+
page: 'home',
51+
keyName: 'pageTitle',
52+
value: 'IFRC GO | Home',
53+
languageCode: 'en',
54+
}),
55+
}
56+
);
57+
58+
return promise;
59+
}
60+
61+
export async function fullAppimport(ifrcApiUrl: string, ifrcApiKey: string) {
62+
const endpoint = resolveUrl(ifrcApiUrl, `Application/${applicationId}/Translation/fullappimport`);
63+
64+
const filePath = path.resolve(process.cwd(), './scripts/translatte/commands/ifrc/translations.xlsx');
65+
const translationFile = readFileSync(filePath);
66+
const uint8FileData = new Uint8Array(translationFile);
67+
const blob = new Blob([uint8FileData], {
68+
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
69+
});
70+
71+
const formData = new FormData();
72+
formData.append('files', blob, 'translations.xlsx');
73+
74+
const headers: RequestInit['headers'] = {
75+
'Accept': 'application/json',
76+
'X-API-KEY': ifrcApiKey,
77+
}
78+
79+
const promise = fetch(
80+
endpoint,
81+
{
82+
method: 'POST',
83+
headers,
84+
body: formData,
85+
}
86+
);
87+
88+
return promise;
89+
}

app/scripts/translatte/main.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import pushStringsFromExcel from './commands/pushStringsFromExcel';
1515
import exportServerStringsToExcel from './commands/exportServerStringsToExcel';
1616
import clearServerStrings from './commands/clearServerStrings';
1717
import pushStringsDref from './commands/pushStringsDref';
18+
import testIfrc from './commands/ifrc/test';
1819

1920
const currentDir = cwd();
2021

@@ -339,6 +340,30 @@ yargs(hideBin(process.argv))
339340
);
340341
},
341342
)
343+
.command(
344+
'test-ifrc',
345+
'Test IFRC translation service',
346+
(yargs) => {
347+
yargs.options({
348+
'api-url': {
349+
type: 'string',
350+
describe: 'URL for the API server',
351+
require: true,
352+
},
353+
'api-key': {
354+
type: 'string',
355+
describe: 'API key for ther service',
356+
require: true,
357+
},
358+
});
359+
},
360+
async (argv) => {
361+
await testIfrc(
362+
argv.apiUrl as string,
363+
argv.apiKey as string,
364+
);
365+
},
366+
)
342367
.strictCommands()
343368
.showHelpOnFail(false)
344369
.parse()

app/src/utils/restRequest/go.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ export interface TransformedError {
3939
debugMessage: string;
4040
}
4141

42+
type ApiType = 'go' | 'risk' | 'translation';
43+
4244
export interface AdditionalOptions {
43-
apiType?: 'go' | 'risk';
45+
apiType?: ApiType;
4446
formData?: boolean;
4547
isCsvRequest?: boolean;
4648
enforceEnglishForQuery?: boolean;
@@ -111,6 +113,18 @@ type GoContextInterface = ContextInterface<
111113
AdditionalOptions
112114
>;
113115

116+
function getEndPoint(apiType: ApiType | undefined) {
117+
if (apiType === 'risk') {
118+
return riskApi;
119+
}
120+
121+
if (apiType === 'translation') {
122+
return 'https://ifrc-translationapi.azurewebsites.net/api/';
123+
}
124+
125+
return api;
126+
}
127+
114128
export const processGoUrls: GoContextInterface['transformUrl'] = (url, _, additionalOptions) => {
115129
if (isFalsyString(url)) {
116130
return '';
@@ -124,7 +138,7 @@ export const processGoUrls: GoContextInterface['transformUrl'] = (url, _, additi
124138
const { apiType } = additionalOptions;
125139

126140
return resolveUrl(
127-
apiType === 'risk' ? riskApi : api,
141+
getEndPoint(apiType),
128142
url,
129143
);
130144
};

app/src/utils/restRequest/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from '@togglecorp/toggle-request';
66

77
import type { paths as riskApiPaths } from '#generated/riskTypes';
8+
import type { paths as translationApiPaths } from '#generated/translationTypes';
89
import type { paths as goApiPaths } from '#generated/types';
910

1011
import type {
@@ -18,6 +19,10 @@ import type {
1819
VALID_METHOD,
1920
} from './overrideTypes';
2021

22+
export type TranslationApiResponse<URL extends keyof translationApiPaths, METHOD extends 'GET' | 'POST' | 'PUT' | 'PATCH' = 'GET'> = ApiResponse<translationApiPaths, URL, METHOD>;
23+
export type TranslationApiUrlQuery<URL extends keyof translationApiPaths, METHOD extends 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' = 'GET'> = ApiUrlQuery<translationApiPaths, URL, METHOD>
24+
export type TranslationApiBody<URL extends keyof translationApiPaths, METHOD extends 'POST' | 'PUT' | 'PATCH'> = ApiBody<translationApiPaths, URL, METHOD>
25+
2126
export type GoApiResponse<URL extends keyof goApiPaths, METHOD extends 'GET' | 'POST' | 'PUT' | 'PATCH' = 'GET'> = ApiResponse<goApiPaths, URL, METHOD>;
2227
export type GoApiUrlQuery<URL extends keyof goApiPaths, METHOD extends 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' = 'GET'> = ApiUrlQuery<goApiPaths, URL, METHOD>
2328
export type GoApiBody<URL extends keyof goApiPaths, METHOD extends 'POST' | 'PUT' | 'PATCH'> = ApiBody<goApiPaths, URL, METHOD>
@@ -36,6 +41,22 @@ export type ListResponseItem<RESPONSE extends {
3641
results?: Array<unknown>
3742
} | undefined> = NonNullable<NonNullable<RESPONSE>['results']>[number];
3843

44+
const useTranslationRequest = useRequest as <
45+
PATH extends keyof translationApiPaths,
46+
METHOD extends VALID_METHOD | undefined = 'GET',
47+
>(
48+
requestOptions: CustomRequestOptions<translationApiPaths, PATH, METHOD> & { apiType: 'translation' }
49+
) => CustomRequestReturn<translationApiPaths, PATH, METHOD>;
50+
51+
// FIXME: identify a way to do this without a cast
52+
const useTranslationLazyRequest = useLazyRequest as <
53+
PATH extends keyof translationApiPaths,
54+
CONTEXT = unknown,
55+
METHOD extends VALID_METHOD | undefined = 'GET',
56+
>(
57+
requestOptions: CustomLazyRequestOptions<translationApiPaths, PATH, METHOD, CONTEXT> & { apiType: 'translation' }
58+
) => CustomLazyRequestReturn<translationApiPaths, PATH, METHOD, CONTEXT>;
59+
3960
// FIXME: identify a way to do this without a cast
4061
const useGoRequest = useRequest as <
4162
PATH extends keyof goApiPaths,
@@ -76,4 +97,6 @@ export {
7697
useGoRequest as useRequest,
7798
useRiskLazyRequest,
7899
useRiskRequest,
100+
useTranslationLazyRequest,
101+
useTranslationRequest,
79102
};

0 commit comments

Comments
 (0)