1- import { ua2b64 } from '../model/ModelHelper'
2- import { NoAuthService } from '../../icc-x-api/auth/NoAuthService'
3- import { AuthService } from '../../icc-x-api/auth/AuthService'
1+ import { ua2b64 , ua2string } from '../model/ModelHelper'
2+ import { NoAuthService } from '../../icc-x-api/auth/NoAuthService'
3+ import { AuthService } from '../../icc-x-api/auth/AuthService'
4+ import { ua2utf8 } from "../../icc-x-api"
45
56export namespace XHR {
67 export class Header {
@@ -49,12 +50,12 @@ export namespace XHR {
4950 fetchImpl : ( input : RequestInfo , init ?: RequestInit ) => Promise < Response > = typeof window !== 'undefined'
5051 ? window . fetch
5152 : typeof self !== 'undefined'
52- ? self . fetch
53- : fetch
53+ ? self . fetch
54+ : fetch
5455 ) : Promise < Response > {
5556 return new Promise ( ( resolve , reject ) => {
5657 // Set timeout timer
57- let timer = setTimeout ( ( ) => reject ( { message : 'Request timed out' , status : 'Request timed out' } ) , timeout )
58+ let timer = setTimeout ( ( ) => reject ( { message : 'Request timed out' , status : 'Request timed out' } ) , timeout )
5859 fetchImpl ( url , init )
5960 . then ( ( response ) => {
6061 clearTimeout ( timer )
@@ -75,11 +76,12 @@ export namespace XHR {
7576 fetchImpl : ( input : RequestInfo , init ?: RequestInit ) => Promise < Response > = typeof window !== 'undefined'
7677 ? window . fetch
7778 : typeof self !== 'undefined'
78- ? self . fetch
79- : fetch ,
79+ ? self . fetch
80+ : fetch ,
8081 contentTypeOverride ?: 'application/json' | 'text/plain' | 'application/octet-stream' ,
8182 headerProvider : AuthService = new NoAuthService ( ) ,
82- minimumAuthenticationClass : number | undefined = undefined
83+ minimumAuthenticationClass : number | undefined = undefined ,
84+ tryHardToParseJson : boolean = false
8385 ) : Promise < Data > {
8486 const authHeaders = await headerProvider . getAuthHeaders ( minimumAuthenticationClass )
8587 const contentType = headers && headers . find ( ( it ) => ( it . header ? it . header . toLowerCase ( ) === 'content-type' : false ) )
@@ -104,18 +106,18 @@ export namespace XHR {
104106 acc [ h . header ] = h . data
105107 return acc
106108 } ,
107- { 'X-Requested-With' : 'XMLHttpRequest' }
109+ { 'X-Requested-With' : 'XMLHttpRequest' }
108110 ) ,
109111 } ,
110112 method === 'POST' || method === 'PUT'
111113 ? {
112- body :
113- ! contentType || contentType . data === 'application/json'
114- ? JSON . stringify ( data , ( k , v ) => {
115- return v instanceof ArrayBuffer || v instanceof Uint8Array ? ua2b64 ( v ) : v
116- } )
117- : data ,
118- }
114+ body :
115+ ! contentType || contentType . data === 'application/json'
116+ ? JSON . stringify ( data , ( k , v ) => {
117+ return v instanceof ArrayBuffer || v instanceof Uint8Array ? ua2b64 ( v ) : v
118+ } )
119+ : data ,
120+ }
119121 : { }
120122 ) ,
121123 timeout ,
@@ -132,24 +134,32 @@ export namespace XHR {
132134 fetchImpl ,
133135 contentTypeOverride ,
134136 headerProvider ,
135- requiredAuthLevelHeader ? parseInt ( requiredAuthLevelHeader ) : undefined
137+ requiredAuthLevelHeader ? parseInt ( requiredAuthLevelHeader ) : undefined ,
138+ tryHardToParseJson
136139 )
137140 } else if ( response . status >= 400 ) {
138141 const error : {
139142 error : string
140143 message : string
141144 status : number
142- } = { error : response . statusText , message : await response . text ( ) , status : response . status }
145+ } = { error : response . statusText , message : await response . text ( ) , status : response . status }
143146 console . warn ( `XHR Error: ${ method } ${ url } - ${ error . status } - ${ error . error } ` , error . message )
144147 throw new XHRError ( url , error . message , error . status , error . error , response . headers )
145148 } else {
146149 const ct = contentTypeOverride || response . headers . get ( 'content-type' ) || 'text/plain'
147150 return (
148151 ct . startsWith ( 'application/json' )
149- ? response . json ( )
152+ ? tryHardToParseJson ? response . arrayBuffer ( ) . then ( async ( ab ) => {
153+ try {
154+ return JSON . parse ( ua2utf8 ( ab ) )
155+ } catch ( e ) {
156+ console . warn ( 'Error parsing JSON fallback on ua2string' , e )
157+ return JSON . parse ( ua2string ( ab ) . replace ( / [ \u0000 - \u001F \u007F - \u009F ] / g, "" ) )
158+ }
159+ } ) : response . json ( )
150160 : ct . startsWith ( 'application/xml' ) || ct . startsWith ( 'text/' )
151- ? response . text ( )
152- : response . arrayBuffer ( )
161+ ? response . text ( )
162+ : response . arrayBuffer ( )
153163 ) . then ( ( d ) => new Data ( response . status , ct , d ) )
154164 }
155165 } )
0 commit comments