Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/XeroClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
});
}
}
4 changes: 3 additions & 1 deletion src/gen/api/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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];
export const APIS = [AccountingApi, AssetApi, FilesApi, ProjectApi, PayrollAuApi, PayrollAuV2Api, BankFeedsApi, PayrollUkApi, PayrollNzApi, AppStoreApi, FinanceApi];
19 changes: 19 additions & 0 deletions src/test/xeroClient.spec.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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)
Expand Down