Skip to content

Commit

Permalink
fix(test): ability to switch to a local server for development and te…
Browse files Browse the repository at this point in the history
…sting
  • Loading branch information
SidMorad committed Oct 9, 2023
1 parent f87aec3 commit a0e9854
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 24 additions & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,34 @@ export type Config = {
apiKey: string
basePath?: string
debug?: boolean
local?: boolean // For development use only, default is false
}

export abstract class Base {
private apiKey: string
private basePath: string
private debug: boolean
private local: boolean

constructor(config: Config) {
this.apiKey = config.apiKey
this.basePath = config.basePath
this.debug = config.debug || false
this.local = config.local || false
}

protected request<T>(
endpoint: string,
options?: RequestInit,
basePath?: string
): Promise<TaakResponse> {
const url = (basePath ? basePath : this.basePath) + endpoint
let actualBasePath = null
if (this.local) {
actualBasePath = this.lookupLocalPath(basePath ? basePath : this.basePath)
} else {
actualBasePath = basePath ? basePath : this.basePath
}
const url = actualBasePath + endpoint
if (this.debug) console.log('URL:', url)
const headers = {
'X-TAAK-API-KEY': this.apiKey,
Expand All @@ -48,4 +57,18 @@ export abstract class Base {
return { status: 0, error: error?.message }
})
}

private lookupLocalPath = (remotePath: string) => {
switch(remotePath) {
case 'https://app-api.taakcloud.com':
return 'http://localhost:8080'
case 'https://gtin.taakcloud.com':
return 'http://localhost:8091'
case 'https://mfactor.taakcloud.com':
return 'http://localhost:8092'
default:
throw new Error(`No local path found for ${remotePath}`)
}
}

}
2 changes: 1 addition & 1 deletion src/mfactor/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('mFactor resource', () => {
.reply(200, {})

// Make the request
const TaakSdkClient = new TaakSDK({ apiKey: 'XYZ', debug: false })
const TaakSdkClient = new TaakSDK({ apiKey: 'XYZ', debug: true, local: false })
await TaakSdkClient.createFactor(cmd)

// Assert that the expected request was made.
Expand Down

0 comments on commit a0e9854

Please sign in to comment.