Skip to content

Commit

Permalink
feat(API hosts): ability to override base paths
Browse files Browse the repository at this point in the history
  • Loading branch information
SidMorad committed Nov 1, 2023
1 parent a0e9854 commit ffdb02d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
30 changes: 30 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@ import { TaakResponse } from './taak-response'

export type Config = {
apiKey: string
appHost?: string
gtinHost?: string
mfactorHost?: string
basePath?: string
debug?: boolean
local?: boolean // For development use only, default is false
}

export abstract class Base {
private apiKey: string
private appHost = 'https://app-api.taakcloud.com'
private gtinHost = 'https://gtin.taakcloud.com'
private mfactorHost = 'https://mfactor.taakcloud.com'
private basePath: string
private debug: boolean
private local: boolean

constructor(config: Config) {
this.apiKey = config.apiKey
this.appHost = config.appHost || this.appHost
this.gtinHost = config.gtinHost || this.gtinHost
this.mfactorHost = config.mfactorHost || this.mfactorHost
this.basePath = config.basePath
this.debug = config.debug || false
this.local = config.local || false
Expand Down Expand Up @@ -58,6 +67,27 @@ export abstract class Base {
})
}

protected appRequest<T>(
endpoint: string,
options?: RequestInit,
): Promise<TaakResponse> {
return this.request(endpoint, options, this.appHost)
}

protected gtinRequest<T>(
endpoint: string,
options?: RequestInit,
): Promise<TaakResponse> {
return this.request(endpoint, options, this.gtinHost)
}

protected mfactorRequest<T>(
endpoint: string,
options?: RequestInit,
): Promise<TaakResponse> {
return this.request(endpoint, options, this.mfactorHost)
}

private lookupLocalPath = (remotePath: string) => {
switch(remotePath) {
case 'https://app-api.taakcloud.com':
Expand Down
4 changes: 1 addition & 3 deletions src/gtin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { TaakResponse } from '../taak-response'
import { ProductDTO } from './types'

export class Gtin extends AppBase {
public static readonly basePath = 'https://gtin.taakcloud.com'

getProduct(gtin: number, currency = 'IRR'): Promise<TaakResponse> {
return this.request<ProductDTO>(
return this.gtinRequest<ProductDTO>(
`/v1/pip/${gtin}/${currency}`,
{},
Gtin.basePath
)
}
}
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: true, local: false })
const TaakSdkClient = new TaakSDK({ apiKey: 'XYZ' })
await TaakSdkClient.createFactor(cmd)

// Assert that the expected request was made.
Expand Down
10 changes: 3 additions & 7 deletions src/mfactor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,30 @@ import {
} from './types'

export class mFactor extends AppBase {
public static readonly basePath = 'https://mfactor.taakcloud.com'
createFactor(command: FactorCreateCommand): Promise<TaakResponse> {
return this.request<FactorDTO>(
return this.mfactorRequest<FactorDTO>(
'/api/v1/factor/create',
{
method: 'POST',
body: JSON.stringify(command),
},
mFactor.basePath
)
}

getFactor(publicId: string): Promise<TaakResponse> {
return this.request<FactorDTO>(
return this.mfactorRequest<FactorDTO>(
`/api/v1/factor/${publicId}`,
{},
mFactor.basePath
)
}

ipgObtainToken(command: ObtainTokenCommand): Promise<TaakResponse> {
return this.request<TokenDTO>(
return this.mfactorRequest<TokenDTO>(
'/api/v1/ipg/token',
{
method: 'POST',
body: JSON.stringify(command),
},
mFactor.basePath
)
}
}
8 changes: 4 additions & 4 deletions src/web-push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ export class WebPush extends AppBase {
static readonly DEFAULT_WEB_PUSH_SERVER_PUBLIC_KEY = 'BBE1u0MfUE82cyodMmjmJlC1cynxKmvDSE0oMdcJN73gAcGp4pdS6ClF9j40mv7NaqOXexbZ-GdjHyGUJ1E4g9s'

subscribeWebPush(command: WebPushSubscribeCommand): Promise<TaakResponse> {
return this.request<WebPushDTO>(resourcePath + '/subscribe', {
return this.appRequest<WebPushDTO>(resourcePath + '/subscribe', {
method: 'POST',
body: JSON.stringify(command),
})
}

getWebPushesByUserId(userId: string): Promise<TaakResponse> {
return this.request<WebPushDTO[]>(`${resourcePath}/${userId}`)
return this.appRequest<WebPushDTO[]>(`${resourcePath}/${userId}`)
}

sendWebPush(command: WebPushSendCommand): Promise<TaakResponse> {
return this.request<void>(resourcePath + '/send', {
return this.appRequest<void>(resourcePath + '/send', {
method: 'POST',
body: JSON.stringify(command),
})
}

deleteWebPush(publicId: string): Promise<TaakResponse> {
return this.request<void>(`${resourcePath}/${publicId}`, {
return this.appRequest<void>(`${resourcePath}/${publicId}`, {
method: 'DELETE',
})
}
Expand Down

0 comments on commit ffdb02d

Please sign in to comment.