1
1
const fetch = require ( "node-fetch" ) ;
2
+
2
3
const headersDefault = {
3
4
"x-forwarded-proto" : "https,http,http" ,
4
5
"x-forwarded-port" : "443,80,80" ,
@@ -11,14 +12,21 @@ function fetchGet(url, header, callback) {
11
12
method : "GET" ,
12
13
headers : newHeaders ,
13
14
} ;
14
- console . log ( "FETCH GET" , url ) ;
15
+
16
+ console . log ( "FETCH GET:" , url ) ;
15
17
fetch ( url , requestOptions )
16
18
. then ( ( response ) => {
19
+ console . log ( "Status da resposta:" , response . status , response . statusText ) ;
17
20
const contentType = response . headers . get ( "content-type" ) ;
21
+ console . log ( "Tipo de conteúdo:" , contentType ) ;
22
+
18
23
// Verifica se houve erro na resposta
19
24
if ( ! response . ok ) {
20
- throw new Error ( `${ response . status } ${ response . statusText } ` ) ;
25
+ return response . json ( ) . then ( ( errorData ) => {
26
+ throw new Error ( `Erro na resposta do servidor: ${ JSON . stringify ( errorData , null , 2 ) } ` ) ;
27
+ } ) ;
21
28
}
29
+
22
30
// Verifica o tipo de conteúdo retornado
23
31
if ( contentType && contentType . includes ( "application/json" ) ) {
24
32
// Se for JSON, retorna o JSON
@@ -30,6 +38,7 @@ function fetchGet(url, header, callback) {
30
38
} )
31
39
. then ( ( data ) => {
32
40
console . log ( "FETCH GET RECEBIDO! OK 200" ) ;
41
+ console . log ( "Dados recebidos:" , data ) ;
33
42
callback ( null , data ) ;
34
43
} )
35
44
. catch ( ( error ) => {
@@ -43,28 +52,34 @@ function fetchPost(url, payload, header, callback) {
43
52
"content-type" : `application/json; charset=UTF-8` ,
44
53
} ;
45
54
var newHeaders = headersDefault ;
46
- newHeaders = Object . assign ( headersDefault , header || defaultContentType ) ;
55
+ newHeaders = Object . assign ( headersDefault , header || defaultContentType ) ;
47
56
const requestOptions = {
48
57
method : "POST" ,
49
58
headers : newHeaders ,
50
59
body : payload ,
51
60
} ;
52
-
53
- if ( newHeaders [ ' content-type' ] == "application/json; charset=UTF-8" ) {
54
- console . log ( "Convertendo payload para json !" ) ;
55
- payload = JSON . stringify ( payload ) ;
56
- }
57
-
61
+
62
+ if ( newHeaders [ " content-type" ] == "application/json; charset=UTF-8" ) {
63
+ console . log ( "Convertendo payload para JSON !" ) ;
64
+ requestOptions . body = JSON . stringify ( payload ) ;
65
+ }
66
+
58
67
console . log ( "FETCH POST" , url ) ;
68
+ console . log ( "FETCH POST PAYLOAD: " , requestOptions . body ) ;
59
69
fetch ( url , requestOptions )
60
70
. then ( ( response ) => {
71
+ console . log ( "Status da resposta:" , response . status , response . statusText ) ;
61
72
const contentType = response . headers . get ( "content-type" ) ;
73
+ console . log ( "Tipo de conteúdo:" , contentType ) ;
74
+
62
75
// Verifica se houve erro na resposta
63
76
if ( ! response . ok ) {
64
- console . error ( response ) ;
65
- throw new Error ( `${ response . status } ${ response . statusText } ` ) ;
77
+ return response . json ( ) . then ( ( errorData ) => {
78
+ throw new Error ( JSON . stringify ( errorData , null , 2 ) ) ;
79
+ } ) ;
66
80
}
67
- // Verifica o tipo de conteúdo retornado
81
+
82
+ // // Verifica o tipo de conteúdo retornado
68
83
if ( contentType && contentType . includes ( "application/json" ) ) {
69
84
// Se for JSON, retorna o JSON
70
85
return response . json ( ) ;
@@ -75,6 +90,7 @@ function fetchPost(url, payload, header, callback) {
75
90
} )
76
91
. then ( ( data ) => {
77
92
console . log ( "FETCH POST ENVIADO! OK 200" ) ;
93
+ console . log ( "Dados recebidos:" , data ) ;
78
94
callback ( null , data ) ;
79
95
} )
80
96
. catch ( ( error ) => {
@@ -83,4 +99,4 @@ function fetchPost(url, payload, header, callback) {
83
99
} ) ;
84
100
}
85
101
86
- module . exports = { fetchGet, fetchPost } ;
102
+ module . exports = { fetchGet, fetchPost } ;
0 commit comments