@asteroidcms/core-utils 0.1.5 → 0.1.7

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
@@ -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
@@ -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) {
@@ -974,6 +1062,6 @@ function extractHeadingsFromElement(root, options = {}) {
974
1062
  return out;
975
1063
  }
976
1064
 
977
- 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 };
978
1066
  //# sourceMappingURL=index.js.map
979
1067
  //# sourceMappingURL=index.js.map