UNPKG

@crowdin/app-project-module

Version:

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

127 lines (126 loc) 6.69 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_uniqby_1 = __importDefault(require("lodash.uniqby")); const types_1 = require("../util/types"); const util_1 = require("../../../util"); const defaults_1 = require("../util/defaults"); const logger_1 = require("../../../util/logger"); const job_1 = require("../util/job"); const files_1 = require("../util/files"); const types_2 = require("../types"); function handle(config, integration) { return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; const projectId = req.crowdinContext.jwtPayload.context.project_id || (req === null || req === void 0 ? void 0 : req.body.projectId); const uploadTranslations = req.query.uploadTranslations === 'true' || ((_a = req.body) === null || _a === void 0 ? void 0 : _a.uploadTranslations); const forcePushSources = req.query.forcePushSources === 'true' || ((_b = req.body) === null || _b === void 0 ? void 0 : _b.forcePushSources); req.logInfo(`Updating crowdin project ${projectId}`); const rootFolder = yield (0, defaults_1.getRootFolder)(config, integration, req.crowdinApiClient, projectId); if (rootFolder) { req.logInfo(`Updating crowdin files under folder ${rootFolder.id}`); } if (req.isApiCall && !req.body.files) { return res.status(400).json({ error: { message: 'Missing required parameter: files', }, }); } // A request via API has a different structure if (((_c = config.api) === null || _c === void 0 ? void 0 : _c.default) && req.body.files) { req.body = req.body.files; } let payload = req.body; const excludedTargetLanguages = yield (0, files_1.getExcludedTargetLanguages)({ client: req.crowdinApiClient, projectId, languages: ((_d = req.query.languages) === null || _d === void 0 ? void 0 : _d.split(',')) || [], }); yield (0, job_1.runAsJob)({ integrationId: req.crowdinContext.clientId, crowdinId: req.crowdinContext.crowdinId, type: types_1.JobType.UPDATE_TO_CROWDIN, title: 'Sync files to Crowdin', payload, res, projectId, client: req.crowdinApiClient, jobType: types_1.JobClientType.MANUAL, jobStoreType: integration.jobStoreType, jobCallback: (job) => __awaiter(this, void 0, void 0, function* () { if (payload && (payload === null || payload === void 0 ? void 0 : payload.length)) { payload = yield (0, files_1.expandFilesTree)(payload, req, integration, job); payload = (0, lodash_uniqby_1.default)(payload, 'id'); } if (integration.forcePushSources === true && !forcePushSources && (payload === null || payload === void 0 ? void 0 : payload.length)) { payload = yield (0, files_1.filterSyncedData)(payload); } const result = yield integration.updateCrowdin({ projectId, client: req.crowdinApiClient, credentials: req.integrationCredentials, request: payload, rootFolder, appSettings: req.integrationSettings, uploadTranslations, job, excludedTargetLanguages: req.query.languages ? excludedTargetLanguages : undefined, }); const currentJob = yield job.get(); if (currentJob && (currentJob === null || currentJob === void 0 ? void 0 : currentJob.errors)) { const errors = JSON.parse(currentJob.errors); yield (0, logger_1.handleUserError)({ action: 'Sync files to Crowdin', error: errors, crowdinId: req.crowdinContext.crowdinId, clientId: req.crowdinContext.clientId, }); throw errors.toString(); } try { yield (0, files_1.updateSyncedData)(req.crowdinContext.clientId, req.crowdinContext.crowdinId, payload, types_2.Provider.INTEGRATION); } catch (e) { (0, logger_1.logError)(e, req.crowdinContext); } let message; if ((0, files_1.isExtendedResultType)(result)) { message = result.message; } return { message }; }), onError: (e, job) => __awaiter(this, void 0, void 0, function* () { try { const currentJob = yield job.get(); if (currentJob === null || currentJob === void 0 ? void 0 : currentJob.processedEntities) { const processedEntities = JSON.parse(currentJob.processedEntities); yield (0, files_1.updateSyncedData)(req.crowdinContext.clientId, req.crowdinContext.crowdinId, processedEntities, types_2.Provider.INTEGRATION); } } catch (syncError) { (0, logger_1.logError)(syncError, req.crowdinContext); } yield (0, logger_1.handleUserError)({ action: 'Sync files to Crowdin', error: e, crowdinId: req.crowdinContext.crowdinId, clientId: req.crowdinContext.clientId, }); throw e; }), }); })); } exports.default = handle;