studiocms
Version:
Astro Native CMS for AstroDB. Built from the ground up by the Astro community.
91 lines (90 loc) • 2.5 kB
JavaScript
import { sdk as sdkConfig } from "studiocms:config";
import { Context, Effect, Layer } from "../../effect.js";
import { SDKCoreError, StudioCMS_SDK_Error } from "./errors.js";
class CacheContext extends Context.Tag("CacheContext")() {
static makeLayer = (context) => Layer.succeed(this, this.of(context));
static makeProvide = (context) => Effect.provide(this.makeLayer(context));
}
const cacheConfig = sdkConfig.cacheConfig;
const isCacheEnabled = Effect.try(() => cacheConfig.enabled);
const _ClearUnknownError = (id, cause) => Effect.fail(
new SDKCoreError({
type: "UNKNOWN",
cause: new StudioCMS_SDK_Error(`${id} Error: ${cause}`)
})
);
const _clearLibSQLError = (id, cause) => Effect.fail(
new SDKCoreError({
type: "LibSQLDatabaseError",
cause: new StudioCMS_SDK_Error(`${id} Error: ${cause}`)
})
);
function folderTreeReturn(data) {
return {
data,
lastCacheUpdate: /* @__PURE__ */ new Date()
};
}
function folderListReturn(data) {
return {
data,
lastCacheUpdate: /* @__PURE__ */ new Date()
};
}
function pageDataReturn(data) {
return {
data,
lastCacheUpdate: /* @__PURE__ */ new Date()
};
}
function siteConfigReturn(siteConfig) {
return {
data: siteConfig,
lastCacheUpdate: /* @__PURE__ */ new Date()
};
}
function versionReturn(version) {
return {
version,
lastCacheUpdate: /* @__PURE__ */ new Date()
};
}
function convertCombinedPageDataToMetaOnly(data) {
if (Array.isArray(data)) {
return data.map(({ lastCacheUpdate: lastCacheUpdate2, data: { defaultContent, multiLangContent, ...data2 } }) => ({
lastCacheUpdate: lastCacheUpdate2,
data: data2
}));
}
const {
lastCacheUpdate,
data: { defaultContent: _dump1, multiLangContent: _dump2, ...metaOnlyData }
} = data;
return {
lastCacheUpdate,
data: metaOnlyData
};
}
function isCacheExpired(entry, lifetime = cacheConfig.lifetime) {
return Date.now() - entry.lastCacheUpdate.getTime() > lifetime;
}
function filterPagesByDraftAndIndex(pages, includeDrafts, hideDefaultIndex) {
return pages.filter(
({ draft, slug }) => (includeDrafts || draft === false || draft === null) && (!hideDefaultIndex || slug !== "index")
);
}
export {
CacheContext,
_ClearUnknownError,
_clearLibSQLError,
cacheConfig,
convertCombinedPageDataToMetaOnly,
filterPagesByDraftAndIndex,
folderListReturn,
folderTreeReturn,
isCacheEnabled,
isCacheExpired,
pageDataReturn,
siteConfigReturn,
versionReturn
};