Skip to content

Commit

Permalink
Return void instead of Promise<void> if running with useRemoteConfig:…
Browse files Browse the repository at this point in the history
… false (#39)
  • Loading branch information
josiah-roberts authored Apr 8, 2024
1 parent 6136f78 commit 98af590
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const Supergood = () => {

let interceptor: BatchInterceptor;

const init = async (
const init = <TConfig extends Partial<ConfigType>>(
{
clientId,
clientSecret,
Expand All @@ -63,17 +63,17 @@ const Supergood = () => {
}: {
clientId?: string;
clientSecret?: string;
config?: Partial<ConfigType>;
config?: TConfig;
metadata?: Partial<MetadataType>;
} = {
clientId: process.env.SUPERGOOD_CLIENT_ID as string,
clientSecret: process.env.SUPERGOOD_CLIENT_SECRET as string,
config: {} as Partial<ConfigType>,
config: {} as TConfig,
metadata: {} as Partial<MetadataType>
},
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<void> => {
if (!clientId) throw new Error(errors.NO_CLIENT_ID);
if (!clientSecret) throw new Error(errors.NO_CLIENT_SECRET);

Expand Down Expand Up @@ -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 <remoteConfigFetchInterval> milliseconds
remoteConfigFetchInterval = setInterval(fetchAndProcessRemoteConfig, supergoodConfig.remoteConfigFetchInterval);
remoteConfigFetchInterval.unref();
}

if(supergoodConfig.useRemoteConfig) {
// Fetch the config ongoing every <remoteConfigFetchInterval> milliseconds
remoteConfigFetchInterval = setInterval(fetchAndProcessRemoteConfig, supergoodConfig.remoteConfigFetchInterval);
remoteConfigFetchInterval.unref();
// Flushes the cache every <flushInterval> milliseconds
flushInterval = setInterval(flushCache, supergoodConfig.flushInterval);
// https://httptoolkit.com/blog/unblocking-node-with-unref/
flushInterval.unref();
}

// Flushes the cache every <flushInterval> 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<void>;
};

const cacheRequest = async (request: RequestType, baseUrl: string) => {
Expand Down
31 changes: 31 additions & 0 deletions test/e2e/core.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 98af590

Please sign in to comment.