@enonic/nextjs-adapter 2.0.1 → 2.0.3
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/ComponentRegistry.d.ts +64 -0
- package/ComponentRegistry.js +121 -0
- package/UrlProcessor.d.ts +25 -0
- package/UrlProcessor.js +127 -0
- package/baseMappings.d.ts +1 -0
- package/{src/baseMappings.ts → baseMappings.js} +3 -13
- package/guillotine/fetchContent.d.ts +49 -0
- package/{src/guillotine/fetchContent.ts → guillotine/fetchContent.js} +244 -493
- package/guillotine/getMetaData.d.ts +94 -0
- package/guillotine/getMetaData.js +128 -0
- package/{src/index.ts → index.d.ts} +1 -1
- package/index.js +5 -0
- package/package.json +2 -2
- package/query/Shortcut.d.ts +1 -0
- package/server/ClearableFileCache.d.ts +31 -0
- package/server/ClearableFileCache.js +472 -0
- package/server/cache.d.ts +2 -0
- package/server/cache.js +129 -0
- package/server/index.d.ts +5 -0
- package/server/index.js +132 -0
- package/utils.d.ts +114 -0
- package/{src/utils.ts → utils.js} +81 -154
- package/views/BaseComponent.d.ts +25 -0
- package/{src/views/BaseComponent.tsx → views/BaseComponent.jsx} +39 -73
- package/views/BaseContent.d.ts +4 -0
- package/{src/views/BaseContent.tsx → views/BaseContent.jsx} +17 -24
- package/views/BaseLayout.d.ts +17 -0
- package/views/BaseLayout.jsx +24 -0
- package/views/BaseMacro.d.ts +16 -0
- package/{src/views/BaseMacro.tsx → views/BaseMacro.jsx} +29 -45
- package/views/BasePage.d.ts +19 -0
- package/views/BasePage.jsx +34 -0
- package/views/BasePart.d.ts +19 -0
- package/views/BasePart.jsx +24 -0
- package/views/Empty.d.ts +3 -0
- package/{src/views/Empty.tsx → views/Empty.jsx} +0 -2
- package/views/Fragment.d.ts +10 -0
- package/views/Fragment.jsx +30 -0
- package/views/MainView.d.ts +4 -0
- package/views/MainView.jsx +14 -0
- package/views/Region.d.ts +20 -0
- package/views/Region.jsx +52 -0
- package/views/RichTextView.d.ts +16 -0
- package/{src/views/RichTextView.tsx → views/RichTextView.jsx} +10 -31
- package/views/SingleComponent.d.ts +5 -0
- package/{src/views/SingleComponent.tsx → views/SingleComponent.jsx} +2 -8
- package/views/StaticContent.d.ts +15 -0
- package/views/StaticContent.jsx +36 -0
- package/views/Text.d.ts +9 -0
- package/views/Text.jsx +4 -0
- package/views/macros/DefaultMacro.d.ts +4 -0
- package/views/macros/DefaultMacro.jsx +3 -0
- package/views/macros/DisableMacro.d.ts +3 -0
- package/views/macros/DisableMacro.jsx +2 -0
- package/.eslint-childrc.js +0 -17
- package/.eslintrc.js +0 -15
- package/.github/dependabot.yml +0 -20
- package/.github/pull-merger.yml +0 -1
- package/.github/workflows/release.yml +0 -39
- package/LICENSE +0 -201
- package/RELEASE.md +0 -75
- package/server/.eslintrc.js +0 -11
- package/server/ClearableFileCache.ts +0 -410
- package/server/cache.ts +0 -50
- package/server/index.ts +0 -74
- package/server/tsconfig.json +0 -23
- package/src/.eslintrc.js +0 -11
- package/src/ComponentRegistry.ts +0 -189
- package/src/UrlProcessor.ts +0 -115
- package/src/guillotine/getMetaData.ts +0 -246
- package/src/tsconfig.json +0 -20
- package/src/views/BaseLayout.tsx +0 -45
- package/src/views/BasePage.tsx +0 -56
- package/src/views/BasePart.tsx +0 -50
- package/src/views/Fragment.tsx +0 -45
- package/src/views/MainView.tsx +0 -21
- package/src/views/Region.tsx +0 -89
- package/src/views/StaticContent.tsx +0 -40
- package/src/views/Text.tsx +0 -15
- package/src/views/macros/DefaultMacro.tsx +0 -9
- package/src/views/macros/DisableMacro.tsx +0 -8
- /package/{src/query/Shortcut.ts → query/Shortcut.js} +0 -0
package/server/index.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
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 __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.withEnonicCache = exports.PURGE_CACHE_URL = void 0;
|
|
43
|
+
var http_1 = require("http");
|
|
44
|
+
var cache_1 = require("./cache");
|
|
45
|
+
var path_1 = __importDefault(require("path"));
|
|
46
|
+
var hostname = process.env.HOST || '127.0.0.1';
|
|
47
|
+
var dev = process.env.NODE_ENV !== 'production';
|
|
48
|
+
var port = process.env.PORT && parseInt(process.env.PORT, 10) || (dev ? 3000 : 4242);
|
|
49
|
+
exports.PURGE_CACHE_URL = '/_/enonic/cache/purge';
|
|
50
|
+
var withEnonicCache = function (config) {
|
|
51
|
+
var exp = config.experimental || {};
|
|
52
|
+
exp.isrMemoryCacheSize = exp.isrMemoryCacheSize || 1000000;
|
|
53
|
+
exp.incrementalCacheHandlerPath = path_1.default.resolve(__dirname, 'ClearableFileCache.js');
|
|
54
|
+
config.experimental = exp;
|
|
55
|
+
return config;
|
|
56
|
+
};
|
|
57
|
+
exports.withEnonicCache = withEnonicCache;
|
|
58
|
+
// Not typed to prevent collision of local types with end-user ones
|
|
59
|
+
exports.default = (function (next) { return __awaiter(void 0, void 0, void 0, function () {
|
|
60
|
+
var nextServer, nextHandler;
|
|
61
|
+
return __generator(this, function (_a) {
|
|
62
|
+
switch (_a.label) {
|
|
63
|
+
case 0:
|
|
64
|
+
nextServer = next({ dev: dev, hostname: hostname, port: port });
|
|
65
|
+
nextHandler = nextServer.getRequestHandler();
|
|
66
|
+
return [4 /*yield*/, nextServer.prepare().then(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
67
|
+
var nextNodeServer, opts, purgeUrl, server;
|
|
68
|
+
var _a;
|
|
69
|
+
return __generator(this, function (_b) {
|
|
70
|
+
switch (_b.label) {
|
|
71
|
+
case 0: return [4 /*yield*/, ((_a = nextServer['getServer']) === null || _a === void 0 ? void 0 : _a.call(nextServer))];
|
|
72
|
+
case 1:
|
|
73
|
+
nextNodeServer = _b.sent();
|
|
74
|
+
if (!nextNodeServer) {
|
|
75
|
+
console.error('Unable to access incremental cache, cache purge endpoint will be disabled');
|
|
76
|
+
return [2 /*return*/, nextServer];
|
|
77
|
+
}
|
|
78
|
+
opts = nextNodeServer['serverOptions'].conf;
|
|
79
|
+
purgeUrl = ((opts === null || opts === void 0 ? void 0 : opts.basePath) || '') + exports.PURGE_CACHE_URL;
|
|
80
|
+
console.info("Staring cache purge endpoint at ".concat(purgeUrl, "..."));
|
|
81
|
+
server = (0, http_1.createServer)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
82
|
+
var parsedUrl, tokenParam, cache, pathParam;
|
|
83
|
+
return __generator(this, function (_a) {
|
|
84
|
+
switch (_a.label) {
|
|
85
|
+
case 0:
|
|
86
|
+
parsedUrl = new URL("http://".concat(hostname, ":").concat(port).concat(req.url || ''));
|
|
87
|
+
if (!(parsedUrl.pathname !== purgeUrl)) return [3 /*break*/, 2];
|
|
88
|
+
return [4 /*yield*/, nextHandler(req, res)];
|
|
89
|
+
case 1:
|
|
90
|
+
_a.sent();
|
|
91
|
+
return [3 /*break*/, 5];
|
|
92
|
+
case 2:
|
|
93
|
+
if (!dev) return [3 /*break*/, 3];
|
|
94
|
+
console.warn('\nCache clearing is not available in DEV mode.\n');
|
|
95
|
+
res.setHeader('Content-Type', 'application/json');
|
|
96
|
+
res.end(JSON.stringify({ purged: false }));
|
|
97
|
+
return [3 /*break*/, 5];
|
|
98
|
+
case 3:
|
|
99
|
+
tokenParam = parsedUrl.searchParams.get('token');
|
|
100
|
+
if (tokenParam !== process.env.ENONIC_API_TOKEN) {
|
|
101
|
+
// XP hijacks 401 to show login page, so send 407 instead
|
|
102
|
+
res.statusCode = 407;
|
|
103
|
+
res.end(JSON.stringify({ message: 'Invalid token' }));
|
|
104
|
+
return [2 /*return*/];
|
|
105
|
+
}
|
|
106
|
+
cache = nextNodeServer.getIncrementalCache({
|
|
107
|
+
requestHeaders: Object.assign({}, req.headers),
|
|
108
|
+
});
|
|
109
|
+
pathParam = parsedUrl.searchParams.get('path') || undefined;
|
|
110
|
+
return [4 /*yield*/, (0, cache_1.purgeCache)(cache, pathParam)];
|
|
111
|
+
case 4:
|
|
112
|
+
_a.sent();
|
|
113
|
+
res.setHeader('Content-Type', 'application/json');
|
|
114
|
+
res.end(JSON.stringify({ purged: true }));
|
|
115
|
+
_a.label = 5;
|
|
116
|
+
case 5: return [2 /*return*/];
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}); }).listen(port, hostname, function () {
|
|
120
|
+
console.info("Server ready on ".concat(hostname, ":").concat(port));
|
|
121
|
+
}).on('error', function (e) {
|
|
122
|
+
console.error('Server error:', e);
|
|
123
|
+
server.close();
|
|
124
|
+
});
|
|
125
|
+
return [2 /*return*/, nextServer];
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}); })];
|
|
129
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}); });
|
package/utils.d.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
/** Import config values from .env, .env.development and .env.production */
|
|
4
|
+
import { GetServerSidePropsContext } from 'next';
|
|
5
|
+
import { ParsedUrlQuery } from 'node:querystring';
|
|
6
|
+
import { IncomingHttpHeaders } from 'http';
|
|
7
|
+
export declare const IS_DEV_MODE: boolean;
|
|
8
|
+
export declare const PURGE_CACHE_URL = "/_/enonic/cache/purge";
|
|
9
|
+
export declare enum ENV_VARS {
|
|
10
|
+
PROJECTS = "ENONIC_PROJECTS",
|
|
11
|
+
API = "ENONIC_API",
|
|
12
|
+
API_TOKEN = "ENONIC_API_TOKEN"
|
|
13
|
+
}
|
|
14
|
+
/** Optional utility value - defining in one place the name of the target app (the app that defines the content types, the app name is therefore part of the content type strings used both in typeselector and in query introspections) */
|
|
15
|
+
export declare const APP_NAME: string;
|
|
16
|
+
/** Optional utility value - derived from APP_NAME, only with underscores instead of dots */
|
|
17
|
+
export declare const APP_NAME_UNDERSCORED: string;
|
|
18
|
+
/** Optional utility value - derived from APP_NAME, only with dashes instead of dots */
|
|
19
|
+
export declare const APP_NAME_DASHED: string;
|
|
20
|
+
export declare const XP_BASE_URL_HEADER = "xpbaseurl";
|
|
21
|
+
export declare const RENDER_MODE_HEADER = "content-studio-mode";
|
|
22
|
+
export declare const PROJECT_ID_HEADER = "content-studio-project";
|
|
23
|
+
export declare const JSESSIONID_HEADER = "jsessionid";
|
|
24
|
+
export declare const PORTAL_COMPONENT_ATTRIBUTE = "data-portal-component-type";
|
|
25
|
+
export declare const PORTAL_REGION_ATTRIBUTE = "data-portal-region";
|
|
26
|
+
export declare const FRAGMENT_CONTENTTYPE_NAME = "portal:fragment";
|
|
27
|
+
export declare const FRAGMENT_DEFAULT_REGION_NAME = "fragment";
|
|
28
|
+
export declare const PAGE_TEMPLATE_CONTENTTYPE_NAME = "portal:page-template";
|
|
29
|
+
export declare const PAGE_TEMPLATE_FOLDER = "portal:template-folder";
|
|
30
|
+
export declare const SITE_CONTENTTYPE_NAME = "portal:site";
|
|
31
|
+
export declare const LOCALE_HEADER = "locale";
|
|
32
|
+
export declare const LOCALES_HEADER = "locales";
|
|
33
|
+
export declare const DEFAULT_LOCALE_HEADER = "default-locale";
|
|
34
|
+
/** excluded contents:
|
|
35
|
+
* - folder
|
|
36
|
+
* - shortcuts (redirect can't be used in prerender)
|
|
37
|
+
* - media
|
|
38
|
+
* - paths with '/_' in them
|
|
39
|
+
*/
|
|
40
|
+
export declare const GET_STATIC_PATHS_QUERY = "query ($pathRegex: String!, $count: Int) {\n guillotine {\n queryDsl(\n first: $count,\n sort: {\n field: \"modifiedTime\",\n direction: DESC \n }\n query: {\n boolean: {\n mustNot: [\n {in: {field: \"type\", stringValues: [\"base:shortcut\", \"portal:fragment\", \"portal:template-folder\", \"portal:page-template\"]}}\n {like: {field: \"type\", value: \"media:*\"}}\n ]\n must: [\n {like: {field:\"_path\", value: $pathRegex}}\n ]\n }\n }\n ) {\n _name\n _path\n site {_name}\n }\n }\n}";
|
|
41
|
+
export declare enum XP_REQUEST_TYPE {
|
|
42
|
+
COMPONENT = "component",
|
|
43
|
+
TYPE = "type",
|
|
44
|
+
PAGE = "page"
|
|
45
|
+
}
|
|
46
|
+
export declare enum RENDER_MODE {
|
|
47
|
+
INLINE = "inline",
|
|
48
|
+
EDIT = "edit",
|
|
49
|
+
PREVIEW = "preview",
|
|
50
|
+
LIVE = "live",
|
|
51
|
+
ADMIN = "admin",
|
|
52
|
+
NEXT = "next"
|
|
53
|
+
}
|
|
54
|
+
export declare enum XP_COMPONENT_TYPE {
|
|
55
|
+
PART = "part",
|
|
56
|
+
LAYOUT = "layout",
|
|
57
|
+
TEXT = "text",
|
|
58
|
+
FRAGMENT = "fragment",
|
|
59
|
+
PAGE = "page"
|
|
60
|
+
}
|
|
61
|
+
export interface ServerSideParams extends ParsedUrlQuery {
|
|
62
|
+
contentPath?: string[];
|
|
63
|
+
mode?: string;
|
|
64
|
+
}
|
|
65
|
+
export interface PreviewParams {
|
|
66
|
+
contentPath: string[];
|
|
67
|
+
headers: Record<string, string>;
|
|
68
|
+
params: Record<string, string>;
|
|
69
|
+
}
|
|
70
|
+
export type Context = GetServerSidePropsContext<ServerSideParams, PreviewParams>;
|
|
71
|
+
export type LocaleProjectConfig = {
|
|
72
|
+
project: string;
|
|
73
|
+
site: string;
|
|
74
|
+
locale: string;
|
|
75
|
+
};
|
|
76
|
+
export type LocaleProjectConfigs = {
|
|
77
|
+
default: LocaleProjectConfig;
|
|
78
|
+
[locale: string]: LocaleProjectConfig;
|
|
79
|
+
};
|
|
80
|
+
export interface MinimalContext {
|
|
81
|
+
req?: {
|
|
82
|
+
headers: IncomingHttpHeaders;
|
|
83
|
+
};
|
|
84
|
+
locale?: string;
|
|
85
|
+
defaultLocale?: string;
|
|
86
|
+
locales?: string[];
|
|
87
|
+
}
|
|
88
|
+
export interface ContentPathItem {
|
|
89
|
+
params: {
|
|
90
|
+
contentPath: string[];
|
|
91
|
+
};
|
|
92
|
+
locale?: string;
|
|
93
|
+
}
|
|
94
|
+
export declare const getRenderMode: (context?: MinimalContext) => RENDER_MODE;
|
|
95
|
+
export declare function normalizeLocale(locale: string): string | undefined;
|
|
96
|
+
export declare function getLocaleProjectConfig(context?: MinimalContext): LocaleProjectConfig;
|
|
97
|
+
export declare function getLocaleProjectConfigById(projectId?: string, useDefault?: boolean): LocaleProjectConfig;
|
|
98
|
+
export declare function getLocaleProjectConfigs(): LocaleProjectConfigs;
|
|
99
|
+
export declare function fixDoubleSlashes(str: string): string;
|
|
100
|
+
export declare function getContentApiUrl(context?: MinimalContext): string;
|
|
101
|
+
export declare function addJsessionHeaders(headers?: Object, context?: MinimalContext): void;
|
|
102
|
+
export declare function addLocaleHeaders(headers?: Object, context?: Context): void;
|
|
103
|
+
export declare const getXpBaseUrl: (context?: MinimalContext) => string;
|
|
104
|
+
export declare const commonChars: (s1?: string, s2?: string, wordSeparators?: string) => string;
|
|
105
|
+
export declare const sanitizeGraphqlName: (text: string) => string;
|
|
106
|
+
declare const adapterConstants: {
|
|
107
|
+
IS_DEV_MODE: boolean;
|
|
108
|
+
APP_NAME: string;
|
|
109
|
+
APP_NAME_UNDERSCORED: string;
|
|
110
|
+
APP_NAME_DASHED: string;
|
|
111
|
+
PORTAL_COMPONENT_ATTRIBUTE: string;
|
|
112
|
+
PORTAL_REGION_ATTRIBUTE: string;
|
|
113
|
+
};
|
|
114
|
+
export default adapterConstants;
|
|
@@ -1,67 +1,46 @@
|
|
|
1
|
-
/** Import config values from .env, .env.development and .env.production */
|
|
2
|
-
import {GetServerSidePropsContext} from 'next';
|
|
3
|
-
import {ParsedUrlQuery} from 'node:querystring';
|
|
4
|
-
import {IncomingHttpHeaders} from 'http';
|
|
5
|
-
|
|
6
1
|
const mode = process.env.MODE || process.env.NEXT_PUBLIC_MODE;
|
|
7
2
|
export const IS_DEV_MODE = (mode === 'development');
|
|
8
|
-
|
|
9
3
|
export const PURGE_CACHE_URL = '/_/enonic/cache/purge';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
PROJECTS =
|
|
13
|
-
API =
|
|
14
|
-
API_TOKEN =
|
|
15
|
-
}
|
|
16
|
-
|
|
4
|
+
export var ENV_VARS;
|
|
5
|
+
(function (ENV_VARS) {
|
|
6
|
+
ENV_VARS["PROJECTS"] = "ENONIC_PROJECTS";
|
|
7
|
+
ENV_VARS["API"] = "ENONIC_API";
|
|
8
|
+
ENV_VARS["API_TOKEN"] = "ENONIC_API_TOKEN";
|
|
9
|
+
})(ENV_VARS || (ENV_VARS = {}));
|
|
17
10
|
/** URL to the guillotine API */
|
|
18
11
|
const API_URL = (process.env.ENONIC_API || process.env.NEXT_PUBLIC_ENONIC_API);
|
|
19
|
-
|
|
20
12
|
/** Locales and Enonic XP projects correspondence list */
|
|
21
13
|
const PROJECTS = (process.env.ENONIC_PROJECTS || process.env.NEXT_PUBLIC_ENONIC_PROJECTS);
|
|
22
|
-
|
|
23
14
|
/** Optional utility value - defining in one place the name of the target app (the app that defines the content types, the app name is therefore part of the content type strings used both in typeselector and in query introspections) */
|
|
24
15
|
export const APP_NAME = (process.env.ENONIC_APP_NAME || process.env.NEXT_PUBLIC_ENONIC_APP_NAME);
|
|
25
|
-
|
|
26
16
|
/** Optional utility value - derived from APP_NAME, only with underscores instead of dots */
|
|
27
17
|
export const APP_NAME_UNDERSCORED = (APP_NAME || '').replace(/\./g, '_');
|
|
28
|
-
|
|
29
18
|
/** Optional utility value - derived from APP_NAME, only with dashes instead of dots */
|
|
30
19
|
export const APP_NAME_DASHED = (APP_NAME || '').replace(/\./g, '-');
|
|
31
|
-
|
|
32
20
|
const PROJECT_CONFIG_REGEXP = new RegExp('^(?:([\\w-]+):)?([^\\/\\s]+)(\\/[\\w-.]+)?$', 'i');
|
|
33
|
-
|
|
34
|
-
|
|
35
21
|
// //////////////////////////////////////////////////////////////////////// Hardcode-able constants
|
|
36
|
-
|
|
37
22
|
// URI parameter marking that a request is for a preview for CS. MUST MATCH THE VALUE OF 'FROM_XP_PARAM' on XP side.
|
|
38
23
|
export const XP_BASE_URL_HEADER = 'xpbaseurl';
|
|
39
24
|
export const RENDER_MODE_HEADER = 'content-studio-mode';
|
|
40
25
|
export const PROJECT_ID_HEADER = 'content-studio-project';
|
|
41
26
|
export const JSESSIONID_HEADER = 'jsessionid';
|
|
42
|
-
|
|
43
27
|
export const PORTAL_COMPONENT_ATTRIBUTE = 'data-portal-component-type';
|
|
44
28
|
export const PORTAL_REGION_ATTRIBUTE = 'data-portal-region';
|
|
45
|
-
|
|
46
29
|
export const FRAGMENT_CONTENTTYPE_NAME = 'portal:fragment';
|
|
47
30
|
export const FRAGMENT_DEFAULT_REGION_NAME = 'fragment';
|
|
48
|
-
|
|
49
31
|
export const PAGE_TEMPLATE_CONTENTTYPE_NAME = 'portal:page-template';
|
|
50
32
|
export const PAGE_TEMPLATE_FOLDER = 'portal:template-folder';
|
|
51
|
-
|
|
52
33
|
export const SITE_CONTENTTYPE_NAME = 'portal:site';
|
|
53
|
-
|
|
54
34
|
export const LOCALE_HEADER = 'locale';
|
|
55
35
|
export const LOCALES_HEADER = 'locales';
|
|
56
36
|
export const DEFAULT_LOCALE_HEADER = 'default-locale';
|
|
57
|
-
|
|
58
37
|
/** excluded contents:
|
|
59
38
|
* - folder
|
|
60
39
|
* - shortcuts (redirect can't be used in prerender)
|
|
61
40
|
* - media
|
|
62
41
|
* - paths with '/_' in them
|
|
63
42
|
*/
|
|
64
|
-
export const GET_STATIC_PATHS_QUERY = `query ($count: Int) {
|
|
43
|
+
export const GET_STATIC_PATHS_QUERY = `query ($pathRegex: String!, $count: Int) {
|
|
65
44
|
guillotine {
|
|
66
45
|
queryDsl(
|
|
67
46
|
first: $count,
|
|
@@ -69,11 +48,17 @@ export const GET_STATIC_PATHS_QUERY = `query ($count: Int) {
|
|
|
69
48
|
field: "modifiedTime",
|
|
70
49
|
direction: DESC
|
|
71
50
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
51
|
+
query: {
|
|
52
|
+
boolean: {
|
|
53
|
+
mustNot: [
|
|
54
|
+
{in: {field: "type", stringValues: ["base:shortcut", "portal:fragment", "portal:template-folder", "portal:page-template"]}}
|
|
55
|
+
{like: {field: "type", value: "media:*"}}
|
|
56
|
+
]
|
|
57
|
+
must: [
|
|
58
|
+
{like: {field:"_path", value: $pathRegex}}
|
|
59
|
+
]
|
|
60
|
+
}
|
|
61
|
+
}
|
|
77
62
|
) {
|
|
78
63
|
_name
|
|
79
64
|
_path
|
|
@@ -81,93 +66,49 @@ export const GET_STATIC_PATHS_QUERY = `query ($count: Int) {
|
|
|
81
66
|
}
|
|
82
67
|
}
|
|
83
68
|
}`;
|
|
84
|
-
|
|
85
|
-
|
|
86
69
|
// ------------------------------- Exports and auxillary functions derived from values above ------------------------------------
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
COMPONENT =
|
|
90
|
-
TYPE =
|
|
91
|
-
PAGE =
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
INLINE =
|
|
96
|
-
EDIT =
|
|
97
|
-
PREVIEW =
|
|
98
|
-
LIVE =
|
|
99
|
-
ADMIN =
|
|
100
|
-
NEXT =
|
|
101
|
-
}
|
|
102
|
-
|
|
70
|
+
export var XP_REQUEST_TYPE;
|
|
71
|
+
(function (XP_REQUEST_TYPE) {
|
|
72
|
+
XP_REQUEST_TYPE["COMPONENT"] = "component";
|
|
73
|
+
XP_REQUEST_TYPE["TYPE"] = "type";
|
|
74
|
+
XP_REQUEST_TYPE["PAGE"] = "page";
|
|
75
|
+
})(XP_REQUEST_TYPE || (XP_REQUEST_TYPE = {}));
|
|
76
|
+
export var RENDER_MODE;
|
|
77
|
+
(function (RENDER_MODE) {
|
|
78
|
+
RENDER_MODE["INLINE"] = "inline";
|
|
79
|
+
RENDER_MODE["EDIT"] = "edit";
|
|
80
|
+
RENDER_MODE["PREVIEW"] = "preview";
|
|
81
|
+
RENDER_MODE["LIVE"] = "live";
|
|
82
|
+
RENDER_MODE["ADMIN"] = "admin";
|
|
83
|
+
RENDER_MODE["NEXT"] = "next"; // Fallback: not using XP proxy but rendering directly with next.js
|
|
84
|
+
})(RENDER_MODE || (RENDER_MODE = {}));
|
|
103
85
|
// TODO: Use these instead of hardcoded strings everywhere
|
|
104
|
-
export
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
export
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
mode?: string;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export interface PreviewParams {
|
|
120
|
-
contentPath: string[];
|
|
121
|
-
headers: Record<string, string>;
|
|
122
|
-
params: Record<string, string>;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export type Context = GetServerSidePropsContext<ServerSideParams, PreviewParams>;
|
|
126
|
-
|
|
127
|
-
export type LocaleProjectConfig = {
|
|
128
|
-
project: string;
|
|
129
|
-
site: string;
|
|
130
|
-
locale: string;
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
export type LocaleProjectConfigs = {
|
|
134
|
-
default: LocaleProjectConfig;
|
|
135
|
-
[locale: string]: LocaleProjectConfig;
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
export interface MinimalContext {
|
|
139
|
-
req?: {
|
|
140
|
-
headers: IncomingHttpHeaders;
|
|
141
|
-
}
|
|
142
|
-
locale?: string;
|
|
143
|
-
defaultLocale?: string;
|
|
144
|
-
locales?: string[];
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export interface ContentPathItem {
|
|
148
|
-
params: { contentPath: string[] },
|
|
149
|
-
locale?: string,
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export const getRenderMode = (context?: MinimalContext): RENDER_MODE => {
|
|
153
|
-
const value = (context?.req?.headers || {})[RENDER_MODE_HEADER] as string | undefined;
|
|
154
|
-
const enumValue = RENDER_MODE[<keyof typeof RENDER_MODE>value?.toUpperCase()];
|
|
86
|
+
export var XP_COMPONENT_TYPE;
|
|
87
|
+
(function (XP_COMPONENT_TYPE) {
|
|
88
|
+
XP_COMPONENT_TYPE["PART"] = "part";
|
|
89
|
+
XP_COMPONENT_TYPE["LAYOUT"] = "layout";
|
|
90
|
+
XP_COMPONENT_TYPE["TEXT"] = "text";
|
|
91
|
+
XP_COMPONENT_TYPE["FRAGMENT"] = "fragment";
|
|
92
|
+
XP_COMPONENT_TYPE["PAGE"] = "page";
|
|
93
|
+
})(XP_COMPONENT_TYPE || (XP_COMPONENT_TYPE = {}));
|
|
94
|
+
export const getRenderMode = (context) => {
|
|
95
|
+
var _a;
|
|
96
|
+
const value = (((_a = context === null || context === void 0 ? void 0 : context.req) === null || _a === void 0 ? void 0 : _a.headers) || {})[RENDER_MODE_HEADER];
|
|
97
|
+
const enumValue = RENDER_MODE[value === null || value === void 0 ? void 0 : value.toUpperCase()];
|
|
155
98
|
return enumValue || RENDER_MODE[process.env.RENDER_MODE] || RENDER_MODE.NEXT;
|
|
156
99
|
};
|
|
157
|
-
|
|
158
|
-
export function normalizeLocale(locale: string): string | undefined {
|
|
100
|
+
export function normalizeLocale(locale) {
|
|
159
101
|
return locale !== 'default' ? locale : undefined;
|
|
160
102
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const projectId = (context
|
|
103
|
+
export function getLocaleProjectConfig(context) {
|
|
104
|
+
var _a;
|
|
105
|
+
const projectId = (((_a = context === null || context === void 0 ? void 0 : context.req) === null || _a === void 0 ? void 0 : _a.headers) || {})[PROJECT_ID_HEADER];
|
|
164
106
|
if (projectId) {
|
|
165
107
|
return getLocaleProjectConfigById(projectId);
|
|
166
108
|
}
|
|
167
|
-
|
|
168
109
|
const projectsConfig = getLocaleProjectConfigs();
|
|
169
|
-
const locale = context
|
|
170
|
-
let config
|
|
110
|
+
const locale = (context === null || context === void 0 ? void 0 : context.locale) || (context === null || context === void 0 ? void 0 : context.defaultLocale);
|
|
111
|
+
let config = projectsConfig[locale];
|
|
171
112
|
if (!config) {
|
|
172
113
|
config = projectsConfig.default;
|
|
173
114
|
}
|
|
@@ -175,32 +116,28 @@ export function getLocaleProjectConfig(context?: MinimalContext): LocaleProjectC
|
|
|
175
116
|
throw new Error(`No config for locale "${locale}" found in "${ENV_VARS.PROJECTS}" environmental variable.
|
|
176
117
|
Format: <default-repository-name>/<site-path>,<language>:<repository-name>/<site-path>,...`);
|
|
177
118
|
}
|
|
178
|
-
|
|
179
119
|
return config;
|
|
180
120
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const projects: LocaleProjectConfigs = getLocaleProjectConfigs();
|
|
121
|
+
export function getLocaleProjectConfigById(projectId, useDefault = true) {
|
|
122
|
+
const projects = getLocaleProjectConfigs();
|
|
184
123
|
if (!projectId) {
|
|
185
124
|
return useDefault ? projects.default : undefined;
|
|
186
125
|
}
|
|
187
|
-
|
|
188
126
|
const match = Object.values(projects).find(p => {
|
|
189
|
-
|
|
127
|
+
var _a;
|
|
128
|
+
return ((_a = p === null || p === void 0 ? void 0 : p.project) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === projectId.toLowerCase();
|
|
190
129
|
});
|
|
191
|
-
|
|
192
130
|
return match || (useDefault ? projects.default : undefined);
|
|
193
131
|
}
|
|
194
|
-
|
|
195
|
-
export function getLocaleProjectConfigs(): LocaleProjectConfigs {
|
|
132
|
+
export function getLocaleProjectConfigs() {
|
|
196
133
|
const str = PROJECTS;
|
|
197
134
|
const envVarName = ENV_VARS.PROJECTS;
|
|
198
|
-
if (!str
|
|
135
|
+
if (!(str === null || str === void 0 ? void 0 : str.length)) {
|
|
199
136
|
throw Error(`"${envVarName}" environmental variable is required.`);
|
|
200
137
|
}
|
|
201
|
-
const result
|
|
202
|
-
const matches
|
|
203
|
-
if (!matches
|
|
138
|
+
const result = str.split(',').reduce((config, prjStr) => {
|
|
139
|
+
const matches = PROJECT_CONFIG_REGEXP.exec(prjStr === null || prjStr === void 0 ? void 0 : prjStr.trim());
|
|
140
|
+
if (!(matches === null || matches === void 0 ? void 0 : matches.length)) {
|
|
204
141
|
return config;
|
|
205
142
|
}
|
|
206
143
|
const [full, lang = 'default', project, site] = matches;
|
|
@@ -210,7 +147,8 @@ export function getLocaleProjectConfigs(): LocaleProjectConfigs {
|
|
|
210
147
|
site,
|
|
211
148
|
locale: lang,
|
|
212
149
|
};
|
|
213
|
-
}
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
214
152
|
throw Error(`Wrong configuration in "${envVarName}": ${prjStr}.
|
|
215
153
|
Format: <default-repository-name>/<site-path>,<language>:<repository-name>/<site-path>,...`);
|
|
216
154
|
}
|
|
@@ -228,49 +166,42 @@ export function getLocaleProjectConfigs(): LocaleProjectConfigs {
|
|
|
228
166
|
}
|
|
229
167
|
return result;
|
|
230
168
|
}
|
|
231
|
-
|
|
232
|
-
export function fixDoubleSlashes(str: string) {
|
|
169
|
+
export function fixDoubleSlashes(str) {
|
|
233
170
|
return str.replace(/(^|[^:/])\/{2,}/g, '$1/');
|
|
234
171
|
}
|
|
235
|
-
|
|
236
|
-
export function getContentApiUrl(context?: MinimalContext): string {
|
|
172
|
+
export function getContentApiUrl(context) {
|
|
237
173
|
const project = getLocaleProjectConfig(context).project;
|
|
238
174
|
const branch = getRenderMode(context) === RENDER_MODE.NEXT ? 'master' : 'draft';
|
|
239
|
-
|
|
240
175
|
return fixDoubleSlashes(`${API_URL}/${project}/${branch}`);
|
|
241
176
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const jsessionid = context
|
|
177
|
+
export function addJsessionHeaders(headers = {}, context) {
|
|
178
|
+
var _a;
|
|
179
|
+
const jsessionid = (_a = context === null || context === void 0 ? void 0 : context.req) === null || _a === void 0 ? void 0 : _a.headers[JSESSIONID_HEADER];
|
|
245
180
|
if (jsessionid) {
|
|
246
181
|
headers['Cookie'] = `${JSESSIONID_HEADER}=${jsessionid}`;
|
|
247
182
|
}
|
|
248
183
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
const locale = context?.locale;
|
|
184
|
+
export function addLocaleHeaders(headers = {}, context) {
|
|
185
|
+
const locale = context === null || context === void 0 ? void 0 : context.locale;
|
|
252
186
|
if (locale) {
|
|
253
187
|
headers[LOCALE_HEADER] = locale;
|
|
254
188
|
}
|
|
255
|
-
const locales = context
|
|
189
|
+
const locales = context === null || context === void 0 ? void 0 : context.locales;
|
|
256
190
|
if (locales) {
|
|
257
191
|
headers[LOCALES_HEADER] = JSON.stringify(locales);
|
|
258
192
|
}
|
|
259
|
-
const defaultLocale = context
|
|
193
|
+
const defaultLocale = context === null || context === void 0 ? void 0 : context.defaultLocale;
|
|
260
194
|
if (defaultLocale) {
|
|
261
195
|
headers[DEFAULT_LOCALE_HEADER] = defaultLocale;
|
|
262
196
|
}
|
|
263
197
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
const header = ((context
|
|
267
|
-
|
|
198
|
+
export const getXpBaseUrl = (context) => {
|
|
199
|
+
var _a;
|
|
200
|
+
const header = ((((_a = context === null || context === void 0 ? void 0 : context.req) === null || _a === void 0 ? void 0 : _a.headers) || {})[XP_BASE_URL_HEADER] || '');
|
|
268
201
|
// TODO: workaround for XP pattern controller mapping not picked up in edit mode
|
|
269
202
|
return (header || '/').replace(/\/edit\//, '/inline/');
|
|
270
203
|
};
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
export const commonChars = (s1?: string, s2?: string, wordSeparators?: string) => {
|
|
204
|
+
export const commonChars = (s1, s2, wordSeparators) => {
|
|
274
205
|
let result = '';
|
|
275
206
|
if (!s1 || s1.length === 0 || !s2 || s2.length === 0) {
|
|
276
207
|
return result;
|
|
@@ -279,26 +210,26 @@ export const commonChars = (s1?: string, s2?: string, wordSeparators?: string) =
|
|
|
279
210
|
let lastCommonIndex = -1;
|
|
280
211
|
for (let i = 0; i < s1.length; i++) {
|
|
281
212
|
const s1El = s1[i];
|
|
282
|
-
if (wordSeparators
|
|
213
|
+
if ((wordSeparators === null || wordSeparators === void 0 ? void 0 : wordSeparators.indexOf(s1El)) >= 0) {
|
|
283
214
|
lastSeparatorIndex = i;
|
|
284
215
|
}
|
|
285
216
|
if (s2[i] === s1El) {
|
|
286
217
|
lastCommonIndex = i;
|
|
287
|
-
}
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
288
220
|
break;
|
|
289
221
|
}
|
|
290
222
|
}
|
|
291
|
-
|
|
292
223
|
if (lastSeparatorIndex > 0 || lastCommonIndex === lastSeparatorIndex) {
|
|
293
224
|
result = s1.substring(0, lastSeparatorIndex);
|
|
294
|
-
}
|
|
225
|
+
}
|
|
226
|
+
else if (lastCommonIndex > -1) {
|
|
295
227
|
result = s1.substring(0, lastCommonIndex + 1);
|
|
296
228
|
}
|
|
297
|
-
|
|
298
229
|
return result;
|
|
299
230
|
};
|
|
300
231
|
// sanitizes text according to graphql naming spec http://spec.graphql.org/October2021/#sec-Names
|
|
301
|
-
export const sanitizeGraphqlName = (text
|
|
232
|
+
export const sanitizeGraphqlName = (text) => {
|
|
302
233
|
if (!text || text.trim().length === 0) {
|
|
303
234
|
return '';
|
|
304
235
|
}
|
|
@@ -308,9 +239,7 @@ export const sanitizeGraphqlName = (text: string) => {
|
|
|
308
239
|
}
|
|
309
240
|
return result;
|
|
310
241
|
};
|
|
311
|
-
|
|
312
242
|
// ---------------------------------------------------------------------------------------------------------------- Export
|
|
313
|
-
|
|
314
243
|
const adapterConstants = {
|
|
315
244
|
IS_DEV_MODE,
|
|
316
245
|
APP_NAME,
|
|
@@ -319,7 +248,6 @@ const adapterConstants = {
|
|
|
319
248
|
PORTAL_COMPONENT_ATTRIBUTE,
|
|
320
249
|
PORTAL_REGION_ATTRIBUTE,
|
|
321
250
|
};
|
|
322
|
-
|
|
323
251
|
// Verify required values
|
|
324
252
|
const NOT_REQUIRED = ['IS_DEV_MODE'];
|
|
325
253
|
Object.keys(adapterConstants).forEach(key => {
|
|
@@ -327,5 +255,4 @@ Object.keys(adapterConstants).forEach(key => {
|
|
|
327
255
|
throw Error(`constants.ts: Config value '${key}' is missing (from .env?)`);
|
|
328
256
|
}
|
|
329
257
|
});
|
|
330
|
-
|
|
331
258
|
export default adapterConstants;
|