1
+
2
+
3
+ /* istanbul ignore file */
4
+ /* tslint:disable */
5
+ /* eslint-disable */
6
+ import FormData from 'form-data' ;
7
+ import fetch , { BodyInit , Headers , RequestInit , Response } from 'node-fetch' ;
8
+ import { types } from 'util' ;
9
+
10
+ import { ApiError } from './ApiError' ;
11
+ import type { ApiRequestOptions } from './ApiRequestOptions' ;
12
+ import type { ApiResult } from './ApiResult' ;
13
+ import { OpenAPI } from './OpenAPI' ;
14
+
15
+ function isDefined < T > ( value : T | null | undefined ) : value is Exclude < T , null | undefined > {
16
+ return value !== undefined && value !== null ;
17
+ }
18
+
19
+ function isString ( value : any ) : value is string {
20
+ return typeof value === 'string' ;
21
+ }
22
+
23
+ function isStringWithValue ( value : any ) : value is string {
24
+ return isString ( value ) && value !== '' ;
25
+ }
26
+
27
+ function isBinary ( value : any ) : value is Buffer | ArrayBuffer | ArrayBufferView {
28
+ const isBuffer = Buffer . isBuffer ( value ) ;
29
+ const isArrayBuffer = types . isArrayBuffer ( value ) ;
30
+ const isArrayBufferView = types . isArrayBufferView ( value ) ;
31
+ return isBuffer || isArrayBuffer || isArrayBufferView ;
32
+ }
33
+
34
+ function getQueryString ( params : Record < string , any > ) : string {
35
+ const qs : string [ ] = [ ] ;
36
+ Object . keys ( params ) . forEach ( key => {
37
+ const value = params [ key ] ;
38
+ if ( isDefined ( value ) ) {
39
+ if ( Array . isArray ( value ) ) {
40
+ value . forEach ( value => {
41
+ qs . push ( `${ encodeURIComponent ( key ) } =${ encodeURIComponent ( String ( value ) ) } ` ) ;
42
+ } ) ;
43
+ } else {
44
+ qs . push ( `${ encodeURIComponent ( key ) } =${ encodeURIComponent ( String ( value ) ) } ` ) ;
45
+ }
46
+ }
47
+ } ) ;
48
+ if ( qs . length > 0 ) {
49
+ return `?${ qs . join ( '&' ) } ` ;
50
+ }
51
+ return '' ;
52
+ }
53
+
54
+ function getUrl ( options : ApiRequestOptions ) : string {
55
+ const path = options . path . replace ( / [: ] / g, '_' ) ;
56
+ const url = `${ OpenAPI . BASE } ${ path } ` ;
57
+
58
+ if ( options . query ) {
59
+ return `${ url } ${ getQueryString ( options . query ) } ` ;
60
+ }
61
+ return url ;
62
+ }
63
+
64
+ function getFormData ( params : Record < string , any > ) : FormData {
65
+ const formData = new FormData ( ) ;
66
+ Object . keys ( params ) . forEach ( key => {
67
+ const value = params [ key ] ;
68
+ if ( isDefined ( value ) ) {
69
+ formData . append ( key , value ) ;
70
+ }
71
+ } ) ;
72
+ return formData ;
73
+ }
74
+
75
+ type Resolver < T > = ( ) => Promise < T > ;
76
+
77
+ async function resolve < T > ( resolver ?: T | Resolver < T > ) : Promise < T | undefined > {
78
+ if ( typeof resolver === 'function' ) {
79
+ return ( resolver as Resolver < T > ) ( ) ;
80
+ }
81
+ return resolver ;
82
+ }
83
+
84
+ async function getHeaders ( options : ApiRequestOptions ) : Promise < Headers > {
85
+ const headers = new Headers ( {
86
+ Accept : 'application/json' ,
87
+ ...OpenAPI . HEADERS ,
88
+ ...options . headers ,
89
+ } ) ;
90
+
91
+ const token = await resolve ( OpenAPI . TOKEN ) ;
92
+ const username = await resolve ( OpenAPI . USERNAME ) ;
93
+ const password = await resolve ( OpenAPI . PASSWORD ) ;
94
+
95
+ if ( isStringWithValue ( token ) ) {
96
+ headers . append ( 'Authorization' , `Bearer ${ token } ` ) ;
97
+ }
98
+
99
+ if ( isStringWithValue ( username ) && isStringWithValue ( password ) ) {
100
+ const credentials = Buffer . from ( `${ username } :${ password } ` ) . toString ( 'base64' ) ;
101
+ headers . append ( 'Authorization' , `Basic ${ credentials } ` ) ;
102
+ }
103
+
104
+ if ( options . body ) {
105
+ if ( isBinary ( options . body ) ) {
106
+ headers . append ( 'Content-Type' , 'application/octet-stream' ) ;
107
+ } else if ( isString ( options . body ) ) {
108
+ headers . append ( 'Content-Type' , 'text/plain' ) ;
109
+ } else {
110
+ headers . append ( 'Content-Type' , 'application/json' ) ;
111
+ }
112
+ }
113
+ return headers ;
114
+ }
115
+
116
+ function getRequestBody ( options : ApiRequestOptions ) : BodyInit | undefined {
117
+ if ( options . formData ) {
118
+ return getFormData ( options . formData ) ;
119
+ }
120
+ if ( options . body ) {
121
+ if ( isString ( options . body ) || isBinary ( options . body ) ) {
122
+ return options . body ;
123
+ } else {
124
+ return JSON . stringify ( options . body ) ;
125
+ }
126
+ }
127
+ return undefined ;
128
+ }
129
+
130
+ async function sendRequest ( options : ApiRequestOptions , url : string ) : Promise < Response > {
131
+ const request : RequestInit = {
132
+ method : options . method ,
133
+ headers : await getHeaders ( options ) ,
134
+ body : getRequestBody ( options ) ,
135
+ } ;
136
+ return await fetch ( url , request ) ;
137
+ }
138
+
139
+ function getResponseHeader ( response : Response , responseHeader ?: string ) : string | null {
140
+ if ( responseHeader ) {
141
+ const content = response . headers . get ( responseHeader ) ;
142
+ if ( isString ( content ) ) {
143
+ return content ;
144
+ }
145
+ }
146
+ return null ;
147
+ }
148
+
149
+ async function getResponseBody ( response : Response ) : Promise < any > {
150
+ try {
151
+ const contentType = response . headers . get ( 'Content-Type' ) ;
152
+ if ( contentType ) {
153
+ const isJSON = contentType . toLowerCase ( ) . startsWith ( 'application/json' ) ;
154
+ if ( isJSON ) {
155
+ return await response . json ( ) ;
156
+ } else {
157
+ return await response . text ( ) ;
158
+ }
159
+ }
160
+ } catch ( error ) {
161
+ console . error ( error ) ;
162
+ }
163
+ return null ;
164
+ }
165
+
166
+ function catchErrors ( options : ApiRequestOptions , result : ApiResult ) : void {
167
+ const errors : Record < number , string > = {
168
+ 400 : 'Bad Request' ,
169
+ 401 : 'Unauthorized' ,
170
+ 403 : 'Forbidden' ,
171
+ 404 : 'Not Found' ,
172
+ 500 : 'Internal Server Error' ,
173
+ 502 : 'Bad Gateway' ,
174
+ 503 : 'Service Unavailable' ,
175
+ ...options . errors ,
176
+ }
177
+
178
+ const error = errors [ result . status ] ;
179
+ if ( error ) {
180
+ throw new ApiError ( result , error ) ;
181
+ }
182
+
183
+ if ( ! result . ok ) {
184
+ throw new ApiError ( result , 'Generic Error' ) ;
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Request using node-fetch client
190
+ * @param options The request options from the the service
191
+ * @result ApiResult
192
+ * @throws ApiError
193
+ */
194
+ export async function request ( options : ApiRequestOptions ) : Promise < ApiResult > {
195
+ const url = getUrl ( options ) ;
196
+
197
+ // Pre-hook on request if a function is provided.
198
+ const requestHookResult = OpenAPI . REQUEST_HOOK ?
199
+ ( await OpenAPI . REQUEST_HOOK ( { url, options} ) ) : { url, options } ;
200
+
201
+ const response = await sendRequest ( requestHookResult . options , requestHookResult . url ) ;
202
+ const responseBody = await getResponseBody ( response ) ;
203
+ const responseHeader = getResponseHeader ( response , requestHookResult . options . responseHeader ) ;
204
+
205
+ const result : ApiResult = {
206
+ url,
207
+ ok : response . ok ,
208
+ status : response . status ,
209
+ statusText : response . statusText ,
210
+ body : responseHeader || responseBody
211
+ } ;
212
+
213
+ // Post-request Hook if provided
214
+ result = OpenAPI . RESPONSE_HOOK ? await OpenAPI . RESPONSE_HOOK ( { url, result, response} ) : result ;
215
+
216
+ catchErrors ( options , result ) ;
217
+ return result ;
218
+ }
0 commit comments