-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
87 lines (77 loc) · 3.84 KB
/
Copy pathindex.ts
File metadata and controls
87 lines (77 loc) · 3.84 KB
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
import { FastifyReply, FastifyRequest } from "fastify";
import { LivePreviewShopify } from "@contentstack/shopify-live-preview-sdk";
import { FieldSchema, Entry } from "@contentstack/shopify-live-preview-sdk/dist/setup-exports";
import _ from "lodash";
import { config } from "../config.js";
import path from "path";
import { fileURLToPath } from 'url';
// Get the current directory for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Define the request body interface
interface PreviewDataRequestBody {
live_preview: string;
ctUid: string;
entryUid: string;
locale: string;
theme_variable: {
liquid_path: string;
data_cslp: string;
payload?: any;
};
}
// Configure the Liquid engine with the middleware's views directory
const viewsPath = path.resolve(__dirname, "../../views");
const liquidEngineOptions = {
root: [
viewsPath,
path.resolve(viewsPath, "snippets"),
path.resolve(viewsPath, "sections"),
path.resolve(viewsPath, "assets"),
path.resolve(viewsPath, "config"),
path.resolve(viewsPath, "layout"),
path.resolve(viewsPath, "locales"),
path.resolve(viewsPath, "templates"),
]
}
const livePreviewShopify = LivePreviewShopify.getInstance({...config.contentstack, liquidEngineOptions });
const engine = livePreviewShopify.getLiquidEngine();
// You can add more filter or register more tags here into the engine if needed
// example: engine.registerFilter('my_custom_filter', (value) => {
// return `Custom filter applied to: ${value}`;
// });
export const getPreviewDataHandler = async (req: FastifyRequest<{ Body: PreviewDataRequestBody }>, res: FastifyReply) => {
const { live_preview, ctUid, entryUid, locale, theme_variable } = req.body;
const { liquid_path } = theme_variable;
const entryData: { schema: FieldSchema, entry: Entry } = await livePreviewShopify.fetchData(ctUid, entryUid, live_preview, locale) as { schema: FieldSchema, entry: Entry };
let shopifyData = { ...theme_variable?.payload };
const keyBasedCt = livePreviewShopify.createContentTypeKeyBased([entryData.schema]);
const updatedEntry = entryData.entry;
if (_.get(shopifyData, 'product.metafields.contentstack_products', null)) {
const currentMetafields = shopifyData.product.metafields.contentstack_products;
const updatedMetafields = await livePreviewShopify.getUpdatedProductMetafields(currentMetafields, keyBasedCt, updatedEntry, { ctUid: ctUid, entryUid: entryUid, hash: live_preview })
shopifyData.product.metafields.contentstack_products = updatedMetafields;
}
if (_.get(shopifyData, 'metaobjects', null)) {
const currentMetaobjects = shopifyData.metaobjects;
const mappedShopifyData = await livePreviewShopify.getUpdatedMetaobject({ ...currentMetaobjects }, keyBasedCt, updatedEntry, { ctUid: ctUid, hash: live_preview });
shopifyData.metaobjects = mappedShopifyData.currentMetaobjects;
}
if(typeof liquid_path !== 'string') {
return res.status(400).send({ message: 'Invalid liquid path' });
}
const liquidFilePath = liquid_path.replace(/\./g, "/");
try {
const newRenderedData = await engine.renderFile(liquidFilePath, shopifyData);
return res.send({ html: newRenderedData });
} catch (error: any) {
console.error('=== LIQUID RENDERING ERROR ===');
console.error('Error message:', error.message);
console.error('Error stack:', error.stack);
console.error('Template path:', liquidFilePath);
console.error('Data passed to template:', JSON.stringify(shopifyData, null, 2));
return res.status(500).send({ message: 'Error rendering liquid file' });
}
}
// Export the views health handler
export { viewsHealthHandler } from './viewsHealthController.js';