@@ -31,7 +31,10 @@ export interface IHttpOptions {
3131 method ?: string
3232 url : string
3333 alwaysUseGet ?: boolean
34- headers : { sessionID ?: string }
34+ headers : {
35+ sessionID ?: string ,
36+ apiKey ?: string
37+ }
3538}
3639export type TFields = string | string [ ]
3740
@@ -60,8 +63,12 @@ export class Api {
6063 constructor ( config ) {
6164 this . _httpOptions = {
6265 url : config . url ,
66+ alwaysUseGet : config . alwaysUseGet ,
6367 headers : { }
6468 }
69+ if ( config . apiKey ) {
70+ this . _httpOptions . headers . apiKey = config . apiKey
71+ }
6572 // Append version to path if provided
6673 let path
6774 if ( [ 'internal' , 'unsupported' , 'asp' ] . indexOf ( config . version ) >= 0 ) {
@@ -85,20 +92,20 @@ export class Api {
8592 */
8693 getApiKey ( username : string , password : string ) : Promise < string > {
8794 return new Promise ( ( resolve , reject ) => {
88- if ( typeof this . _httpParams . apiKey !== 'undefined' ) {
89- resolve ( this . _httpParams . apiKey )
95+ if ( typeof this . _httpOptions . headers . apiKey !== 'undefined' ) {
96+ resolve ( this . _httpOptions . headers . apiKey )
9097 }
9198 else {
9299 this . execute ( 'USER' , null , 'getApiKey' , { username, password} ) . then ( ( getApiKeyData ) => {
93100 if ( getApiKeyData . result === '' ) {
94101 this . execute ( 'USER' , null , 'generateApiKey' , { username, password} ) . then ( ( generateApiKeyData ) => {
95- this . _httpParams . apiKey = generateApiKeyData . result
96- resolve ( this . _httpParams . apiKey )
102+ this . _httpOptions . headers . apiKey = generateApiKeyData . result
103+ resolve ( this . _httpOptions . headers . apiKey )
97104 } , reject )
98105 }
99106 else {
100- this . _httpParams . apiKey = getApiKeyData . result
101- resolve ( this . _httpParams . apiKey )
107+ this . _httpOptions . headers . apiKey = getApiKeyData . result
108+ resolve ( this . _httpOptions . headers . apiKey )
102109 }
103110 } , reject )
104111 }
@@ -151,7 +158,7 @@ export class Api {
151158 return new Promise ( ( resolve , reject ) => {
152159 this . execute ( 'USER' , null , 'clearApiKey' ) . then ( ( result ) => {
153160 if ( result ) {
154- delete this . _httpParams . apiKey
161+ delete this . _httpOptions . headers . apiKey
155162 resolve ( )
156163 } else {
157164 reject ( )
@@ -362,6 +369,9 @@ export class Api {
362369 if ( this . _httpOptions . headers . sessionID ) {
363370 headers . append ( 'sessionID' , this . _httpOptions . headers . sessionID )
364371 }
372+ else if ( this . _httpOptions . headers . apiKey ) {
373+ headers . append ( 'apiKey' , this . _httpOptions . headers . apiKey )
374+ }
365375
366376 let bodyParams = null , queryString = ''
367377 if ( NodeFormData && params instanceof NodeFormData ) {
@@ -385,7 +395,7 @@ export class Api {
385395 }
386396
387397 return fetch ( options . url + options . path + queryString , {
388- method : method ,
398+ method : alwaysUseGet ? 'GET' : method ,
389399 headers : headers ,
390400 body : bodyParams ,
391401 credentials : 'same-origin'
@@ -413,7 +423,7 @@ export class Api {
413423 * @return {string } returns the given api key value
414424 */
415425 setApiKey ( apiKey ) {
416- return this . _httpParams . apiKey = apiKey
426+ return this . _httpOptions . headers . apiKey = apiKey
417427 }
418428
419429 /**
0 commit comments