@crowdin/app-project-module
Version:
Module that generates for you all common endpoints for serving standalone Crowdin App
122 lines (121 loc) • 5.8 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 crowdin_apps_functions_1 = require("@crowdin/crowdin-apps-functions");
const types_1 = require("../types");
const storage_1 = require("../storage");
const util_1 = require("../util");
const logger_1 = require("../util/logger");
const connection_1 = require("../util/connection");
function handle(config) {
return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
const event = req.body;
(0, logger_1.log)(`Received install request ${JSON.stringify(event, null, 2)}`);
const token = yield fetchToken(config, event);
const userId = event.userId;
const organization = (event.domain || event.organizationId).toString();
const credentials = {
id: organization,
appSecret: event.appSecret,
domain: event.domain,
userId,
agentId: event.agentId,
organizationId: event.organizationId,
baseUrl: event.baseUrl,
accessToken: token.accessToken,
refreshToken: token.refreshToken,
expire: (Date.now() / 1000 + token.expiresIn).toString(),
type: event.domain ? types_1.AccountType.ENTERPRISE : types_1.AccountType.NORMAL,
};
const existingCredentials = yield (0, storage_1.getStorage)().getCrowdinCredentials(credentials.id);
if (!!existingCredentials) {
yield (0, storage_1.getStorage)().updateCrowdinCredentials(credentials);
(0, logger_1.log)('An existing App has been reinstalled');
}
else {
yield (0, storage_1.getStorage)().saveCrowdinCredentials(credentials);
(0, logger_1.log)('A new App has been installed');
}
if (config.onInstall) {
const { client } = yield (0, connection_1.prepareCrowdinClient)({ config, credentials, autoRenew: true });
(0, logger_1.log)('Invoking onInstall hook');
yield config.onInstall({ organization, userId, client });
}
res.status(204).end();
}));
}
exports.default = handle;
function fetchToken(config, event) {
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
if (config.authenticationType === types_1.AuthenticationType.CODE) {
const token = yield (0, crowdin_apps_functions_1.generateOAuthToken)({
clientId: config.clientId,
clientSecret: config.clientSecret,
code: event.code || '',
url: (_a = config.crowdinUrls) === null || _a === void 0 ? void 0 : _a.accountUrl,
});
return {
accessToken: (0, util_1.encryptData)(config, token.accessToken),
refreshToken: (0, util_1.encryptData)(config, token.refreshToken),
expiresIn: token.expiresIn,
};
}
if (config.authenticationType === types_1.AuthenticationType.AGENT) {
const token = yield (0, crowdin_apps_functions_1.fetchAgentToken)({
appId: config.identifier,
appSecret: event.appSecret,
clientId: config.clientId,
clientSecret: config.clientSecret,
domain: event.domain || '',
userId: event.userId,
agentId: event.agentId,
url: (_b = config.crowdinUrls) === null || _b === void 0 ? void 0 : _b.accountUrl,
});
return {
accessToken: (0, util_1.encryptData)(config, token.accessToken),
expiresIn: token.expiresIn,
refreshToken: '',
};
}
if (config.authenticationType === types_1.AuthenticationType.APP_WITH_CODE) {
const token = yield (0, crowdin_apps_functions_1.fetchAppWithCodeToken)({
appId: config.identifier,
appSecret: event.appSecret,
clientId: config.clientId,
clientSecret: config.clientSecret,
domain: event.domain || '',
userId: event.userId,
code: event.code,
url: (_c = config.crowdinUrls) === null || _c === void 0 ? void 0 : _c.accountUrl,
});
return {
accessToken: (0, util_1.encryptData)(config, token.accessToken),
expiresIn: token.expiresIn,
refreshToken: '',
};
}
const token = yield (0, crowdin_apps_functions_1.fetchAppToken)({
appId: config.identifier,
appSecret: event.appSecret,
clientId: config.clientId,
clientSecret: config.clientSecret,
domain: event.domain || '',
userId: event.userId,
url: (_d = config.crowdinUrls) === null || _d === void 0 ? void 0 : _d.accountUrl,
});
return {
accessToken: (0, util_1.encryptData)(config, token.accessToken),
expiresIn: token.expiresIn,
refreshToken: '',
};
});
}