@panneau/protocol 5.0.0-alpha.4 → 5.0.0-alpha.42

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.
Files changed (159) hide show
  1. package/dist/addresses.d.ts +23 -0
  2. package/dist/addresses.d.ts.map +1 -0
  3. package/dist/addresses.js +52 -0
  4. package/dist/addresses.js.map +1 -0
  5. package/dist/aggregates.d.ts +135 -0
  6. package/dist/aggregates.d.ts.map +1 -0
  7. package/dist/aggregates.js +236 -0
  8. package/dist/aggregates.js.map +1 -0
  9. package/dist/compound.d.ts +20 -0
  10. package/dist/compound.d.ts.map +1 -0
  11. package/dist/compound.js +27 -0
  12. package/dist/compound.js.map +1 -0
  13. package/dist/conditions.d.ts +1 -1
  14. package/dist/conformance/index.d.ts +6 -0
  15. package/dist/conformance/index.d.ts.map +1 -1
  16. package/dist/conformance/index.js +970 -11
  17. package/dist/conformance/index.js.map +1 -1
  18. package/dist/constants.d.ts +108 -2
  19. package/dist/constants.d.ts.map +1 -1
  20. package/dist/constants.js +181 -3
  21. package/dist/constants.js.map +1 -1
  22. package/dist/context.d.ts +15 -0
  23. package/dist/context.d.ts.map +1 -0
  24. package/dist/context.js +34 -0
  25. package/dist/context.js.map +1 -0
  26. package/dist/currencies.d.ts +22 -0
  27. package/dist/currencies.d.ts.map +1 -0
  28. package/dist/currencies.js +231 -0
  29. package/dist/currencies.js.map +1 -0
  30. package/dist/dashboards.d.ts +95 -0
  31. package/dist/dashboards.d.ts.map +1 -0
  32. package/dist/dashboards.js +140 -0
  33. package/dist/dashboards.js.map +1 -0
  34. package/dist/index.d.ts +4 -4
  35. package/dist/index.js +22 -8
  36. package/dist/libraries.d.ts +33 -0
  37. package/dist/libraries.d.ts.map +1 -0
  38. package/dist/libraries.js +59 -0
  39. package/dist/libraries.js.map +1 -0
  40. package/dist/links.d.ts +38 -0
  41. package/dist/links.d.ts.map +1 -0
  42. package/dist/links.js +65 -0
  43. package/dist/links.js.map +1 -0
  44. package/dist/lint.d.ts +4 -4
  45. package/dist/lint.d.ts.map +1 -1
  46. package/dist/lint.js +540 -15
  47. package/dist/lint.js.map +1 -1
  48. package/dist/mock/auth.d.ts +71 -0
  49. package/dist/mock/auth.d.ts.map +1 -0
  50. package/dist/mock/auth.js +346 -0
  51. package/dist/mock/auth.js.map +1 -0
  52. package/dist/mock/example.d.ts +1 -1
  53. package/dist/mock/example.d.ts.map +1 -1
  54. package/dist/mock/example.js +49 -28
  55. package/dist/mock/example.js.map +1 -1
  56. package/dist/mock/index.d.ts +96 -5
  57. package/dist/mock/index.d.ts.map +1 -1
  58. package/dist/mock/index.js +505 -44
  59. package/dist/mock/index.js.map +1 -1
  60. package/dist/openapi.d.ts.map +1 -1
  61. package/dist/openapi.js +462 -50
  62. package/dist/openapi.js.map +1 -1
  63. package/dist/periods.d.ts +61 -0
  64. package/dist/periods.d.ts.map +1 -0
  65. package/dist/periods.js +130 -0
  66. package/dist/periods.js.map +1 -0
  67. package/dist/problem.d.ts +1 -1
  68. package/dist/query.d.ts +18 -0
  69. package/dist/query.d.ts.map +1 -1
  70. package/dist/query.js +34 -8
  71. package/dist/query.js.map +1 -1
  72. package/dist/resourceTypes.d.ts +39 -0
  73. package/dist/resourceTypes.d.ts.map +1 -0
  74. package/dist/resourceTypes.js +34 -0
  75. package/dist/resourceTypes.js.map +1 -0
  76. package/dist/rules.d.ts +18 -5
  77. package/dist/rules.d.ts.map +1 -1
  78. package/dist/rules.js +233 -15
  79. package/dist/rules.js.map +1 -1
  80. package/dist/runtime/index.d.ts +23 -10
  81. package/dist/runtime/index.d.ts.map +1 -1
  82. package/dist/runtime/index.js +16 -3
  83. package/dist/schemas/action.d.ts +119 -10
  84. package/dist/schemas/action.d.ts.map +1 -1
  85. package/dist/schemas/action.js +20 -3
  86. package/dist/schemas/action.js.map +1 -1
  87. package/dist/schemas/api.d.ts +266 -5
  88. package/dist/schemas/api.d.ts.map +1 -1
  89. package/dist/schemas/api.js +134 -16
  90. package/dist/schemas/api.js.map +1 -1
  91. package/dist/schemas/boot.d.ts +25 -1
  92. package/dist/schemas/boot.d.ts.map +1 -1
  93. package/dist/schemas/boot.js +2 -1
  94. package/dist/schemas/boot.js.map +1 -1
  95. package/dist/schemas/common.d.ts +1 -0
  96. package/dist/schemas/common.d.ts.map +1 -1
  97. package/dist/schemas/common.js +2 -1
  98. package/dist/schemas/common.js.map +1 -1
  99. package/dist/schemas/definition.d.ts +1414 -30
  100. package/dist/schemas/definition.d.ts.map +1 -1
  101. package/dist/schemas/definition.js +77 -17
  102. package/dist/schemas/definition.js.map +1 -1
  103. package/dist/schemas/field.d.ts +358 -2
  104. package/dist/schemas/field.d.ts.map +1 -1
  105. package/dist/schemas/field.js +161 -20
  106. package/dist/schemas/field.js.map +1 -1
  107. package/dist/schemas/index.d.ts +5820 -46
  108. package/dist/schemas/index.d.ts.map +1 -1
  109. package/dist/schemas/index.js +69 -7
  110. package/dist/schemas/index.js.map +1 -1
  111. package/dist/schemas/jsonRules.d.ts +83 -0
  112. package/dist/schemas/jsonRules.d.ts.map +1 -0
  113. package/dist/schemas/jsonRules.js +46 -0
  114. package/dist/schemas/jsonRules.js.map +1 -0
  115. package/dist/schemas/layout.d.ts +2 -1
  116. package/dist/schemas/layout.d.ts.map +1 -1
  117. package/dist/schemas/layout.js +2 -1
  118. package/dist/schemas/layout.js.map +1 -1
  119. package/dist/schemas/list.d.ts +34 -2
  120. package/dist/schemas/list.d.ts.map +1 -1
  121. package/dist/schemas/list.js +21 -6
  122. package/dist/schemas/list.js.map +1 -1
  123. package/dist/schemas/resource.d.ts +826 -17
  124. package/dist/schemas/resource.d.ts.map +1 -1
  125. package/dist/schemas/resource.js +26 -4
  126. package/dist/schemas/resource.js.map +1 -1
  127. package/dist/schemas/theme.d.ts +6 -2
  128. package/dist/schemas/theme.d.ts.map +1 -1
  129. package/dist/schemas/theme.js +1 -0
  130. package/dist/schemas/theme.js.map +1 -1
  131. package/dist/schemas/widget.d.ts +4379 -0
  132. package/dist/schemas/widget.d.ts.map +1 -0
  133. package/dist/schemas/widget.js +269 -0
  134. package/dist/schemas/widget.js.map +1 -0
  135. package/dist/search.d.ts +51 -0
  136. package/dist/search.d.ts.map +1 -0
  137. package/dist/search.js +53 -0
  138. package/dist/search.js.map +1 -0
  139. package/dist/themes.d.ts +1 -1
  140. package/dist/transforms.d.ts +17 -0
  141. package/dist/transforms.d.ts.map +1 -0
  142. package/dist/transforms.js +34 -0
  143. package/dist/transforms.js.map +1 -0
  144. package/dist/validate.d.ts +5 -3
  145. package/dist/validate.d.ts.map +1 -1
  146. package/dist/validate.js +36 -2
  147. package/dist/validate.js.map +1 -1
  148. package/dist/values.d.ts +5 -1
  149. package/dist/values.d.ts.map +1 -1
  150. package/dist/values.js +16 -2
  151. package/dist/values.js.map +1 -1
  152. package/dist/widgets.d.ts +232 -0
  153. package/dist/widgets.d.ts.map +1 -0
  154. package/dist/widgets.js +630 -0
  155. package/dist/widgets.js.map +1 -0
  156. package/package.json +6 -3
  157. package/schema/boot.schema.json +1461 -104
  158. package/schema/definition.schema.json +1459 -105
  159. package/schema/openapi.json +7176 -2000
