@alpaca-headless/alpaca-headless-nextjs 1.0.2684 → 1.0.2686
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/dist/cjs/alpacaContext.js +12 -0
- package/dist/cjs/components/Picture.js +45 -0
- package/dist/cjs/components/Placeholder.js +55 -0
- package/dist/cjs/components/RichText.js +28 -0
- package/dist/cjs/components/Text.js +25 -0
- package/dist/cjs/components/index.js +20 -0
- package/dist/cjs/fieldTypes.js +2 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/layoutData.js +2 -0
- package/dist/cjs/loadRouteData.js +74 -0
- package/dist/esm/alpacaContext.js +8 -0
- package/dist/esm/components/Picture.js +41 -0
- package/dist/esm/components/Placeholder.js +51 -0
- package/dist/esm/components/RichText.js +21 -0
- package/dist/esm/components/Text.js +18 -0
- package/dist/esm/components/index.js +4 -0
- package/dist/esm/fieldTypes.js +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/layoutData.js +1 -0
- package/dist/esm/loadRouteData.js +70 -0
- package/package.json +1 -1
- package/types/alpacaContext.d.ts +14 -0
- package/types/components/Picture.d.ts +16 -0
- package/types/components/Placeholder.d.ts +5 -0
- package/types/components/RichText.d.ts +12 -0
- package/types/components/Text.d.ts +7 -0
- package/types/components/index.d.ts +4 -0
- package/types/fieldTypes.d.ts +17 -0
- package/types/index.d.ts +5 -0
- package/types/layoutData.d.ts +11 -0
- package/types/loadRouteData.d.ts +27 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useAlpacaContext = void 0;
|
|
4
|
+
function useAlpacaContext() {
|
|
5
|
+
let context = globalThis.alpacaContext;
|
|
6
|
+
if (!context) {
|
|
7
|
+
context = {};
|
|
8
|
+
globalThis.alpacaContext = context;
|
|
9
|
+
}
|
|
10
|
+
return context;
|
|
11
|
+
}
|
|
12
|
+
exports.useAlpacaContext = useAlpacaContext;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.Picture = void 0;
|
|
15
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
16
|
+
// const addClassName = (otherAttrs: { [key: string]: unknown }): void => {
|
|
17
|
+
// if (otherAttrs.class) {
|
|
18
|
+
// // if any classes are defined properly already
|
|
19
|
+
// if (otherAttrs.className) {
|
|
20
|
+
// let className: string = otherAttrs.className as string;
|
|
21
|
+
// className += ` ${otherAttrs.class}`;
|
|
22
|
+
// otherAttrs.className = className;
|
|
23
|
+
// } else {
|
|
24
|
+
// otherAttrs.className = otherAttrs.class;
|
|
25
|
+
// }
|
|
26
|
+
// delete otherAttrs.class;
|
|
27
|
+
// }
|
|
28
|
+
// };
|
|
29
|
+
const Picture = (_a) => {
|
|
30
|
+
var _b, _c, _d, _e;
|
|
31
|
+
var { field, imageParams, sources, defaultVariant } = _a, otherProps = __rest(_a, ["field", "imageParams", "sources", "defaultVariant"]);
|
|
32
|
+
const variants = (_b = field === null || field === void 0 ? void 0 : field.value) === null || _b === void 0 ? void 0 : _b.variants;
|
|
33
|
+
const img = (_d = (_c = field === null || field === void 0 ? void 0 : field.value) === null || _c === void 0 ? void 0 : _c.variants.find((x) => x.name.toUpperCase() == (defaultVariant === null || defaultVariant === void 0 ? void 0 : defaultVariant.toUpperCase()))) !== null && _d !== void 0 ? _d : (_e = field === null || field === void 0 ? void 0 : field.value) === null || _e === void 0 ? void 0 : _e.variants[0];
|
|
34
|
+
const attrs = Object.assign(Object.assign({}, img), otherProps);
|
|
35
|
+
if (!(sources === null || sources === void 0 ? void 0 : sources.length)) {
|
|
36
|
+
return (0, jsx_runtime_1.jsx)("img", Object.assign({}, attrs, { alt: "" }));
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return ((0, jsx_runtime_1.jsxs)("picture", { children: [sources === null || sources === void 0 ? void 0 : sources.map((source) => {
|
|
40
|
+
const variant = variants === null || variants === void 0 ? void 0 : variants.find((v) => v.name.toLowerCase() == source.variant.toLowerCase());
|
|
41
|
+
return ((0, jsx_runtime_1.jsx)("source", { srcSet: variant === null || variant === void 0 ? void 0 : variant.src, media: source.media }, source.variant + source.media));
|
|
42
|
+
}), (0, jsx_runtime_1.jsx)("img", Object.assign({}, attrs, { alt: "" }))] }));
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
exports.Picture = Picture;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Placeholder = void 0;
|
|
13
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
14
|
+
const __1 = require("..");
|
|
15
|
+
function Placeholder({ name, content, }) {
|
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
const data = yield content;
|
|
18
|
+
if (!("placeholders" in data))
|
|
19
|
+
return;
|
|
20
|
+
const placeholderData = data.placeholders.find((x) => x.name == name);
|
|
21
|
+
if (!placeholderData)
|
|
22
|
+
return;
|
|
23
|
+
const context = (0, __1.useAlpacaContext)();
|
|
24
|
+
const loader = context.componentModuleLoader;
|
|
25
|
+
const promises = placeholderData === null || placeholderData === void 0 ? void 0 : placeholderData.components.map((component) => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
let module = null;
|
|
27
|
+
try {
|
|
28
|
+
module = yield loader(component.componentType);
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
console.log(e);
|
|
32
|
+
}
|
|
33
|
+
if (!module) {
|
|
34
|
+
console.log("Could not find module " + component.componentType);
|
|
35
|
+
return {
|
|
36
|
+
data: { name: component.componentType },
|
|
37
|
+
Component: ComponentNotFound,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return { data: component, Component: module.default };
|
|
41
|
+
}));
|
|
42
|
+
const components = yield Promise.all(promises);
|
|
43
|
+
return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: components.map(({ Component, data }) => ((0, jsx_runtime_1.jsx)(Component, { component: data }))) }));
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
exports.Placeholder = Placeholder;
|
|
47
|
+
function ComponentNotFound({ component }) {
|
|
48
|
+
return ((0, jsx_runtime_1.jsxs)("div", { style: {
|
|
49
|
+
backgroundColor: "orange",
|
|
50
|
+
color: "red",
|
|
51
|
+
fontSize: 18,
|
|
52
|
+
padding: 10,
|
|
53
|
+
border: "3px solid red",
|
|
54
|
+
}, children: ["Component not found: ", component.name, " "] }));
|
|
55
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.RichText = void 0;
|
|
16
|
+
const react_1 = __importDefault(require("react"));
|
|
17
|
+
function RichText({ field, tag, editable, }) {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
const props = {
|
|
20
|
+
dangerouslySetInnerHTML: {
|
|
21
|
+
__html: field.value,
|
|
22
|
+
},
|
|
23
|
+
editable: editable,
|
|
24
|
+
};
|
|
25
|
+
return react_1.default.createElement(tag || "div", props);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
exports.RichText = RichText;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Text = void 0;
|
|
16
|
+
const react_1 = __importDefault(require("react"));
|
|
17
|
+
function Text({ field, tag, editable, }) {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
if (editable)
|
|
20
|
+
return react_1.default.createElement(tag || "div", null, field.value);
|
|
21
|
+
else
|
|
22
|
+
return react_1.default.createElement(tag || "div", null, field.value);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
exports.Text = Text;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./Placeholder"), exports);
|
|
18
|
+
__exportStar(require("./Text"), exports);
|
|
19
|
+
__exportStar(require("./Picture"), exports);
|
|
20
|
+
__exportStar(require("./RichText"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./layoutData"), exports);
|
|
18
|
+
__exportStar(require("./loadRouteData"), exports);
|
|
19
|
+
__exportStar(require("./components"), exports);
|
|
20
|
+
__exportStar(require("./alpacaContext"), exports);
|
|
21
|
+
__exportStar(require("./fieldTypes"), exports);
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.loadRouteData = void 0;
|
|
13
|
+
const alpacaContext_1 = require("./alpacaContext");
|
|
14
|
+
const headers_1 = require("next/headers");
|
|
15
|
+
const react_1 = require("react");
|
|
16
|
+
const fetchRouteData = (0, react_1.cache)((path, host, mode, itemId, language) => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
const request = {
|
|
18
|
+
Path: path,
|
|
19
|
+
Host: host,
|
|
20
|
+
Mode: mode,
|
|
21
|
+
ItemId: itemId,
|
|
22
|
+
Language: language,
|
|
23
|
+
};
|
|
24
|
+
const url = process.env.LAYOUT_SERVICE_URL + "/alpaca/headless/layout";
|
|
25
|
+
console.log(`Fetching page layout from ${url}. Host:${request.Host} Path: ${request.Path}`);
|
|
26
|
+
const response = yield fetch(url, {
|
|
27
|
+
method: "POST",
|
|
28
|
+
body: JSON.stringify(request),
|
|
29
|
+
credentials: "include",
|
|
30
|
+
headers: {
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
Cookie: (0, headers_1.cookies)().toString(),
|
|
33
|
+
},
|
|
34
|
+
next: { tags: ["pages"], revalidate: 10 },
|
|
35
|
+
});
|
|
36
|
+
if (response.status === 404) {
|
|
37
|
+
return {
|
|
38
|
+
type: "notfound",
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
if (response.status === 301) {
|
|
42
|
+
return {
|
|
43
|
+
type: "redirect",
|
|
44
|
+
location: response.url,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
if (!response.ok) {
|
|
48
|
+
const message = yield response.text();
|
|
49
|
+
return {
|
|
50
|
+
type: "error",
|
|
51
|
+
errorCode: response.status,
|
|
52
|
+
message: message,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const data = yield response.json();
|
|
56
|
+
console.log(data);
|
|
57
|
+
const context = (0, alpacaContext_1.useAlpacaContext)();
|
|
58
|
+
context.pageContext = {
|
|
59
|
+
itemId: data.page.id,
|
|
60
|
+
language: data.page.language,
|
|
61
|
+
site: data.site,
|
|
62
|
+
pageEditing: true,
|
|
63
|
+
pageState: "",
|
|
64
|
+
};
|
|
65
|
+
return Object.assign(Object.assign({}, data.page), { type: "page" });
|
|
66
|
+
}));
|
|
67
|
+
function loadRouteData(params, searchParams, host) {
|
|
68
|
+
var _a, _b;
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
const result = yield fetchRouteData((_b = (_a = params.path) === null || _a === void 0 ? void 0 : _a.join("/")) !== null && _b !== void 0 ? _b : "/", host, searchParams["mode"] || "preview", searchParams["itemid"], searchParams["language"]);
|
|
71
|
+
return result;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
exports.loadRouteData = loadRouteData;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
13
|
+
// const addClassName = (otherAttrs: { [key: string]: unknown }): void => {
|
|
14
|
+
// if (otherAttrs.class) {
|
|
15
|
+
// // if any classes are defined properly already
|
|
16
|
+
// if (otherAttrs.className) {
|
|
17
|
+
// let className: string = otherAttrs.className as string;
|
|
18
|
+
// className += ` ${otherAttrs.class}`;
|
|
19
|
+
// otherAttrs.className = className;
|
|
20
|
+
// } else {
|
|
21
|
+
// otherAttrs.className = otherAttrs.class;
|
|
22
|
+
// }
|
|
23
|
+
// delete otherAttrs.class;
|
|
24
|
+
// }
|
|
25
|
+
// };
|
|
26
|
+
export const Picture = (_a) => {
|
|
27
|
+
var _b, _c, _d, _e;
|
|
28
|
+
var { field, imageParams, sources, defaultVariant } = _a, otherProps = __rest(_a, ["field", "imageParams", "sources", "defaultVariant"]);
|
|
29
|
+
const variants = (_b = field === null || field === void 0 ? void 0 : field.value) === null || _b === void 0 ? void 0 : _b.variants;
|
|
30
|
+
const img = (_d = (_c = field === null || field === void 0 ? void 0 : field.value) === null || _c === void 0 ? void 0 : _c.variants.find((x) => x.name.toUpperCase() == (defaultVariant === null || defaultVariant === void 0 ? void 0 : defaultVariant.toUpperCase()))) !== null && _d !== void 0 ? _d : (_e = field === null || field === void 0 ? void 0 : field.value) === null || _e === void 0 ? void 0 : _e.variants[0];
|
|
31
|
+
const attrs = Object.assign(Object.assign({}, img), otherProps);
|
|
32
|
+
if (!(sources === null || sources === void 0 ? void 0 : sources.length)) {
|
|
33
|
+
return _jsx("img", Object.assign({}, attrs, { alt: "" }));
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
return (_jsxs("picture", { children: [sources === null || sources === void 0 ? void 0 : sources.map((source) => {
|
|
37
|
+
const variant = variants === null || variants === void 0 ? void 0 : variants.find((v) => v.name.toLowerCase() == source.variant.toLowerCase());
|
|
38
|
+
return (_jsx("source", { srcSet: variant === null || variant === void 0 ? void 0 : variant.src, media: source.media }, source.variant + source.media));
|
|
39
|
+
}), _jsx("img", Object.assign({}, attrs, { alt: "" }))] }));
|
|
40
|
+
}
|
|
41
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
11
|
+
import { useAlpacaContext, } from "..";
|
|
12
|
+
export function Placeholder({ name, content, }) {
|
|
13
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
14
|
+
const data = yield content;
|
|
15
|
+
if (!("placeholders" in data))
|
|
16
|
+
return;
|
|
17
|
+
const placeholderData = data.placeholders.find((x) => x.name == name);
|
|
18
|
+
if (!placeholderData)
|
|
19
|
+
return;
|
|
20
|
+
const context = useAlpacaContext();
|
|
21
|
+
const loader = context.componentModuleLoader;
|
|
22
|
+
const promises = placeholderData === null || placeholderData === void 0 ? void 0 : placeholderData.components.map((component) => __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
let module = null;
|
|
24
|
+
try {
|
|
25
|
+
module = yield loader(component.componentType);
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
console.log(e);
|
|
29
|
+
}
|
|
30
|
+
if (!module) {
|
|
31
|
+
console.log("Could not find module " + component.componentType);
|
|
32
|
+
return {
|
|
33
|
+
data: { name: component.componentType },
|
|
34
|
+
Component: ComponentNotFound,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return { data: component, Component: module.default };
|
|
38
|
+
}));
|
|
39
|
+
const components = yield Promise.all(promises);
|
|
40
|
+
return (_jsx(_Fragment, { children: components.map(({ Component, data }) => (_jsx(Component, { component: data }))) }));
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function ComponentNotFound({ component }) {
|
|
44
|
+
return (_jsxs("div", { style: {
|
|
45
|
+
backgroundColor: "orange",
|
|
46
|
+
color: "red",
|
|
47
|
+
fontSize: 18,
|
|
48
|
+
padding: 10,
|
|
49
|
+
border: "3px solid red",
|
|
50
|
+
}, children: ["Component not found: ", component.name, " "] }));
|
|
51
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import React from "react";
|
|
11
|
+
export function RichText({ field, tag, editable, }) {
|
|
12
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
13
|
+
const props = {
|
|
14
|
+
dangerouslySetInnerHTML: {
|
|
15
|
+
__html: field.value,
|
|
16
|
+
},
|
|
17
|
+
editable: editable,
|
|
18
|
+
};
|
|
19
|
+
return React.createElement(tag || "div", props);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import React from "react";
|
|
11
|
+
export function Text({ field, tag, editable, }) {
|
|
12
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
13
|
+
if (editable)
|
|
14
|
+
return React.createElement(tag || "div", null, field.value);
|
|
15
|
+
else
|
|
16
|
+
return React.createElement(tag || "div", null, field.value);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { useAlpacaContext } from "./alpacaContext";
|
|
11
|
+
import { cookies } from "next/headers";
|
|
12
|
+
import { cache } from "react";
|
|
13
|
+
const fetchRouteData = cache((path, host, mode, itemId, language) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
const request = {
|
|
15
|
+
Path: path,
|
|
16
|
+
Host: host,
|
|
17
|
+
Mode: mode,
|
|
18
|
+
ItemId: itemId,
|
|
19
|
+
Language: language,
|
|
20
|
+
};
|
|
21
|
+
const url = process.env.LAYOUT_SERVICE_URL + "/alpaca/headless/layout";
|
|
22
|
+
console.log(`Fetching page layout from ${url}. Host:${request.Host} Path: ${request.Path}`);
|
|
23
|
+
const response = yield fetch(url, {
|
|
24
|
+
method: "POST",
|
|
25
|
+
body: JSON.stringify(request),
|
|
26
|
+
credentials: "include",
|
|
27
|
+
headers: {
|
|
28
|
+
"Content-Type": "application/json",
|
|
29
|
+
Cookie: cookies().toString(),
|
|
30
|
+
},
|
|
31
|
+
next: { tags: ["pages"], revalidate: 10 },
|
|
32
|
+
});
|
|
33
|
+
if (response.status === 404) {
|
|
34
|
+
return {
|
|
35
|
+
type: "notfound",
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
if (response.status === 301) {
|
|
39
|
+
return {
|
|
40
|
+
type: "redirect",
|
|
41
|
+
location: response.url,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
const message = yield response.text();
|
|
46
|
+
return {
|
|
47
|
+
type: "error",
|
|
48
|
+
errorCode: response.status,
|
|
49
|
+
message: message,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const data = yield response.json();
|
|
53
|
+
console.log(data);
|
|
54
|
+
const context = useAlpacaContext();
|
|
55
|
+
context.pageContext = {
|
|
56
|
+
itemId: data.page.id,
|
|
57
|
+
language: data.page.language,
|
|
58
|
+
site: data.site,
|
|
59
|
+
pageEditing: true,
|
|
60
|
+
pageState: "",
|
|
61
|
+
};
|
|
62
|
+
return Object.assign(Object.assign({}, data.page), { type: "page" });
|
|
63
|
+
}));
|
|
64
|
+
export function loadRouteData(params, searchParams, host) {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
const result = yield fetchRouteData((_b = (_a = params.path) === null || _a === void 0 ? void 0 : _a.join("/")) !== null && _b !== void 0 ? _b : "/", host, searchParams["mode"] || "preview", searchParams["itemid"], searchParams["language"]);
|
|
68
|
+
return result;
|
|
69
|
+
});
|
|
70
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type AlpacaContext = {
|
|
2
|
+
componentModuleLoader: (name: string) => Promise<any>;
|
|
3
|
+
pageContext: {
|
|
4
|
+
itemId: string;
|
|
5
|
+
language: string;
|
|
6
|
+
pageEditing: boolean;
|
|
7
|
+
pageState: string;
|
|
8
|
+
site: {
|
|
9
|
+
name: string;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export declare function useAlpacaContext(): AlpacaContext;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { PictureField } from "../fieldTypes";
|
|
3
|
+
export interface PictureSource {
|
|
4
|
+
media: string;
|
|
5
|
+
variant: string;
|
|
6
|
+
}
|
|
7
|
+
export interface PictureProps {
|
|
8
|
+
[attributeName: string]: unknown;
|
|
9
|
+
field?: PictureField;
|
|
10
|
+
imageParams?: {
|
|
11
|
+
[paramName: string]: string | number;
|
|
12
|
+
};
|
|
13
|
+
defaultVariant?: string;
|
|
14
|
+
sources?: PictureSource[];
|
|
15
|
+
}
|
|
16
|
+
export declare const Picture: React.FC<PictureProps>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ComponentData, Page, Redirect, NotFound, Error } from "..";
|
|
2
|
+
export declare function Placeholder({ name, content, }: {
|
|
3
|
+
name: string;
|
|
4
|
+
content: Promise<Error | Page | Redirect | NotFound | ComponentData> | ComponentData;
|
|
5
|
+
}): Promise<import("react/jsx-runtime").JSX.Element | undefined>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { TextField } from "../fieldTypes";
|
|
3
|
+
export declare function RichText({ field, tag, editable, }: {
|
|
4
|
+
field: TextField;
|
|
5
|
+
tag?: string;
|
|
6
|
+
editable?: boolean;
|
|
7
|
+
}): Promise<React.DOMElement<{
|
|
8
|
+
dangerouslySetInnerHTML: {
|
|
9
|
+
__html: string;
|
|
10
|
+
};
|
|
11
|
+
editable: boolean | undefined;
|
|
12
|
+
}, Element>>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type TextField = {
|
|
2
|
+
value: string;
|
|
3
|
+
};
|
|
4
|
+
export interface PictureField {
|
|
5
|
+
value: PictureValue | null;
|
|
6
|
+
}
|
|
7
|
+
export interface PictureValue {
|
|
8
|
+
altText: string;
|
|
9
|
+
alt: string;
|
|
10
|
+
variants: PictureVariant[];
|
|
11
|
+
loopVideos: boolean;
|
|
12
|
+
}
|
|
13
|
+
export interface PictureVariant {
|
|
14
|
+
name: string;
|
|
15
|
+
src: string;
|
|
16
|
+
videoId: string;
|
|
17
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { PlaceholderData } from "./layoutData";
|
|
2
|
+
type Response = {
|
|
3
|
+
type: "page" | "redirect" | "error" | "notfound";
|
|
4
|
+
};
|
|
5
|
+
export type Error = Response & {
|
|
6
|
+
type: "error";
|
|
7
|
+
message: string;
|
|
8
|
+
errorCode: number;
|
|
9
|
+
};
|
|
10
|
+
export type Page = Response & {
|
|
11
|
+
type: "page";
|
|
12
|
+
placeholders: PlaceholderData[];
|
|
13
|
+
meta: {
|
|
14
|
+
title: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export type Redirect = Response & {
|
|
18
|
+
type: "redirect";
|
|
19
|
+
location: string;
|
|
20
|
+
};
|
|
21
|
+
export type NotFound = Response & {
|
|
22
|
+
type: "notfound";
|
|
23
|
+
};
|
|
24
|
+
export declare function loadRouteData(params: {
|
|
25
|
+
path: string[];
|
|
26
|
+
}, searchParams: any, host: string): Promise<Page | Error | Redirect | NotFound>;
|
|
27
|
+
export {};
|