diff --git a/src/XeroClient.ts b/src/XeroClient.ts index 100e1817..f938bf37 100644 --- a/src/XeroClient.ts +++ b/src/XeroClient.ts @@ -59,6 +59,7 @@ export class XeroClient { this.filesApi = new xero.FilesApi(); this.projectApi = new xero.ProjectApi(); this.payrollAUApi = new xero.PayrollAuApi(); + this.payrollAUV2Api = new xero.PayrollAuV2Api(); this.bankFeedsApi = new xero.BankFeedsApi(); this.payrollUKApi = new xero.PayrollUkApi(); this.payrollNZApi = new xero.PayrollNzApi(); @@ -74,6 +75,7 @@ export class XeroClient { readonly filesApi: xero.FilesApi; readonly projectApi: xero.ProjectApi; readonly payrollAUApi: xero.PayrollAuApi; + readonly payrollAUV2Api: xero.PayrollAuV2Api; readonly bankFeedsApi: xero.BankFeedsApi; readonly payrollUKApi: xero.PayrollUkApi; readonly payrollNZApi: xero.PayrollNzApi; @@ -267,21 +269,23 @@ export class XeroClient { }); } + // Derived from the clients built in the constructor and cross-checked against + // xero.APIS, the generated list of every API client class, so a newly wired + // client cannot be missed here. + private get apiClients(): Array<{ accessToken: string }> { + return Object.values(this).filter( + (value): value is { accessToken: string } => xero.APIS.some(apiClass => value instanceof apiClass) + ); + } + private setAccessToken(): void { const accessToken = this._tokenSet.access_token; if (typeof accessToken === 'undefined') { throw new Error('Access token is undefined!'); } - this.accountingApi.accessToken = accessToken; - this.assetApi.accessToken = accessToken; - this.filesApi.accessToken = accessToken; - this.projectApi.accessToken = accessToken; - this.payrollAUApi.accessToken = accessToken; - this.bankFeedsApi.accessToken = accessToken; - this.payrollUKApi.accessToken = accessToken; - this.payrollNZApi.accessToken = accessToken; - this.appStoreApi.accessToken = accessToken; - this.financeApi.accessToken = accessToken; + this.apiClients.forEach(apiClient => { + apiClient.accessToken = accessToken; + }); } } diff --git a/src/gen/api/apis.ts b/src/gen/api/apis.ts index 459e0f40..6704aeb8 100644 --- a/src/gen/api/apis.ts +++ b/src/gen/api/apis.ts @@ -3,6 +3,7 @@ export * from './assetApi'; export * from './projectApi'; export * from './filesApi'; export * from './payrollAUApi'; +export * from './payrollAUV2Api'; export * from './bankfeedsApi'; export * from './payrollUKApi'; export * from './payrollNZApi'; @@ -13,9 +14,10 @@ import { AssetApi } from './assetApi'; import { FilesApi } from './filesApi'; import { ProjectApi } from './projectApi'; import { PayrollAuApi } from './payrollAUApi'; +import { PayrollAuV2Api } from './payrollAUV2Api'; import { BankFeedsApi } from './bankfeedsApi'; import { PayrollUkApi } from './payrollUKApi'; import { PayrollNzApi } from './payrollNZApi'; import { AppStoreApi } from './appStoreApi'; import { FinanceApi } from './financeApi'; -export const APIS = [AccountingApi, AssetApi, FilesApi, ProjectApi, PayrollAuApi, BankFeedsApi, PayrollUkApi, PayrollNzApi, AppStoreApi, FinanceApi]; \ No newline at end of file +export const APIS = [AccountingApi, AssetApi, FilesApi, ProjectApi, PayrollAuApi, PayrollAuV2Api, BankFeedsApi, PayrollUkApi, PayrollNzApi, AppStoreApi, FinanceApi]; diff --git a/src/test/xeroClient.spec.ts b/src/test/xeroClient.spec.ts index 89a8cb4a..c0904097 100644 --- a/src/test/xeroClient.spec.ts +++ b/src/test/xeroClient.spec.ts @@ -1,4 +1,5 @@ import { XeroClient } from "../XeroClient"; +import * as xeroApis from "../gen/api"; const tokenSetJson = require("./mocks/tokenSet.json"); const refreshedTokenSetJson = require("./mocks/refreshedTokenSet.json"); const connectionsResponse = require("./mocks/connectionsResponse.json"); @@ -110,11 +111,29 @@ describe('the XeroClient', () => { expect(xeroClient).toHaveProperty('bankFeedsApi') expect(xeroClient).toHaveProperty('projectApi') expect(xeroClient).toHaveProperty('payrollAUApi') + expect(xeroClient).toHaveProperty('payrollAUV2Api') expect(xeroClient).toHaveProperty('payrollUKApi') expect(xeroClient).toHaveProperty('payrollNZApi') expect(xeroClient).toHaveProperty('appStoreApi') }); + it('propagates access tokens to Payroll AU v2', () => { + xero.setTokenSet(tokenSet) + + expect((xero.payrollAUV2Api as any).authentications.OAuth2.accessToken).toEqual(tokenSet.access_token) + }); + + it('propagates access tokens to every generated API client', () => { + xero.setTokenSet(tokenSet) + + xeroApis.APIS.forEach(apiClass => { + const wiredClient: any = Object.values(xero).find(value => value instanceof apiClass) + + expect(wiredClient).toBeDefined() + expect(wiredClient.authentications.OAuth2.accessToken).toEqual(tokenSet.access_token) + }) + }); + it('readTokenSet() returns the tokenSet', async () => { const xeroTokenSet = await xero.readTokenSet() expect(xeroTokenSet).toEqual(tokenSet)