@simonecoelhosfo/optimizely-mcp-server
Version:
Optimizely MCP Server for AI assistants with integrated CLI tools
83 lines • 3.1 kB
JavaScript
/* Lightweight utilities for Optimizely REST API v2 */
/* Import axios or fetch of your choice and inject as ApiClient */
export const CRUD_PATTERN = {
archive: ["flag", "variation", "audience", "attribute"],
full: ["experiment", "page", "event"],
json: ["project", "campaign", "ruleset", "environment", "rule", "section"],
};
export const UPDATE_FORMAT = (entity) => CRUD_PATTERN.archive.includes(entity)
? "archive"
: CRUD_PATTERN.full.includes(entity)
? "full"
: "json_patch";
/* ---------- OpenAPI schema loader (optional) -------------------------- */
export class OpenAPISchema {
defs = {};
async init(url = "https://api.optimizely.com/v2/swagger.json") {
const response = (await fetch(url).then((r) => r.json()));
this.defs = response.definitions || {};
}
required(entity) {
return this.defs[entity]?.required || [];
}
}
/* ---------- Rate‑limited wrapper ------------------------------------- */
export function withRateLimit(fn) {
let inFlight = 0;
return async function (...args) {
while (inFlight > 8)
await new Promise((r) => setTimeout(r, 200));
try {
inFlight++;
return await fn.apply(this, args);
}
finally {
inFlight--;
}
};
}
/* ---------- Entity Updater ------------------------------------------- */
export class EntityUpdater {
api;
constructor(api) {
this.api = api;
}
patchJson(url, ops) {
return this.api("PATCH", url, ops, {
"Content-Type": "application/json-patch+json",
});
}
async update(entity, ids, changes) {
const mode = UPDATE_FORMAT(entity);
const base = "/v2";
const urlMap = {
project: `${base}/projects/${ids.id}`,
campaign: `${base}/campaigns/${ids.id}`,
audience: `${base}/audiences/${ids.id}`,
attribute: `${base}/attributes/${ids.id}`,
page: `${base}/pages/${ids.id}`,
event: `${base}/events/${ids.id}`,
experiment: `${base}/experiments/${ids.id}`,
flag: `${base}/features/${ids.id}`,
variation: `${base}/projects/${ids.project}/features/${ids.flag}/variations/${ids.id}`,
environment: `${base}/environments/${ids.id}`,
ruleset: `${base}/flags/${ids.flag}/environments/${ids.env}/ruleset`,
rule: `${base}/flags/${ids.flag}/environments/${ids.env}/rules/${ids.id}`,
section: `${base}/experiments/${ids.id}/sections/${ids.id}`,
};
const url = urlMap[entity];
if (mode === "archive") {
return this.api("POST", `${url}/archive`);
}
if (mode === "full") {
return this.api("PATCH", url, changes);
}
const ops = Object.entries(changes).map(([k, v]) => ({
op: "replace",
path: `/${k}`,
value: v,
}));
return this.patchJson(url, ops);
}
}
//# sourceMappingURL=optly-helpers.js.map