UNPKG

studiocms

Version:

Astro Native CMS for AstroDB. Built from the ground up by the Astro community.

61 lines (60 loc) 1.96 kB
import { apiResponseLogger } from "studiocms:logger"; import { Notifications } from "studiocms:notifier"; import { SDKCore } from "studiocms:sdk"; import { AllResponse, createEffectAPIRoutes, createJsonResponse, Effect, genLogger, OptionsResponse, readAPIContextJson } from "../../../../../effect.js"; const { POST, OPTIONS, ALL } = createEffectAPIRoutes( { POST: (ctx) => genLogger("studiocms/routes/api/dashboard/content/diff.POST")(function* () { const [notify, sdk] = yield* Effect.all([Notifications, SDKCore]); const userData = ctx.locals.StudioCMS.security?.userSessionData; if (!userData?.isLoggedIn) { return apiResponseLogger(403, "Unauthorized"); } const isAuthorized = ctx.locals.StudioCMS.security?.userPermissionLevel.isEditor; if (!isAuthorized) { return apiResponseLogger(403, "Unauthorized"); } const jsonData = yield* readAPIContextJson(ctx); const { id, type } = jsonData; if (!id || !type) { return apiResponseLogger(400, "Invalid ID or Type"); } if (!["data", "content", "both"].includes(type)) { return apiResponseLogger(400, "Invalid Type"); } const data = yield* sdk.diffTracking.revertToDiff(id, type); yield* Effect.all([ sdk.CLEAR.pages(), notify.sendEditorNotification("page_updated", data.pageMetaData.end.title || "") ]); return apiResponseLogger(200, "Page Reverted successfully"); }).pipe(Notifications.Provide), OPTIONS: () => Effect.try(() => OptionsResponse({ allowedMethods: ["POST"] })), ALL: () => Effect.try(() => AllResponse()) }, { cors: { methods: ["POST", "OPTIONS"] }, onError: (error) => { console.error("Error in diff API:", error); return createJsonResponse( { error: "Internal Server Error" }, { status: 500 } ); } } ); export { ALL, OPTIONS, POST };