@devcycle/nextjs-sdk
Version:
The Next.js SDK for DevCycle!
57 lines • 2.51 kB
JavaScript
import { DVCPopulatedUser } from '@devcycle/js-client-sdk';
import { generateBucketedConfig } from '@devcycle/bucketing';
import { ConfigBody, ConfigSource } from '@devcycle/types';
import { fetchCDNConfig, sdkConfigAPI } from './requests.js';
import { plainToInstance } from 'class-transformer';
class CDNConfigSource extends ConfigSource {
async getConfig(sdkKey, kind, obfuscated) {
const configResponse = await fetchCDNConfig(sdkKey, obfuscated);
if (!configResponse.ok) {
throw new Error('Could not fetch config');
}
return {
config: plainToInstance(ConfigBody, await configResponse.json()),
lastModified: configResponse.headers.get('last-modified'),
metaData: {},
};
}
// implement a dummy version of this to satisfy shared type definition. Next does not use this method
getConfigURL(sdkKey, kind, obfuscated) {
return '';
}
}
const cdnConfigSource = new CDNConfigSource();
const bucketOrFetchConfig = async (user, config, obfuscated) => {
var _a, _b;
if ((_a = config.debugUsers) === null || _a === void 0 ? void 0 : _a.includes((_b = user.user_id) !== null && _b !== void 0 ? _b : '')) {
const bucketedConfigResponse = await sdkConfigAPI(config.clientSDKKey, obfuscated, user);
return (await bucketedConfigResponse.json());
}
return generateBucketedConfig({
user,
config,
});
};
export const getBucketedConfig = async (sdkKey, user, userAgent, obfuscated, configSource = cdnConfigSource) => {
const { config } = await configSource.getConfig(sdkKey, 'bootstrap', obfuscated, '', true);
const populatedUser = new DVCPopulatedUser(user, {}, undefined, undefined, userAgent !== null && userAgent !== void 0 ? userAgent : undefined);
const bucketedConfig = await bucketOrFetchConfig(populatedUser, config, obfuscated);
for (const feature of Object.values(bucketedConfig.features)) {
if (feature.settings === undefined) {
// next complains about not being able to serialize explicitly undefined values
delete feature.settings;
}
}
return {
config: {
...bucketedConfig,
sse: {
url: config.sse
? new URL(config.sse.path, config.sse.hostname).toString()
: undefined,
inactivityDelay: 1000 * 60 * 2,
},
},
};
};
//# sourceMappingURL=bucketing.js.map