@dosgato/templating 0.0.29 → 0.0.32

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,4 +1,4 @@
1
- import { PageRecord, ComponentData } from './component.js';
1
+ import { PageRecord, ComponentData, DataData, PageData } from './component.js';
2
2
  import { LinkDefinition } from './links.js';
3
3
  export declare type APITemplateType = 'page' | 'component' | 'data';
4
4
  export declare enum ValidationMessageType {
@@ -25,18 +25,6 @@ export interface APITemplate {
25
25
  * A uniquey human-readable name describing this template
26
26
  */
27
27
  name: string;
28
- /**
29
- * Each template must declare its areas and the template keys of components that will be
30
- * permitted inside each area. The list of allowed component templates can be updated beyond
31
- * the list provided here. See templateRegistry.addAvailableComponent's comment for info on why.
32
- */
33
- areas: Record<string, string[]>;
34
- /**
35
- * Each template must provide a list of migrations for upgrading the data schema over time.
36
- * Typically this will start as an empty array and migrations will be added as the template
37
- * gets refactored.
38
- */
39
- migrations: Migration[];
40
28
  /**
41
29
  * Each template must provide a function that returns links from its data so that they
42
30
  * can be indexed. Only fields that are links need to be returned. Links inside rich editor
@@ -49,6 +37,15 @@ export interface APITemplate {
49
37
  * by this function will also be scanned for links.
50
38
  */
51
39
  getFulltext: FulltextGatheringFn;
40
+ }
41
+ export interface APIComponentTemplate extends APITemplate {
42
+ type: 'component';
43
+ /**
44
+ * Each template must declare its areas and the template keys of components that will be
45
+ * permitted inside each area. The list of allowed component templates can be updated beyond
46
+ * the list provided here. See templateRegistry.addAvailableComponent's comment for info on why.
47
+ */
48
+ areas?: Record<string, string[]>;
52
49
  /**
53
50
  * Each template must provide a validation function so that the API can enforce its data is
54
51
  * shaped properly.
@@ -66,7 +63,25 @@ export interface APITemplate {
66
63
  * available as parameters in case you need them. Keep in mind that the current editor MUST
67
64
  * have access to any data you attempt to query in GraphQL.
68
65
  */
69
- validate?: (data: ComponentData, query: <T>(query: string, variables?: any) => Promise<T>, page: PageRecord, path: string) => Promise<ValidationFeedback[]>;
66
+ validate?: (data: ComponentData, query: GraphQLQueryFn, page: PageRecord, path: string) => Promise<ValidationFeedback[]>;
67
+ /**
68
+ * Each template must provide a list of migrations for upgrading the data schema over time.
69
+ * Typically this will start as an empty array and migrations will be added as the template
70
+ * gets refactored.
71
+ */
72
+ migrations: ComponentMigration[];
73
+ }
74
+ export interface APIPageTemplate extends APITemplate {
75
+ type: 'page';
76
+ /**
77
+ * Page areas are the same as components but are required.
78
+ */
79
+ areas: Record<string, string[]>;
80
+ /**
81
+ * Page template implementations do not receive a path like component templates do.
82
+ */
83
+ validate?: (data: ComponentData, query: GraphQLQueryFn, page: PageRecord) => Promise<ValidationFeedback[]>;
84
+ migrations: PageMigration[];
70
85
  /**
71
86
  * Hard-coded properties that may be set on page templates to influence the rendering of
72
87
  * components on the page. For instance, a set of color choices that are customized for
@@ -78,6 +93,17 @@ export interface APITemplate {
78
93
  */
79
94
  templateProperties?: any;
80
95
  }
96
+ export interface APIDataTemplate extends APITemplate {
97
+ type: 'data';
98
+ /**
99
+ * Data template implementations receive the id of the dataroot the data is/will be inside,
100
+ * as well as the folder id (if applicable) and their own id. Keep in mind dataId will be
101
+ * null when it is a creation operation.
102
+ */
103
+ validate?: (data: ComponentData, query: GraphQLQueryFn, dataRootId: string, dataFolderId?: string, dataId?: string) => Promise<ValidationFeedback[]>;
104
+ migrations: DataMigration[];
105
+ }
106
+ export declare type APIAnyTemplate = APIComponentTemplate | APIPageTemplate | APIDataTemplate;
81
107
  /**
82
108
  * In dosgato CMS, the data in the database is not altered except during user activity. This
83
109
  * means that older records could have been saved when the schema expected by component
@@ -97,14 +123,34 @@ export interface APITemplate {
97
123
  *
98
124
  * Your `up` and `down` methods will be applied to components in bottom-up fashion, so you
99
125
  * can assume that any components inside one of your areas has already been processed.
126
+ *
127
+ * All migration functions receive a `query` function for making a graphql query, in case the
128
+ * migration depends on the state of a parent page or something. Be careful not
129
+ * to create an infinite loop - querying a page will trigger that page to be migrated, which
130
+ * could end up calling your code again on that page.
131
+ *
132
+ * If you're migrating a component template, you'll also get the page record and the
133
+ * path inside that page's data to the component being migrated.
100
134
  */
101
135
  export interface Migration {
102
136
  createdAt: Date;
103
- up: (data: ComponentData, page: PageRecord) => ComponentData | Promise<ComponentData>;
104
- down: (data: ComponentData, page: PageRecord) => ComponentData | Promise<ComponentData>;
105
137
  }
138
+ export interface ComponentMigration extends Migration {
139
+ up: (data: ComponentData, query: GraphQLQueryFn, page: PageRecord, path: string) => ComponentData | Promise<ComponentData>;
140
+ down: (data: ComponentData, query: GraphQLQueryFn, page: PageRecord, path: string) => ComponentData | Promise<ComponentData>;
141
+ }
142
+ export interface PageMigration extends Migration {
143
+ up: (data: PageData, query: GraphQLQueryFn, page: PageRecord) => PageData | Promise<PageData>;
144
+ down: (data: PageData, query: GraphQLQueryFn, page: PageRecord) => PageData | Promise<PageData>;
145
+ }
146
+ export interface DataMigration extends Migration {
147
+ up: (data: DataData, query: GraphQLQueryFn, dataId: string) => DataData | Promise<DataData>;
148
+ down: (data: DataData, query: GraphQLQueryFn, dataId: string) => DataData | Promise<DataData>;
149
+ }
150
+ export declare type AnyMigration = ComponentMigration | PageMigration | DataMigration;
106
151
  export declare type LinkGatheringFn = (data: any) => LinkDefinition[];
107
152
  export declare type FulltextGatheringFn = (data: any) => string[];
153
+ export declare type GraphQLQueryFn = <T>(query: string, variables?: any) => Promise<T>;
108
154
  /**
109
155
  * This function is used by API template definitions to help them identify links inside large blocks
110
156
  * of text and return them for indexing.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dosgato/templating",
3
- "version": "0.0.29",
3
+ "version": "0.0.32",
4
4
  "description": "A library to support building templates for dosgato CMS.",
5
5
  "type": "module",
6
6
  "exports": {