UNPKG

@crowdin/app-project-module

Version:

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

391 lines (390 loc) 23.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const types_1 = require("../types"); const util_1 = require("../util"); const subscription_1 = require("../util/subscription"); const api_1 = require("./api/api"); const util_2 = require("./ai-tools/util"); const util_3 = require("./workflow-step-type/util"); const ai_request_processors_1 = require("./ai-request-processors"); function normalizeEnvironments(environments) { if (Array.isArray(environments)) { return environments; } return [environments]; } function handle(config) { var _a, _b, _c; const modules = {}; if (config.projectIntegration) { // prevent possible overrides of the other modules config.projectIntegration = Object.assign(Object.assign({}, config.projectIntegration), { key: config.identifier + '-int' }); modules['project-integrations'] = [ Object.assign({ key: config.projectIntegration.key, name: config.name, description: config.description, logo: (0, util_1.getLogoUrl)(config.projectIntegration, '/integration'), url: '/' }, (!!config.projectIntegration.environments && { environments: normalizeEnvironments(config.projectIntegration.environments), })), ]; } if (config.customFileFormat) { // prevent possible overrides of the other modules config.customFileFormat = Object.assign(Object.assign({}, config.customFileFormat), { key: config.identifier + '-ff' }); modules['custom-file-format'] = [ { key: config.customFileFormat.key, type: config.customFileFormat.type, stringsExport: !!config.customFileFormat.stringsExport, multilingual: !!config.customFileFormat.multilingual, customSrxSupported: !!config.customFileFormat.customSrxSupported, extensions: config.customFileFormat.extensions, signaturePatterns: config.customFileFormat.signaturePatterns, multilingualExport: config.customFileFormat.multilingualExport, url: '/file/process', }, ]; } if (config.filePreImport) { // prevent possible overrides of the other modules config.filePreImport = Object.assign(Object.assign({}, config.filePreImport), { key: config.identifier + '-pri' }); modules['file-pre-import'] = [ { key: config.filePreImport.key, signaturePatterns: config.filePreImport.signaturePatterns, url: '/pre-import', }, ]; } if (config.filePostImport) { // prevent possible overrides of the other modules config.filePostImport = Object.assign(Object.assign({}, config.filePostImport), { key: config.identifier + '-poi' }); modules['file-post-import'] = [ { key: config.filePostImport.key, signaturePatterns: config.filePostImport.signaturePatterns, url: '/post-import', }, ]; } if (config.filePreExport) { // prevent possible overrides of the other modules config.filePreExport = Object.assign(Object.assign({}, config.filePreExport), { key: config.identifier + '-pre' }); modules['file-pre-export'] = [ { key: config.filePreExport.key, signaturePatterns: config.filePreExport.signaturePatterns, url: '/pre-export', }, ]; } if (config.filePostExport) { // prevent possible overrides of the other modules config.filePostExport = Object.assign(Object.assign({}, config.filePostExport), { key: config.identifier + '-poe' }); modules['file-post-export'] = [ { key: config.filePostExport.key, signaturePatterns: config.filePostExport.signaturePatterns, url: '/post-export', }, ]; } if (config.customMT) { // prevent possible overrides of the other modules config.customMT = Object.assign(Object.assign({}, config.customMT), { key: config.identifier + '-mt' }); modules['custom-mt'] = [ Object.assign(Object.assign(Object.assign({ key: config.customMT.key, name: config.name, logo: (0, util_1.getLogoUrl)(config.customMT, '/mt'), url: '/mt/translate', withContext: !!config.customMT.withContext }, (config.customMT.batchSize !== null && { batchSize: config.customMT.batchSize, })), (config.customMT.maskEntities !== null && { maskEntities: config.customMT.maskEntities, })), (!!config.customMT.environments && { environments: normalizeEnvironments(config.customMT.environments), })), ]; } if (config.organizationMenu) { // prevent possible overrides of the other modules config.organizationMenu = Object.assign(Object.assign({}, config.organizationMenu), { key: config.identifier + '-resources' }); modules['organization-menu'] = [ { key: config.organizationMenu.key, name: config.organizationMenu.name || config.name, url: '/organization-menu/' + (config.organizationMenu.fileName || 'index.html'), icon: (0, util_1.getLogoUrl)(config.organizationMenu, '/resources'), }, ]; } if (config.organizationSettingsMenu) { // prevent possible overrides of the other modules config.organizationSettingsMenu = Object.assign(Object.assign({}, config.organizationSettingsMenu), { key: config.identifier + '-organization-settings-menu' }); modules['organization-settings-menu'] = [ { key: config.organizationSettingsMenu.key, name: config.organizationSettingsMenu.name || config.name, url: '/organization-settings/' + (config.organizationSettingsMenu.fileName || 'index.html'), icon: (0, util_1.getLogoUrl)(config.organizationSettingsMenu, '/organization-settings'), }, ]; } if (config.profileResourcesMenu) { // prevent possible overrides of the other modules config.profileResourcesMenu = Object.assign(Object.assign({}, config.profileResourcesMenu), { key: config.identifier + '-profile-resources-menu' }); modules['profile-resources-menu'] = [ Object.assign({ key: config.profileResourcesMenu.key, name: config.profileResourcesMenu.name || config.name, url: '/profile-resources/' + (config.profileResourcesMenu.fileName || 'index.html'), icon: (0, util_1.getLogoUrl)(config.profileResourcesMenu, '/profile-resources') }, (!!config.profileResourcesMenu.environments && { environments: normalizeEnvironments(config.profileResourcesMenu.environments), })), ]; } if (config.profileSettingsMenu) { // prevent possible overrides of the other modules config.profileSettingsMenu = Object.assign(Object.assign({}, config.profileSettingsMenu), { key: config.identifier + '-profile-settings-menu' }); modules['profile-settings-menu'] = [ Object.assign({ key: config.profileSettingsMenu.key, name: config.profileSettingsMenu.name || config.name, url: '/profile-settings/' + (config.profileSettingsMenu.fileName || 'index.html'), icon: (0, util_1.getLogoUrl)(config.profileSettingsMenu, '/profile-settings') }, (!!config.profileSettingsMenu.environments && { environments: normalizeEnvironments(config.profileSettingsMenu.environments), })), ]; } if (config.editorRightPanel) { // prevent possible overrides of the other modules config.editorRightPanel = Object.assign(Object.assign({}, config.editorRightPanel), { key: config.identifier + '-editor-panels' }); modules['editor-right-panel'] = [ Object.assign({ key: config.editorRightPanel.key, name: config.editorRightPanel.name || config.name, url: '/editor-panels/' + (config.editorRightPanel.fileName || 'index.html'), supportsMultipleStrings: (_a = config.editorRightPanel.supportsMultipleStrings) !== null && _a !== void 0 ? _a : false, modes: config.editorRightPanel.modes }, (!!config.editorRightPanel.environments && { environments: normalizeEnvironments(config.editorRightPanel.environments), })), ]; } if (config.projectMenu) { // prevent possible overrides of the other modules config.projectMenu = Object.assign(Object.assign({}, config.projectMenu), { key: config.identifier + '-project-menu' }); modules['project-menu'] = [ Object.assign({ key: config.projectMenu.key, name: config.projectMenu.name || config.name, url: '/project-menu/' + (config.projectMenu.fileName || 'index.html') }, (!!config.projectMenu.environments && { environments: normalizeEnvironments(config.projectMenu.environments), })), ]; } if (config.projectMenuCrowdsource) { // prevent possible overrides of the other modules config.projectMenuCrowdsource = Object.assign(Object.assign({}, config.projectMenuCrowdsource), { key: config.identifier + '-project-menu-crowdsource' }); modules['project-menu-crowdsource'] = [ { key: config.projectMenuCrowdsource.key, name: config.projectMenuCrowdsource.name || config.name, url: '/project-menu-crowdsource/' + (config.projectMenuCrowdsource.fileName || 'index.html'), }, ]; } if (config.projectTools) { // prevent possible overrides of the other modules config.projectTools = Object.assign(Object.assign({}, config.projectTools), { key: config.identifier + '-tools' }); modules['project-tools'] = [ Object.assign({ key: config.projectTools.key, name: config.projectTools.name || config.name, description: config.description, logo: (0, util_1.getLogoUrl)(config.projectTools, '/tools'), url: '/tools/' + (config.projectTools.fileName || 'index.html') }, (!!config.projectTools.environments && { environments: normalizeEnvironments(config.projectTools.environments), })), ]; } if (config.projectReports) { // prevent possible overrides of the other modules config.projectReports = Object.assign(Object.assign({}, config.projectReports), { key: config.identifier + '-project-reports' }); modules['project-reports'] = [ { key: config.projectReports.key, name: config.projectReports.name || config.name, description: config.description, logo: (0, util_1.getLogoUrl)(config.projectReports, '/reports'), url: '/reports/' + (config.projectReports.fileName || 'index.html'), }, ]; } if (config.modal) { let modals = []; if (Array.isArray(config.modal)) { modals = config.modal.map((modal, i) => { const moduleKey = modal.key || `${config.identifier}-modal-${i}`; return Object.assign({ key: moduleKey, name: modal.name || config.name, url: modal.url || `/modal-${moduleKey}/` + (modal.fileName || 'index.html') }, (!!modal.environments && { environments: normalizeEnvironments(modal.environments) })); }); } else { modals = [ Object.assign({ key: config.identifier + '-modal', name: config.modal.name || config.name, url: config.modal.url || '/modal/' + (config.modal.fileName || 'index.html') }, (!!config.modal.environments && { environments: normalizeEnvironments(config.modal.environments), })), ]; } modules['modal'] = modals; } if (config.contextMenu) { let contextMenus = []; if (Array.isArray(config.contextMenu)) { contextMenus = config.contextMenu.map((contextMenu, i) => { const moduleKey = contextMenu.key || `${config.identifier}-context-menu-${i}`; return Object.assign({ key: moduleKey, name: contextMenu.name || config.name, description: config.description, options: Object.assign(Object.assign({ location: contextMenu.location, type: contextMenu.type }, (contextMenu.module && contextMenu.moduleKey ? { module: { [contextMenu.module]: contextMenu.moduleKey, }, } : {})), { url: `/context-${moduleKey}/` + (contextMenu.fileName || 'index.html') }), signaturePatterns: contextMenu.signaturePatterns }, (!!contextMenu.environments && { environments: normalizeEnvironments(contextMenu.environments), })); }); } else { contextMenus = [ Object.assign({ key: config.identifier + '-context-menu', name: config.contextMenu.name || config.name, description: config.description, options: Object.assign(Object.assign({ location: config.contextMenu.location, type: config.contextMenu.type }, (config.contextMenu.module ? { module: { [config.contextMenu.module]: modules[config.contextMenu.module][0].key, }, } : {})), { url: '/context/' + (config.contextMenu.fileName || 'index.html') }), signaturePatterns: config.contextMenu.signaturePatterns }, (!!config.contextMenu.environments && { environments: normalizeEnvironments(config.contextMenu.environments), })), ]; } modules['context-menu'] = contextMenus; } if (config.api) { modules['api'] = (0, api_1.getApiManifest)(config, config.api); } if (config.customSpellchecker) { // prevent possible overrides of the other modules config.customSpellchecker = Object.assign(Object.assign({}, config.customSpellchecker), { key: config.identifier + '-spellchecker' }); const uiModule = config.customSpellchecker.settingsUiModule; modules['custom-spellchecker'] = [ Object.assign(Object.assign({ key: config.customSpellchecker.key, name: config.customSpellchecker.name || config.name, description: config.customSpellchecker.description || config.description, listSupportedLanguagesUrl: '/spellchecker/languages', checkSpellingUrl: '/spellchecker/spellcheck' }, (!!config.customSpellchecker.environments && { environments: normalizeEnvironments(config.customSpellchecker.environments), })), (uiModule ? { url: '/spellchecker/settings/' + (uiModule.fileName || 'index.html') } : {})), ]; } if (config.aiProvider) { // prevent possible overrides of the other modules config.aiProvider = Object.assign(Object.assign({}, config.aiProvider), { key: config.identifier + '-aiprovider' }); const uiModule = config.aiProvider.settingsUiModule; modules['ai-provider'] = [ Object.assign(Object.assign({ key: config.aiProvider.key, name: config.aiProvider.name || config.name, description: config.aiProvider.description || config.description, logo: (0, util_1.getLogoUrl)(config.aiProvider, '/aiprovider'), chatCompletionsUrl: '/ai-provider/completions', modelsUrl: '/ai-provider/models' }, (!!config.aiProvider.environments && { environments: normalizeEnvironments(config.aiProvider.environments), })), (uiModule ? { url: '/settings/' + (uiModule.fileName || 'index.html') } : {})), ]; } [ ...Object.values(ai_request_processors_1.AiRequestProcessorModuleWithoutStream), ...Object.values(ai_request_processors_1.AiRequestProcessorModuleWithStream), ].forEach((moduleType) => { const module = config[moduleType]; if (!module) { return; } const moduleSlug = (0, ai_request_processors_1.generateModuleSlugFromType)(moduleType); modules[moduleSlug] = [ { key: config.identifier + '-' + moduleSlug, processorUrl: '/ai-request-processor/' + moduleSlug, }, ]; }); if (config.aiPromptProvider) { // prevent possible overrides of the other modules config.aiPromptProvider = Object.assign(Object.assign({}, config.aiPromptProvider), { key: config.identifier + '-ai-prompt-provider' }); modules['ai-prompt-provider'] = [ Object.assign({ key: config.aiPromptProvider.key, name: config.aiPromptProvider.name || config.name, logo: (0, util_1.getLogoUrl)(config.aiPromptProvider, '/ai-prompt-provider'), compileUrl: '/prompt-provider/compile' }, (config.aiPromptProvider.formSchema || config.aiPromptProvider.uiPath ? { configuratorUrl: '/prompt-provider/settings/' + (config.aiPromptProvider.fileName || 'index.html'), } : {})), ]; } if (config.aiTools) { // prevent possible overrides of the other modules if (Array.isArray(config.aiTools)) { config.aiTools = config.aiTools.map((aiTool) => (Object.assign({}, aiTool))); } else { config.aiTools = Object.assign({}, config.aiTools); } const tools = Array.isArray(config.aiTools) ? config.aiTools : [config.aiTools]; modules['ai-tools'] = []; for (const tool of tools) { tool.key = config.identifier + '-' + (0, util_2.getAiToolName)(tool); modules['ai-tools'].push(Object.assign({ key: tool.key, toolType: tool.toolType || 'function', function: tool.function, url: (0, util_2.getAiToolUrl)(tool) }, (!!tool.environments && { environments: normalizeEnvironments(tool.environments), }))); } } if (config.aiToolsWidget) { // prevent possible overrides of the other modules if (Array.isArray(config.aiToolsWidget)) { config.aiToolsWidget = config.aiToolsWidget.map((aiToolWidget) => (Object.assign({}, aiToolWidget))); } else { config.aiToolsWidget = Object.assign({}, config.aiToolsWidget); } const tools = Array.isArray(config.aiToolsWidget) ? config.aiToolsWidget : [config.aiToolsWidget]; modules['ai-tools-widget'] = []; for (const tool of tools) { tool.key = config.identifier + '-' + (0, util_2.getAiToolName)(tool); modules['ai-tools-widget'].push(Object.assign({ key: tool.key, toolType: tool.toolType || 'function', function: tool.function, url: (0, util_2.getAiToolWidgetUrl)(tool, true) }, (!!tool.environments && { environments: normalizeEnvironments(tool.environments), }))); } } if (config.webhooks) { // prevent possible overrides of the other modules if (Array.isArray(config.webhooks)) { config.webhooks = config.webhooks.map((webhook) => (Object.assign({}, webhook))); } else { config.webhooks = Object.assign({}, config.webhooks); } const webhooks = Array.isArray(config.webhooks) ? config.webhooks : [config.webhooks]; modules['webhook'] = []; for (let i = 0; i < webhooks.length; i++) { webhooks[i].key = config.identifier + '-' + 'webhook-' + i; modules['webhook'].push({ key: webhooks[i].key, url: '/webhooks', events: webhooks[i].events, }); } } if (config.externalQaCheck) { // prevent possible overrides of the other modules config.externalQaCheck = Object.assign(Object.assign({}, config.externalQaCheck), { key: config.identifier + '-qa-check' }); const uiModule = config.externalQaCheck.settingsUiModule; modules['external-qa-check'] = [ Object.assign(Object.assign({ key: config.externalQaCheck.key, name: config.externalQaCheck.name || config.name, description: config.externalQaCheck.description || config.description, runQaCheckUrl: '/qa-check/validate' }, (config.externalQaCheck.batchSize ? { getBatchSizeUrl: '/qa-check/batch-size' } : {})), (uiModule ? { url: '/qa-check/settings/' + (uiModule.fileName || 'index.html') } : {})), ]; } if (config.workflowStepType) { // prevent possible overrides of the other modules if (Array.isArray(config.workflowStepType)) { config.workflowStepType = config.workflowStepType.map((workflowStep) => (Object.assign({}, workflowStep))); } else { config.workflowStepType = Object.assign({}, config.workflowStepType); } const workflowSteps = Array.isArray(config.workflowStepType) ? config.workflowStepType : [config.workflowStepType]; modules['workflow-step-type'] = []; for (const workflowStep of workflowSteps) { if (!workflowStep.key) { workflowStep.key = config.identifier + '-' + (0, util_3.getWorkflowStepKey)(workflowStep); } const uiModule = ((_b = workflowStep === null || workflowStep === void 0 ? void 0 : workflowStep.settingsUiModule) === null || _b === void 0 ? void 0 : _b.formSchema) || ((_c = workflowStep === null || workflowStep === void 0 ? void 0 : workflowStep.settingsUiModule) === null || _c === void 0 ? void 0 : _c.fileName); modules['workflow-step-type'].push(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ key: workflowStep.key, name: workflowStep.name || config.name }, ((workflowStep === null || workflowStep === void 0 ? void 0 : workflowStep.imagePath) ? { logo: (0, util_1.getLogoUrl)(workflowStep, `-${workflowStep.key}`) } : {})), { description: workflowStep.description || config.description, boundaries: workflowStep.boundaries }), (workflowStep.editorMode ? { editorMode: workflowStep.editorMode } : {})), { updateSettingsUrl: (0, util_3.getWorkflowStepUrl)('/workflow-step/settings', workflowStep), deleteSettingsUrl: (0, util_3.getWorkflowStepUrl)('/workflow-step/delete', workflowStep) }), (uiModule ? { url: (0, util_3.getWorkflowStepUrl)('/workflow-step', workflowStep) } : {}))); } } const events = { installed: '/installed', uninstall: '/uninstall', }; if (!(0, subscription_1.isAppFree)(config)) { events['subscription_paid'] = '/subscription-paid'; } const defaultScopes = config.projectIntegration ? [types_1.Scope.USERS, types_1.Scope.PROJECTS] : [types_1.Scope.PROJECTS]; return (_req, res) => { const manifest = Object.assign(Object.assign(Object.assign(Object.assign({ identifier: config.identifier, name: config.name, logo: (0, util_1.getLogoUrl)(), baseUrl: config.baseUrl, authentication: { type: config.authenticationType || types_1.AuthenticationType.APP, clientId: config.clientId, }, restrictAiToSameApp: config.restrictAiToSameApp }, (config.agent && { agent: config.agent })), { events, scopes: config.scopes ? config.scopes : defaultScopes }), (config.defaultPermissions && { default_permissions: config.defaultPermissions })), { modules }); res.send(manifest); }; } exports.default = handle;