package/dist/lint.js CHANGED
@@ -1,3 +1,7 @@
1
+ import { ITEM_TYPE_KEY } from "./resourceTypes.js";
2
+ import { GROUPED_WIDGET_TYPES, RESOURCE_WIDGET_TYPES, WIDGET_RESERVED_QUERY_PARAMS, getWidgetResource, getWidgetSources, isBuiltInWidgetType } from "./widgets.js";
3
+ import { parseAggregateGroup, parseAggregateMeasure } from "./aggregates.js";
4
+ import { DASHBOARD_COMPONENT, HOME_PAGE, getSummaryPeriodFilter } from "./dashboards.js";
1
5
  //#region src/lint.ts
2
6
  function fieldNames(fields) {
3
7
  return new Set(fields.map((field) => field.name));
@@ -26,19 +30,141 @@ function checkLayout(nodes, names, path, issues) {
26
30
  if (node.children !== void 0) checkLayout(node.children, names, `${nodePath}.children`, issues);
27
31
  });
28
32
  }
33
+ /**
34
+ * The types of a resource (decision 0077): unique ids, fields named apart from those of the
35
+ * resource, and no field `type` in the form, which holds the item's type.
36
+ */
37
+ function checkTypes(resource, names, path, issues) {
38
+ const ids = /* @__PURE__ */ new Set();
39
+ (resource.types ?? []).forEach((type, index) => {
40
+ const typePath = `${path}.types.${index}`;
41
+ if (ids.has(type.id)) issues.push({
42
+ path: `${typePath}.id`,
43
+ message: `Duplicate type id \`${type.id}\`.`
44
+ });
45
+ ids.add(type.id);
46
+ if (fieldNames(type.fields).size !== type.fields.length) issues.push({
47
+ path: `${typePath}.fields`,
48
+ message: "Field names must be unique."
49
+ });
50
+ type.fields.forEach((field, fieldIndex) => {
51
+ if (names.has(field.name)) issues.push({
52
+ path: `${typePath}.fields.${fieldIndex}.name`,
53
+ message: `The resource already has a field named \`${field.name}\`.`
54
+ });
55
+ else if (field.name === "type") issues.push({
56
+ path: `${typePath}.fields.${fieldIndex}.name`,
57
+ message: `\`${ITEM_TYPE_KEY}\` holds the type of the item.`
58
+ });
59
+ });
60
+ });
61
+ const typeField = resource.fields.findIndex((field) => field.name === ITEM_TYPE_KEY);
62
+ if (ids.size > 0 && typeField !== -1 && resource.fields[typeField]?.notInForm !== true) issues.push({
63
+ path: `${path}.fields.${typeField}`,
64
+ message: `\`${ITEM_TYPE_KEY}\` holds the type of the item, set when it is created: its field must be \`notInForm\`.`
65
+ });
66
+ }
67
+ /** The fields of a resource and of its types, with their paths. */
68
+ function allFields(resource, path) {
69
+ return [...resource.fields.map((field, index) => ({
70
+ field,
71
+ path: `${path}.fields.${index}`
72
+ })), ...(resource.types ?? []).flatMap((type, typeIndex) => type.fields.map((field, index) => ({
73
+ field,
74
+ path: `${path}.types.${typeIndex}.fields.${index}`
75
+ })))];
76
+ }
77
+ /** Field types whose values can be summed: numbers, and amounts of `money` of one currency. */
78
+ var SUMMABLE_TYPES = [
79
+ "number",
80
+ "range",
81
+ "duration",
82
+ "money"
83
+ ];
84
+ /** Field types whose days can be grouped by. */
85
+ var DATE_TYPES = ["date", "datetime"];
86
+ /** Field types whose values are structures that can't be grouped by. */
87
+ var UNGROUPABLE_TYPES = [
88
+ "richtext",
89
+ "media",
90
+ "image",
91
+ "file",
92
+ "group",
93
+ "repeater",
94
+ "json",
95
+ "embed",
96
+ "location",
97
+ "address",
98
+ "link",
99
+ "daterange",
100
+ "money"
101
+ ];
102
+ /**
103
+ * The aggregates of a list (decision 0084): each one names a field of the resource that the user
104
+ * can view, `sum` a number or `money` of one currency, `dates` a date, `groupBy` a value.
105
+ */
106
+ function checkAggregates(resource, path, issues) {
107
+ const aggregates = resource.list?.aggregates;
108
+ if (aggregates === void 0) return;
109
+ const fields = allFields(resource, path).map(({ field }) => field);
110
+ const check = (key, accepts) => {
111
+ (aggregates[key] ?? []).forEach((name, index) => {
112
+ const issuePath = `${path}.list.aggregates.${key}.${index}`;
113
+ const field = fields.find((candidate) => candidate.name === name);
114
+ const message = field === void 0 ? `No field named \`${name}\`.` : field.abilities?.view === false ? `The user can't view \`${name}\`: it can't be aggregated.` : accepts(field);
115
+ if (message !== null) issues.push({
116
+ path: issuePath,
117
+ message
118
+ });
119
+ });
120
+ };
121
+ check("sum", (field) => {
122
+ if (!SUMMABLE_TYPES.includes(field.type)) return `\`${field.name}\` can't be summed: only \`number\`, \`range\`, \`duration\` and \`money\` fields can.`;
123
+ const currency = field.settings?.currency;
124
+ return field.type === "money" && typeof currency !== "string" ? `\`${field.name}\` can't be summed: amounts add up only in one \`currency\`.` : null;
125
+ });
126
+ check("dates", (field) => DATE_TYPES.includes(field.type) ? null : `\`${field.name}\` must be a \`date\` or a \`datetime\` to be grouped by period.`);
127
+ check("groupBy", (field) => UNGROUPABLE_TYPES.includes(field.type) ? `The items can't be grouped by a \`${field.type}\` field.` : null);
128
+ }
29
129
  function checkResource(resource, path, issues) {
30
130
  const names = fieldNames(resource.fields);
31
131
  if (names.size !== resource.fields.length) issues.push({
32
132
  path: `${path}.fields`,
33
133
  message: "Field names must be unique."
34
134
  });
35
- if (resource.form?.layout !== void 0) checkLayout(resource.form.layout, names, `${path}.form.layout`, issues);
135
+ checkTypes(resource, names, path, issues);
136
+ if (resource.form?.layout !== void 0) {
137
+ const typeNames = (resource.types ?? []).flatMap((type) => [...fieldNames(type.fields)]);
138
+ checkLayout(resource.form.layout, /* @__PURE__ */ new Set([...names, ...typeNames]), `${path}.form.layout`, issues);
139
+ }
36
140
  (resource.list?.columns ?? []).forEach((column, index) => {
37
141
  if (column.field !== void 0 && !names.has(column.field)) issues.push({
38
142
  path: `${path}.list.columns.${index}.field`,
39
143
  message: `No field named \`${column.field}\`.`
40
144
  });
41
145
  });
146
+ const calendar = resource.list?.calendar;
147
+ if (calendar !== void 0) {
148
+ const field = resource.fields.find((candidate) => candidate.name === calendar.field);
149
+ const filter = calendar.filter ?? calendar.field;
150
+ if (field === void 0) issues.push({
151
+ path: `${path}.list.calendar.field`,
152
+ message: `No field named \`${calendar.field}\`.`
153
+ });
154
+ else if (![
155
+ "date",
156
+ "datetime",
157
+ "daterange"
158
+ ].includes(field.type)) issues.push({
159
+ path: `${path}.list.calendar.field`,
160
+ message: "The field of a calendar must be a `date`, a `datetime` or a `daterange`."
161
+ });
162
+ if (!(resource.list?.filters ?? []).some((candidate) => candidate.id === filter && candidate.type === "date-range")) issues.push({
163
+ path: `${path}.list.calendar`,
164
+ message: `The calendar needs a \`date-range\` filter \`${filter}\` in the list.`
165
+ });
166
+ }
167
+ checkAggregates(resource, path, issues);
42
168
  (resource.actions ?? []).forEach((action, index) => {
43
169
  if (action.layout !== void 0) checkLayout(action.layout, fieldNames(action.fields ?? []), `${path}.actions.${index}.layout`, issues);
44
170
  });
@@ -54,17 +180,21 @@ function checkResource(resource, path, issues) {
54
180
  message: "The `file` field of a `media` resource must be a single `file`."
55
181
  });
