UNPKG

@crowdin/app-project-module

Version:

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

79 lines (78 loc) 3.68 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("../../../util"); const logger_1 = require("../../../util/logger"); const validate_input_1 = require("../util/validate-input"); // Maximum response size: 250KB const MAX_RESPONSE_SIZE = 250 * 1024; function getExecuteHandler(automationAction) { return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () { req.logInfo('Received automation action execute request'); const isTaskTokenMode = automationAction.invocationWaitMode === 'taskToken'; // In taskToken mode, respond immediately to prevent Crowdin retries // Execution continues asynchronously, communicating via API calls if (isTaskTokenMode) { res.status(200).send(); } try { let validationErrors; let inputData = req.body.formData || req.body; if (automationAction.inputSchema) { req.logInfo('Validate automation input data'); const { errors, data } = (0, validate_input_1.validateAutomationData)({ data: req.body, schema: automationAction.inputSchema, }); validationErrors = errors; inputData = data; } const result = yield automationAction.execute({ client: req.crowdinApiClient, inputData, validationErrors, context: req.crowdinContext, req: req.body, }); if (result) { const responseJson = JSON.stringify(result); const responseSize = Buffer.byteLength(responseJson, 'utf8'); // Check response size before sending if (responseSize > MAX_RESPONSE_SIZE) { throw new Error(`Response size (${responseSize} bytes) exceeds the maximum allowed size of ${MAX_RESPONSE_SIZE} bytes (250KB)`); } } if (automationAction.validateOutputData !== false && result && automationAction.outputSchema) { req.logInfo('Validate automation output data'); const { errors } = (0, validate_input_1.validateAutomationData)({ data: result, schema: automationAction.outputSchema, preserveEmptyArrays: true, }); if (errors) { throw new Error(JSON.stringify(errors)); } } if (!isTaskTokenMode) { res.json({ data: result, }); } } catch (error) { req.logError(error); if (!isTaskTokenMode) { res.status(500).send({ error: { message: (0, logger_1.getErrorMessage)(error) } }); } } })); } exports.default = getExecuteHandler;