@@ -10,6 +10,14 @@ import { Response } from "node-fetch";
1010
1111import { Region , RegionType } from "../types" ;
1212
13+ const filterHeaders = [
14+ "api_key" ,
15+ "authorization" ,
16+ "auth_token" ,
17+ "x-api-key" ,
18+ "user-agent" ,
19+ ] ;
20+
1321export function onData < Data extends Record < string , any > > ( data : { data : Data } ) {
1422 if ( typeof data . data === "string" ) {
1523 return Promise . reject ( data . data ) ;
@@ -21,58 +29,38 @@ export function onError(error: Error) {
2129 return Promise . reject ( error ) ;
2230}
2331
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" ;
2653
27- const responseBody = response ?. data || { message } ;
28- const status = response ?. status || 500 ;
29- const statusText = response ?. statusText || "Internal Server Error" ;
3054 const headers = new Headers (
31- sanitizeResponseHeader ( config ? .headers || { } )
55+ sanitizeResponseHeader ( ( error as AxiosResponse ) . headers || { } )
3256 ) ;
3357
3458 return new Response ( JSON . stringify ( responseBody ) , {
3559 status,
3660 statusText,
3761 headers,
3862 } ) ;
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+ }
7664export function formatAppRegion ( region : string ) : RegionType {
7765 return region ?? Region . UNKNOWN ;
7866}
@@ -145,13 +133,3 @@ export const fetchToAxiosConfig = (
145133
146134 return axiosConfig ;
147135} ;
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