@crowdin/app-project-module 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -30,6 +30,7 @@ In both options you will need to provide Crowdin App configuration file. Please
30
30
  - [Background tasks](#background-tasks)
31
31
  - [Error propagation](#error-propagation)
32
32
  - [Custom File Format](#custom-file-format)
33
+ - [Custom MT](#custom-mt)
33
34
  - [Contributing](#contributing)
34
35
  - [Seeking Assistance](#seeking-assistance)
35
36
  - [License](#license)
@@ -377,7 +378,7 @@ configuration.integrartion.getIntegrationFiles = async (credentials, appSettings
377
378
 
378
379
  ## Custom File Format
379
380
 
380
- There is also a possibility to declare [custom file format module](https://support.crowdin.com/crowdin-apps-modules/#custom-file-format-module).
381
+ Example of [custom file format module](https://support.crowdin.com/crowdin-apps-modules/#custom-file-format-module).
381
382
 
382
383
  ```javascript
383
384
  const crowdinModule = require('@crowdin/app-project-module');
@@ -425,6 +426,34 @@ const configuration = {
425
426
  crowdinModule.createApp(configuration);
426
427
  ```
427
428
 
429
+ ## Custom MT (Machine Translation)
430
+
431
+ Example of [custom mt module](https://support.crowdin.com/crowdin-apps-modules/#custom-mt-machine-translation-module).
432
+
433
+ ```javascript
434
+ const crowdinModule = require('@crowdin/app-project-module');
435
+
436
+ const configuration = {
437
+ baseUrl: 'https://123.ngrok.io',
438
+ clientId: 'clientId',
439
+ clientSecret: 'clientSecret',
440
+ name: 'Sample App',
441
+ identifier: 'sample-app',
442
+ description: 'Sample App description',
443
+ dbFolder: __dirname,
444
+ imagePath: __dirname + '/' + 'logo.png',
445
+ customMT: {
446
+ translate: async (client, source, target, strings) => {
447
+ //translate strings
448
+ const translations = ['hello', 'world'];
449
+ return translations;
450
+ }
451
+ }
452
+ };
453
+
454
+ crowdinModule.createApp(configuration);
455
+ ```
456
+
428
457
  ## Contributing
429
458
 
430
459
  If you want to contribute please read the [Contributing](/CONTRIBUTING.md) guidelines.
@@ -0,0 +1,4 @@
1
+ /// <reference types="qs" />
2
+ import { Response } from 'express';
3
+ import { CustomMTLogic } from '../../models';
4
+ export default function handle(config: CustomMTLogic): (req: import("express").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: Function) => void;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const util_1 = require("../../util");
13
+ function handle(config) {
14
+ return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
15
+ const source = req.query.source;
16
+ const target = req.query.target;
17
+ const body = req.body;
18
+ if (source === 'en' && target === 'de' && body.strings && body.strings[0] === 'validation') {
19
+ if (config.validate) {
20
+ yield config.validate(req.crowdinApiClient);
21
+ }
22
+ res.send({ data: { translations: [] } });
23
+ }
24
+ else {
25
+ const translations = yield config.translate(req.crowdinApiClient, source, target, body.strings);
26
+ res.send({ data: { translations } });
27
+ }
28
+ }));
29
+ }
30
+ exports.default = handle;
@@ -23,6 +23,15 @@ function handle(config) {
23
23
  },
24
24
  ];
25
25
  }
26
+ if (config.customMT) {
27
+ modules['custom-mt'] = [
28
+ {
29
+ key: config.identifier,
30
+ name: config.name,
31
+ url: '/translate',
32
+ },
33
+ ];
34
+ }
26
35
  return (_req, res) => {
27
36
  const manifest = {
28
37
  identifier: config.identifier,
package/out/index.js CHANGED
@@ -33,6 +33,7 @@ const crowdin_project_1 = __importDefault(require("./handlers/crowdin-project"))
33
33
  const crowdin_update_1 = __importDefault(require("./handlers/crowdin-update"));
34
34
  const download_1 = __importDefault(require("./handlers/custom-file-format/download"));
35
35
  const process_1 = __importDefault(require("./handlers/custom-file-format/process"));
36
+ const translate_1 = __importDefault(require("./handlers/custom-mt/translate"));
36
37
  const install_1 = __importDefault(require("./handlers/install"));
37
38
  const integration_data_1 = __importDefault(require("./handlers/integration-data"));
38
39
  const integration_login_1 = __importDefault(require("./handlers/integration-login"));
@@ -96,6 +97,9 @@ function addCrowdinEndpoints(app, config) {
96
97
  app.post('/process', (0, process_1.default)(config.baseUrl, config.customFileFormat.filesFolder || config.dbFolder, config.customFileFormat));
97
98
  app.get('/file/download', (0, download_1.default)(config.customFileFormat.filesFolder || config.dbFolder));
98
99
  }
100
+ if (config.customMT) {
101
+ app.post('/translate', (0, crowdin_client_1.default)(config), (0, translate_1.default)(config.customMT));
102
+ }
99
103
  }
100
104
  exports.addCrowdinEndpoints = addCrowdinEndpoints;
101
105
  function createApp(config) {
@@ -14,10 +14,8 @@ const storage_1 = require("../storage");
14
14
  const util_1 = require("../util");
15
15
  function handle(config, optional = false) {
16
16
  return (0, util_1.runAsyncWrapper)((req, res, next) => __awaiter(this, void 0, void 0, function* () {
17
- const origin = req.query.origin;
18
- const clientId = req.query.client_id;
19
- const tokenJwt = req.query.tokenJwt;
20
- if (!origin || !clientId || !tokenJwt || clientId !== config.clientId) {
17
+ const tokenJwt = (req.query.tokenJwt || req.query.jwtToken);
18
+ if (!tokenJwt) {
21
19
  return res.status(403).send({ error: 'Access denied' });
22
20
  }
23
21
  try {
@@ -46,6 +46,10 @@ export interface Config {
46
46
  * custom file format module logic
47
47
  */
48
48
  customFileFormat?: CustomFileFormatLogic;
49
+ /**
50
+ * custom MT module logic
51
+ */
52
+ customMT?: CustomMTLogic;
49
53
  }
50
54
  export interface IntegrationLogic {
51
55
  /**
@@ -345,3 +349,10 @@ export interface ProcessFileString {
345
349
  text: string | SourceStringsModel.PluralText;
346
350
  translations?: any;
347
351
  }
352
+ export interface CustomMTLogic {
353
+ translate: (client: Crowdin, source: string, target: string, strings: string[]) => Promise<string[]>;
354
+ validate?: (client: Crowdin) => Promise<void>;
355
+ }
356
+ export interface CustomMTRequest {
357
+ strings: string[];
358
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdin/app-project-module",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Module that generates for you all common endpoints for serving standalone Crowdin App",
5
5
  "main": "out/index.js",
6
6
  "types": "out/index.d.ts",