@crowdin/app-project-module 0.20.0 → 0.20.2

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
@@ -143,7 +143,7 @@ const configuration = {
143
143
  );
144
144
  },
145
145
  updateIntegration: async (projectId, client, credentials, request, rootFolder, appSettings) => {
146
- ////here should be logic to get translations from Crowdin and upload them to integration
146
+ //here should be logic to get translations from Crowdin and upload them to integration
147
147
  console.log(`Request for updating data in Integration ${JSON.stringify(request)}`);
148
148
  const directories = await client.sourceFilesApi
149
149
  .withFetchAll()
@@ -170,6 +170,9 @@ const configuration = {
170
170
  console.log(response.data);
171
171
  }
172
172
  },
173
+ onLogout: async (projectId, client, credentials, appSettings) => {
174
+ //cleanup logic
175
+ }
173
176
  }
174
177
  };
175
178
 
@@ -293,7 +296,9 @@ configuration.projectIntegration.oauthLogin = {
293
296
  response_type: 'code'
294
297
  },
295
298
  refresh: true,
296
- performGetTokenRequest: async (code) => {
299
+ performGetTokenRequest: async (code, query, url) => {
300
+ //query is an object with all query params
301
+ //url is an url string that OAuth server used to call us back
297
302
  const url = `${tokenUrl}?code=${code}&grant_type=authorization_code`;
298
303
  const headers = {
299
304
  'Authorization': `Bearer ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`
@@ -681,7 +686,8 @@ const configuration = {
681
686
 
682
687
  const crowdinApp = crowdinModule.addCrowdinEndpoints(app, configuration);
683
688
 
684
- async function cleanup(organization) {
689
+ async function cleanup(organization, allCredentials) {
690
+ //Cleanup logic
685
691
  await crowdinApp.deleteMetadata(organization);
686
692
  }
687
693
 
@@ -1,4 +1,4 @@
1
1
  /// <reference types="qs" />
2
2
  import { Response } from 'express';
3
- import { Config } from '../models';
4
- export default function handle(config: Config): (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;
3
+ import { Config, IntegrationLogic } from '../models';
4
+ export default function handle(config: Config, integration: IntegrationLogic): (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;
@@ -12,9 +12,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const storage_1 = require("../storage");
13
13
  const util_1 = require("../util");
14
14
  const connection_1 = require("../util/connection");
15
- function handle(config) {
15
+ function handle(config, integration) {
16
16
  return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
17
17
  (0, util_1.log)('Recieved integration logout request', config.logger);
18
+ if (integration.onLogout) {
19
+ (0, util_1.log)('Invoking onLogout hook', config.logger);
20
+ yield integration.onLogout(req.crowdinContext.jwtPayload.context.project_id, req.crowdinApiClient, req.integrationCredentials, req.integrationSettings);
21
+ }
22
+ (0, util_1.log)(`Deleting integration credentials for ${req.crowdinContext.clientId} client`, config.logger);
18
23
  yield (0, storage_1.getStorage)().deleteIntegrationCredentials(req.crowdinContext.clientId);
19
24
  (0, connection_1.clearCache)(req.crowdinContext.crowdinId);
20
25
  res.status(204).end();
@@ -28,7 +28,7 @@ function handle(config, integration) {
28
28
  let credentials;
29
29
  if (oauthLogin === null || oauthLogin === void 0 ? void 0 : oauthLogin.performGetTokenRequest) {
30
30
  (0, util_1.log)('Performing custom get bearer token request', config.logger);
31
- credentials = yield oauthLogin.performGetTokenRequest(code);
31
+ credentials = yield oauthLogin.performGetTokenRequest(code, req.query, req.originalUrl);
32
32
  }
33
33
  else {
34
34
  const request = {};
@@ -11,15 +11,35 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const storage_1 = require("../storage");
13
13
  const util_1 = require("../util");
14
+ const connection_1 = require("../util/connection");
14
15
  function handle(config) {
15
16
  return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
16
17
  const event = req.body;
17
18
  (0, util_1.log)(`Recieved uninstall request ${JSON.stringify(event, null, 2)}`, config.logger);
19
+ const projectIntegration = config.projectIntegration;
18
20
  const organization = (event.domain || event.organizationId).toString();
19
- yield (0, storage_1.getStorage)().deleteCrowdinCredentials(organization);
20
21
  if (config.onUninstall) {
21
- yield config.onUninstall(organization);
22
+ let allCredentials = [];
23
+ if (projectIntegration) {
24
+ const loadedCredentials = yield (0, storage_1.getStorage)().getAllIntegrationCredentials(organization);
25
+ allCredentials = yield Promise.all(loadedCredentials.map((creds) => __awaiter(this, void 0, void 0, function* () {
26
+ let settings;
27
+ if (creds.config) {
28
+ settings = JSON.parse(creds.config);
29
+ }
30
+ const credentials = yield (0, connection_1.prepareIntegrationCredentials)(config, projectIntegration, creds);
31
+ return { settings, credentials };
32
+ })));
33
+ }
34
+ (0, util_1.log)('Invoking onUninstall hook', config.logger);
35
+ yield config.onUninstall(organization, allCredentials);
36
+ }
37
+ if (projectIntegration) {
38
+ (0, util_1.log)(`Deleting all integration credentials for ${organization} organization`, config.logger);
39
+ yield (0, storage_1.getStorage)().deleteAllIntegrationCredentials(organization);
22
40
  }
41
+ (0, util_1.log)(`Deleting crowdin credentials for ${organization} organization`, config.logger);
42
+ yield (0, storage_1.getStorage)().deleteCrowdinCredentials(organization);
23
43
  (0, util_1.log)('App has been uninstalled', config.logger);
24
44
  res.status(204).end();
25
45
  }), config.onError);
package/out/index.js CHANGED
@@ -106,7 +106,7 @@ function addCrowdinEndpoints(app, config) {
106
106
  app.get('/api/subscription-info', json_response_1.default, (0, crowdin_client_1.default)(config), (0, subscription_info_1.default)(config));
107
107
  app.post('/api/settings', (0, crowdin_client_1.default)(config), (0, integration_credentials_1.default)(config, integrationLogic), (0, settings_save_1.default)(config));
108
108
  app.post('/api/login', (0, crowdin_client_1.default)(config, false, false), (0, integration_login_1.default)(config, integrationLogic));
109
- app.post('/api/logout', (0, crowdin_client_1.default)(config, false, false), (0, integration_logout_1.default)(config));
109
+ app.post('/api/logout', (0, crowdin_client_1.default)(config, false, false), (0, integration_credentials_1.default)(config, integrationLogic), (0, integration_logout_1.default)(config, integrationLogic));
110
110
  app.get('/api/crowdin/files', json_response_1.default, (0, crowdin_client_1.default)(config), (0, integration_credentials_1.default)(config, integrationLogic), (0, crowdin_files_1.default)(config, integrationLogic));
111
111
  app.get('/api/crowdin/project', json_response_1.default, (0, crowdin_client_1.default)(config), (0, crowdin_project_1.default)(config));
112
112
  app.get('/api/crowdin/file-progress/:fileId', (0, crowdin_client_1.default)(config), (0, crowdin_file_progress_1.default)(config));
@@ -95,7 +95,10 @@ export interface Config extends ImagePath {
95
95
  /**
96
96
  * Uninstall hook for cleanup logic
97
97
  */
98
- onUninstall?: (organization: string) => Promise<void>;
98
+ onUninstall?: (organization: string, allCredentials: {
99
+ settings?: any;
100
+ credentials: any;
101
+ }[]) => Promise<void>;
99
102
  /**
100
103
  * Error interceptor (can be used to log error in centralized place)
101
104
  */
@@ -170,6 +173,10 @@ export interface IntegrationLogic {
170
173
  * function to define configuration(settings) modal for you app (by default app will not have any custom settings)
171
174
  */
172
175
  getConfiguration?: (projectId: number, client: Crowdin, apiCredentials: any) => Promise<ConfigurationModalEntity[]>;
176
+ /**
177
+ * Logout hook for cleanup logic
178
+ */
179
+ onLogout?: (projectId: number, client: Crowdin, apiCredentials: any, config?: any) => Promise<void>;
173
180
  /**
174
181
  * flag to turn on auto reload of the tree whenever user updates the configuration
175
182
  */
@@ -317,7 +324,9 @@ export interface OAuthLogin {
317
324
  /**
318
325
  * Override to implement request for retrieving access token (and refresh token if 'refresh' is enabled)
319
326
  */
320
- performGetTokenRequest?: (code: string) => Promise<any>;
327
+ performGetTokenRequest?: (code: string, query: {
328
+ [key: string]: any;
329
+ }, url: string) => Promise<any>;
321
330
  /**
322
331
  * Override to implement request for refreshing token (only if 'refresh' is enabled)
323
332
  */
@@ -12,6 +12,7 @@ export interface Storage<T> {
12
12
  getIntegrationCredentials(id: string): Promise<IntegrationCredentials | undefined>;
13
13
  getAllIntegrationCredentials(crowdinId: string): Promise<IntegrationCredentials[]>;
14
14
  deleteIntegrationCredentials(id: string): Promise<void>;
15
+ deleteAllIntegrationCredentials(crowdinId: string): Promise<void>;
15
16
  saveMetadata(id: string, metadata: any): Promise<void>;
16
17
  updateMetadata(id: string, metadata: any): Promise<void>;
17
18
  getMetadata(id: string): Promise<any | undefined>;
@@ -26,6 +26,7 @@ export declare class MySQLStorage implements Storage<MySQLStorageConfig> {
26
26
  getIntegrationCredentials(id: string): Promise<IntegrationCredentials | undefined>;
27
27
  getAllIntegrationCredentials(crowdinId: string): Promise<IntegrationCredentials[]>;
28
28
  deleteIntegrationCredentials(id: string): Promise<void>;
29
+ deleteAllIntegrationCredentials(crowdinId: string): Promise<void>;
29
30
  saveMetadata(id: string, metadata: any): Promise<void>;
30
31
  updateMetadata(id: string, metadata: any): Promise<void>;
31
32
  getMetadata(id: string): Promise<any>;
@@ -189,6 +189,14 @@ class MySQLStorage {
189
189
  yield ((_b = this.connection) === null || _b === void 0 ? void 0 : _b.execute('DELETE FROM sync_settings where integration_id = ?', [id]));
190
190
  });
191
191
  }
192
+ deleteAllIntegrationCredentials(crowdinId) {
193
+ var _a, _b;
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('DELETE FROM integration_credentials where crowdin_id = ?', [crowdinId]));
197
+ yield ((_b = this.connection) === null || _b === void 0 ? void 0 : _b.execute('DELETE FROM sync_settings where crowdin_id = ?', [crowdinId]));
198
+ });
199
+ }
192
200
  saveMetadata(id, metadata) {
193
201
  var _a;
194
202
  return __awaiter(this, void 0, void 0, function* () {
@@ -30,6 +30,7 @@ export declare class PostgreStorage implements Storage<PostgreStorageConfig> {
30
30
  getIntegrationCredentials(id: string): Promise<IntegrationCredentials | undefined>;
31
31
  getAllIntegrationCredentials(crowdinId: string): Promise<IntegrationCredentials[]>;
32
32
  deleteIntegrationCredentials(id: string): Promise<void>;
33
+ deleteAllIntegrationCredentials(crowdinId: string): Promise<void>;
33
34
  saveMetadata(id: string, metadata: any): Promise<void>;
34
35
  updateMetadata(id: string, metadata: any): Promise<void>;
35
36
  getMetadata(id: string): Promise<any>;
@@ -188,6 +188,14 @@ class PostgreStorage {
188
188
  yield ((_b = this.client) === null || _b === void 0 ? void 0 : _b.query('DELETE FROM sync_settings where integration_id = $1', [id]));
189
189
  });
190
190
  }
191
+ deleteAllIntegrationCredentials(crowdinId) {
192
+ var _a, _b;
193
+ return __awaiter(this, void 0, void 0, function* () {
194
+ yield this.dbPromise;
195
+ yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.query('DELETE FROM integration_credentials where crowdin_id = $1', [crowdinId]));
196
+ yield ((_b = this.client) === null || _b === void 0 ? void 0 : _b.query('DELETE FROM sync_settings where crowdin_id = $1', [crowdinId]));
197
+ });
198
+ }
191
199
  saveMetadata(id, metadata) {
192
200
  var _a;
193
201
  return __awaiter(this, void 0, void 0, function* () {
@@ -25,6 +25,7 @@ export declare class SQLiteStorage implements Storage<SQLiteStorageConfig> {
25
25
  getIntegrationCredentials(id: string): Promise<IntegrationCredentials | undefined>;
26
26
  getAllIntegrationCredentials(crowdinId: string): Promise<IntegrationCredentials[]>;
27
27
  deleteIntegrationCredentials(id: string): Promise<void>;
28
+ deleteAllIntegrationCredentials(crowdinId: string): Promise<void>;
28
29
  saveMetadata(id: string, metadata: any): Promise<void>;
29
30
  updateMetadata(id: string, metadata: any): Promise<void>;
30
31
  getMetadata(id: string): Promise<any>;
@@ -253,6 +253,12 @@ class SQLiteStorage {
253
253
  yield this.run('DELETE FROM sync_settings where integration_id = ?', [id]);
254
254
  });
255
255
  }
256
+ deleteAllIntegrationCredentials(crowdinId) {
257
+ return __awaiter(this, void 0, void 0, function* () {
258
+ yield this.run('DELETE FROM integration_credentials where crowdin_id = ?', [crowdinId]);
259
+ yield this.run('DELETE FROM sync_settings where crowdin_id = ?', [crowdinId]);
260
+ });
261
+ }
256
262
  saveMetadata(id, metadata) {
257
263
  return this.run('INSERT INTO app_metadata(id, data) VALUES (?, ?)', [id, JSON.stringify(metadata)]);
258
264
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdin/app-project-module",
3
- "version": "0.20.0",
3
+ "version": "0.20.2",
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",