|
| 1 | +import { readFileSync } from "fs"; |
| 2 | + |
| 3 | +// FIXME: get this from params |
| 4 | +const applicationId = 18; |
| 5 | + |
| 6 | +function resolveUrl(from: string, to: string) { |
| 7 | + const resolvedUrl = new URL(to, new URL(from, 'resolve://')); |
| 8 | + if (resolvedUrl.protocol === 'resolve:') { |
| 9 | + const { pathname, search, hash } = resolvedUrl; |
| 10 | + return pathname + search + hash; |
| 11 | + } |
| 12 | + return resolvedUrl.toString(); |
| 13 | +} |
| 14 | + |
| 15 | +/* |
| 16 | +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 | +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 | + |
| 62 | +async function fullAppImport(importFilePath: string, ifrcApiUrl: string, ifrcApiKey: string) { |
| 63 | + const endpoint = resolveUrl(ifrcApiUrl, `Application/${applicationId}/Translation/fullappimport`); |
| 64 | + const translationFile = readFileSync(importFilePath); |
| 65 | + const uint8FileData = new Uint8Array(translationFile); |
| 66 | + const blob = new Blob([uint8FileData], { |
| 67 | + type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 68 | + }); |
| 69 | + |
| 70 | + const formData = new FormData(); |
| 71 | + formData.append('files', blob, 'translations.xlsx'); |
| 72 | + |
| 73 | + const headers: RequestInit['headers'] = { |
| 74 | + 'Accept': 'application/json', |
| 75 | + 'X-API-KEY': ifrcApiKey, |
| 76 | + } |
| 77 | + |
| 78 | + const promise = fetch( |
| 79 | + endpoint, |
| 80 | + { |
| 81 | + method: 'POST', |
| 82 | + headers, |
| 83 | + body: formData, |
| 84 | + } |
| 85 | + ); |
| 86 | + |
| 87 | + return promise; |
| 88 | +} |
| 89 | + |
| 90 | +async function pushStringsFromExcelToIfrc(importFilePath: string, apiUrl: string, apiKey: string) { |
| 91 | + const response = await fullAppImport(importFilePath, apiUrl, apiKey); |
| 92 | + |
| 93 | + try { |
| 94 | + const responseJson = await response.json(); |
| 95 | + console.info(responseJson); |
| 96 | + } catch(e) { |
| 97 | + console.info(e); |
| 98 | + const responseText = await response.text(); |
| 99 | + console.info(responseText); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +export default pushStringsFromExcelToIfrc; |
0 commit comments