@oondemand/oon-core-back 0.3.5 → 0.3.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oondemand/oon-core-back",
3
- "version": "0.3.5",
3
+ "version": "0.3.7",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/oondemand/oon-platform.git",
@@ -3,29 +3,40 @@ const ctx = require("../context");
3
3
 
4
4
  const DEFAULT_CENTRAL_ATIVACAO_URL =
5
5
  "https://central-ativacao.central.oondemand.online";
6
+ const DEFAULT_CENTRAL_ATIVACAO_API_URL =
7
+ "https://api.central-ativacao.central.oondemand.online";
6
8
  const LOGIN_PATH = "/integracao/autenticacao-aplicativos/autenticar";
7
9
  const PERMISSION_PATH = "/integracao/autenticacao-aplicativos/verificar-permissao";
8
10
 
9
- function readCentralActivationUrl() {
10
- return (
11
- process.env.CENTRAL_ATIVACAO_URL ||
12
- process.env.MEUS_APPS_BACKEND_URL ||
13
- DEFAULT_CENTRAL_ATIVACAO_URL
14
- );
11
+ function uniq(values) {
12
+ return [...new Set(values.filter(Boolean))];
15
13
  }
16
14
 
17
- function getBaseUrl(req) {
18
- const value = readCentralActivationUrl();
19
-
15
+ function normalizeBaseUrl(value, label) {
20
16
  let parsed;
21
17
  try {
22
18
  parsed = new URL(value);
23
19
  } catch {
24
- throw new GenericError("CENTRAL_ATIVACAO_URL inválida.", {
20
+ throw new GenericError(`${label} inválida.`, {
25
21
  statusCode: 500,
26
22
  });
27
23
  }
28
24
 
25
+ return parsed.toString().replace(/\/$/, "");
26
+ }
27
+
28
+ function withApiPath(value) {
29
+ const parsed = new URL(value);
30
+ const cleanPath = parsed.pathname.replace(/\/$/, "");
31
+
32
+ if (cleanPath.endsWith("/api")) return null;
33
+
34
+ parsed.pathname = `${cleanPath}/api`.replace(/^\/\//, "/");
35
+ return parsed.toString().replace(/\/$/, "");
36
+ }
37
+
38
+ function assertNotSameHost(url, req) {
39
+ const parsed = new URL(url);
29
40
  const currentHost = String(
30
41
  req?.headers?.["x-forwarded-host"] || req?.headers?.host || "",
31
42
  )
@@ -43,8 +54,68 @@ function getBaseUrl(req) {
43
54
  { statusCode: 500 },
44
55
  );
45
56
  }
57
+ }
46
58
 
47
- return parsed.toString().replace(/\/$/, "");
59
+ function readCentralActivationPublicUrl() {
60
+ return (
61
+ process.env.CENTRAL_ATIVACAO_URL ||
62
+ DEFAULT_CENTRAL_ATIVACAO_URL
63
+ );
64
+ }
65
+
66
+ function readCentralActivationApiUrl() {
67
+ return (
68
+ process.env.CENTRAL_ATIVACAO_API_URL ||
69
+ process.env.CENTRAL_ATIVACAO_BACKEND_URL ||
70
+ process.env.MEUS_APPS_BACKEND_URL ||
71
+ DEFAULT_CENTRAL_ATIVACAO_API_URL
72
+ );
73
+ }
74
+
75
+ function getBaseUrl(req) {
76
+ const value = normalizeBaseUrl(
77
+ readCentralActivationApiUrl(),
78
+ "CENTRAL_ATIVACAO_API_URL/CENTRAL_ATIVACAO_BACKEND_URL",
79
+ );
80
+ assertNotSameHost(value, req);
81
+ return value;
82
+ }
83
+
84
+ function getDisplayUrl(req) {
85
+ const value = normalizeBaseUrl(
86
+ readCentralActivationPublicUrl(),
87
+ "CENTRAL_ATIVACAO_URL",
88
+ );
89
+ assertNotSameHost(value, req);
90
+ return value;
91
+ }
92
+
93
+ function getBaseUrlCandidates(req) {
94
+ const explicitApi =
95
+ process.env.CENTRAL_ATIVACAO_API_URL ||
96
+ process.env.CENTRAL_ATIVACAO_BACKEND_URL ||
97
+ process.env.MEUS_APPS_BACKEND_URL;
98
+ const raw = uniq([
99
+ process.env.CENTRAL_ATIVACAO_API_URL,
100
+ process.env.CENTRAL_ATIVACAO_BACKEND_URL,
101
+ process.env.MEUS_APPS_BACKEND_URL,
102
+ DEFAULT_CENTRAL_ATIVACAO_API_URL,
103
+ process.env.CENTRAL_ATIVACAO_URL,
104
+ DEFAULT_CENTRAL_ATIVACAO_URL,
105
+ ]);
106
+
107
+ const normalized = raw.map((value) =>
108
+ normalizeBaseUrl(value, "CENTRAL_ATIVACAO_API_URL/CENTRAL_ATIVACAO_URL"),
109
+ );
110
+
111
+ const expanded = explicitApi
112
+ ? normalized
113
+ : normalized.flatMap((value) => [value, withApiPath(value)]);
114
+
115
+ return uniq(expanded).map((url) => {
116
+ assertNotSameHost(url, req);
117
+ return url;
118
+ });
48
119
  }
49
120
 
50
121
  function getAppCode() {
@@ -76,9 +147,12 @@ function getTimeout() {
76
147
 
77
148
  module.exports = {
78
149
  DEFAULT_CENTRAL_ATIVACAO_URL,
150
+ DEFAULT_CENTRAL_ATIVACAO_API_URL,
79
151
  LOGIN_PATH,
80
152
  PERMISSION_PATH,
81
153
  getBaseUrl,
154
+ getDisplayUrl,
155
+ getBaseUrlCandidates,
82
156
  getAppCode,
83
157
  getTimeout,
84
158
  };
@@ -4,7 +4,12 @@ const axios = require("axios");
4
4
  const ctx = require("../context");
5
5
  const GenericError = require("../errors/GenericError");
6
6
  const Instancia = require("../models/InstanciaEcossistema");
7
- const { getBaseUrl, getAppCode, getTimeout } = require("./activationProvider");
7
+ const {
8
+ getBaseUrl,
9
+ getBaseUrlCandidates,
10
+ getAppCode,
11
+ getTimeout,
12
+ } = require("./activationProvider");
8
13
 
9
14
  const PATHS = {
10
15
  validar: "/integracao/ativacao-instancias/validar-codigo",
@@ -430,12 +435,28 @@ function validateParams(globalParameters = {}, appParameters = {}) {
430
435
  }
431
436
  }
432
437
 
438
+ function shouldTryNextProviderUrl(error) {
439
+ return [404, 405].includes(Number(error.response?.status));
440
+ }
441
+
433
442
  async function providerPost(req, path, payload, headers = {}) {
434
- return axios.post(`${getBaseUrl(req)}${path}`, payload, {
435
- timeout: getTimeout(),
436
- maxRedirects: 0,
437
- headers: { "Content-Type": "application/json", ...headers },
438
- });
443
+ const baseUrls = getBaseUrlCandidates(req);
444
+ let lastError;
445
+
446
+ for (const baseUrl of baseUrls) {
447
+ try {
448
+ return await axios.post(`${baseUrl}${path}`, payload, {
449
+ timeout: getTimeout(),
450
+ maxRedirects: 0,
451
+ headers: { "Content-Type": "application/json", ...headers },
452
+ });
453
+ } catch (error) {
454
+ lastError = error;
455
+ if (!shouldTryNextProviderUrl(error)) throw error;
456
+ }
457
+ }
458
+
459
+ throw lastError;
439
460
  }
440
461
 
441
462
  function providerPayload(req, codigoAtivacao, extra = {}) {