@nitrogenbuilder/connector-payload 0.1.14 → 0.1.16
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.
|
@@ -7,6 +7,8 @@ export const NitrogenEditButton = ({ collection }) => {
|
|
|
7
7
|
const [hasDevelopmentUrl, setHasDevelopmentUrl] = useState(false);
|
|
8
8
|
const [slug, setSlug] = useState(undefined);
|
|
9
9
|
const [frontendUrl, setFrontendUrl] = useState(undefined);
|
|
10
|
+
const [instanceUrl, setInstanceUrl] = useState(undefined);
|
|
11
|
+
const [authorId, setAuthorId] = useState(undefined);
|
|
10
12
|
useEffect(() => {
|
|
11
13
|
const segments = window.location.pathname.split("/").filter(Boolean);
|
|
12
14
|
const docId = segments.length >= 4 ? segments[3] : undefined;
|
|
@@ -24,6 +26,17 @@ export const NitrogenEditButton = ({ collection }) => {
|
|
|
24
26
|
console.error("Failed to fetch document:", err);
|
|
25
27
|
});
|
|
26
28
|
}
|
|
29
|
+
// Fetch the current user's ID for the authorId param
|
|
30
|
+
fetch("/api/users/me")
|
|
31
|
+
.then((res) => res.json())
|
|
32
|
+
.then((data) => {
|
|
33
|
+
if (data.user?.id) {
|
|
34
|
+
setAuthorId(String(data.user.id));
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
.catch((err) => {
|
|
38
|
+
console.error("Failed to fetch current user:", err);
|
|
39
|
+
});
|
|
27
40
|
// Fetch the connector token and URLs from NitrogenSettings global
|
|
28
41
|
fetch("/api/globals/nitrogen-settings")
|
|
29
42
|
.then((res) => res.json())
|
|
@@ -34,6 +47,9 @@ export const NitrogenEditButton = ({ collection }) => {
|
|
|
34
47
|
if (data.developmentUrl) {
|
|
35
48
|
setHasDevelopmentUrl(true);
|
|
36
49
|
}
|
|
50
|
+
if (data.instanceUrl) {
|
|
51
|
+
setInstanceUrl(data.instanceUrl.replace(/\/$/, ""));
|
|
52
|
+
}
|
|
37
53
|
const url = data.frontendUrl;
|
|
38
54
|
if (url) {
|
|
39
55
|
setFrontendUrl(url.replace(/\/$/, ""));
|
|
@@ -43,10 +59,11 @@ export const NitrogenEditButton = ({ collection }) => {
|
|
|
43
59
|
console.error("Failed to fetch nitrogen settings:", err);
|
|
44
60
|
});
|
|
45
61
|
}, [collection]);
|
|
46
|
-
if (!id || !token)
|
|
62
|
+
if (!id || !token || !authorId)
|
|
47
63
|
return null;
|
|
48
64
|
const param = collection === "nitrogen-templates" ? "templateId" : "pageId";
|
|
49
|
-
const
|
|
65
|
+
const editorBase = instanceUrl ?? "";
|
|
66
|
+
const baseHref = `${editorBase}/nitrogen-editor?token=${encodeURIComponent(token)}&collection=${encodeURIComponent(collection)}&${param}=${id}&authorId=${encodeURIComponent(authorId)}`;
|
|
50
67
|
const buttonStyle = {
|
|
51
68
|
display: "inline-flex",
|
|
52
69
|
alignItems: "center",
|
package/package.json
CHANGED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
export const collectionPickerEndpoints = [
|
|
2
|
-
// GET /api/nitrogen/v1/collection/:slug
|
|
3
|
-
// Generic read-only endpoint for fetching any collection's documents for use
|
|
4
|
-
// in the nitrogen editor's CtrlRelationship control.
|
|
5
|
-
{
|
|
6
|
-
path: '/nitrogen/v1/collection/:slug',
|
|
7
|
-
method: 'get',
|
|
8
|
-
handler: async (req) => {
|
|
9
|
-
const { payload, routeParams } = req;
|
|
10
|
-
const collectionSlug = routeParams?.slug;
|
|
11
|
-
if (!collectionSlug) {
|
|
12
|
-
return Response.json({ error: 'Collection slug is required' }, { status: 400 });
|
|
13
|
-
}
|
|
14
|
-
const url = new URL(req.url || '', 'http://localhost');
|
|
15
|
-
const search = url.searchParams.get('search') || '';
|
|
16
|
-
const page = parseInt(url.searchParams.get('page') || '1', 10);
|
|
17
|
-
const limit = parseInt(url.searchParams.get('limit') || '50', 10);
|
|
18
|
-
const depth = parseInt(url.searchParams.get('depth') || '1', 10);
|
|
19
|
-
try {
|
|
20
|
-
const where = {};
|
|
21
|
-
if (search) {
|
|
22
|
-
where['or'] = [
|
|
23
|
-
{ title: { contains: search } },
|
|
24
|
-
{ name: { contains: search } },
|
|
25
|
-
];
|
|
26
|
-
}
|
|
27
|
-
const result = await payload.find({
|
|
28
|
-
collection: collectionSlug,
|
|
29
|
-
where,
|
|
30
|
-
page,
|
|
31
|
-
limit,
|
|
32
|
-
depth,
|
|
33
|
-
overrideAccess: false,
|
|
34
|
-
req,
|
|
35
|
-
});
|
|
36
|
-
return Response.json({
|
|
37
|
-
docs: result.docs,
|
|
38
|
-
totalDocs: result.totalDocs,
|
|
39
|
-
totalPages: result.totalPages,
|
|
40
|
-
page: result.page,
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
catch (err) {
|
|
44
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
45
|
-
if (message.includes('not found') || message.includes('Unknown collection')) {
|
|
46
|
-
return Response.json({ error: `Collection "${collectionSlug}" not found` }, { status: 404 });
|
|
47
|
-
}
|
|
48
|
-
return Response.json({ error: message }, { status: 500 });
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
];
|