@crowdin/app-project-module 0.19.0 → 0.20.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
  - [Storage](#storage)
31
31
  - [SQLite](#sqlite)
32
32
  - [PostgreSQL](#postgresql)
33
+ - [MySQL](#mysql)
33
34
  - [Settings window](#settings-window)
34
35
  - [Info window](#info-window)
35
36
  - [Background tasks](#background-tasks)
@@ -341,6 +342,17 @@ configuration.postgreConfig = {
341
342
  };
342
343
  ```
343
344
 
345
+ ### MySQL
346
+
347
+ ```javascript
348
+ configuration.mysqlConfig = {
349
+ host: 'localhost',
350
+ user: 'root',
351
+ password: 'password',
352
+ database: 'test'
353
+ };
354
+ ```
355
+
344
356
  ## Settings window
345
357
 
346
358
  It is also possible to define settings window for your app where users can customize integration flow.
package/out/index.js CHANGED
@@ -161,7 +161,7 @@ function addCrowdinEndpoints(app, config) {
161
161
  app.use('/reports', (0, ui_module_1.default)(config), express_1.default.static(config.projectReports.uiPath));
162
162
  }
163
163
  return {
164
- getMetadata: storage.getStorage().getMetadata,
164
+ getMetadata: storage.getStorage().getMetadata.bind(storage.getStorage()),
165
165
  saveMetadata: (id, metadata) => __awaiter(this, void 0, void 0, function* () {
166
166
  const existing = yield storage.getStorage().getMetadata(id);
167
167
  if (existing) {
@@ -171,7 +171,7 @@ function addCrowdinEndpoints(app, config) {
171
171
  yield storage.getStorage().saveMetadata(id, metadata);
172
172
  }
173
173
  }),
174
- deleteMetadata: storage.getStorage().deleteMetadata,
174
+ deleteMetadata: storage.getStorage().deleteMetadata.bind(storage.getStorage()),
175
175
  getUserSettings: (clientId) => __awaiter(this, void 0, void 0, function* () {
176
176
  const integrationCredentials = yield storage.getStorage().getIntegrationCredentials(clientId);
177
177
  if (integrationCredentials === null || integrationCredentials === void 0 ? void 0 : integrationCredentials.config) {
@@ -1,6 +1,7 @@
1
1
  import Crowdin, { LanguagesModel, SourceFilesModel, SourceStringsModel } from '@crowdin/crowdin-api-client';
2
2
  import { JwtPayload } from '@crowdin/crowdin-apps-functions';
3
3
  import { Request } from 'express';
4
+ import { MySQLStorageConfig } from '../storage/mysql';
4
5
  import { PostgreStorageConfig } from '../storage/postgre';
5
6
  export interface Config extends ImagePath {
6
7
  /**
@@ -48,9 +49,13 @@ export interface Config extends ImagePath {
48
49
  */
49
50
  dbFolder?: string;
50
51
  /**
51
- * config to configure PostgreSQL as a storage
52
+ * config to configure PostgreSQL as a storage
52
53
  */
53
54
  postgreConfig?: PostgreStorageConfig;
55
+ /**
56
+ * config to configure MySQL as a storage
57
+ */
58
+ mysqlConfig?: MySQLStorageConfig;
54
59
  /**
55
60
  * integration module logic
56
61
  */
@@ -539,7 +544,7 @@ export interface CrowdinAppUtilities {
539
544
  }>;
540
545
  }
541
546
  export interface IntegrationSyncSettings {
542
- id: string;
547
+ id: number;
543
548
  files?: any;
544
549
  integrationId: string;
545
550
  crowdinId: string;
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.getStorage = exports.initialize = void 0;
13
13
  const util_1 = require("../util");
14
+ const mysql_1 = require("./mysql");
14
15
  const postgre_1 = require("./postgre");
15
16
  const sqlite_1 = require("./sqlite");
16
17
  let storage;
@@ -30,6 +31,13 @@ function initialize(config) {
30
31
  yield postgre.connect(config.postgreConfig);
31
32
  return;
32
33
  }
34
+ if (config.mysqlConfig) {
35
+ (0, util_1.log)('Using MySQL database', config.logger);
36
+ const mysql = new mysql_1.MySQLStorage();
37
+ storage = mysql;
38
+ yield mysql.connect(config.mysqlConfig);
39
+ return;
40
+ }
33
41
  throw new Error('Database is not configured');
34
42
  });
35
43
  }
@@ -0,0 +1,38 @@
1
+ import { Storage } from '.';
2
+ import { CrowdinCredentials, IntegrationCredentials, IntegrationSyncSettings } from '../models';
3
+ export interface MySQLStorageConfig {
4
+ uri?: string;
5
+ host?: string;
6
+ user?: string;
7
+ password?: string;
8
+ database?: string;
9
+ port?: number;
10
+ }
11
+ export declare class MySQLStorage implements Storage<MySQLStorageConfig> {
12
+ private connection;
13
+ private _res?;
14
+ private _rej?;
15
+ private dbPromise;
16
+ connect(config: MySQLStorageConfig): Promise<void>;
17
+ migrate(): Promise<void>;
18
+ saveCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
19
+ updateCrowdinCredentials(credentials: CrowdinCredentials): Promise<void>;
20
+ getCrowdinCredentials(id: string): Promise<CrowdinCredentials | undefined>;
21
+ getAllCrowdinCredentials(): Promise<CrowdinCredentials[]>;
22
+ deleteCrowdinCredentials(id: string): Promise<void>;
23
+ saveIntegrationCredentials(id: string, credentials: any, crowdinId: string): Promise<void>;
24
+ updateIntegrationCredentials(id: string, credentials: any): Promise<void>;
25
+ updateIntegrationConfig(id: string, config: any): Promise<void>;
26
+ getIntegrationCredentials(id: string): Promise<IntegrationCredentials | undefined>;
27
+ getAllIntegrationCredentials(crowdinId: string): Promise<IntegrationCredentials[]>;
28
+ deleteIntegrationCredentials(id: string): Promise<void>;
29
+ saveMetadata(id: string, metadata: any): Promise<void>;
30
+ updateMetadata(id: string, metadata: any): Promise<void>;
31
+ getMetadata(id: string): Promise<any>;
32
+ deleteMetadata(id: string): Promise<void>;
33
+ getSyncSettingsByProvider(integrationId: string, provider: string): Promise<IntegrationSyncSettings | undefined>;
34
+ getAllSyncSettingsByType(type: string): Promise<IntegrationSyncSettings[]>;
35
+ saveSyncSettings(files: any, integrationId: string, crowdinId: string, type: string, provider: string): Promise<void>;
36
+ updateSyncSettings(files: any, integrationId: string, crowdinId: string, type: string, provider: string): Promise<void>;
37
+ getSyncSettings(integrationId: string, crowdinId: string, type: string, provider: string): Promise<IntegrationSyncSettings | undefined>;
38
+ }
@@ -0,0 +1,262 @@
1
+ "use strict";
2
+ /* eslint-disable no-unused-expressions */
3
+ /* eslint-disable @typescript-eslint/no-var-requires */
4
+ /* eslint-disable @typescript-eslint/ban-ts-ignore */
5
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
+ return new (P || (P = Promise))(function (resolve, reject) {
8
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
12
+ });
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.MySQLStorage = void 0;
16
+ class MySQLStorage {
17
+ constructor() {
18
+ this.dbPromise = new Promise((res, rej) => {
19
+ this._res = res;
20
+ this._rej = rej;
21
+ });
22
+ }
23
+ connect(config) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ try {
26
+ const mysql = require('mysql2/promise');
27
+ this.connection = yield mysql.createConnection(config);
28
+ yield this.migrate();
29
+ this._res && this._res();
30
+ }
31
+ catch (e) {
32
+ console.error(e);
33
+ this._rej && this._rej();
34
+ }
35
+ });
36
+ }
37
+ migrate() {
38
+ var _a, _b, _c, _d;
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute(`
41
+ create table if not exists crowdin_credentials
42
+ (
43
+ id varchar(255) primary key,
44
+ app_secret text,
45
+ domain varchar(255),
46
+ user_id varchar(255),
47
+ organization_id varchar(255),
48
+ base_url varchar(255),
49
+ access_token text not null,
50
+ refresh_token text not null,
51
+ expire varchar(255) not null,
52
+ type varchar(255) not null
53
+ )
54
+ `));
55
+ yield ((_b = this.connection) === null || _b === void 0 ? void 0 : _b.execute(`
56
+ create table if not exists integration_credentials
57
+ (
58
+ id varchar(255) primary key,
59
+ credentials text,
60
+ config text,
61
+ crowdin_id varchar(255) not null
62
+ )
63
+ `));
64
+ yield ((_c = this.connection) === null || _c === void 0 ? void 0 : _c.execute(`
65
+ create table if not exists sync_settings
66
+ (
67
+ id int auto_increment primary key,
68
+ files text,
69
+ integration_id varchar(255) not null,
70
+ crowdin_id varchar(255) not null,
71
+ type varchar(255) not null,
72
+ provider varchar(255) not null
73
+ )
74
+ `));
75
+ yield ((_d = this.connection) === null || _d === void 0 ? void 0 : _d.execute(`
76
+ create table if not exists app_metadata
77
+ (
78
+ id varchar(255) primary key,
79
+ data text
80
+ )
81
+ `));
82
+ });
83
+ }
84
+ saveCrowdinCredentials(credentials) {
85
+ var _a;
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ yield this.dbPromise;
88
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('INSERT INTO crowdin_credentials(id, app_secret, domain, user_id, organization_id, base_url, access_token, refresh_token, expire, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [
89
+ credentials.id,
90
+ credentials.appSecret,
91
+ credentials.domain,
92
+ credentials.userId,
93
+ credentials.organizationId,
94
+ credentials.baseUrl,
95
+ credentials.accessToken,
96
+ credentials.refreshToken,
97
+ credentials.expire,
98
+ credentials.type,
99
+ ]));
100
+ });
101
+ }
102
+ updateCrowdinCredentials(credentials) {
103
+ var _a;
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ yield this.dbPromise;
106
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('UPDATE crowdin_credentials SET app_secret = ?, domain = ?, user_id = ?, organization_id = ?, base_url = ?, access_token = ?, refresh_token = ?, expire = ? WHERE id = ?', [
107
+ credentials.appSecret,
108
+ credentials.domain,
109
+ credentials.userId,
110
+ credentials.organizationId,
111
+ credentials.baseUrl,
112
+ credentials.accessToken,
113
+ credentials.refreshToken,
114
+ credentials.expire,
115
+ credentials.id,
116
+ ]));
117
+ });
118
+ }
119
+ getCrowdinCredentials(id) {
120
+ var _a;
121
+ return __awaiter(this, void 0, void 0, function* () {
122
+ yield this.dbPromise;
123
+ const [rows,] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, app_secret as "appSecret", domain, user_id as "userId", organization_id as "organizationId", base_url as "baseUrl", access_token as "accessToken", refresh_token as "refreshToken", expire, type FROM crowdin_credentials WHERE id = ?', [id]));
124
+ return (rows || [])[0];
125
+ });
126
+ }
127
+ getAllCrowdinCredentials() {
128
+ var _a;
129
+ return __awaiter(this, void 0, void 0, function* () {
130
+ yield this.dbPromise;
131
+ const [rows] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, app_secret as "appSecret", domain, user_id as "userId", organization_id as "organizationId", base_url as "baseUrl", access_token as "accessToken", refresh_token as "refreshToken", expire, type FROM crowdin_credentials', []));
132
+ return rows || [];
133
+ });
134
+ }
135
+ deleteCrowdinCredentials(id) {
136
+ var _a, _b, _c;
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ yield this.dbPromise;
139
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('DELETE FROM crowdin_credentials where id = ?', [id]));
140
+ yield ((_b = this.connection) === null || _b === void 0 ? void 0 : _b.execute('DELETE FROM integration_credentials where crowdin_id = ?', [id]));
141
+ yield ((_c = this.connection) === null || _c === void 0 ? void 0 : _c.execute('DELETE FROM sync_settings WHERE crowdin_id = ?', [id]));
142
+ });
143
+ }
144
+ saveIntegrationCredentials(id, credentials, crowdinId) {
145
+ var _a;
146
+ return __awaiter(this, void 0, void 0, function* () {
147
+ yield this.dbPromise;
148
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('INSERT INTO integration_credentials(id, credentials, crowdin_id) VALUES (?, ?, ?)', [id, credentials, crowdinId]));
149
+ });
150
+ }
151
+ updateIntegrationCredentials(id, credentials) {
152
+ var _a;
153
+ return __awaiter(this, void 0, void 0, function* () {
154
+ yield this.dbPromise;
155
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('UPDATE integration_credentials SET credentials = ? WHERE id = ?', [
156
+ credentials,
157
+ id,
158
+ ]));
159
+ });
160
+ }
161
+ updateIntegrationConfig(id, config) {
162
+ var _a;
163
+ return __awaiter(this, void 0, void 0, function* () {
164
+ yield this.dbPromise;
165
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('UPDATE integration_credentials SET config = ? WHERE id = ?', [config, id]));
166
+ });
167
+ }
168
+ getIntegrationCredentials(id) {
169
+ var _a;
170
+ return __awaiter(this, void 0, void 0, function* () {
171
+ yield this.dbPromise;
172
+ const [rows,] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, credentials, config, crowdin_id as "crowdinId" FROM integration_credentials WHERE id = ?', [id]));
173
+ return (rows || [])[0];
174
+ });
175
+ }
176
+ getAllIntegrationCredentials(crowdinId) {
177
+ var _a;
178
+ return __awaiter(this, void 0, void 0, function* () {
179
+ yield this.dbPromise;
180
+ const [rows,] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, credentials, config, crowdin_id as "crowdinId" FROM integration_credentials WHERE crowdin_id = ?', [crowdinId]));
181
+ return rows || [];
182
+ });
183
+ }
184
+ deleteIntegrationCredentials(id) {
185
+ var _a, _b;
186
+ return __awaiter(this, void 0, void 0, function* () {
187
+ yield this.dbPromise;
188
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('DELETE FROM integration_credentials where id = ?', [id]));
189
+ yield ((_b = this.connection) === null || _b === void 0 ? void 0 : _b.execute('DELETE FROM sync_settings where integration_id = ?', [id]));
190
+ });
191
+ }
192
+ saveMetadata(id, metadata) {
193
+ var _a;
194
+ return __awaiter(this, void 0, void 0, function* () {
195
+ yield this.dbPromise;
196
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('INSERT INTO app_metadata(id, data) VALUES (?, ?)', [id, metadata]));
197
+ });
198
+ }
199
+ updateMetadata(id, metadata) {
200
+ var _a;
201
+ return __awaiter(this, void 0, void 0, function* () {
202
+ yield this.dbPromise;
203
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('UPDATE app_metadata SET data = ? WHERE id = ?', [id, metadata]));
204
+ });
205
+ }
206
+ getMetadata(id) {
207
+ var _a;
208
+ return __awaiter(this, void 0, void 0, function* () {
209
+ yield this.dbPromise;
210
+ const [rows] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT data FROM app_metadata WHERE id = ?', [id]));
211
+ if (rows && rows[0]) {
212
+ return JSON.parse(rows[0].data);
213
+ }
214
+ });
215
+ }
216
+ deleteMetadata(id) {
217
+ var _a;
218
+ return __awaiter(this, void 0, void 0, function* () {
219
+ yield this.dbPromise;
220
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('DELETE FROM app_metadata where id = ?', [id]));
221
+ });
222
+ }
223
+ getSyncSettingsByProvider(integrationId, provider) {
224
+ var _a;
225
+ return __awaiter(this, void 0, void 0, function* () {
226
+ yield this.dbPromise;
227
+ const [rows,] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type, provider FROM sync_settings WHERE integration_id = ? AND provider = ?', [integrationId, provider]));
228
+ return (rows || [])[0];
229
+ });
230
+ }
231
+ getAllSyncSettingsByType(type) {
232
+ var _a;
233
+ return __awaiter(this, void 0, void 0, function* () {
234
+ yield this.dbPromise;
235
+ const [rows,] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type, provider FROM sync_settings WHERE type = ?', [type]));
236
+ return rows || [];
237
+ });
238
+ }
239
+ saveSyncSettings(files, integrationId, crowdinId, type, provider) {
240
+ var _a;
241
+ return __awaiter(this, void 0, void 0, function* () {
242
+ yield this.dbPromise;
243
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('INSERT INTO sync_settings(files, integration_id, crowdin_id, type, provider) VALUES (?, , ?, ?, ?)', [files, integrationId, crowdinId, type, provider]));
244
+ });
245
+ }
246
+ updateSyncSettings(files, integrationId, crowdinId, type, provider) {
247
+ var _a;
248
+ return __awaiter(this, void 0, void 0, function* () {
249
+ yield this.dbPromise;
250
+ yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('UPDATE sync_settings SET files = ? WHERE integration_id = ? AND crowdin_id = ? AND type = ? AND provider = ?', [files, integrationId, crowdinId, type, provider]));
251
+ });
252
+ }
253
+ getSyncSettings(integrationId, crowdinId, type, provider) {
254
+ var _a;
255
+ return __awaiter(this, void 0, void 0, function* () {
256
+ yield this.dbPromise;
257
+ const [rows,] = yield ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.execute('SELECT id, files, integration_id as "integrationId", crowdin_id as "crowdinId", type FROM sync_settings WHERE integration_id = ? AND crowdin_id = ? AND type = ? AND provider = ?', [integrationId, crowdinId, type, provider]));
258
+ return (rows || [])[0];
259
+ });
260
+ }
261
+ }
262
+ exports.MySQLStorage = MySQLStorage;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdin/app-project-module",
3
- "version": "0.19.0",
3
+ "version": "0.20.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",
@@ -17,6 +17,7 @@
17
17
  "crypto-js": "^4.0.0",
18
18
  "express": "4.17.1",
19
19
  "express-handlebars": "^5.3.4",
20
+ "mysql2": "^2.3.3",
20
21
  "node-cron": "^3.0.0",
21
22
  "pg": "^8.8.0",
22
23
  "sqlite3": "^5.0.2",