@asteroidcms/core-utils 0.1.4 → 0.1.6

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/index.d.cts CHANGED
@@ -41,15 +41,15 @@ declare module "@apollo/client" {
41
41
  }
42
42
  declare function createApolloClient(config: AsteroidCMSConfig): ApolloClient;
43
43
 
44
- type FieldSelector = string | {
44
+ type FieldSelector$1 = string | {
45
45
  field: string;
46
46
  as?: string;
47
47
  };
48
- type ReferenceExpansion = {
48
+ type ReferenceExpansion$1 = {
49
49
  field: string;
50
50
  as?: string;
51
51
  single?: boolean;
52
- select?: readonly (FieldSelector | ReferenceExpansion)[];
52
+ select?: readonly (FieldSelector$1 | ReferenceExpansion$1)[];
53
53
  };
54
54
  type ContentStatus = "DRAFT" | "PUBLISHED" | "ARCHIVED";
55
55
  type CmsSearchCondition = {
@@ -60,7 +60,7 @@ type CmsSearchCondition = {
60
60
  type UseCmsContentOptions = {
61
61
  schema_slug: string;
62
62
  entrySlug?: string;
63
- select?: readonly (FieldSelector | ReferenceExpansion)[];
63
+ select?: readonly (FieldSelector$1 | ReferenceExpansion$1)[];
64
64
  fullData?: boolean;
65
65
  limit?: number;
66
66
  offset?: number;
@@ -75,7 +75,41 @@ declare function buildCmsQuery({ schema_slug, entrySlug, select, fullData, limit
75
75
  isSingle: boolean;
76
76
  };
77
77
 
78
- declare function fetchCmsContent<T>(getClient: () => ApolloClient, opts: UseCmsContentOptions): Promise<T>;
78
+ type AnyApolloClient$1 = {
79
+ query: (...args: any[]) => any;
80
+ };
81
+ declare function fetchCmsContent<T>(getClient: () => AnyApolloClient$1, opts: UseCmsContentOptions): Promise<T>;
82
+
83
+ type MutationType = "create" | "update" | "delete";
84
+ type FieldSelector = string | {
85
+ field: string;
86
+ as?: string;
87
+ single?: boolean;
88
+ select?: readonly (FieldSelector | ReferenceExpansion)[];
89
+ };
90
+ type ReferenceExpansion = {
91
+ field: string;
92
+ as?: string;
93
+ single?: boolean;
94
+ select?: readonly (FieldSelector | ReferenceExpansion)[];
95
+ };
96
+ type CmsMutateOptions = {
97
+ schema_slug: string;
98
+ select?: readonly (FieldSelector | ReferenceExpansion)[];
99
+ fullData?: boolean;
100
+ mutationType?: MutationType;
101
+ entryId?: string;
102
+ variables?: Record<string, unknown>;
103
+ };
104
+ declare function buildCmsMutation({ schema_slug, select, fullData, mutationType, entryId, variables, }: CmsMutateOptions): {
105
+ mutation: DocumentNode;
106
+ variables: Record<string, unknown>;
107
+ };
108
+
109
+ type AnyApolloClient = {
110
+ mutate: (...args: any[]) => any;
111
+ };
112
+ declare function cmsMutate<T>(getClient: () => AnyApolloClient, opts: CmsMutateOptions): Promise<T>;
79
113
 
80
114
  /**
81
115
  * Build a canonical media URL from an asset id. Pass `cmsUrl` explicitly when
@@ -132,7 +166,7 @@ declare function getContentReadTime(content: string, options?: GetContentReadTim
132
166
  *
133
167
  * Idempotent: parseRichText(parseRichText(x, opts), opts) === parseRichText(x, opts).
134
168
  */
135
- type RichTextClassKey = "p" | "br" | "hr" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "ul" | "ol" | "li" | "blockquote" | "pre" | "code" | "inlineCode" | "a" | "strong" | "em" | "u" | "s" | "kbd" | "table" | "tableWrapper" | "thead" | "tbody" | "tr" | "th" | "td" | "figure" | "figcaption" | "img" | "span" | "callout" | "calloutTitle";
169
+ type RichTextClassKey = "p" | "br" | "hr" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "ul" | "ol" | "li" | "blockquote" | "pre" | "code" | "inlineCode" | "a" | "strong" | "em" | "u" | "s" | "kbd" | "table" | "tableWrapper" | "thead" | "tbody" | "tr" | "th" | "td" | "figure" | "figcaption" | "img" | "span" | "callout" | "calloutTitle" | "collapsible" | "collapsibleTitle";
136
170
  type RichTextClassMap = Partial<Record<RichTextClassKey, string>> & {
137
171
  /** Variant overrides keyed as `${matchKey}:${variant}`, e.g. "callout:warning". */
138
172
  variants?: Record<string, string>;
@@ -193,4 +227,4 @@ declare function extractHeadingsFromElement(root: HTMLElement, options?: Extract
193
227
  scrollMarginTop?: number;
194
228
  }): ExtractedHeading[];
195
229
 
196
- export { type AsteroidCMSConfig, type CmsSearchCondition, type ContentStatus, type ExtractHeadingsOptions, type ExtractedHeading, type FieldSelector, type HeadingLevel, type ParseRichTextOptions, type ReferenceExpansion, type ResolvedAsteroidCMSConfig, type RichTextClassKey, type RichTextClassMap, type UseCmsContentOptions, buildCmsQuery, cmsImage, createApolloClient, extractHeadingsFromElement, extractHeadingsFromHtml, fetchCmsContent, getContentReadTime, parseRichText, removeEmptyParagraphs, slugify };
230
+ export { type AsteroidCMSConfig, type CmsMutateOptions, type CmsSearchCondition, type ContentStatus, type ExtractHeadingsOptions, type ExtractedHeading, type FieldSelector$1 as FieldSelector, type HeadingLevel, type MutationType, type ParseRichTextOptions, type ReferenceExpansion$1 as ReferenceExpansion, type ResolvedAsteroidCMSConfig, type RichTextClassKey, type RichTextClassMap, type UseCmsContentOptions, buildCmsMutation, buildCmsQuery, cmsImage, cmsMutate, createApolloClient, extractHeadingsFromElement, extractHeadingsFromHtml, fetchCmsContent, getContentReadTime, parseRichText, removeEmptyParagraphs, slugify };
package/dist/index.d.ts CHANGED
@@ -41,15 +41,15 @@ declare module "@apollo/client" {
41
41
  }
42
42
  declare function createApolloClient(config: AsteroidCMSConfig): ApolloClient;
43
43
 
44
- type FieldSelector = string | {
44
+ type FieldSelector$1 = string | {
45
45
  field: string;
46
46
  as?: string;
47
47
  };
48
- type ReferenceExpansion = {
48
+ type ReferenceExpansion$1 = {
49
49
  field: string;
50
50
  as?: string;
51
51
  single?: boolean;
52
- select?: readonly (FieldSelector | ReferenceExpansion)[];
52
+ select?: readonly (FieldSelector$1 | ReferenceExpansion$1)[];
53
53
  };
54
54
  type ContentStatus = "DRAFT" | "PUBLISHED" | "ARCHIVED";
55
55
  type CmsSearchCondition = {
@@ -60,7 +60,7 @@ type CmsSearchCondition = {
60
60
  type UseCmsContentOptions = {
61
61
  schema_slug: string;
62
62
  entrySlug?: string;
63
- select?: readonly (FieldSelector | ReferenceExpansion)[];
63
+ select?: readonly (FieldSelector$1 | ReferenceExpansion$1)[];
64
64
  fullData?: boolean;
65
65
  limit?: number;
66
66
  offset?: number;
@@ -75,7 +75,41 @@ declare function buildCmsQuery({ schema_slug, entrySlug, select, fullData, limit
75
75
  isSingle: boolean;
76
76
  };
77
77
 
78
- declare function fetchCmsContent<T>(getClient: () => ApolloClient, opts: UseCmsContentOptions): Promise<T>;
78
+ type AnyApolloClient$1 = {
79
+ query: (...args: any[]) => any;
80
+ };
81
+ declare function fetchCmsContent<T>(getClient: () => AnyApolloClient$1, opts: UseCmsContentOptions): Promise<T>;
82
+
83
+ type MutationType = "create" | "update" | "delete";
84
+ type FieldSelector = string | {
85
+ field: string;
86
+ as?: string;
87
+ single?: boolean;
88
+ select?: readonly (FieldSelector | ReferenceExpansion)[];
89
+ };
90
+ type ReferenceExpansion = {
91
+ field: string;
92
+ as?: string;
93
+ single?: boolean;
94
+ select?: readonly (FieldSelector | ReferenceExpansion)[];
95
+ };
96
+ type CmsMutateOptions = {
97
+ schema_slug: string;
98
+ select?: readonly (FieldSelector | ReferenceExpansion)[];
99
+ fullData?: boolean;
100
+ mutationType?: MutationType;
101
+ entryId?: string;
102
+ variables?: Record<string, unknown>;
103
+ };
104
+ declare function buildCmsMutation({ schema_slug, select, fullData, mutationType, entryId, variables, }: CmsMutateOptions): {
105
+ mutation: DocumentNode;
106
+ variables: Record<string, unknown>;
107
+ };
108
+
109
+ type AnyApolloClient = {
110
+ mutate: (...args: any[]) => any;
111
+ };
112
+ declare function cmsMutate<T>(getClient: () => AnyApolloClient, opts: CmsMutateOptions): Promise<T>;
79
113
 
80
114
  /**
81
115
  * Build a canonical media URL from an asset id. Pass `cmsUrl` explicitly when
@@ -132,7 +166,7 @@ declare function getContentReadTime(content: string, options?: GetContentReadTim
132
166
  *
133
167
  * Idempotent: parseRichText(parseRichText(x, opts), opts) === parseRichText(x, opts).
134
168
  */
135
- type RichTextClassKey = "p" | "br" | "hr" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "ul" | "ol" | "li" | "blockquote" | "pre" | "code" | "inlineCode" | "a" | "strong" | "em" | "u" | "s" | "kbd" | "table" | "tableWrapper" | "thead" | "tbody" | "tr" | "th" | "td" | "figure" | "figcaption" | "img" | "span" | "callout" | "calloutTitle";
169
+ type RichTextClassKey = "p" | "br" | "hr" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "ul" | "ol" | "li" | "blockquote" | "pre" | "code" | "inlineCode" | "a" | "strong" | "em" | "u" | "s" | "kbd" | "table" | "tableWrapper" | "thead" | "tbody" | "tr" | "th" | "td" | "figure" | "figcaption" | "img" | "span" | "callout" | "calloutTitle" | "collapsible" | "collapsibleTitle";
136
170
  type RichTextClassMap = Partial<Record<RichTextClassKey, string>> & {
137
171
  /** Variant overrides keyed as `${matchKey}:${variant}`, e.g. "callout:warning". */
138
172
  variants?: Record<string, string>;
@@ -193,4 +227,4 @@ declare function extractHeadingsFromElement(root: HTMLElement, options?: Extract
193
227
  scrollMarginTop?: number;
194
228
  }): ExtractedHeading[];
195
229
 
196
- export { type AsteroidCMSConfig, type CmsSearchCondition, type ContentStatus, type ExtractHeadingsOptions, type ExtractedHeading, type FieldSelector, type HeadingLevel, type ParseRichTextOptions, type ReferenceExpansion, type ResolvedAsteroidCMSConfig, type RichTextClassKey, type RichTextClassMap, type UseCmsContentOptions, buildCmsQuery, cmsImage, createApolloClient, extractHeadingsFromElement, extractHeadingsFromHtml, fetchCmsContent, getContentReadTime, parseRichText, removeEmptyParagraphs, slugify };
230
+ export { type AsteroidCMSConfig, type CmsMutateOptions, type CmsSearchCondition, type ContentStatus, type ExtractHeadingsOptions, type ExtractedHeading, type FieldSelector$1 as FieldSelector, type HeadingLevel, type MutationType, type ParseRichTextOptions, type ReferenceExpansion$1 as ReferenceExpansion, type ResolvedAsteroidCMSConfig, type RichTextClassKey, type RichTextClassMap, type UseCmsContentOptions, buildCmsMutation, buildCmsQuery, cmsImage, cmsMutate, createApolloClient, extractHeadingsFromElement, extractHeadingsFromHtml, fetchCmsContent, getContentReadTime, parseRichText, removeEmptyParagraphs, slugify };
package/dist/index.js CHANGED
@@ -185,6 +185,94 @@ async function fetchCmsContent(getClient, opts) {
185
185
  const { data } = await getClient().query({ query, variables });
186
186
  return isSingle ? data.entry : data.entries;
187
187
  }
188
+ function buildSelection2(items = []) {
189
+ const lines = [];
190
+ for (const item of items) {
191
+ if (typeof item === "string") {
192
+ lines.push(`${item}: dataField(slug: "${item}")`);
193
+ continue;
194
+ }
195
+ const { field, as, single, select: subSelect } = item;
196
+ const alias = as || field;
197
+ if (!subSelect?.length) {
198
+ lines.push(`${alias}: dataField(slug: "${field}")`);
199
+ continue;
200
+ }
201
+ const resolver = single ? "expandedReferenceObject" : "expandedReference";
202
+ const sub = buildSelection2(subSelect) || "id slug";
203
+ lines.push(`
204
+ ${alias}: ${resolver}(slug: "${field}") {
205
+ id
206
+ slug
207
+ ${sub}
208
+ }
209
+ `);
210
+ }
211
+ return lines.join("\n ").trim();
212
+ }
213
+ function buildCmsMutation({
214
+ schema_slug,
215
+ select = [],
216
+ fullData = false,
217
+ mutationType = "create",
218
+ entryId,
219
+ variables = {}
220
+ }) {
221
+ const userSelection = buildSelection2(select);
222
+ let selection = fullData ? "data" : "";
223
+ if (userSelection) {
224
+ selection = selection ? `${selection}
225
+ ${userSelection}` : userSelection;
226
+ }
227
+ if (!selection) {
228
+ selection = "id slug status version created_at updated_at";
229
+ }
230
+ const mutationName = `${mutationType}ContentEntry`;
231
+ const varDecls = ["$schema_slug: String!"];
232
+ const callArgs = ["schema_slug: $schema_slug"];
233
+ if (mutationType === "create" || mutationType === "update") {
234
+ varDecls.unshift("$data: JSONObject!");
235
+ callArgs.unshift("data: $data");
236
+ }
237
+ if (mutationType === "update" || mutationType === "delete") {
238
+ varDecls.push("$id: ID!");
239
+ callArgs.push("id: $id");
240
+ }
241
+ const returnFields = selection.includes("data") ? selection : `${selection}
242
+ data`;
243
+ const mutationStr = `
244
+ mutation ${mutationName}(${varDecls.join(", ")}) {
245
+ result: ${mutationName}(${callArgs.join(", ")}) {
246
+ id
247
+ status
248
+ version
249
+ created_at
250
+ updated_at
251
+ schema {
252
+ id
253
+ slug
254
+ }
255
+ ${returnFields}
256
+ }
257
+ }
258
+ `;
259
+ const mutationVariables = {
260
+ schema_slug,
261
+ ...mutationType === "update" || mutationType === "delete" ? { id: entryId } : {},
262
+ ...variables
263
+ };
264
+ return {
265
+ mutation: gql(mutationStr),
266
+ variables: mutationVariables
267
+ };
268
+ }
269
+
270
+ // src/cmsMutate.ts
271
+ async function cmsMutate(getClient, opts) {
272
+ const { mutation, variables } = buildCmsMutation(opts);
273
+ const { data } = await getClient().mutate({ mutation, variables });
274
+ return data.result;
275
+ }
188
276
 
189
277
  // src/utils/cmsImage.ts
190
278
  function cmsImage(id, options) {
@@ -270,7 +358,9 @@ var DEFAULT_ALLOWLIST = [
270
358
  "section",
271
359
  "article",
272
360
  "div",
273
- "span"
361
+ "span",
362
+ "details",
363
+ "summary"
274
364
  ];
275
365
  var ALLOWED_ATTRS = {
276
366
  a: ["href", "title", "target", "rel"],
@@ -280,7 +370,8 @@ var ALLOWED_ATTRS = {
280
370
  col: ["span", "width"],
281
371
  colgroup: ["span"],
282
372
  table: ["border", "cellpadding", "cellspacing"],
283
- span: ["style"]
373
+ span: ["style"],
374
+ details: ["open"]
284
375
  };
285
376
  var ALLOWED_STYLE_PROPS = {
286
377
  span: ["font-size"]
@@ -314,7 +405,8 @@ var GLOBAL_ALLOWED_ATTRS = [
314
405
  "data-title",
315
406
  "data-callout-title",
316
407
  "data-language",
317
- "data-filename"
408
+ "data-filename",
409
+ "data-icon"
318
410
  ];
319
411
  var URL_ATTRS = /* @__PURE__ */ new Set(["href", "src"]);
320
412
  function parseRichText(html, options = {}) {
@@ -728,6 +820,8 @@ function classKeyForTag(tag, attrs, openStack) {
728
820
  if (tag === "p" && "data-callout-title" in attrs) return "calloutTitle";
729
821
  if (tag === "code" && openStack[openStack.length - 1] !== "pre")
730
822
  return "inlineCode";
823
+ if (tag === "details") return "collapsible";
824
+ if (tag === "summary") return "collapsibleTitle";
731
825
  const known = [
732
826
  "p",
733
827
  "br",
@@ -968,6 +1062,6 @@ function extractHeadingsFromElement(root, options = {}) {
968
1062
  return out;
969
1063
  }
970
1064
 
971
- export { buildCmsQuery, cmsImage, createApolloClient, extractHeadingsFromElement, extractHeadingsFromHtml, fetchCmsContent, getContentReadTime, parseRichText, removeEmptyParagraphs, slugify };
1065
+ export { buildCmsMutation, buildCmsQuery, cmsImage, cmsMutate, createApolloClient, extractHeadingsFromElement, extractHeadingsFromHtml, fetchCmsContent, getContentReadTime, parseRichText, removeEmptyParagraphs, slugify };
972
1066
  //# sourceMappingURL=index.js.map
973
1067
  //# sourceMappingURL=index.js.map