From e6540b5b7e53da634c2fc8c9f6eccfa6625864a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= Date: Fri, 31 May 2024 16:32:09 +0200 Subject: [PATCH] feat(libretranslate): add libretranslate integration --- .../apps/libretranslate/assets/favicon.svg | 28 ++++++++++++ .../src/apps/libretranslate/auth/index.js | 44 +++++++++++++++++++ .../libretranslate/auth/is-still-verified.js | 8 ++++ .../libretranslate/auth/verify-credentials.js | 14 ++++++ .../apps/libretranslate/common/add-api-key.js | 9 ++++ .../libretranslate/common/set-base-url.js | 16 +++++++ .../backend/src/apps/libretranslate/index.js | 17 +++++++ packages/docs/pages/.vitepress/config.js | 8 ++++ .../pages/apps/libretranslate/connection.md | 12 +++++ .../pages/public/favicons/libretranslate.svg | 28 ++++++++++++ 10 files changed, 184 insertions(+) create mode 100644 packages/backend/src/apps/libretranslate/assets/favicon.svg create mode 100644 packages/backend/src/apps/libretranslate/auth/index.js create mode 100644 packages/backend/src/apps/libretranslate/auth/is-still-verified.js create mode 100644 packages/backend/src/apps/libretranslate/auth/verify-credentials.js create mode 100644 packages/backend/src/apps/libretranslate/common/add-api-key.js create mode 100644 packages/backend/src/apps/libretranslate/common/set-base-url.js create mode 100644 packages/backend/src/apps/libretranslate/index.js create mode 100644 packages/docs/pages/apps/libretranslate/connection.md create mode 100644 packages/docs/pages/public/favicons/libretranslate.svg diff --git a/packages/backend/src/apps/libretranslate/assets/favicon.svg b/packages/backend/src/apps/libretranslate/assets/favicon.svg new file mode 100644 index 00000000..b726f2a5 --- /dev/null +++ b/packages/backend/src/apps/libretranslate/assets/favicon.svg @@ -0,0 +1,28 @@ + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/packages/backend/src/apps/libretranslate/auth/index.js b/packages/backend/src/apps/libretranslate/auth/index.js new file mode 100644 index 00000000..f63b3c13 --- /dev/null +++ b/packages/backend/src/apps/libretranslate/auth/index.js @@ -0,0 +1,44 @@ +import verifyCredentials from './verify-credentials.js'; +import isStillVerified from './is-still-verified.js'; + +export default { + fields: [ + { + key: 'screenName', + label: 'Screen Name', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: + 'Screen name of your connection to be used on Automatisch UI.', + clickToCopy: false, + }, + { + key: 'instanceUrl', + label: 'Instance URL', + type: 'string', + required: false, + readOnly: false, + value: null, + placeholder: null, + description: 'For the self hosted version.', + clickToCopy: false, + }, + { + key: 'apiKey', + label: 'API Key', + type: 'string', + required: false, + readOnly: false, + value: null, + placeholder: null, + description: 'LibreTranslate API key of your account.', + clickToCopy: false, + }, + ], + + verifyCredentials, + isStillVerified, +}; diff --git a/packages/backend/src/apps/libretranslate/auth/is-still-verified.js b/packages/backend/src/apps/libretranslate/auth/is-still-verified.js new file mode 100644 index 00000000..6663679a --- /dev/null +++ b/packages/backend/src/apps/libretranslate/auth/is-still-verified.js @@ -0,0 +1,8 @@ +import verifyCredentials from './verify-credentials.js'; + +const isStillVerified = async ($) => { + await verifyCredentials($); + return true; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/libretranslate/auth/verify-credentials.js b/packages/backend/src/apps/libretranslate/auth/verify-credentials.js new file mode 100644 index 00000000..956072ae --- /dev/null +++ b/packages/backend/src/apps/libretranslate/auth/verify-credentials.js @@ -0,0 +1,14 @@ +const verifyCredentials = async ($) => { + const body = { + q: 'Hi!', + }; + + await $.http.post('/detect', body); + + await $.auth.set({ + screenName: $.auth.data.screenName, + apiKey: $.auth.data.apiKey, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/libretranslate/common/add-api-key.js b/packages/backend/src/apps/libretranslate/common/add-api-key.js new file mode 100644 index 00000000..06d944ba --- /dev/null +++ b/packages/backend/src/apps/libretranslate/common/add-api-key.js @@ -0,0 +1,9 @@ +const addApiKey = ($, requestConfig) => { + const apiKey = $.auth.data.apiKey; + + requestConfig.data = { api_key: apiKey, ...(requestConfig.data || {}) }; + + return requestConfig; +}; + +export default addApiKey; diff --git a/packages/backend/src/apps/libretranslate/common/set-base-url.js b/packages/backend/src/apps/libretranslate/common/set-base-url.js new file mode 100644 index 00000000..4f3e00e9 --- /dev/null +++ b/packages/backend/src/apps/libretranslate/common/set-base-url.js @@ -0,0 +1,16 @@ +const setBaseUrl = ($, requestConfig) => { + const instanceUrl = $.auth.data.instanceUrl; + const apiKey = $.auth.data.apiKey; + + if (instanceUrl) { + requestConfig.baseURL = instanceUrl; + } else if ($.app.apiBaseUrl) { + requestConfig.baseURL = $.app.apiBaseUrl; + } + + requestConfig.data = { api_key: apiKey, ...(requestConfig.data || {}) }; + + return requestConfig; +}; + +export default setBaseUrl; diff --git a/packages/backend/src/apps/libretranslate/index.js b/packages/backend/src/apps/libretranslate/index.js new file mode 100644 index 00000000..7fa195cc --- /dev/null +++ b/packages/backend/src/apps/libretranslate/index.js @@ -0,0 +1,17 @@ +import defineApp from '../../helpers/define-app.js'; +import auth from './auth/index.js'; +import addApiKey from './common/add-api-key.js'; +import setBaseUrl from './common/set-base-url.js'; + +export default defineApp({ + name: 'LibreTranslate', + key: 'libretranslate', + iconUrl: '{BASE_URL}/apps/libretranslate/assets/favicon.svg', + authDocUrl: '{DOCS_URL}/apps/libretranslate/connection', + supportsConnections: true, + baseUrl: 'https://libretranslate.com', + apiBaseUrl: 'https://libretranslate.com', + primaryColor: 'ffffff', + beforeRequest: [setBaseUrl, addApiKey], + auth, +}); diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 219a1d51..d40ad2ef 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -243,6 +243,14 @@ export default defineConfig({ { text: 'Connection', link: '/apps/invoice-ninja/connection' }, ], }, + { + text: 'LibreTranslate', + collapsible: true, + collapsed: true, + items: [ + { text: 'Connection', link: '/apps/libretranslate/connection' }, + ], + }, { text: 'Mattermost', collapsible: true, diff --git a/packages/docs/pages/apps/libretranslate/connection.md b/packages/docs/pages/apps/libretranslate/connection.md new file mode 100644 index 00000000..f2c3d9b3 --- /dev/null +++ b/packages/docs/pages/apps/libretranslate/connection.md @@ -0,0 +1,12 @@ +# LibreTranslate + +:::info +This page explains the steps you need to follow to set up the LibreTranslate +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Login to your LibreTranslate account. +2. Copy your `API key` from the page to the `API Key` field on Automatisch. +3. If you are using a self hosted version of LibreTranslate, copy your `Instance URl` to the `Instance URL` field on Automatisch. +4. Write any screen name to be displayed in Automatisch. +5. Now, you can start using the LibreTranslate connection with Automatisch. diff --git a/packages/docs/pages/public/favicons/libretranslate.svg b/packages/docs/pages/public/favicons/libretranslate.svg new file mode 100644 index 00000000..b726f2a5 --- /dev/null +++ b/packages/docs/pages/public/favicons/libretranslate.svg @@ -0,0 +1,28 @@ + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + \ No newline at end of file