@asteroidcms/core-utils 0.1.1 → 0.1.2
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/README.md +71 -0
- package/dist/index.cjs +131 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +53 -17
- package/dist/index.d.ts +53 -17
- package/dist/index.js +130 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -195,6 +195,77 @@ const { data: topStories } = useCmsContent({
|
|
|
195
195
|
|
|
196
196
|
---
|
|
197
197
|
|
|
198
|
+
## `fetchCmsContent` (Next.js / RSC)
|
|
199
|
+
|
|
200
|
+
Server-side counterpart to `useCmsContent`. Use it in Next.js Server Components, route handlers, or any other server context. Accepts a server-side Apollo client plus the same options object as `useCmsContent`, and returns the resolved data directly.
|
|
201
|
+
|
|
202
|
+
Pass a `getClient` function that returns a server-side Apollo client. The shape matches what `registerApolloClient` from `@apollo/client-integration-nextjs` already returns, so you can hand it through directly.
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
// app/lib/cms-server.ts
|
|
206
|
+
import "server-only";
|
|
207
|
+
import { ApolloClient, HttpLink, InMemoryCache } from "@apollo/client";
|
|
208
|
+
import { registerApolloClient } from "@apollo/client-integration-nextjs";
|
|
209
|
+
|
|
210
|
+
export const { getClient, query, PreloadQuery } = registerApolloClient(() => {
|
|
211
|
+
return new ApolloClient({
|
|
212
|
+
cache: new InMemoryCache(),
|
|
213
|
+
link: new HttpLink({
|
|
214
|
+
uri: `${process.env.CMS_API_BASE_URL}/graphql`,
|
|
215
|
+
headers: { "X-API-Key": process.env.CMS_X_API_KEY ?? "" },
|
|
216
|
+
fetchOptions: { next: { revalidate: 60, tags: ["cms:landing_page"] } },
|
|
217
|
+
}),
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
// app/news/[slug]/page.tsx
|
|
224
|
+
import { fetchCmsContent } from "@asteroidcms/core-utils";
|
|
225
|
+
import { getClient } from "@/app/lib/cms-server";
|
|
226
|
+
|
|
227
|
+
// Single entry
|
|
228
|
+
const article = await fetchCmsContent<Article>(getClient, {
|
|
229
|
+
schema_slug: "news",
|
|
230
|
+
entrySlug: params.slug,
|
|
231
|
+
fullData: true,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
// List
|
|
235
|
+
const articles = await fetchCmsContent<Article[]>(getClient, {
|
|
236
|
+
schema_slug: "news",
|
|
237
|
+
limit: 10,
|
|
238
|
+
status: "PUBLISHED",
|
|
239
|
+
select: ["title", "slug", "publish_date"],
|
|
240
|
+
});
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
Outside Next.js you can pass any `() => ApolloClient` - e.g. `() => createApolloClient({ cmsUrl, apiKey })`.
|
|
244
|
+
|
|
245
|
+
Add `import "server-only"` in the file that calls it if you want Next.js to fail the build when it leaks into a client component.
|
|
246
|
+
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
## `buildCmsQuery`
|
|
250
|
+
|
|
251
|
+
Lower-level helper that turns a declarative selection into a GraphQL `DocumentNode` plus variables. Used internally by `useCmsContent` and `fetchCmsContent`; exported so you can drive your own Apollo calls (cache reads, prefetching, batching, etc.).
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
import { buildCmsQuery } from "@asteroidcms/core-utils";
|
|
255
|
+
|
|
256
|
+
const { query, variables, isSingle } = buildCmsQuery({
|
|
257
|
+
schema_slug: "news",
|
|
258
|
+
limit: 10,
|
|
259
|
+
status: "PUBLISHED",
|
|
260
|
+
select: ["title", "slug"],
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const { data } = await apolloClient.query({ query, variables });
|
|
264
|
+
const entries = isSingle ? data.entry : data.entries;
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
198
269
|
## `useCmsMutate`
|
|
199
270
|
|
|
200
271
|
`create` / `update` / `delete` against a schema, with the same selection syntax.
|
package/dist/index.cjs
CHANGED
|
@@ -119,7 +119,7 @@ function useCmsContent({
|
|
|
119
119
|
variables = {}
|
|
120
120
|
}) {
|
|
121
121
|
const isSingle = !!entrySlug;
|
|
122
|
-
const
|
|
122
|
+
const buildSelection2 = (items = []) => {
|
|
123
123
|
const lines = [];
|
|
124
124
|
items.forEach((item) => {
|
|
125
125
|
if (typeof item === "string") {
|
|
@@ -132,7 +132,7 @@ function useCmsContent({
|
|
|
132
132
|
}
|
|
133
133
|
const alias = item.as || item.field;
|
|
134
134
|
const resolver = item.single ? "expandedReferenceObject" : "expandedReference";
|
|
135
|
-
const subSelection = item.select?.length ?
|
|
135
|
+
const subSelection = item.select?.length ? buildSelection2(item.select) : "data { ... }";
|
|
136
136
|
lines.push(`
|
|
137
137
|
${alias}: ${resolver}(slug: "${item.field}") {
|
|
138
138
|
${subSelection}
|
|
@@ -145,7 +145,7 @@ function useCmsContent({
|
|
|
145
145
|
const selectionParts = [];
|
|
146
146
|
if (fullData) selectionParts.push("data");
|
|
147
147
|
if (select.length > 0) {
|
|
148
|
-
const userSelection =
|
|
148
|
+
const userSelection = buildSelection2(select);
|
|
149
149
|
if (userSelection) selectionParts.push(userSelection);
|
|
150
150
|
}
|
|
151
151
|
const selection = selectionParts.join("\n ").trim() || "id";
|
|
@@ -229,7 +229,7 @@ function useCmsMutate({
|
|
|
229
229
|
entryId,
|
|
230
230
|
variables: inputVariables = {}
|
|
231
231
|
}) {
|
|
232
|
-
const
|
|
232
|
+
const buildSelection2 = (items = []) => {
|
|
233
233
|
const lines = [];
|
|
234
234
|
for (const item of items) {
|
|
235
235
|
if (typeof item === "string") {
|
|
@@ -243,7 +243,7 @@ function useCmsMutate({
|
|
|
243
243
|
continue;
|
|
244
244
|
}
|
|
245
245
|
const resolver = single ? "expandedReferenceObject" : "expandedReference";
|
|
246
|
-
const sub =
|
|
246
|
+
const sub = buildSelection2(subSelect) || "id slug";
|
|
247
247
|
lines.push(`
|
|
248
248
|
${alias}: ${resolver}(slug: "${field}") {
|
|
249
249
|
id
|
|
@@ -254,7 +254,7 @@ function useCmsMutate({
|
|
|
254
254
|
}
|
|
255
255
|
return lines.join("\n ").trim();
|
|
256
256
|
};
|
|
257
|
-
const userSelection =
|
|
257
|
+
const userSelection = buildSelection2(select);
|
|
258
258
|
let selection = fullData ? "data" : "";
|
|
259
259
|
if (userSelection) {
|
|
260
260
|
selection = selection ? `${selection}
|
|
@@ -311,6 +311,129 @@ function useCmsMutate({
|
|
|
311
311
|
data: resultData
|
|
312
312
|
};
|
|
313
313
|
}
|
|
314
|
+
function buildSelection(items = []) {
|
|
315
|
+
const lines = [];
|
|
316
|
+
items.forEach((item) => {
|
|
317
|
+
if (typeof item === "string") {
|
|
318
|
+
lines.push(`${item}: dataField(slug: "${item}")`);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if ("field" in item && typeof item.field === "string") {
|
|
322
|
+
if (!("select" in item)) {
|
|
323
|
+
const alias2 = item.as || item.field;
|
|
324
|
+
lines.push(`${alias2}: dataField(slug: "${item.field}")`);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
const alias = item.as || item.field;
|
|
328
|
+
const resolver = item.single ? "expandedReferenceObject" : "expandedReference";
|
|
329
|
+
const subSelection = item.select?.length ? buildSelection(item.select) : "data { ... }";
|
|
330
|
+
lines.push(`
|
|
331
|
+
${alias}: ${resolver}(slug: "${item.field}") {
|
|
332
|
+
${subSelection}
|
|
333
|
+
}
|
|
334
|
+
`);
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
return lines.join("\n ").trim();
|
|
338
|
+
}
|
|
339
|
+
function buildCmsQuery({
|
|
340
|
+
schema_slug,
|
|
341
|
+
entrySlug,
|
|
342
|
+
select = [],
|
|
343
|
+
fullData = false,
|
|
344
|
+
limit,
|
|
345
|
+
offset,
|
|
346
|
+
status = "PUBLISHED",
|
|
347
|
+
filter,
|
|
348
|
+
search,
|
|
349
|
+
variables = {}
|
|
350
|
+
}) {
|
|
351
|
+
const isSingle = Boolean(entrySlug);
|
|
352
|
+
const selectionParts = [];
|
|
353
|
+
if (fullData) {
|
|
354
|
+
selectionParts.push("data");
|
|
355
|
+
}
|
|
356
|
+
if (select.length > 0) {
|
|
357
|
+
const userSelection = buildSelection(select);
|
|
358
|
+
if (userSelection) {
|
|
359
|
+
selectionParts.push(userSelection);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
const selection = selectionParts.join("\n ").trim() || "id";
|
|
363
|
+
const queryVariables = {
|
|
364
|
+
schema_slug,
|
|
365
|
+
...entrySlug && { slug: entrySlug },
|
|
366
|
+
...variables
|
|
367
|
+
};
|
|
368
|
+
const fieldArgParts = ["schema_slug: $schema_slug"];
|
|
369
|
+
if (!isSingle) {
|
|
370
|
+
if (typeof limit === "number" && limit >= 0) {
|
|
371
|
+
fieldArgParts.push("limit: $limit");
|
|
372
|
+
queryVariables.limit = limit;
|
|
373
|
+
}
|
|
374
|
+
if (typeof offset === "number" && offset >= 0) {
|
|
375
|
+
fieldArgParts.push("offset: $offset");
|
|
376
|
+
queryVariables.offset = offset;
|
|
377
|
+
}
|
|
378
|
+
if (status) {
|
|
379
|
+
const statuses = status;
|
|
380
|
+
if (statuses.length > 0) {
|
|
381
|
+
fieldArgParts.push("status: $status");
|
|
382
|
+
queryVariables.status = statuses;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
const hasSearch = Array.isArray(search) && search.length > 0;
|
|
386
|
+
const hasFilter = filter && typeof filter === "object" && Object.keys(filter).length > 0;
|
|
387
|
+
if (hasSearch || hasFilter) {
|
|
388
|
+
const mergedFilter = hasFilter ? { ...filter } : {};
|
|
389
|
+
if (hasSearch) {
|
|
390
|
+
for (const condition of search) {
|
|
391
|
+
mergedFilter[condition.field] = {
|
|
392
|
+
regex: true,
|
|
393
|
+
value: condition.value,
|
|
394
|
+
mode: condition.mode ?? "i"
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
fieldArgParts.push("data: $filter");
|
|
399
|
+
queryVariables.filter = mergedFilter;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
const varDecls = ["$schema_slug: String!"];
|
|
403
|
+
if ("limit" in queryVariables) varDecls.push("$limit: Float");
|
|
404
|
+
if ("offset" in queryVariables) varDecls.push("$offset: Float");
|
|
405
|
+
if ("status" in queryVariables) varDecls.push("$status: ContentStatus");
|
|
406
|
+
if ("filter" in queryVariables) varDecls.push("$filter: JSONObject");
|
|
407
|
+
const varBlock = varDecls.length > 0 ? `(
|
|
408
|
+
${varDecls.join("\n ")}
|
|
409
|
+
)` : "";
|
|
410
|
+
const contentFieldArgs = fieldArgParts.join(", ");
|
|
411
|
+
const queryStr = isSingle ? `
|
|
412
|
+
query Get${schema_slug}Entry($schema_slug: String!, $slug: String!) {
|
|
413
|
+
entry: contentEntry(schema_slug: $schema_slug, slug: $slug) {
|
|
414
|
+
${selection}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
` : `
|
|
418
|
+
query Get${schema_slug}Entries${varBlock} {
|
|
419
|
+
entries: contentEntries(${contentFieldArgs}) {
|
|
420
|
+
${selection}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
`;
|
|
424
|
+
return {
|
|
425
|
+
query: client.gql(queryStr),
|
|
426
|
+
variables: queryVariables,
|
|
427
|
+
isSingle
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// src/fetchCmsContent.ts
|
|
432
|
+
async function fetchCmsContent(getClient, opts) {
|
|
433
|
+
const { query, variables, isSingle } = buildCmsQuery(opts);
|
|
434
|
+
const { data } = await getClient().query({ query, variables });
|
|
435
|
+
return isSingle ? data.entry : data.entries;
|
|
436
|
+
}
|
|
314
437
|
|
|
315
438
|
// src/utils/cmsImage.ts
|
|
316
439
|
function cmsImage(id, options) {
|
|
@@ -1487,8 +1610,10 @@ function RichTextContent({
|
|
|
1487
1610
|
|
|
1488
1611
|
exports.AsteroidCMSProvider = AsteroidCMSProvider;
|
|
1489
1612
|
exports.RichTextContent = RichTextContent;
|
|
1613
|
+
exports.buildCmsQuery = buildCmsQuery;
|
|
1490
1614
|
exports.cmsImage = cmsImage;
|
|
1491
1615
|
exports.createApolloClient = createApolloClient;
|
|
1616
|
+
exports.fetchCmsContent = fetchCmsContent;
|
|
1492
1617
|
exports.getContentReadTime = getContentReadTime;
|
|
1493
1618
|
exports.parseRichText = parseRichText;
|
|
1494
1619
|
exports.removeEmptyParagraphs = removeEmptyParagraphs;
|