@enonic/nextjs-adapter 3.0.0-B4 → 3.0.0-RC1
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/common/constants.d.ts +1 -1
- package/common/constants.js +1 -1
- package/common/env.d.ts +1 -1
- package/common/env.js +2 -2
- package/guillotine/fetchContentData.js +4 -1
- package/guillotine/fetchContentData.js.map +1 -1
- package/guillotine/fetchContentPathsForLocale.js +1 -1
- package/guillotine/fetchContentPathsForLocale.js.map +1 -1
- package/guillotine/fetchFromApi.d.ts +2 -2
- package/guillotine/fetchFromApi.js +22 -13
- package/guillotine/fetchFromApi.js.map +1 -1
- package/guillotine/fetchGuillotine.d.ts +2 -2
- package/guillotine/fetchGuillotine.js +4 -4
- package/guillotine/fetchGuillotine.js.map +1 -1
- package/guillotine/fetchMetaData.js +4 -1
- package/guillotine/fetchMetaData.js.map +1 -1
- package/package.json +7 -7
- package/src/common/constants.ts +1 -1
- package/src/common/env.ts +2 -2
- package/src/guillotine/fetchContentData.ts +6 -7
- package/src/guillotine/fetchContentPathsForLocale.ts +1 -1
- package/src/guillotine/fetchFromApi.ts +26 -19
- package/src/guillotine/fetchGuillotine.ts +6 -7
- package/src/guillotine/fetchMetaData.ts +6 -7
- package/src/types/index.ts +8 -6
- package/src/utils/getProjectLocaleConfigs.ts +4 -4
- package/types/index.d.ts +7 -5
- package/utils/getProjectLocaleConfigs.js +3 -3
package/common/constants.d.ts
CHANGED
package/common/constants.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export const CATCH_ALL = '*';
|
|
2
2
|
export var ENV_VARS;
|
|
3
3
|
(function (ENV_VARS) {
|
|
4
|
-
ENV_VARS["
|
|
4
|
+
ENV_VARS["MAPPINGS"] = "ENONIC_MAPPINGS";
|
|
5
5
|
ENV_VARS["API_URL"] = "ENONIC_API";
|
|
6
6
|
ENV_VARS["APP_NAME"] = "ENONIC_APP_NAME";
|
|
7
7
|
})(ENV_VARS || (ENV_VARS = {}));
|
package/common/env.d.ts
CHANGED
|
@@ -8,4 +8,4 @@ export declare const APP_NAME_UNDERSCORED: string;
|
|
|
8
8
|
export declare const APP_NAME_DASHED: string;
|
|
9
9
|
export declare const IS_DEV_MODE: boolean;
|
|
10
10
|
/** Locales and Enonic XP projects correspondence list */
|
|
11
|
-
export declare const
|
|
11
|
+
export declare const MAPPINGS: string;
|
package/common/env.js
CHANGED
|
@@ -13,11 +13,11 @@ export const APP_NAME_DASHED = (APP_NAME || '').replace(/\./g, '-');
|
|
|
13
13
|
const mode = isServer ? process.env.MODE : process.env.NEXT_PUBLIC_MODE;
|
|
14
14
|
export const IS_DEV_MODE = (mode === 'development');
|
|
15
15
|
/** Locales and Enonic XP projects correspondence list */
|
|
16
|
-
export const
|
|
16
|
+
export const MAPPINGS = isServer ? process.env[ENV_VARS.MAPPINGS] : process.env['NEXT_PUBLIC_ENONIC_MAPPINGS'];
|
|
17
17
|
const requiredConstants = {
|
|
18
18
|
[ENV_VARS.APP_NAME]: APP_NAME,
|
|
19
19
|
[ENV_VARS.API_URL]: API_URL,
|
|
20
|
-
[ENV_VARS.
|
|
20
|
+
[ENV_VARS.MAPPINGS]: MAPPINGS,
|
|
21
21
|
};
|
|
22
22
|
// Verify required values on server-side only
|
|
23
23
|
Object.keys(requiredConstants).forEach((key) => {
|
|
@@ -13,7 +13,10 @@ export const fetchContentData = (contentApiUrl, xpContentPath, projectConfig, qu
|
|
|
13
13
|
if (variables && Object.keys(variables).length > 0) {
|
|
14
14
|
body.variables = variables;
|
|
15
15
|
}
|
|
16
|
-
const contentResults = yield fetchGuillotine(contentApiUrl,
|
|
16
|
+
const contentResults = yield fetchGuillotine(contentApiUrl, projectConfig, {
|
|
17
|
+
body,
|
|
18
|
+
headers,
|
|
19
|
+
});
|
|
17
20
|
if (contentResults.error) {
|
|
18
21
|
return contentResults;
|
|
19
22
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchContentData.js","sourceRoot":"","sources":["../src/guillotine/fetchContentData.ts"],"names":[],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"fetchContentData.js","sourceRoot":"","sources":["../src/guillotine/fetchContentData.ts"],"names":[],"mappings":";;;;;;;;;AAGA,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAGlD,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC5B,aAAqB,EACrB,aAAqB,EACrB,aAAkC,EAClC,KAAa,EACb,SAAc,EACd,OAAY,EACU,EAAE;IACxB,MAAM,IAAI,GAAuB,EAAC,KAAK,EAAC,CAAC;IACzC,IAAI,SAAS,IAAI,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACjD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC/B,CAAC;IACD,MAAM,cAAc,GAAG,MAAM,eAAe,CAAyB,aAAa,EAAE,aAAa,EAAE;QAC/F,IAAI;QACJ,OAAO;KACV,CAAC,CAAC;IAEH,IAAI,cAAc,CAAC,KAAK,EAAE,CAAC;QACvB,OAAO,cAAc,CAAC;IAC1B,CAAC;SAAM,CAAC;QACJ,OAAO;YACH,qCAAqC;YACrC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC;SAC1C,CAAC;IACN,CAAC;AACL,CAAC,CAAA,CAAC"}
|
|
@@ -23,7 +23,7 @@ export function fetchContentPathsForLocale(path, config, query = GET_STATIC_PATH
|
|
|
23
23
|
count,
|
|
24
24
|
},
|
|
25
25
|
};
|
|
26
|
-
return fetchGuillotine(contentApiUrl,
|
|
26
|
+
return fetchGuillotine(contentApiUrl, config, { body }).then((results) => {
|
|
27
27
|
return results.guillotine.queryDsl.reduce((prev, child) => {
|
|
28
28
|
var _a;
|
|
29
29
|
const regexp = new RegExp(`^/${((_a = child.site) === null || _a === void 0 ? void 0 : _a._name) ? `${child.site._name}/?` : ''}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchContentPathsForLocale.js","sourceRoot":"","sources":["../src/guillotine/fetchContentPathsForLocale.ts"],"names":[],"mappings":";;;;;;;;;AAGA,OAAO,EAAC,sBAAsB,EAAC,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAC,gBAAgB,EAAC,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAGlD,MAAM,UAAgB,0BAA0B,CAC5C,IAAY,EACZ,MAA2B,EAC3B,QAAgB,sBAAsB,EACtC,KAAK,GAAG,GAAG;;QAEX,MAAM,aAAa,GAAG,gBAAgB,CAAC;YACnC,WAAW,EAAE,IAAI;YACjB,MAAM,EAAE,MAAM,CAAC,MAAM;SACxB,CAAC,CAAC;QACH,MAAM,IAAI,GAAuB;YAC7B,KAAK;YACL,SAAS,EAAE;gBACP,IAAI;gBACJ,KAAK;aACR;SACJ,CAAC;QACF,OAAO,eAAe,CAAoB,aAAa,EAAE,
|
|
1
|
+
{"version":3,"file":"fetchContentPathsForLocale.js","sourceRoot":"","sources":["../src/guillotine/fetchContentPathsForLocale.ts"],"names":[],"mappings":";;;;;;;;;AAGA,OAAO,EAAC,sBAAsB,EAAC,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAC,gBAAgB,EAAC,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAGlD,MAAM,UAAgB,0BAA0B,CAC5C,IAAY,EACZ,MAA2B,EAC3B,QAAgB,sBAAsB,EACtC,KAAK,GAAG,GAAG;;QAEX,MAAM,aAAa,GAAG,gBAAgB,CAAC;YACnC,WAAW,EAAE,IAAI;YACjB,MAAM,EAAE,MAAM,CAAC,MAAM;SACxB,CAAC,CAAC;QACH,MAAM,IAAI,GAAuB;YAC7B,KAAK;YACL,SAAS,EAAE;gBACP,IAAI;gBACJ,KAAK;aACR;SACJ,CAAC;QACF,OAAO,eAAe,CAAoB,aAAa,EAAE,MAAM,EAAE,EAAC,IAAI,EAAC,CAAC,CAAC,IAAI,CAAC,CAC1E,OAAyB,EAC3B,EAAE;YACA,OAAO,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,CACtC,IAAuB,EACvB,KAAU,EACZ,EAAE;;gBACA,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,KAAK,CAAA,MAAA,KAAK,CAAC,IAAI,0CAAE,KAAK,EAAC,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;gBACnF,MAAM,WAAW,GAAG,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;gBACpD,IAAI,CAAC,IAAI,CAAC;oBACN,WAAW,EAAE,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC;oBACnC,MAAM,EAAE,MAAM,CAAC,MAAM;iBACxB,CAAC,CAAC;gBACH,OAAO,IAAI,CAAC;YAChB,CAAC,EAAE,EAAE,CAAC,CAAC;QACX,CAAC,CAAC,CAAC;IACP,CAAC;CAAA"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FetchOptions, GuillotineResponseJson, ProjectLocaleConfig } from '../types';
|
|
2
2
|
/** Generic fetch */
|
|
3
|
-
export declare function fetchFromApi<Data = Record<string, unknown>>(apiUrl: string,
|
|
3
|
+
export declare function fetchFromApi<Data = Record<string, unknown>>(apiUrl: string, projectConfig: ProjectLocaleConfig, options?: FetchOptions): Promise<GuillotineResponseJson>;
|
|
@@ -8,23 +8,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
/** Generic fetch */
|
|
11
|
-
export function fetchFromApi(apiUrl,
|
|
11
|
+
export function fetchFromApi(apiUrl, projectConfig, options) {
|
|
12
12
|
return __awaiter(this, void 0, void 0, function* () {
|
|
13
|
-
const
|
|
14
|
-
method,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
},
|
|
20
|
-
body: JSON.stringify(body),
|
|
13
|
+
const opts = {
|
|
14
|
+
method: (options === null || options === void 0 ? void 0 : options.method) || 'POST',
|
|
15
|
+
body: (options === null || options === void 0 ? void 0 : options.body) ? JSON.stringify(options === null || options === void 0 ? void 0 : options.body) : null,
|
|
16
|
+
headers: mergeHeaders(projectConfig, options === null || options === void 0 ? void 0 : options.headers),
|
|
17
|
+
next: options === null || options === void 0 ? void 0 : options.next,
|
|
18
|
+
cache: options === null || options === void 0 ? void 0 : options.cache,
|
|
21
19
|
};
|
|
22
|
-
if (headers) {
|
|
23
|
-
Object.assign(options.headers, headers);
|
|
24
|
-
}
|
|
25
20
|
let res;
|
|
26
21
|
try {
|
|
27
|
-
res = yield fetch(apiUrl,
|
|
22
|
+
res = yield fetch(apiUrl, opts);
|
|
28
23
|
}
|
|
29
24
|
catch (e) {
|
|
30
25
|
console.warn(apiUrl, e);
|
|
@@ -64,4 +59,18 @@ export function fetchFromApi(apiUrl, body, projectConfig, headers, method = 'POS
|
|
|
64
59
|
return json;
|
|
65
60
|
});
|
|
66
61
|
}
|
|
62
|
+
function mergeHeaders(projectConfig, headers) {
|
|
63
|
+
const newHeaders = new Headers({
|
|
64
|
+
'Accept': 'application/json',
|
|
65
|
+
'Content-Type': 'application/json',
|
|
66
|
+
});
|
|
67
|
+
if (headers) {
|
|
68
|
+
for (const headersKey in headers) {
|
|
69
|
+
newHeaders.set(headersKey, headers[headersKey]);
|
|
70
|
+
}
|
|
71
|
+
// append last to make sure it is not overwritten
|
|
72
|
+
}
|
|
73
|
+
newHeaders.set('X-Guillotine-SiteKey', projectConfig.site);
|
|
74
|
+
return newHeaders;
|
|
75
|
+
}
|
|
67
76
|
//# sourceMappingURL=fetchFromApi.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchFromApi.js","sourceRoot":"","sources":["../src/guillotine/fetchFromApi.ts"],"names":[],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"fetchFromApi.js","sourceRoot":"","sources":["../src/guillotine/fetchFromApi.ts"],"names":[],"mappings":";;;;;;;;;AAEA,oBAAoB;AACpB,MAAM,UAAgB,YAAY,CAC9B,MAAc,EACd,aAAkC,EAClC,OAAsB;;QAGtB,MAAM,IAAI,GAAG;YACT,MAAM,EAAE,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,KAAI,MAAM;YACjC,IAAI,EAAE,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,EAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI;YAC1D,OAAO,EAAE,YAAY,CAAC,aAAa,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAC;YACtD,IAAI,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI;YACnB,KAAK,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK;SACxB,CAAC;QAEF,IAAI,GAA6B,CAAC;QAClC,IAAI,CAAC;YACD,GAAG,GAAG,MAAM,KAAK,CAAC,MAAM,EAAE,IAAmB,CAAC,CAAC;QACnD,CAAC;QAAC,OAAO,CAAM,EAAE,CAAC;YACd,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC;gBAC3B,IAAI,EAAE,KAAK;gBACX,OAAO,EAAE,CAAC,CAAC,OAAO;aACrB,CAAC,CAAC,CAAC;QACR,CAAC;QAED,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;YACV,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC;g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,EAAE,CAAC;YAC/B,UAAU,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC;QACpD,CAAC;QACD,iDAAiD;IACrD,CAAC;IACD,UAAU,CAAC,GAAG,CAAC,sBAAsB,EAAE,aAAa,CAAC,IAAI,CAAC,CAAC;IAC3D,OAAO,UAAU,CAAC;AACtB,CAAC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FetchOptions, GuillotineResult, ProjectLocaleConfig } from '../types';
|
|
2
2
|
/** Guillotine-specialized fetch, using the generic fetch above */
|
|
3
|
-
export declare function fetchGuillotine<Data = Record<string, unknown>>(contentApiUrl: string,
|
|
3
|
+
export declare function fetchGuillotine<Data = Record<string, unknown>>(contentApiUrl: string, projectConfig: ProjectLocaleConfig, options?: FetchOptions): Promise<GuillotineResult>;
|
|
@@ -9,9 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { fetchFromApi } from './fetchFromApi';
|
|
11
11
|
/** Guillotine-specialized fetch, using the generic fetch above */
|
|
12
|
-
export function fetchGuillotine(contentApiUrl,
|
|
12
|
+
export function fetchGuillotine(contentApiUrl, projectConfig, options) {
|
|
13
13
|
var _a;
|
|
14
14
|
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
const body = options === null || options === void 0 ? void 0 : options.body;
|
|
15
16
|
if (typeof body.query !== 'string' || !body.query.trim()) {
|
|
16
17
|
return {
|
|
17
18
|
error: {
|
|
@@ -20,9 +21,9 @@ export function fetchGuillotine(contentApiUrl, body, projectConfig, headers) {
|
|
|
20
21
|
},
|
|
21
22
|
};
|
|
22
23
|
}
|
|
23
|
-
const path = (_a = body.variables) === null || _a === void 0 ? void 0 : _a.path;
|
|
24
|
+
const path = (_a = body === null || body === void 0 ? void 0 : body.variables) === null || _a === void 0 ? void 0 : _a.path;
|
|
24
25
|
const pathMessage = path ? JSON.stringify(path) : '';
|
|
25
|
-
return yield fetchFromApi(contentApiUrl,
|
|
26
|
+
return yield fetchFromApi(contentApiUrl, projectConfig, options)
|
|
26
27
|
.then(json => {
|
|
27
28
|
let errors = json.errors; // fetchFromApi ensures json is an object
|
|
28
29
|
if (errors) {
|
|
@@ -30,7 +31,6 @@ export function fetchGuillotine(contentApiUrl, body, projectConfig, headers) {
|
|
|
30
31
|
errors = [errors];
|
|
31
32
|
}
|
|
32
33
|
console.error(`${errors.length} error(s) when fetching data from: ${contentApiUrl}`);
|
|
33
|
-
console.error(`headers: ${JSON.stringify(headers, null, 2)} \nvariables: ${JSON.stringify(body.variables, null, 2)}`);
|
|
34
34
|
errors.forEach(error => {
|
|
35
35
|
console.error(error);
|
|
36
36
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchGuillotine.js","sourceRoot":"","sources":["../src/guillotine/fetchGuillotine.ts"],"names":[],"mappings":";;;;;;;;;AAGA,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAG5C,kEAAkE;AAClE,MAAM,UAAgB,eAAe,CACjC,aAAqB,EACrB,
|
|
1
|
+
{"version":3,"file":"fetchGuillotine.js","sourceRoot":"","sources":["../src/guillotine/fetchGuillotine.ts"],"names":[],"mappings":";;;;;;;;;AAGA,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAG5C,kEAAkE;AAClE,MAAM,UAAgB,eAAe,CACjC,aAAqB,EACrB,aAAkC,EAClC,OAAsB;;;QAEtB,MAAM,IAAI,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,CAAC;QAC3B,IAAI,OAAO,IAAI,CAAC,KAAK,KAAK,QAAQ,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;YACvD,OAAO;gBACH,KAAK,EAAE;oBACH,IAAI,EAAE,KAAK;oBACX,OAAO,EAAE,qDAAqD,IAAI,CAAC,SAAS,CA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AAO,EAAE,EAAE,CAAC;gBACV,OAAO,EAAC,KAAK,EAAE,EAAC,IAAI,EAAE,mBAAmB,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAC,EAAC,CAAC;YACtE,CAAC;QACL,CAAC,CAAC,CAAC;;CACV"}
|
|
@@ -17,7 +17,10 @@ export const fetchMetaData = (contentApiUrl, xpContentPath, projectConfig, heade
|
|
|
17
17
|
path: xpContentPath,
|
|
18
18
|
},
|
|
19
19
|
};
|
|
20
|
-
const metaResult = yield fetchGuillotine(contentApiUrl,
|
|
20
|
+
const metaResult = yield fetchGuillotine(contentApiUrl, projectConfig, {
|
|
21
|
+
body,
|
|
22
|
+
headers,
|
|
23
|
+
});
|
|
21
24
|
if (metaResult.error) {
|
|
22
25
|
return metaResult;
|
|
23
26
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchMetaData.js","sourceRoot":"","sources":["../src/guillotine/fetchMetaData.ts"],"names":[],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"fetchMetaData.js","sourceRoot":"","sources":["../src/guillotine/fetchMetaData.ts"],"names":[],"mappings":";;;;;;;;;AAGA,OAAO,EAAC,YAAY,EAAE,iBAAiB,EAAC,MAAM,eAAe,CAAC;AAC9D,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAGlD,MAAM,CAAC,MAAM,aAAa,GAAG,CACzB,aAAqB,EACrB,aAAqB,EACrB,aAAkC,EAClC,OAAY,EACO,EAAE;;IACrB,MAAM,IAAI,GAAuB;QAC7B,KAAK,EAAE,YAAY,CAAC,iBAAiB,EAAE,CAAC;QACxC,SAAS,EAAE;YACP,IAAI,EAAE,aAAa;SACtB;KACJ,CAAC;IACF,MAAM,UAAU,GAAG,MAAM,eAAe,CAAa,aAAa,EAAE,aAAa,EAAE;QAC/E,IAAI;QACJ,OAAO;KACV,CAAC,CAAC;IACH,IAAI,UAAU,CAAC,KAAK,EAAE,CAAC;QACnB,OAAO,UAAU,CAAC;IACtB,CAAC;SAAM,CAAC;QACJ,OAAO;YACH,IAAI,EAAE,MAAA,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,UAAU,0CAAE,GAAG;SACpC,CAAC;IACN,CAAC;AACL,CAAC,CAAA,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enonic/nextjs-adapter",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-RC1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "Pavel Milkevich <pmi@enonic.com>",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@formatjs/intl-localematcher": "^0.5.4",
|
|
51
|
-
"html-react-parser": "^5.1.
|
|
51
|
+
"html-react-parser": "^5.1.7",
|
|
52
52
|
"negotiator": "^0.6.3",
|
|
53
53
|
"next": "^14.1.0",
|
|
54
54
|
"react": "18.2.0",
|
|
@@ -56,21 +56,21 @@
|
|
|
56
56
|
"unescape": "^1.0.1"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@edge-runtime/jest-environment": "^2.3.
|
|
59
|
+
"@edge-runtime/jest-environment": "^2.3.10",
|
|
60
60
|
"@enonic-types/core": "^7.14.0-B2",
|
|
61
61
|
"@enonic/eslint-config": "^1.1.0",
|
|
62
62
|
"@testing-library/jest-dom": "^6.4.2",
|
|
63
63
|
"@testing-library/react": "^14.2.1",
|
|
64
64
|
"@types/negotiator": "^0.6.3",
|
|
65
|
-
"@types/node": "^20.11.
|
|
66
|
-
"@types/react": "^18.2.
|
|
65
|
+
"@types/node": "^20.11.20",
|
|
66
|
+
"@types/react": "^18.2.58",
|
|
67
67
|
"@types/react-dom": "^18.2.19",
|
|
68
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
68
|
+
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
|
69
69
|
"@typescript-eslint/parser": "^6.21.0",
|
|
70
70
|
"concurrently": "^8.2.2",
|
|
71
71
|
"cpy-cli": "^5.0.0",
|
|
72
72
|
"del-cli": "^5.1.0",
|
|
73
|
-
"eslint": "^8.
|
|
73
|
+
"eslint": "^8.57.0",
|
|
74
74
|
"eslint-plugin-react": "^7.33.2",
|
|
75
75
|
"jest": "^29.7.0",
|
|
76
76
|
"jest-environment-jsdom": "^29.7.0",
|
package/src/common/constants.ts
CHANGED
package/src/common/env.ts
CHANGED
|
@@ -21,12 +21,12 @@ const mode = isServer ? process.env.MODE : process.env.NEXT_PUBLIC_MODE;
|
|
|
21
21
|
export const IS_DEV_MODE = (mode === 'development');
|
|
22
22
|
|
|
23
23
|
/** Locales and Enonic XP projects correspondence list */
|
|
24
|
-
export const
|
|
24
|
+
export const MAPPINGS = isServer ? process.env[ENV_VARS.MAPPINGS] : process.env['NEXT_PUBLIC_ENONIC_MAPPINGS'];
|
|
25
25
|
|
|
26
26
|
const requiredConstants = {
|
|
27
27
|
[ENV_VARS.APP_NAME]: APP_NAME,
|
|
28
28
|
[ENV_VARS.API_URL]: API_URL,
|
|
29
|
-
[ENV_VARS.
|
|
29
|
+
[ENV_VARS.MAPPINGS]: MAPPINGS,
|
|
30
30
|
};
|
|
31
31
|
|
|
32
32
|
// Verify required values on server-side only
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ContentApiBaseBody,
|
|
3
|
-
ContentResult,
|
|
4
|
-
ProjectLocaleConfig,
|
|
5
|
-
} from '../types';
|
|
1
|
+
import type {ContentApiBaseBody, ContentResult, ProjectLocaleConfig} from '../types';
|
|
6
2
|
|
|
7
3
|
|
|
8
4
|
import {fetchGuillotine} from './fetchGuillotine';
|
|
@@ -20,7 +16,10 @@ export const fetchContentData = async <Content = Record<string, unknown>>(
|
|
|
20
16
|
if (variables && Object.keys(variables).length > 0) {
|
|
21
17
|
body.variables = variables;
|
|
22
18
|
}
|
|
23
|
-
const contentResults = await fetchGuillotine<ContentResult<Content>>(contentApiUrl,
|
|
19
|
+
const contentResults = await fetchGuillotine<ContentResult<Content>>(contentApiUrl, projectConfig, {
|
|
20
|
+
body,
|
|
21
|
+
headers,
|
|
22
|
+
});
|
|
24
23
|
|
|
25
24
|
if (contentResults.error) {
|
|
26
25
|
return contentResults;
|
|
@@ -30,4 +29,4 @@ export const fetchContentData = async <Content = Record<string, unknown>>(
|
|
|
30
29
|
contents: Object.values(contentResults),
|
|
31
30
|
};
|
|
32
31
|
}
|
|
33
|
-
};
|
|
32
|
+
};
|
|
@@ -23,7 +23,7 @@ export async function fetchContentPathsForLocale(
|
|
|
23
23
|
count,
|
|
24
24
|
},
|
|
25
25
|
};
|
|
26
|
-
return fetchGuillotine<ContentPathItem[]>(contentApiUrl,
|
|
26
|
+
return fetchGuillotine<ContentPathItem[]>(contentApiUrl, config, {body}).then((
|
|
27
27
|
results: GuillotineResult,
|
|
28
28
|
) => {
|
|
29
29
|
return results.guillotine.queryDsl.reduce((
|
|
@@ -1,31 +1,23 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type {FetchOptions, GuillotineResponse, GuillotineResponseJson, ProjectLocaleConfig} from '../types';
|
|
3
2
|
|
|
4
3
|
/** Generic fetch */
|
|
5
4
|
export async function fetchFromApi<Data = Record<string, unknown>>(
|
|
6
5
|
apiUrl: string,
|
|
7
|
-
body: ContentApiBaseBody,
|
|
8
6
|
projectConfig: ProjectLocaleConfig,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
) {
|
|
12
|
-
const options = {
|
|
13
|
-
method,
|
|
14
|
-
headers: {
|
|
15
|
-
'Accept': 'application/json',
|
|
16
|
-
'Content-Type': 'application/json',
|
|
17
|
-
'X-Guillotine-SiteKey': projectConfig.site,
|
|
18
|
-
},
|
|
19
|
-
body: JSON.stringify(body),
|
|
20
|
-
};
|
|
7
|
+
options?: FetchOptions,
|
|
8
|
+
): Promise<GuillotineResponseJson> {
|
|
21
9
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
10
|
+
const opts = {
|
|
11
|
+
method: options?.method || 'POST',
|
|
12
|
+
body: options?.body ? JSON.stringify(options?.body) : null,
|
|
13
|
+
headers: mergeHeaders(projectConfig, options?.headers),
|
|
14
|
+
next: options?.next,
|
|
15
|
+
cache: options?.cache,
|
|
16
|
+
};
|
|
25
17
|
|
|
26
18
|
let res: GuillotineResponse<Data>;
|
|
27
19
|
try {
|
|
28
|
-
res = await fetch(apiUrl,
|
|
20
|
+
res = await fetch(apiUrl, opts as RequestInit);
|
|
29
21
|
} catch (e: any) {
|
|
30
22
|
console.warn(apiUrl, e);
|
|
31
23
|
throw new Error(JSON.stringify({
|
|
@@ -67,3 +59,18 @@ export async function fetchFromApi<Data = Record<string, unknown>>(
|
|
|
67
59
|
|
|
68
60
|
return json;
|
|
69
61
|
}
|
|
62
|
+
|
|
63
|
+
function mergeHeaders(projectConfig: ProjectLocaleConfig, headers?: HeadersInit): Headers {
|
|
64
|
+
const newHeaders = new Headers({
|
|
65
|
+
'Accept': 'application/json',
|
|
66
|
+
'Content-Type': 'application/json',
|
|
67
|
+
});
|
|
68
|
+
if (headers) {
|
|
69
|
+
for (const headersKey in headers) {
|
|
70
|
+
newHeaders.set(headersKey, headers[headersKey]);
|
|
71
|
+
}
|
|
72
|
+
// append last to make sure it is not overwritten
|
|
73
|
+
}
|
|
74
|
+
newHeaders.set('X-Guillotine-SiteKey', projectConfig.site);
|
|
75
|
+
return newHeaders;
|
|
76
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {FetchOptions, GuillotineResult, ProjectLocaleConfig} from '../types';
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
import {fetchFromApi} from './fetchFromApi';
|
|
@@ -7,10 +7,10 @@ import {fetchFromApi} from './fetchFromApi';
|
|
|
7
7
|
/** Guillotine-specialized fetch, using the generic fetch above */
|
|
8
8
|
export async function fetchGuillotine<Data = Record<string, unknown>>(
|
|
9
9
|
contentApiUrl: string,
|
|
10
|
-
body: ContentApiBaseBody,
|
|
11
10
|
projectConfig: ProjectLocaleConfig,
|
|
12
|
-
|
|
11
|
+
options?: FetchOptions,
|
|
13
12
|
): Promise<GuillotineResult> {
|
|
13
|
+
const body = options?.body;
|
|
14
14
|
if (typeof body.query !== 'string' || !body.query.trim()) {
|
|
15
15
|
return {
|
|
16
16
|
error: {
|
|
@@ -19,13 +19,13 @@ export async function fetchGuillotine<Data = Record<string, unknown>>(
|
|
|
19
19
|
},
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
|
-
|
|
22
|
+
|
|
23
|
+
const path = body?.variables?.path;
|
|
23
24
|
const pathMessage = path ? JSON.stringify(path) : '';
|
|
24
25
|
return await fetchFromApi<Data>(
|
|
25
26
|
contentApiUrl,
|
|
26
|
-
body,
|
|
27
27
|
projectConfig,
|
|
28
|
-
|
|
28
|
+
options,
|
|
29
29
|
)
|
|
30
30
|
.then(json => {
|
|
31
31
|
let errors: any[] = json.errors; // fetchFromApi ensures json is an object
|
|
@@ -35,7 +35,6 @@ export async function fetchGuillotine<Data = Record<string, unknown>>(
|
|
|
35
35
|
errors = [errors];
|
|
36
36
|
}
|
|
37
37
|
console.error(`${errors.length} error(s) when fetching data from: ${contentApiUrl}`);
|
|
38
|
-
console.error(`headers: ${JSON.stringify(headers, null, 2)} \nvariables: ${JSON.stringify(body.variables, null, 2)}`);
|
|
39
38
|
errors.forEach(error => {
|
|
40
39
|
console.error(error);
|
|
41
40
|
});
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ContentApiBaseBody,
|
|
3
|
-
MetaResult,
|
|
4
|
-
ProjectLocaleConfig,
|
|
5
|
-
} from '../types';
|
|
1
|
+
import type {ContentApiBaseBody, MetaResult, ProjectLocaleConfig} from '../types';
|
|
6
2
|
|
|
7
3
|
|
|
8
4
|
import {getMetaQuery, pageFragmentQuery} from './getMetaData';
|
|
@@ -21,7 +17,10 @@ export const fetchMetaData = async (
|
|
|
21
17
|
path: xpContentPath,
|
|
22
18
|
},
|
|
23
19
|
};
|
|
24
|
-
const metaResult = await fetchGuillotine<MetaResult>(contentApiUrl,
|
|
20
|
+
const metaResult = await fetchGuillotine<MetaResult>(contentApiUrl, projectConfig, {
|
|
21
|
+
body,
|
|
22
|
+
headers,
|
|
23
|
+
});
|
|
25
24
|
if (metaResult.error) {
|
|
26
25
|
return metaResult;
|
|
27
26
|
} else {
|
|
@@ -29,4 +28,4 @@ export const fetchMetaData = async (
|
|
|
29
28
|
meta: metaResult?.guillotine?.get,
|
|
30
29
|
};
|
|
31
30
|
}
|
|
32
|
-
};
|
|
31
|
+
};
|
package/src/types/index.ts
CHANGED
|
@@ -44,12 +44,14 @@ export type ProjectLocalesConfig = Record<string, ProjectLocaleConfig>;
|
|
|
44
44
|
|
|
45
45
|
export type SelectorName = 'contentType' | 'page' | 'component' | 'part' | 'layout' | 'macro';
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
47
|
+
// https://nextjs.org/docs/app/api-reference/functions/fetch
|
|
48
|
+
export type FetchOptions = Omit<RequestInit, 'body'> & {
|
|
49
|
+
body?: ContentApiBaseBody,
|
|
50
|
+
next?: {
|
|
51
|
+
revalidate?: boolean | number,
|
|
52
|
+
tags?: string[],
|
|
53
|
+
}
|
|
54
|
+
};
|
|
53
55
|
|
|
54
56
|
export interface StaticContentProps extends Record<string, any> {
|
|
55
57
|
children?: React.ReactNode;
|
|
@@ -2,7 +2,7 @@ import type {ProjectLocalesConfig} from '../types';
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
import {ENV_VARS} from '../common/constants';
|
|
5
|
-
import {
|
|
5
|
+
import {MAPPINGS} from '../common/env';
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
// NOTE: Dissallowing slash and any whitespace in 2nd capture group.
|
|
@@ -10,9 +10,9 @@ const PROJECT_CONFIG_REGEXP = /^([\w-]+):([^/\s]+)(\/[\w.-]+)?$/i;
|
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
export function getProjectLocaleConfigs(): ProjectLocalesConfig {
|
|
13
|
-
const str =
|
|
13
|
+
const str = MAPPINGS;
|
|
14
14
|
if (!str?.length) {
|
|
15
|
-
throw new Error(`Did you forget to define "${ENV_VARS.
|
|
15
|
+
throw new Error(`Did you forget to define "${ENV_VARS.MAPPINGS}" environmental variable?
|
|
16
16
|
Format: <default-language>:<default-repository-name>/<default-site-path>,<language>:<repository-name>/<site-path>,...`);
|
|
17
17
|
}
|
|
18
18
|
return str.split(',').reduce((config, prjStr, index: number) => {
|
|
@@ -33,4 +33,4 @@ export function getProjectLocaleConfigs(): ProjectLocalesConfig {
|
|
|
33
33
|
};
|
|
34
34
|
return config;
|
|
35
35
|
}, {});
|
|
36
|
-
}
|
|
36
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
/// <reference types="react" />
|
|
3
2
|
export type * from './component';
|
|
4
3
|
export type * from './componentProps';
|
|
@@ -34,10 +33,13 @@ export interface ProjectLocaleConfig {
|
|
|
34
33
|
}
|
|
35
34
|
export type ProjectLocalesConfig = Record<string, ProjectLocaleConfig>;
|
|
36
35
|
export type SelectorName = 'contentType' | 'page' | 'component' | 'part' | 'layout' | 'macro';
|
|
37
|
-
export
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
export type FetchOptions = Omit<RequestInit, 'body'> & {
|
|
37
|
+
body?: ContentApiBaseBody;
|
|
38
|
+
next?: {
|
|
39
|
+
revalidate?: boolean | number;
|
|
40
|
+
tags?: string[];
|
|
41
|
+
};
|
|
42
|
+
};
|
|
41
43
|
export interface StaticContentProps extends Record<string, any> {
|
|
42
44
|
children?: React.ReactNode;
|
|
43
45
|
element?: string;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { ENV_VARS } from '../common/constants';
|
|
2
|
-
import {
|
|
2
|
+
import { MAPPINGS } from '../common/env';
|
|
3
3
|
// NOTE: Dissallowing slash and any whitespace in 2nd capture group.
|
|
4
4
|
const PROJECT_CONFIG_REGEXP = /^([\w-]+):([^/\s]+)(\/[\w.-]+)?$/i;
|
|
5
5
|
export function getProjectLocaleConfigs() {
|
|
6
|
-
const str =
|
|
6
|
+
const str = MAPPINGS;
|
|
7
7
|
if (!(str === null || str === void 0 ? void 0 : str.length)) {
|
|
8
|
-
throw new Error(`Did you forget to define "${ENV_VARS.
|
|
8
|
+
throw new Error(`Did you forget to define "${ENV_VARS.MAPPINGS}" environmental variable?
|
|
9
9
|
Format: <default-language>:<default-repository-name>/<default-site-path>,<language>:<repository-name>/<site-path>,...`);
|
|
10
10
|
}
|
|
11
11
|
return str.split(',').reduce((config, prjStr, index) => {
|