From dcb63a67dfc8f5c0fe9b86b5896d71b54e99b9ba Mon Sep 17 00:00:00 2001 From: Embbnux Ji Date: Thu, 9 Feb 2023 09:02:37 +0800 Subject: [PATCH] enhancement: check and fetch discovery data at login (#201) * enhancement: check discovery data at login * feat: fetch init discovery again when not init data at login --- sdk/src/platform/Platform-spec.ts | 23 +++++++++++++++++++++++ sdk/src/platform/Platform.ts | 7 ++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/sdk/src/platform/Platform-spec.ts b/sdk/src/platform/Platform-spec.ts index 1fbee67..386494e 100644 --- a/sdk/src/platform/Platform-spec.ts +++ b/sdk/src/platform/Platform-spec.ts @@ -1200,6 +1200,29 @@ describe('RingCentral.platform.Platform', () => { expect(await platform.discovery().externalDataExpired()).to.equal(false); }); + it('should fetch external discovery successfully when login without initDiscovery data', async () => { + // mock + const initialDiscoveryData = getInitialDiscoveryMockData(); + const externalDiscoveryData = getExternalDiscoveryMockData(); + apiCall('GET', '/.well-known/entry-points/initial?clientId=whatever', initialDiscoveryData); + apiCall('GET', '/.well-known/entry-points/external', externalDiscoveryData); + authentication(); + + const sdk = createSdk({ + enableDiscovery: true, + discoveryServer: 'http://whatever', + discoveryAutoInit: false, + server: '', + }); + const platform = sdk.platform(); + await platform.login({ + code: 'whatever', + }); + const externalData = await platform.discovery().externalData(); + expect(externalData.coreApi.baseUri).to.equal(externalDiscoveryData.coreApi.baseUri); + expect(await platform.discovery().externalDataExpired()).to.equal(false); + }); + it('should fetch external discovery when login without discovery_uri and token_uri', async () => { // mock const initialDiscoveryData = getInitialDiscoveryMockData(); diff --git a/sdk/src/platform/Platform.ts b/sdk/src/platform/Platform.ts index 0d2a1d6..ac4a68b 100644 --- a/sdk/src/platform/Platform.ts +++ b/sdk/src/platform/Platform.ts @@ -549,7 +549,12 @@ export default class Platform extends EventEmitter { if (this._discoveryInitPromise) { await this._discoveryInitPromise; } - const discoveryData = await this._discovery.initialData(); + let discoveryData = await this._discovery.initialData(); + // check if discovery data is initialized successfully + if (!discoveryData) { + // discovery request fail in previous init, try re-fetch discovery + discoveryData = await this._discovery.fetchInitialData(); + } if (!tokenEndpoint) { tokenEndpoint = discoveryData.authApi.defaultTokenUri; }