@toggle.tiger/toggletiger
Version:
A package to retrieve configuration from blobstore based on org, app, env, and config ID.
54 lines (53 loc) • 2.34 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import axios from "axios";
export const fetchConfigDetails = (organizationId, teamId, applicationId, configId, environmentId) => __awaiter(void 0, void 0, void 0, function* () {
const response = yield axios.get(`/organizations/${organizationId}/teams/${teamId}/applications/${applicationId}/configs/${configId}/environments/${environmentId}/latest-snapshot`);
return response.data;
});
export const fetchConfigBlobWithCaching = (locationUri, etag) => __awaiter(void 0, void 0, void 0, function* () {
try {
// Set up headers with If-None-Match if we have an ETag
const headers = {};
if (etag) {
headers["If-None-Match"] = etag;
}
const response = yield axios.get(locationUri, {
headers,
validateStatus: (status) => {
// Accept 200 (OK) and 304 (Not Modified) as valid responses
return status === 200 || status === 304;
}
});
// If response is 304 Not Modified, return notModified: true
if (response.status === 304) {
return {
response: null,
etag: etag || null,
notModified: true
};
}
// Get the new ETag from the response
const newEtag = response.headers.etag || null;
return {
response: response.data,
etag: newEtag,
notModified: false
};
}
catch (error) {
console.error("Error fetching blob with caching:", error);
return {
response: null,
etag: null,
notModified: false
};
}
});