11/* istanbul ignore file */
22/* tslint:disable */
33/* eslint-disable */
4- import FormData from 'form-data' ;
5- import fetch , { BodyInit , Headers , RequestInit , Response } from 'node-fetch' ;
6- import { types } from 'util' ;
7-
84import { ApiError } from './ApiError' ;
95import type { ApiRequestOptions } from './ApiRequestOptions' ;
106import type { ApiResult } from './ApiResult' ;
@@ -22,11 +18,8 @@ function isStringWithValue(value: any): value is string {
2218 return isString ( value ) && value !== '' ;
2319}
2420
25- function isBinary ( value : any ) : value is Buffer | ArrayBuffer | ArrayBufferView {
26- const isBuffer = Buffer . isBuffer ( value ) ;
27- const isArrayBuffer = types . isArrayBuffer ( value ) ;
28- const isArrayBufferView = types . isArrayBufferView ( value ) ;
29- return isBuffer || isArrayBuffer || isArrayBufferView ;
21+ function isBlob ( value : any ) : value is Blob {
22+ return value instanceof Blob ;
3023}
3124
3225function getQueryString ( params : Record < string , any > ) : string {
@@ -70,38 +63,39 @@ function getFormData(params: Record<string, any>): FormData {
7063 return formData ;
7164}
7265
73- type Resolver < T > = ( ) => Promise < T > ;
66+ type Resolver < T > = ( options : ApiRequestOptions ) => Promise < T > ;
7467
75- async function resolve < T > ( resolver ?: T | Resolver < T > ) : Promise < T | undefined > {
68+ async function resolve < T > ( options : ApiRequestOptions , resolver ?: T | Resolver < T > ) : Promise < T | undefined > {
7669 if ( typeof resolver === 'function' ) {
77- return ( resolver as Resolver < T > ) ( ) ;
70+ return ( resolver as Resolver < T > ) ( options ) ;
7871 }
7972 return resolver ;
8073}
8174
8275async function getHeaders ( options : ApiRequestOptions ) : Promise < Headers > {
76+ const token = await resolve ( options , OpenAPI . TOKEN ) ;
77+ const username = await resolve ( options , OpenAPI . USERNAME ) ;
78+ const password = await resolve ( options , OpenAPI . PASSWORD ) ;
79+ const defaultHeaders = await resolve ( options , OpenAPI . HEADERS ) ;
80+
8381 const headers = new Headers ( {
8482 Accept : 'application/json' ,
85- ...OpenAPI . HEADERS ,
83+ ...defaultHeaders ,
8684 ...options . headers ,
8785 } ) ;
8886
89- const token = await resolve ( OpenAPI . TOKEN ) ;
90- const username = await resolve ( OpenAPI . USERNAME ) ;
91- const password = await resolve ( OpenAPI . PASSWORD ) ;
92-
9387 if ( isStringWithValue ( token ) ) {
9488 headers . append ( 'Authorization' , `Bearer ${ token } ` ) ;
9589 }
9690
9791 if ( isStringWithValue ( username ) && isStringWithValue ( password ) ) {
98- const credentials = Buffer . from ( `${ username } :${ password } ` ) . toString ( 'base64' ) ;
92+ const credentials = btoa ( `${ username } :${ password } ` ) ;
9993 headers . append ( 'Authorization' , `Basic ${ credentials } ` ) ;
10094 }
10195
10296 if ( options . body ) {
103- if ( isBinary ( options . body ) ) {
104- headers . append ( 'Content-Type' , 'application/octet-stream' ) ;
97+ if ( isBlob ( options . body ) ) {
98+ headers . append ( 'Content-Type' , options . body . type || 'application/octet-stream' ) ;
10599 } else if ( isString ( options . body ) ) {
106100 headers . append ( 'Content-Type' , 'text/plain' ) ;
107101 } else {
@@ -116,7 +110,7 @@ function getRequestBody(options: ApiRequestOptions): BodyInit | undefined {
116110 return getFormData ( options . formData ) ;
117111 }
118112 if ( options . body ) {
119- if ( isString ( options . body ) || isBinary ( options . body ) ) {
113+ if ( isString ( options . body ) || isBlob ( options . body ) ) {
120114 return options . body ;
121115 } else {
122116 return JSON . stringify ( options . body ) ;
@@ -131,6 +125,9 @@ async function sendRequest(options: ApiRequestOptions, url: string): Promise<Res
131125 headers : await getHeaders ( options ) ,
132126 body : getRequestBody ( options ) ,
133127 } ;
128+ if ( OpenAPI . WITH_CREDENTIALS ) {
129+ request . credentials = 'include' ;
130+ }
134131 return await fetch ( url , request ) ;
135132}
136133
@@ -184,7 +181,7 @@ function catchErrors(options: ApiRequestOptions, result: ApiResult): void {
184181}
185182
186183/**
187- * Request using node- fetch client
184+ * Request using fetch client
188185 * @param options The request options from the the service
189186 * @returns ApiResult
190187 * @throws ApiError
0 commit comments