@oondemand/oon-core-back 0.3.5 → 0.3.6

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.6",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/oondemand/oon-platform.git",
@@ -6,26 +6,35 @@ const DEFAULT_CENTRAL_ATIVACAO_URL =
6
6
  const LOGIN_PATH = "/integracao/autenticacao-aplicativos/autenticar";
7
7
  const PERMISSION_PATH = "/integracao/autenticacao-aplicativos/verificar-permissao";
8
8
 
9
- function readCentralActivationUrl() {
10
- return (
11
- process.env.CENTRAL_ATIVACAO_URL ||
12
- process.env.MEUS_APPS_BACKEND_URL ||
13
- DEFAULT_CENTRAL_ATIVACAO_URL
14
- );
9
+ function uniq(values) {
10
+ return [...new Set(values.filter(Boolean))];
15
11
  }
16
12
 
17
- function getBaseUrl(req) {
18
- const value = readCentralActivationUrl();
19
-
13
+ function normalizeBaseUrl(value, label) {
20
14
  let parsed;
21
15
  try {
22
16
  parsed = new URL(value);
23
17
  } catch {
24
- throw new GenericError("CENTRAL_ATIVACAO_URL inválida.", {
18
+ throw new GenericError(`${label} inválida.`, {
25
19
  statusCode: 500,
26
20
  });
27
21
  }
28
22
 
23
+ return parsed.toString().replace(/\/$/, "");
24
+ }
25
+
26
+ function withApiPath(value) {
27
+ const parsed = new URL(value);
28
+ const cleanPath = parsed.pathname.replace(/\/$/, "");
29
+
30
+ if (cleanPath.endsWith("/api")) return null;
31
+
32
+ parsed.pathname = `${cleanPath}/api`.replace(/^\/\//, "/");
33
+ return parsed.toString().replace(/\/$/, "");
34
+ }
35
+
36
+ function assertNotSameHost(url, req) {
37
+ const parsed = new URL(url);
29
38
  const currentHost = String(
30
39
  req?.headers?.["x-forwarded-host"] || req?.headers?.host || "",
31
40
  )
@@ -43,8 +52,66 @@ function getBaseUrl(req) {
43
52
  { statusCode: 500 },
44
53
  );
45
54
  }
55
+ }
46
56
 
47
- return parsed.toString().replace(/\/$/, "");
57
+ function readCentralActivationPublicUrl() {
58
+ return (
59
+ process.env.CENTRAL_ATIVACAO_URL ||
60
+ process.env.MEUS_APPS_BACKEND_URL ||
61
+ DEFAULT_CENTRAL_ATIVACAO_URL
62
+ );
63
+ }
64
+
65
+ function readCentralActivationApiUrl() {
66
+ return (
67
+ process.env.CENTRAL_ATIVACAO_API_URL ||
68
+ process.env.CENTRAL_ATIVACAO_BACKEND_URL ||
69
+ process.env.CENTRAL_ATIVACAO_URL ||
70
+ process.env.MEUS_APPS_BACKEND_URL ||
71
+ DEFAULT_CENTRAL_ATIVACAO_URL
72
+ );
73
+ }
74
+
75
+ function getBaseUrl(req) {
76
+ const value = normalizeBaseUrl(
77
+ readCentralActivationApiUrl(),
78
+ "CENTRAL_ATIVACAO_API_URL/CENTRAL_ATIVACAO_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 = process.env.CENTRAL_ATIVACAO_API_URL;
95
+ const raw = uniq([
96
+ process.env.CENTRAL_ATIVACAO_API_URL,
97
+ process.env.CENTRAL_ATIVACAO_BACKEND_URL,
98
+ process.env.CENTRAL_ATIVACAO_URL,
99
+ process.env.MEUS_APPS_BACKEND_URL,
100
+ DEFAULT_CENTRAL_ATIVACAO_URL,
101
+ ]);
102
+
103
+ const normalized = raw.map((value) =>
104
+ normalizeBaseUrl(value, "CENTRAL_ATIVACAO_API_URL/CENTRAL_ATIVACAO_URL"),
105
+ );
106
+
107
+ const expanded = explicitApi
108
+ ? normalized
109
+ : normalized.flatMap((value) => [value, withApiPath(value)]);
110
+
111
+ return uniq(expanded).map((url) => {
112
+ assertNotSameHost(url, req);
113
+ return url;
114
+ });
48
115
  }
49
116
 
50
117
  function getAppCode() {
@@ -79,6 +146,8 @@ module.exports = {
79
146
  LOGIN_PATH,
80
147
  PERMISSION_PATH,
81
148
  getBaseUrl,
149
+ getDisplayUrl,
150
+ getBaseUrlCandidates,
82
151
  getAppCode,
83
152
  getTimeout,
84
153
  };
@@ -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 = {}) {