alinea
Version:
Headless git-based CMS
108 lines (106 loc) • 3.44 kB
JavaScript
import "../../chunks/chunk-NZLE2WMY.js";
// src/adapter/next/cms.tsx
import { Headers } from "@alinea/iso";
import { Client } from "alinea/core/Client";
import { CMS } from "alinea/core/CMS";
import { outcome } from "alinea/core/Outcome";
import { getPreviewPayloadFromCookies } from "alinea/preview/PreviewCookies";
import { requestContext } from "./context.js";
import { jsx } from "react/jsx-runtime";
var NextCMS = class extends CMS {
constructor(config) {
super(config);
}
async resolve(query) {
let status = query.status;
const { handlerUrl, apiKey } = await requestContext(this.config);
const client = new Client({
config: this.config,
url: handlerUrl.href,
applyAuth: (init) => {
const headers = new Headers(init?.headers);
headers.set("Authorization", `Bearer ${apiKey}`);
return { ...init, headers };
}
});
let preview;
const { cookies, draftMode } = await import("next/headers");
const [isDraft] = await outcome(async () => (await draftMode()).isEnabled);
if (isDraft) {
if (!status) status = "preferDraft";
const cookie = await cookies();
const payload = getPreviewPayloadFromCookies(cookie.getAll());
if (payload) preview = { payload };
}
return client.resolve({ preview, ...query, status });
}
async #authenticatedClient() {
const { handlerUrl, apiKey } = await requestContext(this.config);
const authCookies = [];
try {
const { cookies } = await import("next/headers");
const cookie = await cookies();
for (const { name, value } of cookie.getAll()) {
if (name.startsWith("alinea.")) {
authCookies.push([name, value]);
}
}
} catch {
}
return new Client({
config: this.config,
url: handlerUrl.href,
applyAuth: (init) => {
const headers = new Headers(init?.headers);
headers.set("Authorization", `Bearer ${apiKey}`);
if (authCookies.length) {
for (const [name, value] of authCookies) {
headers.set("Cookie", `${name}=${value}`);
}
}
return { ...init, headers };
}
});
}
async user() {
const client = await this.#authenticatedClient();
return client.user();
}
async mutate(mutations) {
const client = await this.#authenticatedClient();
return client.mutate(mutations);
}
async prepareUpload(file) {
const client = await this.#authenticatedClient();
return client.prepareUpload(file);
}
previews = async ({ widget, workspace, root }) => {
const { draftMode } = await import("next/headers");
const { default: dynamic } = await import("next/dynamic");
const [isDraft] = await outcome(async () => (await draftMode()).isEnabled);
if (!isDraft) return null;
const { isDev, handlerUrl } = await requestContext(this.config);
let file = this.config.dashboardFile ?? "/admin.html";
if (!file.startsWith("/")) file = `/${file}`;
const dashboardUrl = isDev ? new URL("/", handlerUrl) : new URL(file, handlerUrl);
const NextPreviews = dynamic(() => import("./previews.js"), {
ssr: false
});
return /* @__PURE__ */ jsx(
NextPreviews,
{
dashboardUrl: dashboardUrl.href,
widget,
workspace,
root
}
);
};
};
function createCMS(config) {
return new NextCMS(config);
}
export {
NextCMS,
createCMS
};