-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path+server.ts
98 lines (86 loc) · 3.12 KB
/
+server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { env as privateEnv } from '$env/dynamic/private';
import { recordToWebsiteRoute } from '$lib/datocms/recordInfo';
import { json } from '@sveltejs/kit';
import { handleUnexpectedError, invalidRequestResponse, withCORS } from '../utils';
import type { RequestHandler } from './$types';
export const OPTIONS: RequestHandler = ({ request }) => {
return new Response('OK', withCORS());
};
type PreviewLink = {
label: string;
url: string;
reloadPreviewOnRecordUpdate?: boolean | { delayInMs: number };
};
type WebPreviewsResponse = {
previewLinks: PreviewLink[];
};
/**
* This route handler implements the Previews webhook required for the "Web
* Previews" plugin:
*
* https://www.datocms.com/marketplace/plugins/i/datocms-plugin-web-previews#the-previews-webhook
*/
export const POST: RequestHandler = async ({ url, request }) => {
try {
// Parse query string parameters
const token = url.searchParams.get('token');
// Ensure that the request is coming from a trusted source
if (token !== privateEnv.PRIVATE_SECRET_API_TOKEN) {
return invalidRequestResponse('Invalid token', 401);
}
/**
* The plugin sends the record and model for which the user wants a preview,
* along with information about which locale they are currently viewing in
* the interface
*/
const { item, itemType, locale } = await request.json();
// We can use this info to generate the frontend URL associated
const recordUrl = await recordToWebsiteRoute(item, itemType.attributes.api_key, locale);
const response: WebPreviewsResponse = { previewLinks: [] };
if (recordUrl) {
/**
* If status is not published, it means that it has a current version that's
* different from the published one, so it has a draft version!
*/
if (item.meta.status !== 'published') {
/**
* Generate a URL that initially enters Draft Mode, and then
* redirects to the desired URL
*/
response.previewLinks.push({
label: 'Draft version',
url: new URL(
/*
* We generate the URL in a way that it first passes through the
* endpoint that enables the Draft Mode.
*/
`/api/draft-mode/enable?url=${recordUrl}&token=${token}`,
request.url,
).toString(),
});
}
/** If status is not draft, it means that it has a published version! */
if (item.meta.status !== 'draft') {
/**
* Generate a URL that first exits from Draft Mode, and then
* redirects to the desired URL.
*/
response.previewLinks.push({
label: 'Published version',
url: new URL(
/*
* We generate the URL in a way that it first passes through the
* endpoint that disables the Draft Mode.
*/
`/api/draft-mode/disable?url=${recordUrl}`,
request.url,
).toString(),
});
}
}
// Respond in the format expected by the plugin
return json(response, withCORS());
} catch (error) {
return handleUnexpectedError(error);
}
};