56
182
  }
57
- resource.fields.forEach((field, index) => {
183
+ allFields(resource, path).forEach(({ field, path: fieldPath }) => {
58
184
  if (field.type === "relation" && field.resource === void 0) issues.push({
59
- path: `${path}.fields.${index}.resource`,
185
+ path: `${fieldPath}.resource`,
60
186
  message: "A `relation` field needs a `resource`."
61
187
  });
188
+ else if (field.type === "relation" && Array.isArray(field.resource)) issues.push({
189
+ path: `${fieldPath}.resource`,
190
+ message: "A `relation` field names one resource."
191
+ });
62
192
  });
63
193
  }
64
194
  /**
65
195
  * Checks what the schema can't: references between parts of a definition (layout nodes and
66
- * columns that name existing fields, relations and media fields that name a resource, unique
67
- * names), and the `file` field of `media` resources.
196
+ * columns that name existing fields, relations, `media` and `image` fields that name a resource,
197
+ * unique names), the types of resources, and the `file` field of `media` resources.
68
198
  */
69
199
  function lintDefinition(definition) {
70
200
  const issues = [];
@@ -72,27 +202,422 @@ function lintDefinition(definition) {
72
202
  const libraries = definition.resources.filter((resource) => resource.kind === "media").map((resource) => resource.id);
73
203
  definition.resources.forEach((resource, index) => {
74
204
  checkResource(resource, `resources.${index}`, issues);
75
- resource.fields.forEach((field, fieldIndex) => {
76
- if (field.type === "relation" && field.resource !== void 0 && !resourceIds.has(field.resource)) issues.push({
77
- path: `resources.${index}.fields.${fieldIndex}.resource`,
205
+ if (resource.summary !== void 0) {
206
+ const summaryPath = `resources.${index}.summary`;
207
+ const period = resource.summary.period;
208
+ if (period !== void 0) {
209
+ checkPeriod(period, `${summaryPath}.period`, issues);
210
+ if (getSummaryPeriodFilter(resource) === null) issues.push({
211
+ path: `${summaryPath}.period${period.filter !== void 0 ? ".filter" : ""}`,
212
+ message: period.filter !== void 0 ? `The list of \`${resource.id}\` has no \`date-range\` filter \`${period.filter}\`.` : `The period of a summary is a \`date-range\` filter of the list of \`${resource.id}\`: it has none.`
213
+ });
214
+ }
215
+ checkWidgets(definition, resource.summary.widgets, `${summaryPath}.widgets`, issues, {
216
+ summaryOf: resource.id,
217
+ hasPeriod: period !== void 0
218
+ });
219
+ }
220
+ allFields(resource, `resources.${index}`).forEach(({ field, path }) => {
221
+ if (field.type === "relation" && typeof field.resource === "string" && !resourceIds.has(field.resource)) issues.push({
222
+ path: `${path}.resource`,
78
223
  message: `No resource \`${field.resource}\`.`
79
224
  });
80
- if (field.type === "media") {
81
- const fieldPath = `resources.${index}.fields.${fieldIndex}.resource`;
225
+ if (field.type === "media" || field.type === "image") {
226
+ const fieldPath = `${path}.resource`;
82
227
  if (field.resource === void 0) {
83
- if (libraries.length !== 1) issues.push({
228
+ if (libraries.length === 0) issues.push({
84
229
  path: fieldPath,
85
- message: libraries.length === 0 ? "A `media` field needs a resource of kind `media`." : "A `media` field needs a `resource` when there are several libraries."
230
+ message: `${field.type === "image" ? "An" : "A"} \`${field.type}\` field needs a resource of kind \`media\`.`
231
+ });
232
+ } else (typeof field.resource === "string" ? [field.resource] : field.resource).forEach((id, idIndex) => {
233
+ if (!libraries.includes(id)) issues.push({
234
+ path: typeof field.resource === "string" ? fieldPath : `${fieldPath}.${String(idIndex)}`,
235
+ message: `No resource of kind \`media\` named \`${id}\`.`
86
236
  });
87
- } else if (!libraries.includes(field.resource)) issues.push({
88
- path: fieldPath,
89
- message: `No resource of kind \`media\` named \`${field.resource}\`.`
90
237
  });
238
+ if (field.transforms === false) {
239
+ if (field.ratios !== void 0 && field.ratios.length > 0) issues.push({
240
+ path: `${path}.ratios`,
241
+ message: "A field without transforms has no editor to suggest ratios."
242
+ });
243
+ field.rules?.forEach((rule, ruleIndex) => {
244
+ if (rule.rule === "crop") issues.push({
245
+ path: `${path}.rules.${String(ruleIndex)}`,
246
+ message: "A field without transforms keeps no crop for the `crop` rule."
247
+ });
248
+ });
249
+ }
91
250
  }
92
251
  });
93
252
  });
253
+ checkDashboards(definition, issues);
94
254
  return issues;
95
255
  }
256
+ /**
257
+ * The dashboards (decision 0084): the widgets of each page whose component is the built-in
258
+ * `dashboard`, and those of `dashboard`, the shorthand of the home page, unless `pages.home` has
259
+ * its own, which can't come with them. Only a `dashboard` page has widgets.
260
+ */
261
+ function checkDashboards(definition, issues) {
262
+ const home = definition.pages?.[HOME_PAGE];
263
+ const homePeriod = (home?.component === "dashboard" ? home.period : void 0) ?? definition.dashboard?.period;
264
+ Object.entries(definition.pages ?? {}).forEach(([id, page]) => {
265
+ if (page.period !== void 0) {
266
+ if (page.component !== "dashboard") issues.push({
267
+ path: `pages.${id}.period`,
268
+ message: `Only a \`${DASHBOARD_COMPONENT}\` page has a period.`
269
+ });
270
+ else checkPeriod(page.period, `pages.${id}.period`, issues);
271
+ }
272
+ if (page.widgets === void 0) return;
273
+ if (page.component !== "dashboard") {
274
+ issues.push({
275
+ path: `pages.${id}.widgets`,
276
+ message: `Only a \`${DASHBOARD_COMPONENT}\` page has widgets.`
277
+ });
278
+ return;
279
+ }
280
+ checkWidgets(definition, page.widgets, `pages.${id}.widgets`, issues, { hasPeriod: (id === "home" ? homePeriod : page.period) !== void 0 });
281
+ });
282
+ if (definition.dashboard === void 0) return;
283
+ if (definition.dashboard.period !== void 0) {
284
+ if (home?.component === "dashboard" && home.period !== void 0) issues.push({
285
+ path: "dashboard.period",
286
+ message: `\`pages.${HOME_PAGE}.period\` replaces it: set only one.`
287
+ });
288
+ else checkPeriod(definition.dashboard.period, "dashboard.period", issues);
289
+ }
290
+ if (home?.component === "dashboard" && home.widgets !== void 0) {
291
+ if (definition.dashboard.widgets.length > 0) issues.push({
292
+ path: "dashboard.widgets",
293
+ message: `\`pages.${HOME_PAGE}.widgets\` replaces them: set only one.`
294
+ });
295
+ return;
296
+ }
297
+ checkWidgets(definition, definition.dashboard.widgets, "dashboard.widgets", issues, { hasPeriod: homePeriod !== void 0 });
298
+ }
299
+ /** A period (decision 0084): its options once each, and its default among them. */
300
+ function checkPeriod(period, path, issues) {
301
+ if (new Set(period.options).size !== period.options.length) issues.push({
302
+ path: `${path}.options`,
303
+ message: "Each period is offered once."
304
+ });
305
+ if (period.default !== void 0 && !period.options.includes(period.default)) issues.push({
306
+ path: `${path}.default`,
307
+ message: `\`${period.default}\` isn't one of the options.`
308
+ });
309
+ }
310
+ var FILTER_PARAM = /^filter\[([^\]]+)\]/;
311
+ /** The settings of a widget that go with a `resource`. */
312
+ var RESOURCE_SETTINGS = [
313
+ "query",
314
+ "measure",
315
+ "group",
316
+ "target",
317
+ "points",
318
+ "by",
319
+ "split"
320
+ ];
321
+ /** The settings of a built-in widget that only some types have (decision 0089), and those types. */
322
+ var TYPED_SETTINGS = {
323
+ goodWhen: ["metric", "table"],
324
+ points: ["metric", "table"],
325
+ by: ["metric"],
326
+ split: ["series"],
327
+ sources: ["series"],
328
+ stacked: ["series"],
329
+ rows: ["table"]
330
+ };
331
+ /** A `measure` of the aggregates of a list: `count`, or the `sum:` of a field of its `sum`. */
332
+ function checkMeasure(value, resource, path, issues) {
333
+ if (value === void 0) return;
334
+ const measure = typeof value === "string" ? parseAggregateMeasure(value) : null;
335
+ if (measure === null) issues.push({
336
+ path,
337
+ message: "The measure must be `count` or `sum:{field}`."
338
+ });
339
+ else if (measure.type === "sum" && !(resource.list?.aggregates?.sum ?? []).includes(measure.field)) issues.push({
340
+ path,
341
+ message: `The list of \`${resource.id}\` can't sum \`${measure.field}\`: it isn't in the \`sum\` of its aggregates.`
342
+ });
343
+ }
344
+ /** A group on the period of a date of the aggregates of a list: `day:created_at`. */
345
+ function checkPeriodGroup(value, resource, path, issues, what) {
346
+ const group = typeof value === "string" ? parseAggregateGroup(value) : null;
347
+ if (group?.type !== "period") issues.push({
348
+ path,
349
+ message: `${what} needs a period of a date: \`day:created_at\`.`
350
+ });
351
+ else if (!(resource.list?.aggregates?.dates ?? []).includes(group.field)) issues.push({
352
+ path,
353
+ message: `The list of \`${resource.id}\` can't group by \`${group.field}\`: it isn't in the \`dates\` of its aggregates.`
354
+ });
355
+ }
356
+ /** A field of the `groupBy` of the aggregates of a list. */
357
+ function checkGroupByField(value, resource, path, issues) {
358
+ if (typeof value !== "string" || !(resource.list?.aggregates?.groupBy ?? []).includes(value)) issues.push({
359
+ path,
360
+ message: `The list of \`${resource.id}\` can't group by \`${String(value)}\`: it isn't in the \`groupBy\` of its aggregates.`
361
+ });
362
+ }
363
+ /** The filters of a query of a widget or of a source: filters of the list, never the paging. */
364
+ function checkQuery(query, resource, path, issues) {
365
+ if (query === null || typeof query !== "object") return;
366
+ const filters = new Set((resource.list?.filters ?? []).map((filter) => filter.id));
367
+ Object.keys(query).forEach((key) => {
368
+ const filter = FILTER_PARAM.exec(key)?.[1];
369
+ if (WIDGET_RESERVED_QUERY_PARAMS.includes(key)) issues.push({
370
+ path: `${path}.${key}`,
371
+ message: `The client sets \`${key}\`.`
372
+ });
373
+ else if (filter !== void 0 && !filters.has(filter)) issues.push({
374
+ path: `${path}.${key}`,
375
+ message: `The list of \`${resource.id}\` has no filter \`${filter}\`.`
376
+ });
377
+ });
378
+ }
379
+ /** `period`: a `date-range` filter of the list of the resource. */
380
+ function checkPeriodFilter(period, resource, path, issues) {
381
+ if (typeof period === "string" && !(resource.list?.filters ?? []).some((filter) => filter.id === period && filter.type === "date-range")) issues.push({
382
+ path,
383
+ message: `The list of \`${resource.id}\` has no \`date-range\` filter \`${period}\`.`
384
+ });
385
+ }
386
+ /**
387
+ * The `sources` of a `series` or the `rows` of a `table` (decision 0089): each on a resource that
388
+ * exists and declares aggregates, with a measure, a group on a period of a date (required for a
389
+ * line of a series), a period and a query, and an alert, that its list declares.
390
+ */
391
+ function checkSources(definition, widget, path, issues, hasPeriod) {
392
+ const settings = widget.settings ?? {};
393
+ const key = widget.type === "series" ? "sources" : "rows";
394
+ const entries = settings[key];
395
+ if (!Array.isArray(entries)) return;
396
+ const sources = getWidgetSources(widget);
397
+ const periods = new Set(sources.flatMap((source) => {
398
+ const group = source.group !== null ? parseAggregateGroup(source.group) : null;
399
+ return group?.type === "period" ? [group.period] : [];
400
+ }));
401
+ if (periods.size > 1) issues.push({
402
+ path: `${path}.settings.${key}`,
403
+ message: `Its ${widget.type === "series" ? "lines" : "trends"} have different periods (${[...periods].join(", ")}): they share one.`
404
+ });
405
+ entries.forEach((entry, index) => {
406
+ const sourcePath = `${path}.settings.${key}.${index}`;
407
+ const source = sources[index];
408
+ if (source === void 0 || entry === null || typeof entry !== "object") return;
409
+ const raw = entry;
410
+ const resource = definition.resources.find((candidate) => candidate.id === source.resource);
411
+ if (resource === void 0) {
412
+ issues.push({
413
+ path: `${sourcePath}.resource`,
414
+ message: `No resource \`${source.resource}\`.`
415
+ });
416
+ return;
417
+ }
418
+ if (resource.list?.aggregates === void 0) {
419
+ issues.push({
420
+ path: `${sourcePath}.resource`,
421
+ message: `The list of \`${resource.id}\` declares no \`aggregates\`.`
422
+ });
423
+ return;
424
+ }
425
+ checkMeasure(raw.measure, resource, `${sourcePath}.measure`, issues);
426
+ const groupKey = widget.type === "series" ? "group" : "points";
427
+ if (widget.type === "series" || source.group !== null) checkPeriodGroup(source.group, resource, `${raw[groupKey] !== void 0 ? sourcePath : `${path}.settings`}.${groupKey}`, issues, widget.type === "series" ? "A line of a `series`" : "The trend of a row");
428
+ if (typeof source.period === "string") {
429
+ if (!hasPeriod && raw.period !== void 0) issues.push({
430
+ path: `${raw.period !== void 0 ? sourcePath : `${path}.settings`}.period`,
431
+ message: "Its overview has no `period`."
432
+ });
433
+ checkPeriodFilter(source.period, resource, `${raw.period !== void 0 ? sourcePath : `${path}.settings`}.period`, issues);
434
+ }
435
+ checkQuery(raw.query, resource, `${sourcePath}.query`, issues);
436
+ const alert = raw.alert;
437
+ if (alert !== void 0 && alert !== null && typeof alert === "object") checkQuery(alert.query, resource, `${sourcePath}.alert.query`, issues);
438
+ });
439
+ }
440
+ /**
441
+ * The measure and the group of a widget on a resource (decision 0084): a `sum:` and a group name
442
+ * aggregates of its list, a `series` needs a period of a date, a `breakdown` a field of `groupBy`,
443
+ * and a `list` has neither.
444
+ */
445
+ function checkAggregateSettings(widget, resource, path, issues) {
446
+ const settings = widget.settings ?? {};
447
+ const aggregates = resource.list?.aggregates ?? {};
448
+ const grouped = GROUPED_WIDGET_TYPES.includes(widget.type);
449
+ if (widget.type === "list") {
450
+ ["measure", "group"].forEach((key) => {
451
+ if (settings[key] !== void 0) issues.push({
452
+ path: `${path}.settings.${key}`,
453
+ message: `A \`list\` shows items: it has no \`${key}\`.`
454
+ });
455
+ });
456
+ return;
457
+ }
458
+ if (settings.measure !== void 0) {
459
+ const measure = typeof settings.measure === "string" ? parseAggregateMeasure(settings.measure) : null;
460
+ if (measure === null) issues.push({
461
+ path: `${path}.settings.measure`,
462
+ message: "The measure must be `count` or `sum:{field}`."
463
+ });
464
+ else if (measure.type === "sum" && !(aggregates.sum ?? []).includes(measure.field)) issues.push({
465
+ path: `${path}.settings.measure`,
466
+ message: `The list of \`${resource.id}\` can't sum \`${measure.field}\`: it isn't in the \`sum\` of its aggregates.`
467
+ });
468
+ }
469
+ if (!grouped) {
470
+ if (settings.group !== void 0) issues.push({
471
+ path: `${path}.settings.group`,
472
+ message: `A \`${widget.type}\` isn't grouped: only a \`series\` and a \`breakdown\` are.`
473
+ });
474
+ if (widget.type === "metric") {
475
+ if (resource.list?.aggregates === void 0) {
476
+ issues.push({
477
+ path: `${path}.settings.resource`,
478
+ message: `The list of \`${resource.id}\` declares no \`aggregates\`: a \`metric\` reads them.`
479
+ });
480
+ return;
481
+ }
482
+ if (settings.points !== void 0) checkPeriodGroup(settings.points, resource, `${path}.settings.points`, issues, "The trend of a `metric`");
483
+ if (settings.by !== void 0) checkGroupByField(settings.by, resource, `${path}.settings.by`, issues);
484
+ }
485
+ return;
486
+ }
487
+ const group = typeof settings.group === "string" ? parseAggregateGroup(settings.group) : null;
488
+ if (widget.type === "series") {
489
+ if (group?.type !== "period") issues.push({
490
+ path: `${path}.settings.group`,
491
+ message: "A `series` on a resource needs a `group` on the period of a date: `day:created_at`."
492
+ });
493
+ else if (!(aggregates.dates ?? []).includes(group.field)) issues.push({
494
+ path: `${path}.settings.group`,
495
+ message: `The list of \`${resource.id}\` can't group by \`${group.field}\`: it isn't in the \`dates\` of its aggregates.`
496
+ });
497
+ if (settings.split !== void 0) {
498
+ checkGroupByField(settings.split, resource, `${path}.settings.split`, issues);
499
+ if (!(resource.list?.filters ?? []).some((filter) => filter.id === settings.split && filter.type !== "date-range")) issues.push({
500
+ path: `${path}.settings.split`,
501
+ message: `The list of \`${resource.id}\` has no filter \`${String(settings.split)}\` to read each of its values.`
502
+ });
503
+ }
504
+ return;
505
+ }
506
+ if (group?.type !== "field") issues.push({
507
+ path: `${path}.settings.group`,
508
+ message: "A `breakdown` on a resource needs a `group` on a field: `status`."
509
+ });
510
+ else if (!(aggregates.groupBy ?? []).includes(group.field)) issues.push({
511
+ path: `${path}.settings.group`,
512
+ message: `The list of \`${resource.id}\` can't group by \`${group.field}\`: it isn't in the \`groupBy\` of its aggregates.`
513
+ });
514
+ }
515
+ /**
516
+ * The widgets of a dashboard or of the summary of a resource (decisions 0064 and 0084): unique ids,
517
+ * and the built-in types on a resource that exists, with filters of its list and aggregates it
518
+ * declares. A `stat` that counts reads the total of the list, so the list can't be paged with
519
+ * cursors, which give no total. Only the widgets of a summary follow its list (`followsList`): those
520
+ * on its resource, and those with data of their own.
521
+ */
522
+ function checkWidgets(definition, widgets, widgetsPath, issues, { summaryOf, hasPeriod = false } = {}) {
523
+ const ids = /* @__PURE__ */ new Set();
524
+ widgets.forEach((widget, index) => {
525
+ const path = `${widgetsPath}.${index}`;
526
+ if (ids.has(widget.id)) issues.push({
527
+ path: `${path}.id`,
528
+ message: `Duplicate widget id \`${widget.id}\`.`
529
+ });
530
+ ids.add(widget.id);
531
+ if (widget.settings?.followsList !== void 0) {
532
+ const other = getWidgetResource(widget)?.resource;
533
+ if (summaryOf === void 0) issues.push({
534
+ path: `${path}.settings.followsList`,
535
+ message: "Only the widgets of the summary of a resource follow its list."
536
+ });
537
+ else if (other !== void 0 && other !== summaryOf) issues.push({
538
+ path: `${path}.settings.followsList`,
539
+ message: `A widget on \`${other}\` doesn't follow the list of \`${summaryOf}\`.`
540
+ });
541
+ }
542
+ if (!isBuiltInWidgetType(widget.type)) return;
543
+ const settings = widget.settings ?? {};
544
+ const onResource = typeof settings.resource === "string";
545
+ if (settings.period !== void 0 && !hasPeriod) issues.push({
546
+ path: `${path}.settings.period`,
547
+ message: `Its ${summaryOf !== void 0 ? "summary" : "dashboard"} has no \`period\`.`
548
+ });
549
+ Object.entries(TYPED_SETTINGS).forEach(([key, types]) => {
550
+ if (settings[key] !== void 0 && !types.includes(widget.type)) issues.push({
551
+ path: `${path}.settings.${key}`,
552
+ message: `Only a ${types.map((type) => `\`${type}\``).join(" or a ")} has \`${key}\`.`
553
+ });
554
+ });
555
+ const sourcesKey = widget.type === "series" ? "sources" : "rows";
556
+ if (settings[sourcesKey] !== void 0 && TYPED_SETTINGS[sourcesKey]?.includes(widget.type)) {
557
+ if (onResource) {
558
+ issues.push({
559
+ path: `${path}.settings.${sourcesKey}`,
560
+ message: `\`${sourcesKey}\` name their resources: the widget has no \`resource\`.`
561
+ });
562
+ return;
563
+ }
564
+ [
565
+ "query",
566
+ "measure",
567
+ "target",
568
+ "by",
569
+ "split"
570
+ ].forEach((key) => {
571
+ if (settings[key] !== void 0) issues.push({
572
+ path: `${path}.settings.${key}`,
573
+ message: `\`${key}\` goes with a \`resource\`, not \`${sourcesKey}\`.`
574
+ });
575
+ });
576
+ checkSources(definition, widget, path, issues, hasPeriod);
577
+ return;
578
+ }
579
+ if (!onResource) {
580
+ if (typeof settings.period === "string") issues.push({
581
+ path: `${path}.settings.period`,
582
+ message: "`period` names a `date-range` filter of the list of its `resource`: without one, it can only be `false`."
583
+ });
584
+ RESOURCE_SETTINGS.forEach((key) => {
585
+ if (settings[key] !== void 0) issues.push({
586
+ path: `${path}.settings.${key}`,
587
+ message: `\`${key}\` goes with a \`resource\`.`
588
+ });
589
+ });
590
+ return;
591
+ }
592
+ if (!RESOURCE_WIDGET_TYPES.includes(widget.type)) {
593
+ issues.push({
594
+ path: `${path}.settings.resource`,
595
+ message: `A \`${widget.type}\` widget has data of its own: it can't read a resource.`
596
+ });
597
+ return;
598
+ }
599
+ const resource = definition.resources.find((candidate) => candidate.id === settings.resource);
600
+ if (resource === void 0) {
601
+ issues.push({
602
+ path: `${path}.settings.resource`,
603
+ message: `No resource \`${String(settings.resource)}\`.`
604
+ });
605
+ return;
606
+ }
607
+ if (settings.target !== void 0 && widget.type !== "stat" && widget.type !== "metric") issues.push({
608
+ path: `${path}.settings.target`,
609
+ message: "Only a `stat` and a `metric` have a `target`."
610
+ });
611
+ checkAggregateSettings(widget, resource, path, issues);
612
+ const sums = typeof settings.measure === "string" && settings.measure.startsWith("sum:");
613
+ if (widget.type === "stat" && !sums && resource.list?.pagination === "cursor") issues.push({
614
+ path: `${path}.settings.resource`,
615
+ message: `The list of \`${resource.id}\` is paged with cursors and has no total: it can't feed a \`stat\` that counts.`
616
+ });
617
+ checkPeriodFilter(settings.period, resource, `${path}.settings.period`, issues);
618
+ checkQuery(settings.query, resource, `${path}.settings.query`, issues);
619
+ });
620
+ }
96
621
  //#endregion
97
622
  export { lintDefinition };
98
623