@nitrogenbuilder/connector-payload 1.2.1 → 1.2.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.
@@ -1,10 +1,31 @@
1
+ import { existsSync } from "node:fs";
1
2
  import fs from "node:fs/promises";
3
+ import { createRequire } from "node:module";
2
4
  import path from "node:path";
3
5
  import { fileURLToPath } from "node:url";
4
6
  import { NextResponse } from "next/server";
5
7
  const __filename = fileURLToPath(import.meta.url);
6
8
  const __dirname = path.dirname(__filename);
7
- const ASSETS_DIR = path.resolve(__dirname, "..", "editor-assets");
9
+ // Next bundles npm packages into server chunks, so in a production build
10
+ // __dirname points into .next/server and the relative lookup fails. Resolve
11
+ // the installed package on disk through Node's resolver instead (works from
12
+ // inside a bundle because createRequire resolves against the host app's real
13
+ // node_modules), and keep the relative path as a fallback for unbundled /
14
+ // linked-dev execution where the package specifier may not resolve.
15
+ function resolveAssetsDir() {
16
+ try {
17
+ const nodeRequire = createRequire(import.meta.url);
18
+ const pkgRoot = path.dirname(nodeRequire.resolve("@nitrogenbuilder/connector-payload/package.json"));
19
+ const dir = path.join(pkgRoot, "dist", "editor-assets");
20
+ if (existsSync(dir))
21
+ return dir;
22
+ }
23
+ catch {
24
+ // fall through to the relative fallback
25
+ }
26
+ return path.resolve(__dirname, "..", "editor-assets");
27
+ }
28
+ const ASSETS_DIR = resolveAssetsDir();
8
29
  function getContentType(filePath) {
9
30
  switch (path.extname(filePath).toLowerCase()) {
10
31
  case ".css":
@@ -29,10 +29,10 @@ function requestSignature(batchReq) {
29
29
  .map((k) => [k, params[k]]));
30
30
  return JSON.stringify({ endpoint: batchReq.endpoint, params: sortedParams });
31
31
  }
32
- async function resolveRelationshipIds(payload, collection, values) {
32
+ async function resolveRelationshipIds(payload, collection, values, includeDirectIds = false) {
33
33
  if (values.length === 0)
34
34
  return [];
35
- const [slugMatches, titleMatches] = await Promise.all([
35
+ const [slugMatches, titleMatches, idMatches] = await Promise.all([
36
36
  payload.find({
37
37
  collection,
38
38
  where: {
@@ -51,8 +51,19 @@ async function resolveRelationshipIds(payload, collection, values) {
51
51
  depth: 0,
52
52
  pagination: false,
53
53
  }),
54
+ includeDirectIds
55
+ ? payload.find({
56
+ collection,
57
+ where: {
58
+ id: { in: values },
59
+ },
60
+ limit: values.length,
61
+ depth: 0,
62
+ pagination: false,
63
+ })
64
+ : Promise.resolve({ docs: [] }),
54
65
  ]);
55
- return [...slugMatches.docs, ...titleMatches.docs].map((doc) => doc.id);
66
+ return [...slugMatches.docs, ...titleMatches.docs, ...idMatches.docs].map((doc) => doc.id);
56
67
  }
57
68
  async function resolveAllRelationshipIds(payload, collection) {
58
69
  const result = await payload.find({
@@ -63,8 +74,8 @@ async function resolveAllRelationshipIds(payload, collection) {
63
74
  });
64
75
  return result.docs.map((doc) => doc.id);
65
76
  }
66
- async function applyRelationshipFilter({ payload, results, key, where, field, collection, includeValues, excludeValues = [], }) {
67
- const includeIds = await resolveRelationshipIds(payload, collection, includeValues);
77
+ async function applyRelationshipFilter({ payload, results, key, where, field, collection, includeValues, excludeValues = [], includeDirectIds = false, }) {
78
+ const includeIds = await resolveRelationshipIds(payload, collection, includeValues, includeDirectIds);
68
79
  if (includeValues.length > 0) {
69
80
  if (includeIds.length === 0) {
70
81
  results[key] = {
@@ -265,21 +276,17 @@ export const batchEndpoints = [
265
276
  const authors = toArray(params.authors);
266
277
  if (authors.length > 0) {
267
278
  if (collectionSlug === "posts") {
268
- const authorIds = await resolveRelationshipIds(payload, "author", authors);
269
- const authorFilters = [];
270
- if (authorIds.length > 0) {
271
- authorFilters.push({
272
- wpAuthors: { in: Array.from(new Set(authorIds)) },
273
- });
274
- }
275
- authorFilters.push({
276
- wpAuthorName: { in: authors },
277
- });
278
- if (authorFilters.length === 1) {
279
- Object.assign(where, authorFilters[0]);
280
- }
281
- else {
282
- where.or = [...(where.or ?? []), ...authorFilters];
279
+ if (!(await applyRelationshipFilter({
280
+ payload,
281
+ results,
282
+ key,
283
+ where,
284
+ field: "contentAuthors",
285
+ collection: "author",
286
+ includeValues: authors,
287
+ includeDirectIds: true,
288
+ }))) {
289
+ return;
283
290
  }
284
291
  }
285
292
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitrogenbuilder/connector-payload",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "Nitrogen page builder connector plugin for Payload CMS 3.x",
5
5
  "author": "Leonardo Dentzien <leo@torchmedia.ca>",
6
6
  "type": "module",
@@ -23,13 +23,15 @@
23
23
  "types": "./dist/editor/NitrogenEditorAssetsRoute.d.ts",
24
24
  "default": "./dist/editor/NitrogenEditorAssetsRoute.js"
25
25
  },
26
- "./components/*": "./dist/components/*"
26
+ "./components/*": "./dist/components/*",
27
+ "./package.json": "./package.json"
27
28
  },
28
29
  "files": [
29
30
  "dist"
30
31
  ],
31
32
  "scripts": {
32
33
  "build": "tsc && node ./scripts/copy-editor-assets.mjs",
34
+ "test": "pnpm build && node --test tests/*.test.mjs",
33
35
  "typecheck": "tsc --noEmit",
34
36
  "prepublishOnly": "pnpm build"
35
37
  },
@@ -55,4 +57,4 @@
55
57
  "@nitrogenbuilder/client-core": "link:../monogen/packages/client-core",
56
58
  "@nitrogenbuilder/types": "link:../monogen/packages/types"
57
59
  }
58
- }
60
+ }