@crowdin/app-project-module 0.6.1 → 0.6.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
@@ -357,6 +357,23 @@ configuration.integration.checkConnection = (credentials) => {
357
357
  };
358
358
  ```
359
359
 
360
+ Or if you need to manually control users liveness session you can throw an error with `401` code then your app will automatically do a log out action.
361
+ e.g. when your service has some specific session duration timeout or extra conditions which are not covered by this framework
362
+
363
+ ```javascript
364
+ configuration.integrartion.getIntegrationFiles = async (credentials, appSettings) => {
365
+ //do a request/custom logic here
366
+ const sessionStillValid = false;
367
+ if (!sessionStillValid) {
368
+ throw {
369
+ message: 'session expired',
370
+ code: 401
371
+ }
372
+ }
373
+ //business logic
374
+ }
375
+ ```
376
+
360
377
  ## Contributing
361
378
 
362
379
  If you want to contribute please read the [Contributing](/CONTRIBUTING.md) guidelines.
@@ -24,7 +24,13 @@ function handle(config, optional = false) {
24
24
  if (integrationCredentials.config) {
25
25
  req.integrationSettings = JSON.parse(integrationCredentials.config);
26
26
  }
27
- req.integrationCredentials = yield (0, util_1.prepareIntegrationCredentials)(config, integrationCredentials);
27
+ try {
28
+ req.integrationCredentials = yield (0, util_1.prepareIntegrationCredentials)(config, integrationCredentials);
29
+ }
30
+ catch (e) {
31
+ console.error(e);
32
+ throw new util_1.CodeError('Credentials to integration either exprired or invalid', 401);
33
+ }
28
34
  next();
29
35
  }));
30
36
  }
@@ -44,6 +44,10 @@ function showToast(message) {
44
44
  }
45
45
 
46
46
  function catchRejection(e, message) {
47
+ //session expired
48
+ if (e.code && e.code === 401) {
49
+ reloadLocation();
50
+ }
47
51
  showToast(e.message || message);
48
52
  }
49
53
 
@@ -1,7 +1,10 @@
1
1
  import Crowdin, { SourceFilesModel } from '@crowdin/crowdin-api-client';
2
2
  import { Request, Response } from 'express';
3
3
  import { Config, CronJob, CrowdinCredentials, IntegrationCredentials } from '../models';
4
- export declare function handleError(e: any, res: Response): void;
4
+ export declare class CodeError extends Error {
5
+ code: number | undefined;
6
+ constructor(message: string, code?: number);
7
+ }
5
8
  export declare function runAsyncWrapper(callback: Function): (req: Request, res: Response, next: Function) => void;
6
9
  export declare function encryptData(secret: string, data: string): string;
7
10
  export declare function decryptData(secret: string, data: string): string;
package/out/util/index.js CHANGED
@@ -31,26 +31,43 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
- exports.runJob = exports.prepareIntegrationCredentials = exports.prepareCrowdinClient = exports.applyDefaults = exports.getRootFolder = exports.getOauthRoute = exports.decryptData = exports.encryptData = exports.runAsyncWrapper = exports.handleError = void 0;
34
+ exports.runJob = exports.prepareIntegrationCredentials = exports.prepareCrowdinClient = exports.applyDefaults = exports.getRootFolder = exports.getOauthRoute = exports.decryptData = exports.encryptData = exports.runAsyncWrapper = exports.CodeError = void 0;
35
35
  const crowdin_api_client_1 = __importDefault(require("@crowdin/crowdin-api-client"));
36
36
  const crowdinAppFunctions = __importStar(require("@crowdin/crowdin-apps-functions"));
37
37
  const axios_1 = __importDefault(require("axios"));
38
38
  const crypto = __importStar(require("crypto-js"));
39
39
  const models_1 = require("../models");
40
40
  const storage_1 = require("../storage");
41
- function handleError(e, res) {
42
- console.error(e);
43
- if (typeof e === 'object') {
44
- res.status(500).send({ message: e.message ? e.message : JSON.stringify(e) });
45
- }
46
- else {
47
- res.status(500).send({ message: `${e}` });
41
+ class CodeError extends Error {
42
+ constructor(message, code) {
43
+ super(message);
44
+ this.code = code;
48
45
  }
49
46
  }
50
- exports.handleError = handleError;
47
+ exports.CodeError = CodeError;
48
+ function isCrowdinClientRequest(req) {
49
+ return req.crowdinContext;
50
+ }
51
+ function handleError(err, req, res) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ console.error(err);
54
+ const code = err.code ? err.code : 500;
55
+ if (code === 401 && isCrowdinClientRequest(req)) {
56
+ yield (0, storage_1.deleteIntegrationCredentials)(req.crowdinContext.clientId);
57
+ }
58
+ if (code === 401 && req.path === '/') {
59
+ res.redirect('/');
60
+ return;
61
+ }
62
+ res.status(code).send({
63
+ message: err.message ? err.message : JSON.stringify(err),
64
+ code,
65
+ });
66
+ });
67
+ }
51
68
  function runAsyncWrapper(callback) {
52
69
  return (req, res, next) => {
53
- callback(req, res, next).catch((e) => handleError(e, res));
70
+ callback(req, res, next).catch((e) => handleError(e, req, res));
54
71
  };
55
72
  }
56
73
  exports.runAsyncWrapper = runAsyncWrapper;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdin/app-project-module",
3
- "version": "0.6.1",
3
+ "version": "0.6.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",