@nitrogenbuilder/connector-payload 0.1.32 → 0.1.33
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/endpoints/batch.js +141 -55
- package/package.json +1 -1
package/dist/endpoints/batch.js
CHANGED
|
@@ -2,9 +2,7 @@ import { buildPermalink, buildRelativePermalink, resolveCollection, } from "../c
|
|
|
2
2
|
import { getNitrogenSettings, buildDynamicData, buildPageResponse, } from "./helpers";
|
|
3
3
|
function toArray(value) {
|
|
4
4
|
if (Array.isArray(value)) {
|
|
5
|
-
return value
|
|
6
|
-
.map((item) => String(item).trim())
|
|
7
|
-
.filter(Boolean);
|
|
5
|
+
return value.map((item) => String(item).trim()).filter(Boolean);
|
|
8
6
|
}
|
|
9
7
|
if (typeof value === "string") {
|
|
10
8
|
return value
|
|
@@ -14,6 +12,12 @@ function toArray(value) {
|
|
|
14
12
|
}
|
|
15
13
|
return [];
|
|
16
14
|
}
|
|
15
|
+
function isTruthyParam(value) {
|
|
16
|
+
return value === true || value === "true" || value === "1" || value === 1;
|
|
17
|
+
}
|
|
18
|
+
function unique(values) {
|
|
19
|
+
return Array.from(new Set(values));
|
|
20
|
+
}
|
|
17
21
|
async function resolveRelationshipIds(payload, collection, values) {
|
|
18
22
|
if (values.length === 0)
|
|
19
23
|
return [];
|
|
@@ -39,6 +43,37 @@ async function resolveRelationshipIds(payload, collection, values) {
|
|
|
39
43
|
]);
|
|
40
44
|
return [...slugMatches.docs, ...titleMatches.docs].map((doc) => doc.id);
|
|
41
45
|
}
|
|
46
|
+
async function resolveAllRelationshipIds(payload, collection) {
|
|
47
|
+
const result = await payload.find({
|
|
48
|
+
collection,
|
|
49
|
+
limit: 1000,
|
|
50
|
+
depth: 0,
|
|
51
|
+
pagination: false,
|
|
52
|
+
});
|
|
53
|
+
return result.docs.map((doc) => doc.id);
|
|
54
|
+
}
|
|
55
|
+
async function applyRelationshipFilter({ payload, results, key, where, field, collection, includeValues, excludeValues = [], }) {
|
|
56
|
+
const includeIds = await resolveRelationshipIds(payload, collection, includeValues);
|
|
57
|
+
if (includeValues.length > 0) {
|
|
58
|
+
if (includeIds.length === 0) {
|
|
59
|
+
results[key] = {
|
|
60
|
+
data: [],
|
|
61
|
+
total: 0,
|
|
62
|
+
totalPages: 0,
|
|
63
|
+
};
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
where[field] = { in: unique(includeIds) };
|
|
67
|
+
}
|
|
68
|
+
const excludeIds = await resolveRelationshipIds(payload, collection, excludeValues);
|
|
69
|
+
if (excludeIds.length > 0) {
|
|
70
|
+
where[field] = {
|
|
71
|
+
...(where[field] ?? {}),
|
|
72
|
+
not_in: unique(excludeIds),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
42
77
|
export const batchEndpoints = [
|
|
43
78
|
// POST /api/nitrogen/v1/batch-data — Batch fetch multiple collections
|
|
44
79
|
{
|
|
@@ -76,10 +111,7 @@ export const batchEndpoints = [
|
|
|
76
111
|
if (!statuses.includes("any")) {
|
|
77
112
|
where._status = { in: statuses };
|
|
78
113
|
}
|
|
79
|
-
const slugs = [
|
|
80
|
-
...toArray(params.slug),
|
|
81
|
-
...toArray(params.slugs),
|
|
82
|
-
];
|
|
114
|
+
const slugs = [...toArray(params.slug), ...toArray(params.slugs)];
|
|
83
115
|
if (slugs.length > 0) {
|
|
84
116
|
where.slug = { in: Array.from(new Set(slugs)) };
|
|
85
117
|
}
|
|
@@ -90,63 +122,117 @@ export const batchEndpoints = [
|
|
|
90
122
|
if (titles.length > 0) {
|
|
91
123
|
where.title = { in: Array.from(new Set(titles)) };
|
|
92
124
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
125
|
+
if (!(await applyRelationshipFilter({
|
|
126
|
+
payload,
|
|
127
|
+
results,
|
|
128
|
+
key,
|
|
129
|
+
where,
|
|
130
|
+
field: "categories",
|
|
131
|
+
collection: "post-category",
|
|
132
|
+
includeValues: toArray(params.categories),
|
|
133
|
+
excludeValues: toArray(params.categories_excluded),
|
|
134
|
+
}))) {
|
|
135
|
+
return;
|
|
104
136
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
137
|
+
if (!(await applyRelationshipFilter({
|
|
138
|
+
payload,
|
|
139
|
+
results,
|
|
140
|
+
key,
|
|
141
|
+
where,
|
|
142
|
+
field: "tags",
|
|
143
|
+
collection: "tag",
|
|
144
|
+
includeValues: toArray(params.tags),
|
|
145
|
+
excludeValues: toArray(params.tags_excluded),
|
|
146
|
+
}))) {
|
|
147
|
+
return;
|
|
111
148
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
149
|
+
if (!(await applyRelationshipFilter({
|
|
150
|
+
payload,
|
|
151
|
+
results,
|
|
152
|
+
key,
|
|
153
|
+
where,
|
|
154
|
+
field: "industryTags",
|
|
155
|
+
collection: "industry-tag",
|
|
156
|
+
includeValues: [
|
|
157
|
+
...toArray(params.industry_tags),
|
|
158
|
+
...toArray(params.industryTags),
|
|
159
|
+
],
|
|
160
|
+
excludeValues: toArray(params.industry_tags_excluded),
|
|
161
|
+
}))) {
|
|
162
|
+
return;
|
|
123
163
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
164
|
+
if (!(await applyRelationshipFilter({
|
|
165
|
+
payload,
|
|
166
|
+
results,
|
|
167
|
+
key,
|
|
168
|
+
where,
|
|
169
|
+
field: "productTags",
|
|
170
|
+
collection: "product-tag",
|
|
171
|
+
includeValues: [
|
|
172
|
+
...toArray(params.product_tags),
|
|
173
|
+
...toArray(params.productTags),
|
|
174
|
+
],
|
|
175
|
+
excludeValues: toArray(params.product_tags_excluded),
|
|
176
|
+
}))) {
|
|
177
|
+
return;
|
|
130
178
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
179
|
+
if (!(await applyRelationshipFilter({
|
|
180
|
+
payload,
|
|
181
|
+
results,
|
|
182
|
+
key,
|
|
183
|
+
where,
|
|
184
|
+
field: "languageTags",
|
|
185
|
+
collection: "language-tag",
|
|
186
|
+
includeValues: [
|
|
187
|
+
...toArray(params.language_tags),
|
|
188
|
+
...toArray(params.languageTags),
|
|
189
|
+
],
|
|
190
|
+
excludeValues: toArray(params.language_tags_excluded),
|
|
191
|
+
}))) {
|
|
192
|
+
return;
|
|
142
193
|
}
|
|
143
|
-
|
|
144
|
-
|
|
194
|
+
if (!(await applyRelationshipFilter({
|
|
195
|
+
payload,
|
|
196
|
+
results,
|
|
197
|
+
key,
|
|
198
|
+
where,
|
|
199
|
+
field: "featuredTags",
|
|
200
|
+
collection: "featured-tag",
|
|
201
|
+
includeValues: [
|
|
202
|
+
...toArray(params.featured),
|
|
203
|
+
...toArray(params.featured_tags),
|
|
204
|
+
...toArray(params.featuredTags),
|
|
205
|
+
],
|
|
206
|
+
excludeValues: [
|
|
207
|
+
...toArray(params.featured_excluded),
|
|
208
|
+
...toArray(params.featured_tags_excluded),
|
|
209
|
+
],
|
|
210
|
+
}))) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (isTruthyParam(params.featured_only)) {
|
|
214
|
+
const featuredIds = await resolveAllRelationshipIds(payload, "featured-tag");
|
|
145
215
|
where.featuredTags = {
|
|
146
216
|
...(where.featuredTags ?? {}),
|
|
147
|
-
|
|
217
|
+
in: unique(featuredIds),
|
|
148
218
|
};
|
|
149
219
|
}
|
|
220
|
+
if (isTruthyParam(params.exclude_featured)) {
|
|
221
|
+
const featuredIds = await resolveAllRelationshipIds(payload, "featured-tag");
|
|
222
|
+
where.featuredTags = {
|
|
223
|
+
...(where.featuredTags ?? {}),
|
|
224
|
+
not_in: unique(featuredIds),
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
const search = String(params.q ?? params.search ?? "").trim();
|
|
228
|
+
if (search) {
|
|
229
|
+
where.or = [
|
|
230
|
+
...(where.or ?? []),
|
|
231
|
+
{ title: { contains: search } },
|
|
232
|
+
{ "meta.description": { contains: search } },
|
|
233
|
+
{ content: { contains: search } },
|
|
234
|
+
];
|
|
235
|
+
}
|
|
150
236
|
const authors = toArray(params.authors);
|
|
151
237
|
if (authors.length > 0) {
|
|
152
238
|
if (collectionSlug === "posts") {
|
package/package.json
CHANGED