@@ -10,6 +10,14 @@ import { Response } from "node-fetch";
10
10
11
11
import { Region , RegionType } from "../types" ;
12
12
13
+ const filterHeaders = [
14
+ "api_key" ,
15
+ "authorization" ,
16
+ "auth_token" ,
17
+ "x-api-key" ,
18
+ "user-agent" ,
19
+ ] ;
20
+
13
21
export function onData < Data extends Record < string , any > > ( data : { data : Data } ) {
14
22
if ( typeof data . data === "string" ) {
15
23
return Promise . reject ( data . data ) ;
@@ -21,58 +29,38 @@ export function onError(error: Error) {
21
29
return Promise . reject ( error ) ;
22
30
}
23
31
24
- export const createAxiosErrorResponse = ( error : AxiosError ) : Response => {
25
- const { response, message, config } = error ;
32
+ export function sanitizeResponseHeader ( axiosHeaders ) {
33
+ const fetchHeaders = new Headers ( ) ;
34
+ for ( const key in axiosHeaders ) {
35
+ if ( axiosHeaders . hasOwnProperty ( key ) && ! filterHeaders . includes ( key ) ) {
36
+ fetchHeaders . append ( key , axiosHeaders [ key ] ) ;
37
+ }
38
+ }
39
+ return fetchHeaders ;
40
+ } ;
41
+ export const handleApiError = ( error : AxiosResponse | AxiosError ) : Response => {
42
+ // Extract relevant information from the error
43
+ const isServerError = ( error as AxiosResponse ) . status >= 500 ;
44
+ const responseBody = isServerError
45
+ ? ( error as AxiosError ) . stack || "Internal Server Error"
46
+ : ( error as AxiosResponse ) . data || "An error occurred" ;
47
+
48
+ const status = ( error as AxiosResponse ) . status || 500 ;
49
+ const statusText =
50
+ isServerError
51
+ ? ( error as AxiosError ) . message || "Internal Server Error"
52
+ : ( error as AxiosResponse ) . statusText || "Error" ;
26
53
27
- const responseBody = response ?. data || { message } ;
28
- const status = response ?. status || 500 ;
29
- const statusText = response ?. statusText || "Internal Server Error" ;
30
54
const headers = new Headers (
31
- sanitizeResponseHeader ( config ? .headers || { } )
55
+ sanitizeResponseHeader ( ( error as AxiosResponse ) . headers || { } )
32
56
) ;
33
57
34
58
return new Response ( JSON . stringify ( responseBody ) , {
35
59
status,
36
60
statusText,
37
61
headers,
38
62
} ) ;
39
- } ;
40
-
41
- export const sanitizeResponseHeader = ( headers : RawAxiosRequestHeaders ) => {
42
- const responseHeaders = new Headers ( ) ;
43
- const filterHeaders = [
44
- "api_key" ,
45
- "authorization" ,
46
- "x-api-key" ,
47
- "user-agent" ,
48
- ] ;
49
- if ( headers instanceof Headers ) {
50
- headers . forEach ( ( value , key ) => {
51
- if ( ! filterHeaders . includes ( key . toLowerCase ( ) ) ) {
52
- responseHeaders . set ( key , value ) ;
53
- }
54
- } ) ;
55
- }
56
- return responseHeaders ;
57
- } ;
58
-
59
- export const handleApiError = ( error : unknown ) : Response => {
60
- return isAxiosError ( error )
61
- ? createErrorResponse ( createAxiosErrorResponse ( error ) )
62
- : createErrorResponse ( error as Error ) ;
63
- } ;
64
-
65
- export const createErrorResponse = ( error : Error ) : Response => {
66
- return new Response (
67
- JSON . stringify ( { message : ( error ) . message || error } ) ,
68
- {
69
- status : 500 ,
70
- statusText : "Internal Server Error" ,
71
- headers : new Headers ( ) ,
72
- }
73
- ) ;
74
- } ;
75
-
63
+ }
76
64
export function formatAppRegion ( region : string ) : RegionType {
77
65
return region ?? Region . UNKNOWN ;
78
66
}
@@ -145,13 +133,3 @@ export const fetchToAxiosConfig = (
145
133
146
134
return axiosConfig ;
147
135
} ;
148
-
149
- export const serializeAxiosResponse = ( responseData : AxiosResponse , config ) => {
150
- return {
151
- data : responseData . data ,
152
- status : responseData . status ,
153
- statusText : responseData . statusText ,
154
- headers : responseData . headers as AxiosHeaders ,
155
- config,
156
- } ;
157
- } ;
0 commit comments