@flags-sdk/statsig
Version:
Statsig provider for the Flags SDK
117 lines (116 loc) • 3.43 kB
JavaScript
// src/provider/index.ts
async function getProviderData(options) {
const consoleApiKey = options.consoleApiKey || options.statsigConsoleApiKey;
if (!consoleApiKey) {
return {
definitions: {},
hints: [
{
key: "statsig/missing-api-key",
text: "Missing Statsig Console API Key"
}
]
};
}
const hints = [];
const [gates, experiments] = await Promise.allSettled([
getFeatureGates({ consoleApiKey }),
getExperiments({ consoleApiKey })
]);
const definitions = {};
if (gates.status === "fulfilled") {
gates.value.forEach((gate) => {
definitions[gate.id] = {
description: gate.description,
origin: options.projectId ? `https://console.statsig.com/${options.projectId}/gates/${gate.id}` : void 0,
options: [
{ label: "Off", value: false },
{ label: "On", value: true }
],
createdAt: gate.createdTime,
updatedAt: gate.lastModifiedTime
};
});
} else {
hints.push({
key: "statsig/failed-to-load-feature-gates",
text: gates.reason.message
});
}
if (experiments.status === "fulfilled") {
experiments.value.forEach((experiment) => {
definitions[experiment.id] = {
description: experiment.description,
origin: options.projectId ? `https://console.statsig.com/${options.projectId}/experiments/${experiment.id}/setup` : void 0,
options: experiment.groups.map((group) => {
return {
label: group.name,
value: group.parameterValues
};
}),
createdAt: experiment.createdTime,
updatedAt: experiment.lastModifiedTime
};
});
} else {
hints.push({
key: "statsig/failed-to-load-experiments",
text: experiments.reason.message
});
}
return { definitions, hints };
}
async function getFeatureGates(options) {
const data = [];
let suffix = "/console/v1/gates";
do {
const response = await fetch(`https://statsigapi.net${suffix}`, {
method: "GET",
headers: {
"content-type": "application/json",
"STATSIG-API-KEY": options.consoleApiKey
},
// @ts-expect-error some Next.js versions need this
cache: "no-store"
});
if (response.status !== 200) {
await response.arrayBuffer();
throw new Error(
`Failed to fetch Statsig (Received ${response.status} response)`
);
}
const body = await response.json();
suffix = body.pagination?.nextPage || null;
data.push(...body.data);
} while (suffix);
return data;
}
async function getExperiments(options) {
const data = [];
let suffix = "/console/v1/experiments";
do {
const response = await fetch(`https://statsigapi.net${suffix}`, {
method: "GET",
headers: {
"content-type": "application/json",
"STATSIG-API-KEY": options.consoleApiKey
},
// @ts-expect-error some Next.js versions need this
cache: "no-store"
});
if (response.status !== 200) {
await response.arrayBuffer();
throw new Error(
`Failed to fetch Statsig (Received ${response.status} response)`
);
}
const body = await response.json();
suffix = body.pagination?.nextPage || null;
data.push(...body.data);
} while (suffix);
return data;
}
export {
getProviderData
};
//# sourceMappingURL=chunk-U4AIHMP7.js.map