diff --git a/src/index.ts b/src/index.ts index ee2cb4b..c0bfd5e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -54,7 +54,7 @@ const Supergood = () => { let interceptor: BatchInterceptor; - const init = async ( + const init = >( { clientId, clientSecret, @@ -63,17 +63,17 @@ const Supergood = () => { }: { clientId?: string; clientSecret?: string; - config?: Partial; + config?: TConfig; metadata?: Partial; } = { clientId: process.env.SUPERGOOD_CLIENT_ID as string, clientSecret: process.env.SUPERGOOD_CLIENT_SECRET as string, - config: {} as Partial, + config: {} as TConfig, metadata: {} as Partial }, baseUrl = process.env.SUPERGOOD_BASE_URL || 'https://api.supergood.ai', baseTelemetryUrl = process.env.SUPERGOOD_TELEMETRY_BASE_URL || 'https://telemetry.supergood.ai' - ) => { + ): TConfig extends { useRemoteConfig: false } ? void : Promise => { if (!clientId) throw new Error(errors.NO_CLIENT_ID); if (!clientSecret) throw new Error(errors.NO_CLIENT_SECRET); @@ -230,24 +230,28 @@ const Supergood = () => { }; // Fetch the initial config and process it - if(supergoodConfig.useRemoteConfig) { - await fetchAndProcessRemoteConfig(); - } else { - supergoodConfig.remoteConfig = supergoodConfig.remoteConfig ?? {}; - } + const continuation = supergoodConfig.useRemoteConfig + ? fetchAndProcessRemoteConfig() + : void (supergoodConfig.remoteConfig = supergoodConfig.remoteConfig ?? {}) + + const remainingWork = () => { + initializeInterceptors(); - initializeInterceptors(); + if(supergoodConfig.useRemoteConfig) { + // Fetch the config ongoing every milliseconds + remoteConfigFetchInterval = setInterval(fetchAndProcessRemoteConfig, supergoodConfig.remoteConfigFetchInterval); + remoteConfigFetchInterval.unref(); + } - if(supergoodConfig.useRemoteConfig) { - // Fetch the config ongoing every milliseconds - remoteConfigFetchInterval = setInterval(fetchAndProcessRemoteConfig, supergoodConfig.remoteConfigFetchInterval); - remoteConfigFetchInterval.unref(); + // Flushes the cache every milliseconds + flushInterval = setInterval(flushCache, supergoodConfig.flushInterval); + // https://httptoolkit.com/blog/unblocking-node-with-unref/ + flushInterval.unref(); } - // Flushes the cache every milliseconds - flushInterval = setInterval(flushCache, supergoodConfig.flushInterval); - // https://httptoolkit.com/blog/unblocking-node-with-unref/ - flushInterval.unref(); + return ( + continuation?.then(remainingWork) ?? remainingWork() + ) as TConfig extends { useRemoteConfig: false } ? void : Promise; }; const cacheRequest = async (request: RequestType, baseUrl: string) => { diff --git a/test/e2e/core.e2e.test.ts b/test/e2e/core.e2e.test.ts index a38d11e..bed0d81 100644 --- a/test/e2e/core.e2e.test.ts +++ b/test/e2e/core.e2e.test.ts @@ -201,6 +201,37 @@ describe('core functionality', () => { }) }); }, 10000); + + it('should return a promise when run without useRemoteConfig: false', async () => { + const ret = Supergood.init( + { + config: { ...SUPERGOOD_CONFIG, allowLocalUrls: true }, + clientId: SUPERGOOD_CLIENT_ID, + clientSecret: SUPERGOOD_CLIENT_SECRET + }, + SUPERGOOD_SERVER + ); + expect(ret).toHaveProperty('then'); + await Supergood.close(); + }); + + it('should return void when run with useRemoteConfig: false', async () => { + const ret = Supergood.init( + { + config: { + ...SUPERGOOD_CONFIG, + allowLocalUrls: true, + useRemoteConfig: false + }, + clientId: SUPERGOOD_CLIENT_ID, + clientSecret: SUPERGOOD_CLIENT_SECRET + }, + SUPERGOOD_SERVER + ); + + expect(ret).toBe(undefined); + await Supergood.close(); + }); }); describe('encoding', () => {