@@ -33,7 +33,6 @@ export interface IHttpClientOptions {
33
33
/** Helpers for HTTP clients. */
34
34
// eslint-disable-next-line @typescript-eslint/no-namespace
35
35
export namespace HttpClientUtility {
36
-
37
36
/** The fetch function. */
38
37
export interface IFetch {
39
38
( uri : string , request : IFetchRequest ) : Promise < IFetchResponse > ;
@@ -73,31 +72,37 @@ export namespace HttpClientUtility {
73
72
'413' : 'RequestTooLarge' ,
74
73
'429' : 'TooManyRequests' ,
75
74
'500' : 'InternalError' ,
76
- '503' : 'ServiceUnavailable'
75
+ '503' : 'ServiceUnavailable' ,
77
76
} ;
78
77
79
78
const jsonContentType = 'application/json' ;
80
79
81
80
/** Fetch JSON using the specified fetch, URI, and request. */
82
- export function fetchResponse ( fetch : IFetch , uri : string , request : IFetchRequest ) : Promise < IFetchedResponseWithContent > {
83
- return fetch ( uri , request )
84
- . then ( response => {
85
- if ( ! response . headers || ! response . status || typeof response . json !== 'function' ) {
86
- throw new TypeError ( 'fetch must resolve Promise with { status, headers, json() }.' ) ;
87
- }
88
- const contentType = response . headers . get ( 'content-type' ) ;
89
- if ( ! contentType ) {
90
- return Promise . resolve ( { response : response , json : { } } ) ;
81
+ export function fetchResponse (
82
+ fetch : IFetch ,
83
+ uri : string ,
84
+ request : IFetchRequest
85
+ ) : Promise < IFetchedResponseWithContent > {
86
+ return fetch ( uri , request ) . then ( ( response ) => {
87
+ if ( ! response . headers || ! response . status || typeof response . json !== 'function' ) {
88
+ throw new TypeError ( 'fetch must resolve Promise with { status, headers, json() }.' ) ;
89
+ }
90
+ const contentType = response . headers . get ( 'content-type' ) ;
91
+ if ( ! contentType ) {
92
+ return Promise . resolve ( { response : response , json : { } } ) ;
93
+ }
94
+ if ( contentType . toLowerCase ( ) . substr ( 0 , jsonContentType . length ) === jsonContentType ) {
95
+ const jsonPromise = response . json ( ) ;
96
+ if ( ! jsonPromise || typeof jsonPromise . then !== 'function' ) {
97
+ throw new TypeError ( 'json() of fetch response must return a Promise.' ) ;
91
98
}
92
- if ( contentType . toLowerCase ( ) . substr ( 0 , jsonContentType . length ) === jsonContentType ) {
93
- const jsonPromise = response . json ( ) ;
94
- if ( ! jsonPromise || typeof jsonPromise . then !== 'function' ) {
95
- throw new TypeError ( 'json() of fetch response must return a Promise.' ) ;
96
- }
97
- return jsonPromise . then ( json => ( { response : response , json : json } ) ) ;
98
- }
99
- return Promise . resolve ( { response : response } ) ;
100
- } ) ;
99
+ return jsonPromise . then ( ( json ) => ( {
100
+ response : response ,
101
+ json : json ,
102
+ } ) ) ;
103
+ }
104
+ return Promise . resolve ( { response : response } ) ;
105
+ } ) ;
101
106
}
102
107
103
108
/** Creates an error result for the specified response. */
@@ -108,13 +113,22 @@ export namespace HttpClientUtility {
108
113
const isClientError = status >= 400 && status <= 499 ;
109
114
const isServerError = status >= 500 && status <= 599 ;
110
115
const errorCode = standardErrorCodes [ status ] || ( isClientError ? 'InvalidRequest' : 'InvalidResponse' ) ;
111
- const message = isServerError ? 'HTTP server error' : isClientError ? 'HTTP client error' : 'Unexpected HTTP status code' ;
116
+ const message = isServerError
117
+ ? 'HTTP server error'
118
+ : isClientError
119
+ ? 'HTTP client error'
120
+ : 'Unexpected HTTP status code' ;
112
121
return { error : { code : errorCode , message : `${ message } : ${ status } ` } } ;
113
122
}
114
123
115
124
/** Creates an error result for a required request field. */
116
125
export function createRequiredRequestFieldError ( name : string ) : IServiceResultBase {
117
- return { error : { code : 'InvalidRequest' , message : `The request field '${ name } ' is required.` } } ;
126
+ return {
127
+ error : {
128
+ code : 'InvalidRequest' ,
129
+ message : `The request field '${ name } ' is required.` ,
130
+ } ,
131
+ } ;
118
132
}
119
133
}
120
134
0 commit comments