diff --git a/src/services/http/Auth.ts b/src/services/http/Auth.ts index edfd2399d..f56ed06bc 100644 --- a/src/services/http/Auth.ts +++ b/src/services/http/Auth.ts @@ -1,4 +1,3 @@ -import get from 'lodash.get' import IConfiguration from '../../configuration/IConfiguration' import { AuthTypes } from './AuthTypes' import { IHttpOptions } from './IHttpOptions' @@ -7,12 +6,12 @@ export class Auth { public static chooseAuth(opts: IHttpOptions = {}, config: IConfiguration = {}): string | undefined { let type if (opts.authType) { - if (opts.authType !== 'none' && get(config, opts.authType)) { + if (opts.authType !== 'none' && opts.authType in config) { type = opts.authType } } else { for (const key in AuthTypes) { - if (get(config, key)) { + if (config[key as keyof typeof AuthTypes]) { type = key } } diff --git a/src/services/http/AuthMethods.ts b/src/services/http/AuthMethods.ts deleted file mode 100644 index 1a3c58151..000000000 --- a/src/services/http/AuthMethods.ts +++ /dev/null @@ -1,4 +0,0 @@ -export enum AuthMethods { - hapikey = 'hapikey', - accessToken = 'accessToken', -} diff --git a/src/services/http/AuthTypes.ts b/src/services/http/AuthTypes.ts index 51fa38d0c..f133611d8 100644 --- a/src/services/http/AuthTypes.ts +++ b/src/services/http/AuthTypes.ts @@ -1,7 +1,5 @@ -import { AuthMethods } from './AuthMethods' - export enum AuthTypes { - apiKey = AuthMethods.hapikey, - accessToken = AuthMethods.accessToken, - developerApiKey = AuthMethods.hapikey, + apiKey, + developerApiKey, + accessToken, } diff --git a/src/services/http/Request.ts b/src/services/http/Request.ts index 970306e11..b711afa61 100644 --- a/src/services/http/Request.ts +++ b/src/services/http/Request.ts @@ -1,8 +1,6 @@ -import get from 'lodash.get' import { ApiClientConfigurator } from '../../configuration/ApiClientConfigurator' import IConfiguration from '../../configuration/IConfiguration' import { Auth } from './Auth' -import { AuthMethods } from './AuthMethods' import { AuthTypes } from './AuthTypes' import { IHttpOptions } from './IHttpOptions' @@ -52,20 +50,20 @@ export class Request { protected applyAuth() { const authType = Auth.chooseAuth(this.opts, this.config) - if (authType) { - const method = get(AuthTypes, authType) - const value = get(this.config, authType) - if (method === AuthMethods.hapikey) { + if (authType && authType in AuthTypes) { + const type = authType as keyof typeof AuthTypes + const value: string = this.config[type] ?? '' + if (AuthTypes[type] <= AuthTypes.developerApiKey) { this.url.searchParams.set('hapikey', value) } - if (method === AuthMethods.accessToken) { + if (AuthTypes[type] === AuthTypes.accessToken) { this.headers = Object.assign(this.headers, { Authorization: `Bearer ${value}` }) } } } protected initHeaders() { - if (get(this.opts, 'defaultJson', true)) { + if (this.opts?.defaultJson ?? true) { this.headers = { 'Content-Type': 'application/json' } } @@ -91,7 +89,7 @@ export class Request { protected setBody() { if (this.opts.body) { this.body = this.opts.body - if (get(this.headers, 'Content-Type') === 'application/json' && get(this.opts, 'defaultJson', true)) { + if (this.opts?.defaultJson ?? true) { this.body = JSON.stringify(this.body) } }