@mapled/next 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/index.js +28 -7
- package/dist/server.d.ts +8 -0
- package/dist/server.js +53 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -54,6 +54,24 @@ export const POST = createRevalidateHandler({
|
|
|
54
54
|
|
|
55
55
|
Then in Mapled (project → Integrations → **Your site**) set the revalidation webhook to `https://your-site.com/api/mapled/revalidate` and copy the signing secret into `MAPLED_WEBHOOK_SECRET`. Every publish sends a signed ping; the handler verifies the `x-mapled-signature` HMAC and revalidates the `mapled` tags.
|
|
56
56
|
|
|
57
|
+
## Preview drafts
|
|
58
|
+
|
|
59
|
+
Mount the preview routes:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
// app/api/mapled/preview/route.ts
|
|
63
|
+
import { createPreviewHandler } from "@mapled/next/server";
|
|
64
|
+
export const GET = createPreviewHandler();
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
// app/api/mapled/preview/exit/route.ts
|
|
69
|
+
import { createExitPreviewHandler } from "@mapled/next/server";
|
|
70
|
+
export const GET = createExitPreviewHandler();
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The **Preview** button in Mapled opens your site through a one-time link: the route turns on Next.js draft mode, and every `getRecords`/`getRecord`/`getSingle` call automatically switches to live drafts, uncached. No code changes in your components — the client detects draft mode on its own. Sessions last two hours; `/api/mapled/preview/exit` leaves preview.
|
|
74
|
+
|
|
57
75
|
## API
|
|
58
76
|
|
|
59
77
|
- `createClient({ key, apiUrl? })` → `getRecords(collection, opts?)`, `getRecord(collection, id, opts?)`, `getSingle(key, opts?)`
|
package/dist/index.js
CHANGED
|
@@ -9,19 +9,40 @@ export class MapledError extends Error {
|
|
|
9
9
|
this.status = status;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
+
/** The draft session cookie set by the preview route. When Next draft
|
|
13
|
+
mode is on, every read switches to live drafts, uncached. Outside a
|
|
14
|
+
Next request context this quietly resolves to null. */
|
|
15
|
+
async function draftSession() {
|
|
16
|
+
try {
|
|
17
|
+
const mod = await import("next/headers");
|
|
18
|
+
if (!(await mod.draftMode()).isEnabled)
|
|
19
|
+
return null;
|
|
20
|
+
const jar = await mod.cookies();
|
|
21
|
+
return jar.get("mapled_draft")?.value ?? null;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
12
27
|
export function createClient(config) {
|
|
13
28
|
const base = (config.apiUrl ?? "https://api.mapled.io").replace(/\/+$/, "");
|
|
14
29
|
if (!config.key)
|
|
15
30
|
throw new Error("Mapled: a delivery key is required.");
|
|
16
31
|
async function call(path, search, opts, cacheTags) {
|
|
17
32
|
const qs = search.toString();
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
33
|
+
const draft = await draftSession();
|
|
34
|
+
const init = draft
|
|
35
|
+
? {
|
|
36
|
+
headers: { "x-mapled-key": config.key, "x-mapled-draft": draft },
|
|
37
|
+
cache: "no-store",
|
|
38
|
+
}
|
|
39
|
+
: {
|
|
40
|
+
headers: { "x-mapled-key": config.key },
|
|
41
|
+
next: {
|
|
42
|
+
revalidate: opts.revalidate ?? 3600,
|
|
43
|
+
tags: ["mapled", ...cacheTags, ...(opts.tags ?? [])],
|
|
44
|
+
},
|
|
45
|
+
};
|
|
25
46
|
const res = await fetch(`${base}${path}${qs ? `?${qs}` : ""}`, init);
|
|
26
47
|
if (!res.ok) {
|
|
27
48
|
let message = `Mapled request failed (${res.status}).`;
|
package/dist/server.d.ts
CHANGED
|
@@ -10,6 +10,14 @@
|
|
|
10
10
|
then refreshes all "mapled"-tagged fetches instantly. */
|
|
11
11
|
/** Constant-time check of the "x-mapled-signature" header value. */
|
|
12
12
|
export declare function verifySignature(secret: string, body: string, signature: string): Promise<boolean>;
|
|
13
|
+
/** GET handler for the preview route (mount at /api/mapled/preview).
|
|
14
|
+
Trades the one-time link token from Mapled for a draft session,
|
|
15
|
+
turns on Next draft mode, and redirects into the site. */
|
|
16
|
+
export declare function createPreviewHandler(options?: {
|
|
17
|
+
apiUrl?: string;
|
|
18
|
+
}): (req: Request) => Promise<Response>;
|
|
19
|
+
/** GET handler to leave preview (mount at /api/mapled/preview/exit). */
|
|
20
|
+
export declare function createExitPreviewHandler(): (req: Request) => Promise<Response>;
|
|
13
21
|
export declare function createRevalidateHandler(options: {
|
|
14
22
|
secret: string;
|
|
15
23
|
/** Cache tags to refresh — defaults to ["mapled"], which covers every
|
package/dist/server.js
CHANGED
|
@@ -27,6 +27,59 @@ export async function verifySignature(secret, body, signature) {
|
|
|
27
27
|
}
|
|
28
28
|
return diff === 0;
|
|
29
29
|
}
|
|
30
|
+
const DRAFT_COOKIE = "mapled_draft";
|
|
31
|
+
/** Same-app relative path only — preview links can't bounce elsewhere. */
|
|
32
|
+
function safeNext(raw) {
|
|
33
|
+
return raw && /^\/(?!\/)/.test(raw) ? raw : "/";
|
|
34
|
+
}
|
|
35
|
+
/** GET handler for the preview route (mount at /api/mapled/preview).
|
|
36
|
+
Trades the one-time link token from Mapled for a draft session,
|
|
37
|
+
turns on Next draft mode, and redirects into the site. */
|
|
38
|
+
export function createPreviewHandler(options = {}) {
|
|
39
|
+
const base = (options.apiUrl ?? "https://api.mapled.io").replace(/\/+$/, "");
|
|
40
|
+
return async function GET(req) {
|
|
41
|
+
const url = new URL(req.url);
|
|
42
|
+
const token = url.searchParams.get("token");
|
|
43
|
+
if (!token)
|
|
44
|
+
return new Response("Missing preview token.", { status: 400 });
|
|
45
|
+
const res = await fetch(`${base}/v1/delivery/preview/exchange`, {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers: { "content-type": "application/json" },
|
|
48
|
+
body: JSON.stringify({ token }),
|
|
49
|
+
});
|
|
50
|
+
if (!res.ok) {
|
|
51
|
+
return new Response("This preview link has expired. Open it from Mapled again.", {
|
|
52
|
+
status: 401,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const { draftToken, expiresAt } = (await res.json());
|
|
56
|
+
const { draftMode, cookies } = await import("next/headers");
|
|
57
|
+
(await draftMode()).enable();
|
|
58
|
+
(await cookies()).set(DRAFT_COOKIE, draftToken, {
|
|
59
|
+
httpOnly: true,
|
|
60
|
+
secure: true,
|
|
61
|
+
sameSite: "lax",
|
|
62
|
+
path: "/",
|
|
63
|
+
expires: new Date(expiresAt),
|
|
64
|
+
});
|
|
65
|
+
return new Response(null, {
|
|
66
|
+
status: 307,
|
|
67
|
+
headers: { location: safeNext(url.searchParams.get("next")) },
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** GET handler to leave preview (mount at /api/mapled/preview/exit). */
|
|
72
|
+
export function createExitPreviewHandler() {
|
|
73
|
+
return async function GET(req) {
|
|
74
|
+
const { draftMode, cookies } = await import("next/headers");
|
|
75
|
+
(await draftMode()).disable();
|
|
76
|
+
(await cookies()).delete(DRAFT_COOKIE);
|
|
77
|
+
return new Response(null, {
|
|
78
|
+
status: 307,
|
|
79
|
+
headers: { location: safeNext(new URL(req.url).searchParams.get("next")) },
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
}
|
|
30
83
|
export function createRevalidateHandler(options) {
|
|
31
84
|
if (!options.secret) {
|
|
32
85
|
throw new Error("Mapled: the webhook secret is required.");
|
package/package.json
CHANGED