@nitrogenbuilder/connector-payload 0.1.42 → 0.1.43
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/components/NitrogenEditButtonRuntime.js +21 -1
- package/dist/components/NitrogenViewButtonRuntime.js +21 -1
- package/dist/endpoints/all.js +13 -10
- package/dist/endpoints/batch.js +4 -4
- package/dist/endpoints/helpers.d.ts +2 -0
- package/dist/endpoints/helpers.js +26 -5
- package/dist/endpoints/templates.js +4 -4
- package/dist/types.d.ts +5 -0
- package/package.json +1 -1
|
@@ -38,6 +38,23 @@ function normalizeRoutePattern(routePattern) {
|
|
|
38
38
|
function trimSlashes(value) {
|
|
39
39
|
return value.replace(/^\/+|\/+$/g, '');
|
|
40
40
|
}
|
|
41
|
+
function normalizeRelativePath(value) {
|
|
42
|
+
const trimmed = value.trim();
|
|
43
|
+
if (!trimmed || trimmed === '/home')
|
|
44
|
+
return '/';
|
|
45
|
+
return trimmed.startsWith('/') ? trimmed : `/${trimmed}`;
|
|
46
|
+
}
|
|
47
|
+
function getBreadcrumbRelativePath(data) {
|
|
48
|
+
if (!data || typeof data !== 'object')
|
|
49
|
+
return undefined;
|
|
50
|
+
const breadcrumbs = data.breadcrumbs;
|
|
51
|
+
const lastUrl = Array.isArray(breadcrumbs)
|
|
52
|
+
? breadcrumbs.at(-1)?.url
|
|
53
|
+
: undefined;
|
|
54
|
+
return typeof lastUrl === 'string' && lastUrl.trim()
|
|
55
|
+
? normalizeRelativePath(lastUrl)
|
|
56
|
+
: undefined;
|
|
57
|
+
}
|
|
41
58
|
function buildRelativePath(slug, routePattern, templateSlug) {
|
|
42
59
|
const normalizedRoutePattern = normalizeRoutePattern(routePattern);
|
|
43
60
|
if (normalizedRoutePattern) {
|
|
@@ -60,6 +77,7 @@ export const NitrogenEditButtonRuntime = ({ collection, routePattern }) => {
|
|
|
60
77
|
const [id, setId] = useState(undefined);
|
|
61
78
|
const [token, setToken] = useState(undefined);
|
|
62
79
|
const [slug, setSlug] = useState(undefined);
|
|
80
|
+
const [documentRelativePath, setDocumentRelativePath] = useState(undefined);
|
|
63
81
|
const [frontendUrl, setFrontendUrl] = useState(undefined);
|
|
64
82
|
const [instanceUrl, setInstanceUrl] = useState(undefined);
|
|
65
83
|
const [authorId, setAuthorId] = useState(undefined);
|
|
@@ -73,6 +91,7 @@ export const NitrogenEditButtonRuntime = ({ collection, routePattern }) => {
|
|
|
73
91
|
fetch(`/api/${collection}/${docId}`)
|
|
74
92
|
.then((res) => res.json())
|
|
75
93
|
.then((data) => {
|
|
94
|
+
setDocumentRelativePath(getBreadcrumbRelativePath(data));
|
|
76
95
|
if (data.slug) {
|
|
77
96
|
setSlug(data.slug);
|
|
78
97
|
}
|
|
@@ -142,7 +161,8 @@ export const NitrogenEditButtonRuntime = ({ collection, routePattern }) => {
|
|
|
142
161
|
const editHref = `${editorBase}/nitrogen-editor?token=${encodeURIComponent(token)}&collection=${encodeURIComponent(collection)}&pageId=${id}&authorId=${encodedAuthorId}&author=${encodedAuthorId}${developmentFlags}`;
|
|
143
162
|
const viewRelativePath = collection === 'nitrogen-templates'
|
|
144
163
|
? `/nitrogen-templates/${trimSlashes(slug || '')}`
|
|
145
|
-
:
|
|
164
|
+
: documentRelativePath ||
|
|
165
|
+
buildRelativePath(slug || '', routePattern, templateSlug ?? undefined);
|
|
146
166
|
const viewHref = `${frontendUrl}${viewRelativePath}`;
|
|
147
167
|
const editButtonVars = isDevelopmentMode
|
|
148
168
|
? nitrogenEditDevButtonVars
|
|
@@ -24,6 +24,23 @@ function normalizeRoutePattern(routePattern) {
|
|
|
24
24
|
function trimSlashes(value) {
|
|
25
25
|
return value.replace(/^\/+|\/+$/g, '');
|
|
26
26
|
}
|
|
27
|
+
function normalizeRelativePath(value) {
|
|
28
|
+
const trimmed = value.trim();
|
|
29
|
+
if (!trimmed || trimmed === '/home')
|
|
30
|
+
return '/';
|
|
31
|
+
return trimmed.startsWith('/') ? trimmed : `/${trimmed}`;
|
|
32
|
+
}
|
|
33
|
+
function getBreadcrumbRelativePath(data) {
|
|
34
|
+
if (!data || typeof data !== 'object')
|
|
35
|
+
return undefined;
|
|
36
|
+
const breadcrumbs = data.breadcrumbs;
|
|
37
|
+
const lastUrl = Array.isArray(breadcrumbs)
|
|
38
|
+
? breadcrumbs.at(-1)?.url
|
|
39
|
+
: undefined;
|
|
40
|
+
return typeof lastUrl === 'string' && lastUrl.trim()
|
|
41
|
+
? normalizeRelativePath(lastUrl)
|
|
42
|
+
: undefined;
|
|
43
|
+
}
|
|
27
44
|
function buildRelativePath(slug, routePattern, templateSlug) {
|
|
28
45
|
const normalizedRoutePattern = normalizeRoutePattern(routePattern);
|
|
29
46
|
if (normalizedRoutePattern) {
|
|
@@ -44,6 +61,7 @@ function buildRelativePath(slug, routePattern, templateSlug) {
|
|
|
44
61
|
}
|
|
45
62
|
export const NitrogenViewButtonRuntime = ({ collection, routePattern }) => {
|
|
46
63
|
const [slug, setSlug] = useState(undefined);
|
|
64
|
+
const [documentRelativePath, setDocumentRelativePath] = useState(undefined);
|
|
47
65
|
const [siteUrl, setSiteUrl] = useState(undefined);
|
|
48
66
|
const [templateSlug, setTemplateSlug] = useState(undefined);
|
|
49
67
|
useEffect(() => {
|
|
@@ -55,6 +73,7 @@ export const NitrogenViewButtonRuntime = ({ collection, routePattern }) => {
|
|
|
55
73
|
fetch(`/api/${collection}/${docId}`)
|
|
56
74
|
.then((res) => res.json())
|
|
57
75
|
.then((data) => {
|
|
76
|
+
setDocumentRelativePath(getBreadcrumbRelativePath(data));
|
|
58
77
|
if (data.slug) {
|
|
59
78
|
setSlug(data.slug);
|
|
60
79
|
}
|
|
@@ -105,7 +124,8 @@ export const NitrogenViewButtonRuntime = ({ collection, routePattern }) => {
|
|
|
105
124
|
}
|
|
106
125
|
const relativePath = collection === 'nitrogen-templates'
|
|
107
126
|
? `/nitrogen-templates/${trimSlashes(slug)}`
|
|
108
|
-
:
|
|
127
|
+
: documentRelativePath ||
|
|
128
|
+
buildRelativePath(slug, routePattern, templateSlug ?? undefined);
|
|
109
129
|
const href = `${siteUrl}${relativePath}`;
|
|
110
130
|
return (_jsx(Button, { buttonStyle: "primary", el: "anchor", margin: false, newTab: true, url: href, extraButtonProps: {
|
|
111
131
|
style: nitrogenViewButtonVars,
|
package/dist/endpoints/all.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getNitrogenSettings } from './helpers.js';
|
|
2
|
-
import {
|
|
1
|
+
import { getDocumentPermalink, getDocumentRelativePermalink, getNitrogenSettings, } from './helpers.js';
|
|
2
|
+
import { resolveCollection } from '../collection-registry.js';
|
|
3
3
|
export const allEndpoints = [
|
|
4
4
|
// GET /api/nitrogen/v1/all — List all items with filtering
|
|
5
5
|
{
|
|
@@ -30,14 +30,17 @@ export const allEndpoints = [
|
|
|
30
30
|
limit: perPage,
|
|
31
31
|
depth: 0,
|
|
32
32
|
});
|
|
33
|
-
const items = result.docs.map((doc) =>
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
const items = result.docs.map((doc) => {
|
|
34
|
+
const document = doc;
|
|
35
|
+
return {
|
|
36
|
+
id: document.id,
|
|
37
|
+
title: String(document.title || ''),
|
|
38
|
+
slug: String(document.slug || ''),
|
|
39
|
+
permalink: getDocumentPermalink(document, settings, postType),
|
|
40
|
+
relative_permalink: getDocumentRelativePermalink(document, postType),
|
|
41
|
+
type: collection,
|
|
42
|
+
};
|
|
43
|
+
});
|
|
41
44
|
return Response.json(items);
|
|
42
45
|
}
|
|
43
46
|
catch {
|
package/dist/endpoints/batch.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getNitrogenSettings, buildDynamicData, buildPageResponse, } from "./helpers.js";
|
|
1
|
+
import { resolveCollection } from "../collection-registry.js";
|
|
2
|
+
import { getNitrogenSettings, buildDynamicData, buildPageResponse, getDocumentPermalink, getDocumentRelativePermalink, } from "./helpers.js";
|
|
3
3
|
function toArray(value) {
|
|
4
4
|
if (Array.isArray(value)) {
|
|
5
5
|
return value.map((item) => String(item).trim()).filter(Boolean);
|
|
@@ -286,8 +286,8 @@ export const batchEndpoints = [
|
|
|
286
286
|
id: d.id,
|
|
287
287
|
title: String(d.title || ""),
|
|
288
288
|
slug,
|
|
289
|
-
permalink:
|
|
290
|
-
relative_permalink:
|
|
289
|
+
permalink: getDocumentPermalink(d, settings, endpoint),
|
|
290
|
+
relative_permalink: getDocumentRelativePermalink(d, endpoint),
|
|
291
291
|
};
|
|
292
292
|
});
|
|
293
293
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { Payload, PayloadRequest } from 'payload';
|
|
2
2
|
import type { JsonObject, NitrogenPageDoc, NitrogenTemplateDoc, NitrogenSettingsGlobal, MediaDoc } from '../types.js';
|
|
3
|
+
export declare function getDocumentRelativePermalink(doc: NitrogenPageDoc | NitrogenTemplateDoc | Record<string, unknown>, collectionSlug?: string): string;
|
|
4
|
+
export declare function getDocumentPermalink(doc: NitrogenPageDoc | NitrogenTemplateDoc | Record<string, unknown>, settings: NitrogenSettingsGlobal, collectionSlug?: string): string;
|
|
3
5
|
export interface TemplateRef {
|
|
4
6
|
ID: string | number;
|
|
5
7
|
content: string;
|
|
@@ -1,4 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { buildRelativePermalink } from '../collection-registry.js';
|
|
2
|
+
function normalizeRelativePath(value) {
|
|
3
|
+
const trimmed = value.trim();
|
|
4
|
+
if (!trimmed || trimmed === '/home')
|
|
5
|
+
return '/';
|
|
6
|
+
return trimmed.startsWith('/') ? trimmed : `/${trimmed}`;
|
|
7
|
+
}
|
|
8
|
+
export function getDocumentRelativePermalink(doc, collectionSlug = 'pages') {
|
|
9
|
+
const breadcrumbs = doc.breadcrumbs;
|
|
10
|
+
const lastUrl = Array.isArray(breadcrumbs)
|
|
11
|
+
? breadcrumbs.at(-1)?.url
|
|
12
|
+
: undefined;
|
|
13
|
+
if (typeof lastUrl === 'string' && lastUrl.trim()) {
|
|
14
|
+
return normalizeRelativePath(lastUrl);
|
|
15
|
+
}
|
|
16
|
+
return buildRelativePermalink(collectionSlug, String(doc.slug || ''));
|
|
17
|
+
}
|
|
18
|
+
export function getDocumentPermalink(doc, settings, collectionSlug = 'pages') {
|
|
19
|
+
const relativePermalink = getDocumentRelativePermalink(doc, collectionSlug);
|
|
20
|
+
const baseUrl = (settings.frontendUrl || '').replace(/\/$/, '');
|
|
21
|
+
return baseUrl ? `${baseUrl}${relativePermalink}` : relativePermalink;
|
|
22
|
+
}
|
|
2
23
|
export function formatDate(date) {
|
|
3
24
|
const d = new Date(date);
|
|
4
25
|
const pad = (n) => String(n).padStart(2, '0');
|
|
@@ -60,8 +81,8 @@ export function buildPageResponse(doc, settings, dynamicData, collectionSlug = '
|
|
|
60
81
|
title: doc.title,
|
|
61
82
|
author: doc.author && typeof doc.author === 'object' ? doc.author.id : (doc.author || ''),
|
|
62
83
|
slug,
|
|
63
|
-
permalink:
|
|
64
|
-
relative_permalink:
|
|
84
|
+
permalink: getDocumentPermalink(doc, settings, collectionSlug),
|
|
85
|
+
relative_permalink: getDocumentRelativePermalink(doc, collectionSlug),
|
|
65
86
|
content: doc.nitrogenData ? JSON.stringify(doc.nitrogenData) : '[]',
|
|
66
87
|
dynamic_data: dynamicData,
|
|
67
88
|
settings: pageSettings || templateSettings || {
|
|
@@ -82,8 +103,8 @@ export function buildListItemResponse(doc, settings, collectionSlug = 'pages') {
|
|
|
82
103
|
id: doc.id,
|
|
83
104
|
title: doc.title,
|
|
84
105
|
slug,
|
|
85
|
-
permalink:
|
|
86
|
-
relative_permalink:
|
|
106
|
+
permalink: getDocumentPermalink(doc, settings, collectionSlug),
|
|
107
|
+
relative_permalink: getDocumentRelativePermalink(doc, collectionSlug),
|
|
87
108
|
content: doc.nitrogenData ? JSON.stringify(doc.nitrogenData) : '[]',
|
|
88
109
|
settings: pageSettings || templateSettings || {},
|
|
89
110
|
nitrogen_settings: {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { getNitrogenSettings, buildDynamicData, buildPageResponse, buildListItemResponse, getTemplateForType, requireAuth, } from './helpers.js';
|
|
2
|
-
import { buildPermalink, buildRelativePermalink } from '../collection-registry.js';
|
|
1
|
+
import { getNitrogenSettings, buildDynamicData, buildPageResponse, buildListItemResponse, getDocumentPermalink, getDocumentRelativePermalink, getTemplateForType, requireAuth, } from './helpers.js';
|
|
3
2
|
function isContentTemplateCollection(collection) {
|
|
4
3
|
return !!collection && collection !== 'header' && collection !== 'footer';
|
|
5
4
|
}
|
|
@@ -11,10 +10,11 @@ function getTemplatePreviewTarget(doc, settings, associatedCollection, associate
|
|
|
11
10
|
? associatedCollection
|
|
12
11
|
: 'nitrogen-templates';
|
|
13
12
|
const previewSlug = String(associatedDoc?.slug || doc.slug || '');
|
|
13
|
+
const previewDoc = associatedDoc || doc;
|
|
14
14
|
return {
|
|
15
15
|
slug: previewSlug,
|
|
16
|
-
permalink:
|
|
17
|
-
relative_permalink:
|
|
16
|
+
permalink: getDocumentPermalink(previewDoc, settings, previewCollection),
|
|
17
|
+
relative_permalink: getDocumentRelativePermalink(previewDoc, previewCollection),
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
export const templatesEndpoints = [
|
package/dist/types.d.ts
CHANGED
|
@@ -11,6 +11,11 @@ interface NitrogenDocBase {
|
|
|
11
11
|
id: string | number;
|
|
12
12
|
title: string;
|
|
13
13
|
slug: string;
|
|
14
|
+
breadcrumbs?: Array<{
|
|
15
|
+
url?: string | null;
|
|
16
|
+
label?: string | null;
|
|
17
|
+
doc?: string | number | null;
|
|
18
|
+
}>;
|
|
14
19
|
status?: 'draft' | 'published';
|
|
15
20
|
author?: string | number | {
|
|
16
21
|
id: string | number;
|
package/package.json
CHANGED