Skip to content

Commit

Permalink
Merge pull request #581 from HubSpot/feature/migrateFromLoadashGetToN…
Browse files Browse the repository at this point in the history
…ativeOperators

apiRequest: Migrate from lodash.get to native code
  • Loading branch information
ksvirkou-hubspot authored Feb 27, 2025
2 parents 1b553a5 + 6dc41db commit b7a0a6b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
5 changes: 2 additions & 3 deletions src/services/http/Auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import get from 'lodash.get'
import IConfiguration from '../../configuration/IConfiguration'
import { AuthTypes } from './AuthTypes'
import { IHttpOptions } from './IHttpOptions'
Expand All @@ -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
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/services/http/AuthMethods.ts

This file was deleted.

8 changes: 3 additions & 5 deletions src/services/http/AuthTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { AuthMethods } from './AuthMethods'

export enum AuthTypes {
apiKey = AuthMethods.hapikey,
accessToken = AuthMethods.accessToken,
developerApiKey = AuthMethods.hapikey,
apiKey,
developerApiKey,
accessToken,
}
16 changes: 7 additions & 9 deletions src/services/http/Request.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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' }
}

Expand All @@ -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)
}
}
Expand Down

0 comments on commit b7a0a6b

Please sign in to comment.