UNPKG

@crowdin/app-project-module

Version:

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

303 lines (302 loc) 13.8 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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 }); exports.getOAuthLoginFormId = exports.getOAuthPollingId = exports.constructOauthUrl = exports.applyIntegrationModuleDefaults = exports.getOauthRoute = exports.getRootFolder = void 0; const crowdinAppFunctions = __importStar(require("@crowdin/crowdin-apps-functions")); function getRootFolder(config, integration, client, projectId) { return __awaiter(this, void 0, void 0, function* () { if (!integration.withRootFolder) { return; } const folder = integration.appFolderName || config.name; const directories = (yield client.sourceFilesApi.withFetchAll().listProjectDirectories(projectId)).data.map((d) => d.data); const { folder: rootFolder } = yield crowdinAppFunctions.getOrCreateFolder({ directories, client, projectId, directoryName: folder, }); return rootFolder; }); } exports.getRootFolder = getRootFolder; function getOauthRoute(integration) { var _a; return ((_a = integration.oauthLogin) === null || _a === void 0 ? void 0 : _a.redirectUriRoute) || '/oauth/code'; } exports.getOauthRoute = getOauthRoute; function applyIntegrationModuleDefaults(config, integration) { var _a, _b; if (!integration.getCrowdinFiles) { integration.getCrowdinFiles = (projectId, client, rootFolder) => __awaiter(this, void 0, void 0, function* () { const allBranches = (yield client.sourceFilesApi.withFetchAll().listProjectBranches(projectId)).data.map((d) => d.data); let options = {}; if (rootFolder) { options = { directoryId: rootFolder.id, recursion: 'true', }; } const allDirectories = (yield client.sourceFilesApi.withFetchAll().listProjectDirectories(projectId, options)).data.map((d) => d.data); const directoryIds = allDirectories.map((d) => d.id); let files = (yield client.sourceFilesApi.withFetchAll().listProjectFiles(projectId, options)).data.map((d) => d.data); files = files.filter((f) => (rootFolder && f.directoryId === rootFolder.id) || directoryIds.includes(f.directoryId) || (!rootFolder && !f.directoryId)); const res = []; allDirectories.forEach((e) => { let parentId = rootFolder && e.directoryId === rootFolder.id ? undefined : e.directoryId; if (!parentId && e.branchId) { const branch = allBranches.find((branch) => branch.id === e.branchId); if (branch) { parentId = branch.id; if (!res.find((node) => node.id === branch.id.toString())) { res.push({ id: branch.id.toString(), name: branch.name, nodeType: '2', }); } } } res.push({ id: e.id.toString(), parentId: parentId ? parentId.toString() : undefined, name: e.name, }); }); files.forEach((e) => { let parentId = rootFolder && e.directoryId === rootFolder.id ? undefined : e.directoryId; if (!parentId && e.branchId) { const branch = allBranches.find((branch) => branch.id === e.branchId); if (branch) { parentId = branch.id; if (!res.find((node) => node.id === branch.id.toString())) { res.push({ id: branch.id.toString(), name: branch.name, nodeType: '2', }); } } } res.push({ id: e.id.toString(), parentId: parentId ? parentId.toString() : undefined, name: e.title || e.name, type: e.type, excludedTargetLanguages: e.excludedTargetLanguages, }); }); return res; }); } if (!integration.getFileProgress) { integration.getFileProgress = (projectId, client, fileId) => __awaiter(this, void 0, void 0, function* () { const progress = yield client.translationStatusApi.withFetchAll().getFileProgress(projectId, fileId); return { [fileId]: progress.data.map((e) => e.data) }; }); } if (!integration.oauthLogin && !integration.loginForm) { integration.loginForm = { fields: [ { helpText: 'You need to create standard api key', key: 'apikey', label: `${config.name} API Key`, }, ], }; } const getUserSettings = integration.getConfiguration; integration.getConfiguration = (projectId, crowdinClient, integrationCredentials) => __awaiter(this, void 0, void 0, function* () { var _c, _d; let fields = []; const project = (yield crowdinClient.projectsGroupsApi.getProject(projectId)); if (getUserSettings) { fields = yield getUserSettings(projectId, crowdinClient, integrationCredentials); } const defaultSettings = []; if (project.data.inContext) { defaultSettings.push({ key: 'inContext', label: 'Show In-Context Pseudo Language', type: 'checkbox', defaultValue: 'false', }); } if (integration.withCronSync || integration.webhooks) { defaultSettings.push({ key: 'schedule', label: 'Auto sync', helpText: 'Adjust the update frequency for sources and translations. If enabled, make sure Auto Sync is enabled for your selected directories and files in the dual pane view.', type: 'select', defaultValue: '0', options: [ { value: '0', label: 'Disabled', }, { value: '1', label: '1 hour', }, { value: '3', label: '3 hours', }, { value: '6', label: '6 hours', }, { value: '12', label: '12 hours', }, { value: '24', label: '24 hours', }, ], }); if ((_c = integration.syncNewElements) === null || _c === void 0 ? void 0 : _c.crowdin) { defaultSettings.push({ key: 'new-crowdin-files', label: 'Automatically sync new translations from Crowdin', type: 'checkbox', dependencySettings: JSON.stringify([{ '#schedule-settings': { type: '!equal', value: ['0'] } }]), }); } if ((_d = integration.syncNewElements) === null || _d === void 0 ? void 0 : _d.integration) { defaultSettings.push({ key: 'new-integration-files', label: `Automatically sync new content from ${config.name}`, type: 'checkbox', dependencySettings: JSON.stringify([{ '#schedule-settings': { type: '!equal', value: ['0'] } }]), }); } if (integration.uploadTranslations) { defaultSettings.push({ key: 'importEqSuggestions', label: 'Allow target translation to match source', type: 'checkbox', }, { key: 'autoApproveImported', label: 'Approve added translations', type: 'checkbox', }, { key: 'translateHidden', label: 'Translate hidden strings', type: 'checkbox', }); } defaultSettings.push({ key: 'condition', label: 'Files export settings', type: 'select', defaultValue: '0', dependencySettings: JSON.stringify([{ '#schedule-settings': { type: '!equal', value: ['0'] } }]), options: [ { value: '0', label: 'Export all', }, { value: '1', label: 'Export translated only', }, { value: '2', label: 'Export approved only', }, ], }); } return [...defaultSettings, ...fields]; }); if (!integration.checkConnection) { integration.checkConnection = (apiCredentials) => __awaiter(this, void 0, void 0, function* () { yield integration.getIntegrationFiles(apiCredentials); }); } if (integration.webhooks && !((_a = integration.webhooks) === null || _a === void 0 ? void 0 : _a.urlParam)) { integration.webhooks.urlParam = 'crowdinData'; } if (!((_b = integration.filtering) === null || _b === void 0 ? void 0 : _b.hasOwnProperty('crowdinLanguages'))) { integration.filtering = Object.assign(Object.assign({}, (integration.filtering || {})), { crowdinLanguages: true }); } if (!integration.userErrorLifetimeDays) { integration.userErrorLifetimeDays = 30; } integration.jobStoreType = integration.jobStoreType || 'db'; } exports.applyIntegrationModuleDefaults = applyIntegrationModuleDefaults; function constructOauthUrl({ config, integration, clientId, loginForm, }) { var _a, _b, _c, _d, _e; const oauth = integration.oauthLogin; if (!oauth) { return; } if (oauth.getAuthorizationUrl) { if (!loginForm) { return; } let url = oauth.getAuthorizationUrl(`${config.baseUrl}${getOauthRoute(integration)}`, loginForm); url += `&${((_a = oauth.fieldsMapping) === null || _a === void 0 ? void 0 : _a.state) || 'state'}=${Buffer.from(clientId).toString('base64')}`; return url; } if (!oauth.authorizationUrl) { return; } let url = oauth.authorizationUrl || ''; url += `?${((_b = oauth.fieldsMapping) === null || _b === void 0 ? void 0 : _b.clientId) || 'client_id'}=${oauth.clientId}`; url += `&${((_c = oauth.fieldsMapping) === null || _c === void 0 ? void 0 : _c.redirectUri) || 'redirect_uri'}=${config.baseUrl}${getOauthRoute(integration)}`; url += `&${((_d = oauth.fieldsMapping) === null || _d === void 0 ? void 0 : _d.state) || 'state'}=${Buffer.from(clientId).toString('base64')}`; if (oauth.scope) { url += `&${((_e = oauth.fieldsMapping) === null || _e === void 0 ? void 0 : _e.scope) || 'scope'}=${oauth.scope}`; } if (oauth.extraAutorizationUrlParameters) { Object.entries(oauth.extraAutorizationUrlParameters).forEach(([key, value]) => (url += `&${key}=${value}`)); } return url; } exports.constructOauthUrl = constructOauthUrl; function getOAuthPollingId(clientId) { return `oauth_${clientId}`; } exports.getOAuthPollingId = getOAuthPollingId; function getOAuthLoginFormId(clientId) { return `oauth_form_${clientId}`; } exports.getOAuthLoginFormId = getOAuthLoginFormId;