@crowdin/app-project-module
Version:
Module that generates for you all common endpoints for serving standalone Crowdin App
88 lines (87 loc) • 3.97 kB
JavaScript
;
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 storage_1 = require("../../../storage");
const util_1 = require("../../../util");
const snapshot_1 = require("../util/snapshot");
const webhooks_1 = require("../util/webhooks");
const types_1 = require("../types");
const cron_1 = require("../util/cron");
function handle(config, integration) {
return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
const appSettings = req.body.config;
if (req.isApiCall && !req.body.config) {
return res.status(400).json({
error: {
message: 'Missing required parameter: config',
},
});
}
if (integration.validateSettings) {
const validationResult = yield integration.validateSettings({
client: req.crowdinApiClient,
credentials: req.integrationCredentials,
appSettings,
});
if (validationResult && Object.keys(validationResult).length > 0) {
return res.status(400).json({
error: {
type: 'validation_error',
details: validationResult,
},
});
}
}
const clientId = req.crowdinContext.clientId;
req.logInfo(`Saving settings ${JSON.stringify(appSettings, null, 2)}`);
const integrationConfig = yield (0, storage_1.getStorage)().getIntegrationConfig(clientId);
if (!integrationConfig) {
yield (0, storage_1.getStorage)().saveIntegrationConfig(clientId, req.crowdinContext.crowdinId, JSON.stringify(appSettings));
}
else {
yield (0, storage_1.getStorage)().updateIntegrationConfig(clientId, JSON.stringify(appSettings));
}
if (integration.webhooks) {
yield (0, webhooks_1.registerWebhooks)({
config,
integration,
client: req.crowdinApiClient,
crowdinContext: req.crowdinContext,
apiCredentials: req.integrationCredentials,
appSettings,
});
}
else {
if (appSettings === null || appSettings === void 0 ? void 0 : appSettings['new-crowdin-files']) {
(0, snapshot_1.createOrUpdateFileSnapshot)(config, integration, req, types_1.Provider.CROWDIN);
}
if (appSettings === null || appSettings === void 0 ? void 0 : appSettings['new-integration-files']) {
(0, snapshot_1.createOrUpdateFileSnapshot)(config, integration, req, types_1.Provider.INTEGRATION);
}
}
if (integration.syncNewElements) {
yield (0, cron_1.createOrUpdateSyncSettings)({
req,
files: {},
provider: types_1.Provider.CROWDIN,
onlyCreate: true,
});
yield (0, cron_1.createOrUpdateSyncSettings)({
req,
files: [],
provider: types_1.Provider.INTEGRATION,
onlyCreate: true,
});
}
res.status(204).end();
}));
}
exports.default = handle;