UNPKG

@crowdin/app-project-module

Version:

Module that generates for you all common endpoints for serving standalone Crowdin App

166 lines (165 loc) 8.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerCustomFileFormat = registerCustomFileFormat; exports.registerFilePreImport = registerFilePreImport; exports.registerFilePostImport = registerFilePostImport; exports.registerFilePreExport = registerFilePreExport; exports.registerFilePostExport = registerFilePostExport; exports.registerFileTranslationsAlignmentExport = registerFileTranslationsAlignmentExport; const crowdin_client_1 = __importDefault(require("../../middlewares/crowdin-client")); const normalize_module_1 = require("../../util/normalize-module"); const custom_file_format_1 = __importDefault(require("./handlers/custom-file-format")); const file_download_1 = __importDefault(require("./handlers/file-download")); const pre_post_process_1 = __importDefault(require("./handlers/pre-post-process")); const translations_alignment_1 = __importDefault(require("./handlers/translations-alignment")); const types_1 = require("./types"); const defaults_1 = require("./util/defaults"); const folder_1 = require("./util/folder"); function registerCustomFileFormat({ config, app }) { var _a; if (!config.customFileFormat) { return; } const items = (0, normalize_module_1.normalizeToArray)(config.customFileFormat); const isSingle = items.length === 1; for (const [index, ff] of items.entries()) { const key = (_a = ff.key) !== null && _a !== void 0 ? _a : (0, normalize_module_1.resolveInstanceKey)({ identifier: config.identifier, suffix: 'ff', item: ff, index, isSingle }); (0, defaults_1.applyFileProcessorsModuleDefaults)(config, ff); const processUrl = isSingle ? '/file/process' : `/file/${key}/process`; app.post(processUrl, (0, crowdin_client_1.default)({ config, optional: false, checkSubscriptionExpiration: true, moduleKey: key, }), (0, custom_file_format_1.default)({ baseUrl: config.baseUrl, folder: (0, folder_1.resolveModuleFilesFolder)(config.dbFolder, ff.filesFolder), config: ff, fileStore: config.fileStore, })); app.get(isSingle ? '/file/download' : `/file/${key}/download`, (0, file_download_1.default)(config, ff, 'custom-file-format')); } if (isSingle) { const ff = items[0]; // TEMPORARY CODE: it needs to support old path app.post('/process', (0, crowdin_client_1.default)({ config, optional: false, checkSubscriptionExpiration: true, }), (0, custom_file_format_1.default)({ baseUrl: config.baseUrl, folder: (0, folder_1.resolveModuleFilesFolder)(config.dbFolder, ff.filesFolder), config: ff, fileStore: config.fileStore, })); // END TEMPORARY CODE } } function registerFilePreImport({ config, app }) { var _a; if (!config.filePreImport) { return; } const items = (0, normalize_module_1.normalizeToArray)(config.filePreImport); const isSingle = items.length === 1; for (const [index, item] of items.entries()) { const key = (_a = item.key) !== null && _a !== void 0 ? _a : (0, normalize_module_1.resolveInstanceKey)({ identifier: config.identifier, suffix: 'pri', item, index, isSingle }); (0, defaults_1.applyFileProcessorsModuleDefaults)(config, item); const processUrl = isSingle ? '/pre-import' : `/pre-import/${key}`; app.post(processUrl, (0, crowdin_client_1.default)({ config, optional: false, checkSubscriptionExpiration: true, moduleKey: key, }), (0, pre_post_process_1.default)(config, item, types_1.ProcessFileJobType.PRE_IMPORT)); } // Register download route once — shared across all instances app.get(`/file/download/${types_1.ProcessFileJobType.PRE_IMPORT}`, (0, file_download_1.default)(config, items[0], types_1.ProcessFileJobType.PRE_IMPORT)); } function registerFilePostImport({ config, app }) { var _a; if (!config.filePostImport) { return; } const items = (0, normalize_module_1.normalizeToArray)(config.filePostImport); const isSingle = items.length === 1; for (const [index, item] of items.entries()) { const key = (_a = item.key) !== null && _a !== void 0 ? _a : (0, normalize_module_1.resolveInstanceKey)({ identifier: config.identifier, suffix: 'poi', item, index, isSingle }); (0, defaults_1.applyFileProcessorsModuleDefaults)(config, item); const processUrl = isSingle ? '/post-import' : `/post-import/${key}`; app.post(processUrl, (0, crowdin_client_1.default)({ config, optional: false, checkSubscriptionExpiration: true, moduleKey: key, }), (0, pre_post_process_1.default)(config, item, types_1.ProcessFileJobType.POST_IMPORT)); } // Register download route once — shared across all instances app.get(`/file/download/${types_1.ProcessFileJobType.POST_IMPORT}`, (0, file_download_1.default)(config, items[0], types_1.ProcessFileJobType.POST_IMPORT)); } function registerFilePreExport({ config, app }) { var _a; if (!config.filePreExport) { return; } const items = (0, normalize_module_1.normalizeToArray)(config.filePreExport); const isSingle = items.length === 1; for (const [index, item] of items.entries()) { const key = (_a = item.key) !== null && _a !== void 0 ? _a : (0, normalize_module_1.resolveInstanceKey)({ identifier: config.identifier, suffix: 'pre', item, index, isSingle }); (0, defaults_1.applyFileProcessorsModuleDefaults)(config, item); const processUrl = isSingle ? '/pre-export' : `/pre-export/${key}`; app.post(processUrl, (0, crowdin_client_1.default)({ config, optional: false, checkSubscriptionExpiration: true, moduleKey: key, }), (0, pre_post_process_1.default)(config, item, types_1.ProcessFileJobType.PRE_EXPORT)); } // Register download route once — shared across all instances app.get(`/file/download/${types_1.ProcessFileJobType.PRE_EXPORT}`, (0, file_download_1.default)(config, items[0], types_1.ProcessFileJobType.PRE_EXPORT)); } function registerFilePostExport({ config, app }) { var _a; if (!config.filePostExport) { return; } const items = (0, normalize_module_1.normalizeToArray)(config.filePostExport); const isSingle = items.length === 1; for (const [index, item] of items.entries()) { const key = (_a = item.key) !== null && _a !== void 0 ? _a : (0, normalize_module_1.resolveInstanceKey)({ identifier: config.identifier, suffix: 'poe', item, index, isSingle }); (0, defaults_1.applyFileProcessorsModuleDefaults)(config, item); const processUrl = isSingle ? '/post-export' : `/post-export/${key}`; app.post(processUrl, (0, crowdin_client_1.default)({ config, optional: false, checkSubscriptionExpiration: true, moduleKey: key, }), (0, pre_post_process_1.default)(config, item, types_1.ProcessFileJobType.POST_EXPORT)); } // Register download route once — shared across all instances app.get(`/file/download/${types_1.ProcessFileJobType.POST_EXPORT}`, (0, file_download_1.default)(config, items[0], types_1.ProcessFileJobType.POST_EXPORT)); } function registerFileTranslationsAlignmentExport({ config, app }) { var _a; if (!config.fileTranslationsAlignmentExport) { return; } const items = (0, normalize_module_1.normalizeToArray)(config.fileTranslationsAlignmentExport); const isSingle = items.length === 1; for (const [index, item] of items.entries()) { const key = (_a = item.key) !== null && _a !== void 0 ? _a : (0, normalize_module_1.resolveInstanceKey)({ identifier: config.identifier, suffix: 'ftae', item, index, isSingle }); (0, defaults_1.applyFileProcessorsModuleDefaults)(config, item); const processUrl = isSingle ? '/translations-alignment' : `/translations-alignment/${key}`; app.post(processUrl, (0, crowdin_client_1.default)({ config, optional: false, checkSubscriptionExpiration: true, moduleKey: key, }), (0, translations_alignment_1.default)(config, item, types_1.ProcessFileJobType.TRANSLATIONS_ALIGNMENT)); } // Register download route once — shared across all instances app.get(`/file/download/${types_1.ProcessFileJobType.POST_EXPORT}`, (0, file_download_1.default)(config, items[0], types_1.ProcessFileJobType.TRANSLATIONS_ALIGNMENT)); }