http-sankhya 1.0.4 → 1.0.5
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/dist/Sankhya.js +3 -2
- package/dist/SankhyaHelper.d.ts +10 -0
- package/dist/SankhyaHelper.js +49 -3
- package/package.json +1 -1
package/dist/Sankhya.js
CHANGED
|
@@ -203,10 +203,11 @@ class Sankhya {
|
|
|
203
203
|
serviceName,
|
|
204
204
|
requestBody: transformedBody
|
|
205
205
|
};
|
|
206
|
-
|
|
206
|
+
const response = await this.post('/gateway/v1/mgecom/service.sbr', payload, {
|
|
207
207
|
serviceName,
|
|
208
208
|
outputType
|
|
209
|
-
}, {});
|
|
209
|
+
}, { 'Content-Type': 'application/json' });
|
|
210
|
+
return SankhyaHelper_1.SankhyaHelper.flattenMgeComResponse(response);
|
|
210
211
|
}
|
|
211
212
|
isEmptyObject(obj) {
|
|
212
213
|
return obj != null &&
|
package/dist/SankhyaHelper.d.ts
CHANGED
|
@@ -6,9 +6,19 @@ export declare class SankhyaHelper {
|
|
|
6
6
|
static processResponse(response: any, options?: {
|
|
7
7
|
serviceName?: string;
|
|
8
8
|
}): any;
|
|
9
|
+
/**
|
|
10
|
+
* Remove recursivamente os wrappers { "$": "valor" } do retorno do Sankhya,
|
|
11
|
+
* retornando um JSON limpo e amigável.
|
|
12
|
+
* Utilizado para tratar retornos do endpoint mgecom (ex: CACSP.incluirNota).
|
|
13
|
+
*/
|
|
14
|
+
static flattenMgeComResponse(obj: any): any;
|
|
9
15
|
/**
|
|
10
16
|
* Transforma recursivamente todos os valores primitivos (string/number) de um objeto
|
|
11
17
|
* para o formato Sankhya { "$": valor }, percorrendo objetos e arrays aninhados.
|
|
18
|
+
*
|
|
19
|
+
* Strings que são "irmãs" de arrays no mesmo objeto NÃO são encapsuladas,
|
|
20
|
+
* pois representam atributos XML (ex: INFORMARPRECO em "itens").
|
|
21
|
+
*
|
|
12
22
|
* Ex: { nota: { cabecalho: { CAMPO: "VALOR" } } } -> { nota: { cabecalho: { CAMPO: { "$": "VALOR" } } } }
|
|
13
23
|
*/
|
|
14
24
|
static transformDeepFields(obj: any): any;
|
package/dist/SankhyaHelper.js
CHANGED
|
@@ -22,16 +22,51 @@ class SankhyaHelper {
|
|
|
22
22
|
return response;
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Remove recursivamente os wrappers { "$": "valor" } do retorno do Sankhya,
|
|
27
|
+
* retornando um JSON limpo e amigável.
|
|
28
|
+
* Utilizado para tratar retornos do endpoint mgecom (ex: CACSP.incluirNota).
|
|
29
|
+
*/
|
|
30
|
+
static flattenMgeComResponse(obj) {
|
|
31
|
+
if (obj === null || obj === undefined) {
|
|
32
|
+
return obj;
|
|
33
|
+
}
|
|
34
|
+
// Se for um objeto com apenas a chave "$", extrai o valor
|
|
35
|
+
if (typeof obj === 'object' && !Array.isArray(obj) && '$' in obj && Object.keys(obj).length === 1) {
|
|
36
|
+
return obj.$;
|
|
37
|
+
}
|
|
38
|
+
// Se for array, aplica recursivamente em cada elemento
|
|
39
|
+
if (Array.isArray(obj)) {
|
|
40
|
+
return obj.map(item => this.flattenMgeComResponse(item));
|
|
41
|
+
}
|
|
42
|
+
// Se for objeto, aplica recursivamente em cada propriedade
|
|
43
|
+
if (typeof obj === 'object') {
|
|
44
|
+
const result = {};
|
|
45
|
+
for (const key of Object.keys(obj)) {
|
|
46
|
+
result[key] = this.flattenMgeComResponse(obj[key]);
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
return obj;
|
|
51
|
+
}
|
|
25
52
|
/**
|
|
26
53
|
* Transforma recursivamente todos os valores primitivos (string/number) de um objeto
|
|
27
54
|
* para o formato Sankhya { "$": valor }, percorrendo objetos e arrays aninhados.
|
|
55
|
+
*
|
|
56
|
+
* Strings que são "irmãs" de arrays no mesmo objeto NÃO são encapsuladas,
|
|
57
|
+
* pois representam atributos XML (ex: INFORMARPRECO em "itens").
|
|
58
|
+
*
|
|
28
59
|
* Ex: { nota: { cabecalho: { CAMPO: "VALOR" } } } -> { nota: { cabecalho: { CAMPO: { "$": "VALOR" } } } }
|
|
29
60
|
*/
|
|
30
61
|
static transformDeepFields(obj) {
|
|
62
|
+
// Campos com valor null ou undefined são tratados como placeholders (ex: PKs)
|
|
63
|
+
// e enviados como objeto vazio {} no formato Sankhya
|
|
31
64
|
if (obj === null || obj === undefined) {
|
|
32
|
-
return
|
|
65
|
+
return {};
|
|
33
66
|
}
|
|
34
67
|
// Se for string ou number, transforma para { "$": valor }
|
|
68
|
+
// (este caso é chamado recursivamente; a decisão de NÃO transformar
|
|
69
|
+
// é feita no nível do objeto pai, abaixo)
|
|
35
70
|
if (typeof obj === 'string' || typeof obj === 'number') {
|
|
36
71
|
return { $: String(obj) };
|
|
37
72
|
}
|
|
@@ -45,9 +80,20 @@ class SankhyaHelper {
|
|
|
45
80
|
}
|
|
46
81
|
// Se for objeto, aplica recursivamente em cada propriedade
|
|
47
82
|
if (typeof obj === 'object') {
|
|
83
|
+
const keys = Object.keys(obj);
|
|
84
|
+
// Verifica se o objeto possui algum filho que é array.
|
|
85
|
+
// Se sim, strings neste nível são atributos XML e NÃO devem ser encapsuladas.
|
|
86
|
+
const hasArrayChild = keys.some(k => Array.isArray(obj[k]));
|
|
48
87
|
const transformed = {};
|
|
49
|
-
for (const key of
|
|
50
|
-
|
|
88
|
+
for (const key of keys) {
|
|
89
|
+
const value = obj[key];
|
|
90
|
+
if (hasArrayChild && (typeof value === 'string' || typeof value === 'number')) {
|
|
91
|
+
// Atributo XML: mantém como string pura
|
|
92
|
+
transformed[key] = value;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
transformed[key] = this.transformDeepFields(value);
|
|
96
|
+
}
|
|
51
97
|
}
|
|
52
98
|
return transformed;
|
|
53
99
|
}
|